1 | #!/usr/bin/env python
|
---|
2 | # -*- coding: utf-8 -*-
|
---|
3 | #
|
---|
4 | # Puzzlebox - Brainstorms - Client - ThinkGear Connector
|
---|
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, sys
|
---|
16 | import simplejson as json
|
---|
17 |
|
---|
18 | from twisted.internet import reactor, protocol, defer
|
---|
19 | from twisted.protocols import basic
|
---|
20 |
|
---|
21 | import puzzlebox_brainstorms_configuration as configuration
|
---|
22 | import puzzlebox_brainstorms_client as client
|
---|
23 |
|
---|
24 | #####################################################################
|
---|
25 | # Globals
|
---|
26 | #####################################################################
|
---|
27 |
|
---|
28 | DEBUG = 1
|
---|
29 |
|
---|
30 | SERVER_HOST = configuration.THINKGEAR_SERVER_HOST
|
---|
31 | SERVER_PORT = configuration.THINKGEAR_SERVER_PORT
|
---|
32 |
|
---|
33 | MAX_CONNECTION_ATTEMPTS = configuration.MAX_CONNECTION_ATTEMPTS
|
---|
34 | NO_REPLY_WAIT = configuration.NO_REPLY_WAIT
|
---|
35 |
|
---|
36 |
|
---|
37 | AUTHORIZATION_REQUEST = configuration.THINKGEAR_AUTHORIZATION_REQUEST
|
---|
38 |
|
---|
39 | THINKGEAR_PARAMETERS = {"enableRawOutput": False, "format": "Json"}
|
---|
40 | #THINKGEAR_PARAMETERS = {"enableRawOutput": True, "format": "Json"}
|
---|
41 |
|
---|
42 | #####################################################################
|
---|
43 | # Classes
|
---|
44 | #####################################################################
|
---|
45 |
|
---|
46 | class puzzlebox_brainstorms_client_thinkgear(client.puzzlebox_brainstorms_client):
|
---|
47 |
|
---|
48 | def __init__(self, log, \
|
---|
49 | authorization_request, \
|
---|
50 | server_host=SERVER_HOST, \
|
---|
51 | server_port=SERVER_PORT, \
|
---|
52 | DEBUG=DEBUG):
|
---|
53 |
|
---|
54 | self.log = log
|
---|
55 | self.DEBUG=DEBUG
|
---|
56 |
|
---|
57 | self.authorization_request = authorization_request
|
---|
58 | self.server_host = server_host
|
---|
59 | self.server_port = server_port
|
---|
60 | self.max_connection_attempts = MAX_CONNECTION_ATTEMPTS
|
---|
61 |
|
---|
62 |
|
---|
63 | ##################################################################
|
---|
64 |
|
---|
65 | def send_command(self, \
|
---|
66 | command, \
|
---|
67 | max_connection_attempts=MAX_CONNECTION_ATTEMPTS):
|
---|
68 |
|
---|
69 | factory = puzzlebox_brainstorms_client_thinkgear_factory(self.log, \
|
---|
70 | command, \
|
---|
71 | self.server_host, \
|
---|
72 | self.server_port, \
|
---|
73 | max_connection_attempts, \
|
---|
74 | self.DEBUG)
|
---|
75 |
|
---|
76 | reactor.connectTCP(self.server_host, self.server_port, factory)
|
---|
77 |
|
---|
78 | return factory.replyDefer
|
---|
79 |
|
---|
80 |
|
---|
81 | ##################################################################
|
---|
82 |
|
---|
83 | def send_command_and_print_response(self, command):
|
---|
84 |
|
---|
85 | if self.DEBUG:
|
---|
86 | print "---> [Client] Sending:",
|
---|
87 | print command
|
---|
88 |
|
---|
89 |
|
---|
90 | d = self.send_command(command)
|
---|
91 | d.addCallback(self.print_response)
|
---|
92 |
|
---|
93 |
|
---|
94 | ##################################################################
|
---|
95 |
|
---|
96 | def print_response(self, response):
|
---|
97 |
|
---|
98 | if self.DEBUG:
|
---|
99 | print "---> [Client] Server Response:",
|
---|
100 | print response
|
---|
101 |
|
---|
102 |
|
---|
103 | #####################################################################
|
---|
104 | # ThinkGear Client Protocol class
|
---|
105 | #####################################################################
|
---|
106 |
|
---|
107 | class puzzlebox_brainstorms_client_thinkgear_protocol( \
|
---|
108 | basic.LineReceiver):
|
---|
109 |
|
---|
110 | delimiter='\r'
|
---|
111 |
|
---|
112 | def __init__(self):
|
---|
113 |
|
---|
114 | self.DEBUG = DEBUG
|
---|
115 |
|
---|
116 |
|
---|
117 | ##################################################################
|
---|
118 |
|
---|
119 | def connectionMade(self):
|
---|
120 |
|
---|
121 | #data = pickle.dumps(self.factory.command)
|
---|
122 | data = json.dumps(self.factory.command)
|
---|
123 | self.transport.write(data)
|
---|
124 |
|
---|
125 | self.factory.noReply = reactor.callLater(NO_REPLY_WAIT, self.noReply)
|
---|
126 |
|
---|
127 |
|
---|
128 | ##################################################################
|
---|
129 |
|
---|
130 | def noReply(self):
|
---|
131 |
|
---|
132 | try:
|
---|
133 | self.factory.replyDefer.callback('NO_REPLY')
|
---|
134 | except:
|
---|
135 | if self.DEBUG:
|
---|
136 | print "noReply failed to call callback"
|
---|
137 | #self.factory.log.error("noReply failed to call callback")
|
---|
138 |
|
---|
139 | self.transport.loseConnection()
|
---|
140 |
|
---|
141 |
|
---|
142 | ##################################################################
|
---|
143 |
|
---|
144 | def lineReceived(self, line):
|
---|
145 |
|
---|
146 | # Ignore blank lines
|
---|
147 | if not line:
|
---|
148 | return
|
---|
149 |
|
---|
150 |
|
---|
151 | data = json.loads(line)
|
---|
152 |
|
---|
153 | if self.DEBUG:
|
---|
154 | if ('rawEeg' in data.keys()):
|
---|
155 | if self.DEBUG > 1:
|
---|
156 | print "data:",
|
---|
157 | print data
|
---|
158 | else:
|
---|
159 | print "data:",
|
---|
160 | print data
|
---|
161 |
|
---|
162 |
|
---|
163 | #####################################################################
|
---|
164 | # ThinkGear Client Factory class
|
---|
165 | #####################################################################
|
---|
166 |
|
---|
167 | class puzzlebox_brainstorms_client_thinkgear_factory( \
|
---|
168 | client.puzzlebox_brainstorms_client_send_command_factory):
|
---|
169 |
|
---|
170 | def __init__(self, log, \
|
---|
171 | command, \
|
---|
172 | server_host=SERVER_HOST, \
|
---|
173 | server_port=SERVER_PORT, \
|
---|
174 | max_connection_attempts=MAX_CONNECTION_ATTEMPTS, \
|
---|
175 | DEBUG=DEBUG):
|
---|
176 |
|
---|
177 | self.log = log
|
---|
178 | self.DEBUG = DEBUG
|
---|
179 | self.server_host = server_host
|
---|
180 | self.server_port = server_port
|
---|
181 | self.command = command
|
---|
182 |
|
---|
183 | self.max_connection_attempts = max_connection_attempts
|
---|
184 | self.connection_attempt = 1
|
---|
185 |
|
---|
186 | self.protocol = \
|
---|
187 | puzzlebox_brainstorms_client_thinkgear_protocol
|
---|
188 |
|
---|
189 | self.replyDefer = defer.Deferred()
|
---|
190 |
|
---|
191 |
|
---|
192 | #####################################################################
|
---|
193 | # Main
|
---|
194 | #####################################################################
|
---|
195 |
|
---|
196 | if __name__ == '__main__':
|
---|
197 |
|
---|
198 | #log = puzzlebox_logger.puzzlebox_logger(logfile='client')
|
---|
199 | log = None
|
---|
200 |
|
---|
201 | #authorization_request = AUTHORIZATION_REQUEST
|
---|
202 | authorization_request = None # ThinkGear authentication not working
|
---|
203 | thinkgear_parameters = THINKGEAR_PARAMETERS
|
---|
204 |
|
---|
205 | #log.info("Command parameters: %s" % command_parameters)
|
---|
206 |
|
---|
207 | thinkgear_client = puzzlebox_brainstorms_client_thinkgear( \
|
---|
208 | log, \
|
---|
209 | authorization_request, \
|
---|
210 | server_host=SERVER_HOST, \
|
---|
211 | server_port=SERVER_PORT, \
|
---|
212 | DEBUG=DEBUG)
|
---|
213 |
|
---|
214 | reactor.callWhenRunning( \
|
---|
215 | thinkgear_client.send_command_and_print_response, \
|
---|
216 | thinkgear_parameters)
|
---|
217 |
|
---|
218 |
|
---|
219 | reactor.run()
|
---|
220 |
|
---|