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