1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | # |
---|
4 | # Puzzlebox - Brainstorms - Server |
---|
5 | # |
---|
6 | # Copyright Puzzlebox Productions, LLC (2010) |
---|
7 | # |
---|
8 | # Portions of this code have been previously |
---|
9 | # released under the GNU Pulic License (GPL) version 2 |
---|
10 | # and is Copyright Steven M. Castellotti (2010) |
---|
11 | # For more information please refer to http://www.gnu.org/copyleft/gpl.htm |
---|
12 | # |
---|
13 | # Last Update: 2010.01.28 |
---|
14 | # |
---|
15 | ##################################################################### |
---|
16 | |
---|
17 | import os, signal, sys, time |
---|
18 | import cPickle as pickle |
---|
19 | |
---|
20 | from twisted.internet import reactor, protocol, defer |
---|
21 | |
---|
22 | import puzzlebox_brainstorms_configuration as configuration |
---|
23 | import puzzlebox_brainstorms_client |
---|
24 | #import puzzlebox_logger |
---|
25 | |
---|
26 | ##################################################################### |
---|
27 | # Globals |
---|
28 | ##################################################################### |
---|
29 | |
---|
30 | DEBUG = 1 |
---|
31 | |
---|
32 | SERVER_HOST = configuration.SERVER_HOST |
---|
33 | SERVER_PORT = configuration.SERVER_PORT |
---|
34 | |
---|
35 | ##################################################################### |
---|
36 | # Classes |
---|
37 | ##################################################################### |
---|
38 | |
---|
39 | class puzzlebox_master_control(protocol.ServerFactory): |
---|
40 | |
---|
41 | def __init__(self, log, DEBUG=DEBUG): |
---|
42 | |
---|
43 | self.protocol = puzzlebox_master_control_server_protocol |
---|
44 | |
---|
45 | self.log = log |
---|
46 | self.DEBUG = DEBUG |
---|
47 | |
---|
48 | self.registry = {} |
---|
49 | |
---|
50 | |
---|
51 | ################################################################## |
---|
52 | |
---|
53 | def process_instruction(self, instruction): |
---|
54 | |
---|
55 | #self.log.debug("Received instruction: %s" % instruction) |
---|
56 | #if self.DEBUG: |
---|
57 | #print "Received instruction: %s" % instruction |
---|
58 | |
---|
59 | d = defer.Deferred() |
---|
60 | response = 'instruction received' |
---|
61 | |
---|
62 | if 'command' in instruction: |
---|
63 | response = '%s instruction received' % instruction['command'] |
---|
64 | |
---|
65 | |
---|
66 | if 'information' in instruction: |
---|
67 | information = instruction['information'] |
---|
68 | |
---|
69 | |
---|
70 | if instruction['command'] == 'test_drive': |
---|
71 | command = 'python puzzlebox_brainstorms_rc.py --command=test_drive' |
---|
72 | os.system(command) |
---|
73 | |
---|
74 | |
---|
75 | elif instruction['command'] == 'drive_forward': |
---|
76 | command = 'python puzzlebox_brainstorms_rc.py --command=drive_forward' |
---|
77 | os.system(command) |
---|
78 | |
---|
79 | |
---|
80 | elif instruction['command'] == 'turn_in_reverse': |
---|
81 | command = 'python puzzlebox_brainstorms_rc.py --command=turn_in_reverse' |
---|
82 | os.system(command) |
---|
83 | |
---|
84 | |
---|
85 | elif instruction['command'] == 'turn_left': |
---|
86 | command = 'python puzzlebox_brainstorms_rc.py --command=turn_left' |
---|
87 | os.system(command) |
---|
88 | |
---|
89 | |
---|
90 | elif instruction['command'] == 'turn_right': |
---|
91 | command = 'python puzzlebox_brainstorms_rc.py --command=turn_right' |
---|
92 | os.system(command) |
---|
93 | |
---|
94 | |
---|
95 | else: |
---|
96 | |
---|
97 | #self.log.error("Unrecognized command received: %s" % instruction) |
---|
98 | if self.DEBUG: |
---|
99 | print "Unrecognized command received: %s" % instruction |
---|
100 | reponse = 'unrecognized instruction' |
---|
101 | |
---|
102 | |
---|
103 | if response: |
---|
104 | d.callback(response) |
---|
105 | |
---|
106 | |
---|
107 | return d |
---|
108 | |
---|
109 | |
---|
110 | ################################################################## |
---|
111 | |
---|
112 | def process_connection_lost(self, instruction): |
---|
113 | |
---|
114 | if not instruction: |
---|
115 | |
---|
116 | #self.log.debug("Connection lost with no instruction") |
---|
117 | |
---|
118 | if self.DEBUG: |
---|
119 | print "Connection lost with no instruction" |
---|
120 | |
---|
121 | |
---|
122 | else: |
---|
123 | |
---|
124 | #self.log.debug("Connection lost with no instruction") |
---|
125 | |
---|
126 | if self.DEBUG: |
---|
127 | print "Connection lost instruction" |
---|
128 | |
---|
129 | |
---|
130 | ##################################################################### |
---|
131 | # Protocol |
---|
132 | ##################################################################### |
---|
133 | |
---|
134 | class puzzlebox_master_control_server_protocol(protocol.Protocol): |
---|
135 | |
---|
136 | def __init__(self): |
---|
137 | self.instruction = {} |
---|
138 | self.data_chunk = "" |
---|
139 | |
---|
140 | |
---|
141 | ################################################################## |
---|
142 | |
---|
143 | def dataReceived(self, data): |
---|
144 | |
---|
145 | self.data_chunk += data |
---|
146 | try: |
---|
147 | self.instruction = pickle.loads(self.data_chunk) |
---|
148 | except Exception, e: |
---|
149 | self.factory.log.error("Partial data received (or error: %s)." % e) |
---|
150 | else: |
---|
151 | self.data_chunk = "" |
---|
152 | |
---|
153 | d = self.factory.process_instruction(self.instruction.copy()) |
---|
154 | d.addCallback(self.send_response) |
---|
155 | |
---|
156 | |
---|
157 | ################################################################## |
---|
158 | |
---|
159 | def send_response(self, response): |
---|
160 | |
---|
161 | #if response == "browser exit": |
---|
162 | #self.instruction['command'] = response |
---|
163 | |
---|
164 | response = pickle.dumps(response) |
---|
165 | |
---|
166 | self.transport.write(response) |
---|
167 | |
---|
168 | |
---|
169 | ################################################################## |
---|
170 | |
---|
171 | def connectionLost(self, reason): |
---|
172 | |
---|
173 | self.factory.process_connection_lost(self.instruction) |
---|
174 | |
---|
175 | |
---|
176 | ##################################################################### |
---|
177 | # Main |
---|
178 | ##################################################################### |
---|
179 | |
---|
180 | if __name__ == '__main__': |
---|
181 | |
---|
182 | #log = puzzlebox_logger.puzzlebox_logger(logfile='master_control') |
---|
183 | log = None |
---|
184 | |
---|
185 | # Collect default settings and command line parameters |
---|
186 | server_host = SERVER_HOST |
---|
187 | server_port = SERVER_PORT |
---|
188 | |
---|
189 | for each in sys.argv: |
---|
190 | |
---|
191 | if each.startswith("--host="): |
---|
192 | server_host = each[ len("--host="): ] |
---|
193 | if each.startswith("--port="): |
---|
194 | server_port = each[ len("--port="): ] |
---|
195 | |
---|
196 | |
---|
197 | mcp = puzzlebox_master_control(log, DEBUG) |
---|
198 | reactor.listenTCP(port=server_port, factory=mcp, interface=server_host) |
---|
199 | reactor.run() |
---|
200 | |
---|