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.07.06 |
---|
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 | self.rc = remote_control.puzzlebox_brainstorms_remote_control( \ |
---|
64 | device=BLUETOOTH_DEVICE, \ |
---|
65 | DEBUG=DEBUG) |
---|
66 | |
---|
67 | |
---|
68 | ################################################################## |
---|
69 | |
---|
70 | def configureNetwork(self): |
---|
71 | |
---|
72 | #self.blockSize = 0 |
---|
73 | self.socket = QtNetwork.QTcpServer() |
---|
74 | self.socket.name = 'Brainstorms Server' |
---|
75 | |
---|
76 | if self.DEBUG: |
---|
77 | print "<---- [%s] Initializing server on %s:%i" % \ |
---|
78 | (self.socket.name, self.server_interface, self.server_port) |
---|
79 | |
---|
80 | if (self.server_interface == ''): |
---|
81 | result = self.socket.listen(port=self.server_port) |
---|
82 | else: |
---|
83 | result = self.socket.listen(address=self.server_interface, \ |
---|
84 | port=self.server_port) |
---|
85 | |
---|
86 | if not result: |
---|
87 | if self.DEBUG: |
---|
88 | print "ERROR [%s] Unable to start the server:", self.socket.name, |
---|
89 | print self.socket.errorString() |
---|
90 | |
---|
91 | self.socket.close() |
---|
92 | return |
---|
93 | |
---|
94 | |
---|
95 | self.socket.newConnection.connect(self.processConnection) |
---|
96 | #self.socket.error.connect(self.displayError) |
---|
97 | |
---|
98 | |
---|
99 | ################################################################## |
---|
100 | |
---|
101 | def processConnection(self): |
---|
102 | |
---|
103 | clientConnection = self.socket.nextPendingConnection() |
---|
104 | clientConnection.disconnected.connect(clientConnection.deleteLater) |
---|
105 | |
---|
106 | if not clientConnection.waitForReadyRead(CLIENT_NO_REPLY_WAIT): |
---|
107 | if self.DEBUG: |
---|
108 | print "WARNING [%s] Timeout waiting for client to transmit data" % \ |
---|
109 | self.socket.name |
---|
110 | print "Status:", |
---|
111 | print clientConnection.state() |
---|
112 | #return |
---|
113 | |
---|
114 | socket_buffer = clientConnection.readAll() |
---|
115 | |
---|
116 | for packet in socket_buffer.split(DELIMITER): |
---|
117 | |
---|
118 | if packet != '': |
---|
119 | |
---|
120 | try: |
---|
121 | data = json.loads(packet.data()) |
---|
122 | except: |
---|
123 | data = packet |
---|
124 | |
---|
125 | if self.DEBUG: |
---|
126 | print "--> [%s] Received:" % self.socket.name, |
---|
127 | print data |
---|
128 | |
---|
129 | response = self.processCommand(data) |
---|
130 | |
---|
131 | if response != None: |
---|
132 | |
---|
133 | data = json.dumps(response) |
---|
134 | |
---|
135 | |
---|
136 | if clientConnection.waitForConnected(CLIENT_NO_REPLY_WAIT): |
---|
137 | clientConnection.write(data) |
---|
138 | |
---|
139 | |
---|
140 | clientConnection.disconnectFromHost() |
---|
141 | |
---|
142 | |
---|
143 | ################################################################## |
---|
144 | |
---|
145 | def processCommand(self, data): |
---|
146 | |
---|
147 | #response = '%s command received' % command |
---|
148 | response = None |
---|
149 | |
---|
150 | if DISCRETE_CONTROL_COMMANDS: |
---|
151 | |
---|
152 | self.executeCommand(data['command'], data['power']) |
---|
153 | |
---|
154 | |
---|
155 | elif self.rc.connection != None: |
---|
156 | |
---|
157 | self.rc.run(data['command'], data['power']) |
---|
158 | |
---|
159 | |
---|
160 | return(response) |
---|
161 | |
---|
162 | |
---|
163 | ################################################################## |
---|
164 | |
---|
165 | def executeCommand(self, command): |
---|
166 | |
---|
167 | #command_line = 'python puzzlebox_brainstorms_remote_control.py --command=%s' % command |
---|
168 | |
---|
169 | #os.system(command_line) |
---|
170 | |
---|
171 | rc = remote_control.puzzlebox_brainstorms_remote_control( \ |
---|
172 | device=BLUETOOTH_DEVICE, \ |
---|
173 | command=command, \ |
---|
174 | DEBUG=DEBUG) |
---|
175 | |
---|
176 | if rc.connection != None: |
---|
177 | rc.run(rc.command) |
---|
178 | rc.stop() |
---|
179 | |
---|
180 | |
---|
181 | ##################################################################### |
---|
182 | # Main |
---|
183 | ##################################################################### |
---|
184 | |
---|
185 | if __name__ == '__main__': |
---|
186 | |
---|
187 | # Perform correct KeyboardInterrupt handling |
---|
188 | signal.signal(signal.SIGINT, signal.SIG_DFL) |
---|
189 | |
---|
190 | #log = puzzlebox_logger.puzzlebox_logger(logfile='server_brainstorms') |
---|
191 | log = None |
---|
192 | |
---|
193 | # Collect default settings and command line parameters |
---|
194 | server_interface = SERVER_INTERFACE |
---|
195 | server_port = SERVER_PORT |
---|
196 | |
---|
197 | for each in sys.argv: |
---|
198 | |
---|
199 | if each.startswith("--interface="): |
---|
200 | server_interface = each[ len("--interface="): ] |
---|
201 | if each.startswith("--port="): |
---|
202 | server_port = each[ len("--port="): ] |
---|
203 | |
---|
204 | |
---|
205 | app = QtCore.QCoreApplication(sys.argv) |
---|
206 | |
---|
207 | server = puzzlebox_brainstorms_network_server(log, \ |
---|
208 | server_interface, \ |
---|
209 | server_port, \ |
---|
210 | DEBUG=DEBUG) |
---|
211 | |
---|
212 | sys.exit(app.exec_()) |
---|
213 | |
---|