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