1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - 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.16 |
---|
12 | # |
---|
13 | ##################################################################### |
---|
14 | |
---|
15 | import os, signal, sys, time |
---|
16 | #import cPickle as pickle |
---|
17 | import simplejson as json |
---|
18 | |
---|
19 | from twisted.internet import reactor, protocol, defer |
---|
20 | |
---|
21 | import puzzlebox_brainstorms_configuration as configuration |
---|
22 | import puzzlebox_brainstorms_remote_control as remote_control |
---|
23 | #import puzzlebox_logger |
---|
24 | |
---|
25 | ##################################################################### |
---|
26 | # Globals |
---|
27 | ##################################################################### |
---|
28 | |
---|
29 | DEBUG = 1 |
---|
30 | |
---|
31 | DISCRETE_CONTROL_COMMANDS = configuration.DISCRETE_CONTROL_COMMANDS |
---|
32 | |
---|
33 | SERVER_INTERFACE = configuration.SERVER_INTERFACE |
---|
34 | SERVER_PORT = configuration.SERVER_PORT |
---|
35 | |
---|
36 | ##################################################################### |
---|
37 | # Classes |
---|
38 | ##################################################################### |
---|
39 | |
---|
40 | class puzzlebox_brainstorms_server(protocol.ServerFactory): |
---|
41 | |
---|
42 | def __init__(self, log, DEBUG=DEBUG): |
---|
43 | |
---|
44 | self.log = log |
---|
45 | self.DEBUG = DEBUG |
---|
46 | |
---|
47 | self.protocol = puzzlebox_brainstorms_server_protocol |
---|
48 | |
---|
49 | |
---|
50 | ################################################################## |
---|
51 | |
---|
52 | def process_command(self, command): |
---|
53 | |
---|
54 | d = defer.Deferred() |
---|
55 | |
---|
56 | response = '%s command received' % command |
---|
57 | |
---|
58 | if self.DEBUG: |
---|
59 | print '--> [Server] %s' % response |
---|
60 | |
---|
61 | |
---|
62 | if DISCRETE_CONTROL_COMMANDS: |
---|
63 | |
---|
64 | self.execute_command(command) |
---|
65 | |
---|
66 | |
---|
67 | if response: |
---|
68 | d.callback(response) |
---|
69 | |
---|
70 | |
---|
71 | return d |
---|
72 | |
---|
73 | |
---|
74 | ################################################################## |
---|
75 | |
---|
76 | def execute_command(self, command): |
---|
77 | |
---|
78 | #command_line = 'python puzzlebox_brainstorms_remote_control.py --command=%s' % command |
---|
79 | |
---|
80 | #os.system(command_line) |
---|
81 | |
---|
82 | rc = remote_control.puzzlebox_brainstorms_rc( \ |
---|
83 | device=configuration.BLUETOOTH_DEVICE, \ |
---|
84 | command=command, \ |
---|
85 | DEBUG=DEBUG) |
---|
86 | |
---|
87 | if rc.connection != None: |
---|
88 | rc.run(rc.command) |
---|
89 | rc.stop() |
---|
90 | |
---|
91 | |
---|
92 | ################################################################## |
---|
93 | |
---|
94 | def process_connection_lost(self, command): |
---|
95 | |
---|
96 | if not command: |
---|
97 | |
---|
98 | #self.log.debug("Connection lost with no command") |
---|
99 | |
---|
100 | if self.DEBUG: |
---|
101 | print "--> [Server] Connection lost with no command" |
---|
102 | |
---|
103 | |
---|
104 | else: |
---|
105 | |
---|
106 | #self.log.debug("Connection lost") |
---|
107 | |
---|
108 | #if self.DEBUG: |
---|
109 | #print "--> [Server] Connection lost" |
---|
110 | |
---|
111 | pass |
---|
112 | |
---|
113 | |
---|
114 | ##################################################################### |
---|
115 | # Protocol |
---|
116 | ##################################################################### |
---|
117 | |
---|
118 | class puzzlebox_brainstorms_server_protocol(protocol.Protocol): |
---|
119 | |
---|
120 | def __init__(self): |
---|
121 | |
---|
122 | self.command = None |
---|
123 | self.data_chunk = "" |
---|
124 | |
---|
125 | |
---|
126 | ################################################################## |
---|
127 | |
---|
128 | def dataReceived(self, data): |
---|
129 | |
---|
130 | self.data_chunk += data |
---|
131 | |
---|
132 | try: |
---|
133 | #self.command = pickle.loads(self.data_chunk) |
---|
134 | self.command = json.loads(self.data_chunk) |
---|
135 | |
---|
136 | except Exception, e: |
---|
137 | #self.factory.log.error("Partial data received (or error: %s)." % e) |
---|
138 | if DEBUG: |
---|
139 | print "Partial data received (or error:", |
---|
140 | print e |
---|
141 | print ")." |
---|
142 | |
---|
143 | else: |
---|
144 | self.data_chunk = "" |
---|
145 | |
---|
146 | d = self.factory.process_command("%s" % self.command) |
---|
147 | d.addCallback(self.send_response) |
---|
148 | |
---|
149 | |
---|
150 | ################################################################## |
---|
151 | |
---|
152 | def send_response(self, response): |
---|
153 | |
---|
154 | #response = pickle.dumps(response) |
---|
155 | response = json.dumps(response) |
---|
156 | |
---|
157 | self.transport.write(response) |
---|
158 | |
---|
159 | |
---|
160 | ################################################################## |
---|
161 | |
---|
162 | def connectionLost(self, reason): |
---|
163 | |
---|
164 | self.factory.process_connection_lost(self.command) |
---|
165 | |
---|
166 | |
---|
167 | ##################################################################### |
---|
168 | # Main |
---|
169 | ##################################################################### |
---|
170 | |
---|
171 | if __name__ == '__main__': |
---|
172 | |
---|
173 | #log = puzzlebox_logger.puzzlebox_logger(logfile='master_control') |
---|
174 | log = None |
---|
175 | |
---|
176 | # Collect default settings and command line parameters |
---|
177 | server_interface = SERVER_INTERFACE |
---|
178 | server_port = SERVER_PORT |
---|
179 | |
---|
180 | for each in sys.argv: |
---|
181 | |
---|
182 | if each.startswith("--interface="): |
---|
183 | server_interface = each[ len("--interface="): ] |
---|
184 | if each.startswith("--port="): |
---|
185 | server_port = each[ len("--port="): ] |
---|
186 | |
---|
187 | |
---|
188 | server = puzzlebox_brainstorms_server(log, DEBUG=DEBUG) |
---|
189 | |
---|
190 | if DEBUG: |
---|
191 | print "--> [Server] Initializing server on %s:%i" % \ |
---|
192 | (server_interface, server_port) |
---|
193 | |
---|
194 | reactor.listenTCP(interface=server_interface, port=server_port, factory=server) |
---|
195 | |
---|
196 | reactor.run() |
---|
197 | |
---|