-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommon.hws
177 lines (145 loc) · 4.81 KB
/
common.hws
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
; Appends a message to the specified TextView
Function p_WriteToTextView(textView$, message$)
Local currentMessages$ = moai.Get(textView$, "Text")
If currentMessages$ = ""
currentMessages$ = message$
Else
currentMessages$ = currentMessages$ .. "\r\n" .. message$
EndIf
moai.Set(textView$, "Text", currentMessages$)
EndFunction
; Intercept special types of messages
Function p_ProcessMessagesByType(message$)
If #DEBUG Then DebugPrint("p_ProcessMessagesByType(): Detecting message type for:", message$)
If StartsWith(message$, "Server: /CLIENTS ")
If #DEBUG Then DebugPrint("p_ProcessMessagesByType(): New Client list received")
ElseIf StartsWith(message$, "Server: /COMMAND ")
If #DEBUG Then DebugPrint("p_ProcessMessagesByType(): New command received")
ElseIf StartsWith(message$, "Server: /NICKS ")
If #DEBUG Then DebugPrint("p_ProcessMessagesByType(): New list of Nicks received")
ElseIf StartsWith(message$, "Server: /DISCONNECT")
p_Log("Received Disconnect command from server")
p_WriteToTextView("textviewMessages", "Server was stopped, all clients disconnected!")
p_DisconnectFromServer()
Else
If #DEBUG Then DebugPrint("p_ProcessMessagesByType(): Simple text, broadcasting to TextView...")
p_WriteToTextView("textviewMessages", message$)
EndIf
EndFunction
Function p_Log(text$)
moai.DoMethod("log", "insert", "bottom", GetTime(True) .. ": " .. text$)
moai.DoMethod("log", "jump", "bottom")
EndFunction
; Send a message to all clients
Function p_SendMessage(message$)
p_Log("Sending message: " .. message$)
p_WriteToTextView("textviewMessages", message$)
message$ = nick$ .. ": " .. message$
ForEach(clients, p_SendData, message$ .. "\r\n")
EndFunction
; Send a message to a connected client
Function p_SendData(index, clientId, message$)
SendData(clientId, message$)
EndFunction
; Insert a Client in Listview
Function p_InsertClientInListview(index, clientId)
moai.DoMethod("lv_clients", "Insert", index, GetConnectionIP(clientId))
EndFunction
; Update the Clients listview
Function p_UpdateClientsListview(clients)
moai.DoMethod("lv_clients", "Clear")
ForEach(clients, p_InsertClientInListview)
EndFunction
; Add a new connection to the clients Table
Function p_AddConnection(clientId)
If Not HaveItem(clients, clientId)
If #DEBUG Then DebugPrint("New Client connected: ", clientId)
InsertItem(clients, clientId)
p_UpdateClientsListview(clients)
EndIf
EndFunction
Function p_LocateClientIndex(index, Id)
If Id = clientIdToRemove Then RemoveItem(clients, index)
EndFunction
; Remove a connection from the clients table
Function p_RemoveConnection(Id)
If #DEBUG Then DebugPrint("Client disconnected: ", Id)
clientIdToRemove = Id
ForEach(clients, p_LocateClientIndex)
p_UpdateClientsListview(clients)
clientIdToRemove = Nil
EndFunction
; Handle RapaGUI events
Function p_EventFunc(msg)
Switch msg.action
Case "RapaGUI"
Switch msg.attribute
Case "Pressed":
If #DEBUG Then DebugPrint("Received Button Pressed Event")
p_GUIEvent(msg.id)
Case "Acknowledge":
If #DEBUG Then DebugPrint("Received Textentry.Acknowledge event")
p_GUIEvent(msg.id)
Case "Selected":
If #DEBUG Then DebugPrint("Received Menu Selected Event")
p_GUIEvent(UnrightStr(msg.id, 3))
Case "File":
p_Log("LWSN is found at: " .. msg.triggervalue)
Case "Path":
Switch msg.id
Case "CmdDir":
p_Log("Cmd Dir: " .. msg.triggervalue)
Case "ProjectsDir":
p_Log("Projects Path: " .. msg.triggervalue)
EndSwitch
Case "CloseRequest":
moai.Set(msg.id, "open", False)
EndSwitch
EndSwitch
EndFunction
; Handle GUI events
Function p_GUIEvent(id)
Switch id
Case "start":
port$ = moai.Get("txtPort", "Text")
p_StartServer(port$)
Case "stop":
p_StopServer()
Case "connect":
server$ = moai.Get("txtServer", "Text")
port$ = moai.Get("txtPort", "Text")
p_ConnectToServer(server$, port$)
Case "disconnect":
p_DisconnectFromServer()
Case "txtMessage":
message$ = moai.Get("txtMessage", "Text")
p_SendMessage(message$)
moai.Set("txtMessage", "Text", "")
Case "send":
message$ = moai.Get("txtMessage", "Text")
;Local receiver = Moai.Get("listClients", "active")
p_SendMessage(message$)
moai.Set("txtMessage", "Text", "")
Case "settings":
moai.Set("settings", "Open", True)
moai.Set("settings", "Activate", True)
Case "SaveSettings":
p_ApplySettings()
SavePrefs(prefs)
moai.Set("settings", "open", False)
Case "Cancel":
moai.Set("settings", "open", False)
Case "about":
Local info = GetApplicationInfo()
moai.Request(info.Title,
info.Version .. "\r\n" ..
info.Copyright .. "\r\r\n" ..
info.Description .. "\n", "OK")
Case "quit":
p_DisconnectFromServer()
End
Case "quitServer":
p_StopServer()
End
EndSwitch
EndFunction