1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - Network - Server |
---|
5 | # |
---|
6 | # Copyright Puzzlebox Productions, LLC (2010) |
---|
7 | # |
---|
8 | # This code is released under the GNU Pulic License (GPL) version 2 |
---|
9 | # For more information please refer to http://www.gnu.org/copyleft/gpl.html |
---|
10 | # |
---|
11 | # Last Update: 2010.06.30 |
---|
12 | # |
---|
13 | ##################################################################### |
---|
14 | |
---|
15 | import os, sys |
---|
16 | import signal |
---|
17 | |
---|
18 | import simplejson as json |
---|
19 | |
---|
20 | from PyQt4 import QtCore, QtNetwork |
---|
21 | |
---|
22 | import puzzlebox_brainstorms_configuration as configuration |
---|
23 | import puzzlebox_brainstorms_remote_control as remote_control |
---|
24 | #import puzzlebox_logger |
---|
25 | |
---|
26 | ##################################################################### |
---|
27 | # Globals |
---|
28 | ##################################################################### |
---|
29 | |
---|
30 | DEBUG = 1 |
---|
31 | |
---|
32 | SERVER_INTERFACE = configuration.BRAINSTORMS_SERVER_INTERFACE |
---|
33 | SERVER_PORT = configuration.BRAINSTORMS_SERVER_PORT |
---|
34 | |
---|
35 | CLIENT_NO_REPLY_WAIT = configuration.CLIENT_NO_REPLY_WAIT * 1000 |
---|
36 | |
---|
37 | DELIMITER = configuration.BRAINSTORMS_DELIMITER |
---|
38 | |
---|
39 | BLUETOOTH_DEVICE = configuration.NXT_BLUETOOTH_DEVICE |
---|
40 | |
---|
41 | DISCRETE_CONTROL_COMMANDS = configuration.BRAINSTORMS_DISCRETE_CONTROL_COMMANDS |
---|
42 | |
---|
43 | ##################################################################### |
---|
44 | # Classes |
---|
45 | ##################################################################### |
---|
46 | |
---|
47 | class puzzlebox_brainstorms_network_server: |
---|
48 | |
---|
49 | def __init__(self, log, \ |
---|
50 | server_interface=SERVER_INTERFACE, \ |
---|
51 | server_port=SERVER_PORT, \ |
---|
52 | DEBUG=DEBUG, \ |
---|
53 | parent=None): |
---|
54 | |
---|
55 | self.log = log |
---|
56 | self.DEBUG = DEBUG |
---|
57 | |
---|
58 | self.server_interface = server_interface |
---|
59 | self.server_port = server_port |
---|
60 | |
---|
61 | self.configureNetwork() |
---|
62 | |
---|
63 | |
---|
64 | ################################################################## |
---|
65 | |
---|
66 | def configureNetwork(self): |
---|
67 | |
---|
68 | #self.blockSize = 0 |
---|
69 | self.socket = QtNetwork.QTcpServer() |
---|
70 | self.socket.name = 'Puzzlebox Brainstorms Server' |
---|
71 | |
---|
72 | if self.DEBUG: |
---|
73 | print "----> [%s] Initializing server on %s:%i" % \ |
---|
74 | (self.socket.name, self.server_interface, self.server_port) |
---|
75 | |
---|
76 | if (self.server_interface == ''): |
---|
77 | result = self.socket.listen(port=self.server_port) |
---|
78 | else: |
---|
79 | result = self.socket.listen(address=self.server_interface, \ |
---|
80 | port=self.server_port) |
---|
81 | |
---|
82 | if not result: |
---|
83 | if self.DEBUG: |
---|
84 | print "ERROR [%s] Unable to start the server:", self.socket.name, |
---|
85 | print self.socket.errorString() |
---|
86 | |
---|
87 | self.socket.close() |
---|
88 | return |
---|
89 | |
---|
90 | |
---|
91 | self.socket.newConnection.connect(self.processConnection) |
---|
92 | #self.socket.error.connect(self.displayError) |
---|
93 | |
---|
94 | |
---|
95 | ################################################################## |
---|
96 | |
---|
97 | def processConnection(self): |
---|
98 | |
---|
99 | clientConnection = self.socket.nextPendingConnection() |
---|
100 | clientConnection.disconnected.connect(clientConnection.deleteLater) |
---|
101 | |
---|
102 | if not clientConnection.waitForReadyRead(CLIENT_NO_REPLY_WAIT): |
---|
103 | if self.DEBUG: |
---|
104 | print "ERROR [%s] Timeout waiting for client to transmit data" |
---|
105 | return |
---|
106 | |
---|
107 | socket_buffer = clientConnection.readAll() |
---|
108 | |
---|
109 | for packet in socket_buffer.split(DELIMITER): |
---|
110 | |
---|
111 | if packet != '': |
---|
112 | |
---|
113 | try: |
---|
114 | data = json.loads(packet.data()) |
---|
115 | except: |
---|
116 | data = packet |
---|
117 | |
---|
118 | if self.DEBUG: |
---|
119 | print "<-- [%s] Received:" % self.socket.name, |
---|
120 | print data |
---|
121 | |
---|
122 | response = self.processCommand(data) |
---|
123 | |
---|
124 | data = json.dumps(response) |
---|
125 | |
---|
126 | |
---|
127 | if clientConnection.waitForConnected(CLIENT_NO_REPLY_WAIT): |
---|
128 | clientConnection.write(data) |
---|
129 | |
---|
130 | clientConnection.disconnectFromHost() |
---|
131 | |
---|
132 | |
---|
133 | ################################################################## |
---|
134 | |
---|
135 | def processCommand(self, command): |
---|
136 | |
---|
137 | response = '%s command received' % command |
---|
138 | |
---|
139 | if DISCRETE_CONTROL_COMMANDS: |
---|
140 | |
---|
141 | self.executeCommand(command) |
---|
142 | |
---|
143 | |
---|
144 | return(response) |
---|
145 | |
---|
146 | |
---|
147 | ################################################################## |
---|
148 | |
---|
149 | def executeCommand(self, command): |
---|
150 | |
---|
151 | #command_line = 'python puzzlebox_brainstorms_remote_control.py --command=%s' % command |
---|
152 | |
---|
153 | #os.system(command_line) |
---|
154 | |
---|
155 | rc = remote_control.puzzlebox_brainstorms_remote_control( \ |
---|
156 | device=BLUETOOTH_DEVICE, \ |
---|
157 | command=command, \ |
---|
158 | DEBUG=DEBUG) |
---|
159 | |
---|
160 | if rc.connection != None: |
---|
161 | rc.run(rc.command) |
---|
162 | rc.stop() |
---|
163 | |
---|
164 | |
---|
165 | ################################################################## |
---|
166 | |
---|
167 | #def process_connection_lost(self, command): |
---|
168 | |
---|
169 | #if not command: |
---|
170 | |
---|
171 | ##self.log.debug("Connection lost with no command") |
---|
172 | |
---|
173 | #if self.DEBUG: |
---|
174 | #print "--> [Server] Connection lost with no command" |
---|
175 | |
---|
176 | |
---|
177 | #else: |
---|
178 | |
---|
179 | ##self.log.debug("Connection lost") |
---|
180 | |
---|
181 | ##if self.DEBUG: |
---|
182 | ##print "--> [Server] Connection lost" |
---|
183 | |
---|
184 | #pass |
---|
185 | |
---|
186 | |
---|
187 | ################################################################## |
---|
188 | |
---|
189 | #def dataReceived(self, data): |
---|
190 | |
---|
191 | #self.data_chunk += data |
---|
192 | |
---|
193 | #try: |
---|
194 | ##self.command = pickle.loads(self.data_chunk) |
---|
195 | #self.command = json.loads(self.data_chunk) |
---|
196 | |
---|
197 | #except Exception, e: |
---|
198 | ##self.factory.log.error("Partial data received (or error: %s)." % e) |
---|
199 | #if DEBUG: |
---|
200 | #print "Partial data received (or error:", |
---|
201 | #print e |
---|
202 | #print ")." |
---|
203 | |
---|
204 | #else: |
---|
205 | #self.data_chunk = "" |
---|
206 | |
---|
207 | #d = self.factory.process_command("%s" % self.command) |
---|
208 | #d.addCallback(self.send_response) |
---|
209 | |
---|
210 | |
---|
211 | ################################################################## |
---|
212 | |
---|
213 | #def send_response(self, response): |
---|
214 | |
---|
215 | ##response = pickle.dumps(response) |
---|
216 | #response = json.dumps(response) |
---|
217 | |
---|
218 | #self.transport.write(response) |
---|
219 | |
---|
220 | |
---|
221 | ################################################################## |
---|
222 | |
---|
223 | #def connectionLost(self, reason): |
---|
224 | |
---|
225 | #self.factory.process_connection_lost(self.command) |
---|
226 | |
---|
227 | |
---|
228 | ##################################################################### |
---|
229 | # Main |
---|
230 | ##################################################################### |
---|
231 | |
---|
232 | if __name__ == '__main__': |
---|
233 | |
---|
234 | # Perform correct KeyboardInterrupt handling |
---|
235 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
236 | |
---|
237 | #log = puzzlebox_logger.puzzlebox_logger(logfile='server_brainstorms') |
---|
238 | log = None |
---|
239 | |
---|
240 | # Collect default settings and command line parameters |
---|
241 | server_interface = SERVER_INTERFACE |
---|
242 | server_port = SERVER_PORT |
---|
243 | |
---|
244 | for each in sys.argv: |
---|
245 | |
---|
246 | if each.startswith("--interface="): |
---|
247 | server_interface = each[ len("--interface="): ] |
---|
248 | if each.startswith("--port="): |
---|
249 | server_port = each[ len("--port="): ] |
---|
250 | |
---|
251 | |
---|
252 | app = QtCore.QCoreApplication(sys.argv) |
---|
253 | |
---|
254 | server = puzzlebox_brainstorms_network_server(log, \ |
---|
255 | server_interface, \ |
---|
256 | server_port, \ |
---|
257 | DEBUG=DEBUG) |
---|
258 | |
---|
259 | sys.exit(app.exec_()) |
---|
260 | |
---|