Changeset 57 for remote_control
- Timestamp:
- 06/21/10 16:02:12 (12 years ago)
- Location:
- remote_control
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
remote_control/puzzlebox_brainstorms_client_thinkgear.py
r56 r57 49 49 50 50 def __init__(self, log, \ 51 authorization_request, \ 52 server_host=SERVER_HOST, \ 53 server_port=SERVER_PORT, \ 54 DEBUG=DEBUG): 51 server_host=SERVER_HOST, \ 52 server_port=SERVER_PORT, \ 53 DEBUG=DEBUG): 55 54 56 55 self.log = log 57 56 self.DEBUG=DEBUG 58 57 59 self.authorization_request = authorization_request60 58 self.server_host = server_host 61 59 self.server_port = server_port 62 60 self.max_connection_attempts = MAX_CONNECTION_ATTEMPTS 63 61 64 65 ################################################################## 66 67 def send_command(self, \ 68 command, \ 69 max_connection_attempts=MAX_CONNECTION_ATTEMPTS): 70 71 factory = puzzlebox_brainstorms_client_thinkgear_factory(self.log, \ 72 command, \ 73 self.server_host, \ 74 self.server_port, \ 75 max_connection_attempts, \ 76 self.DEBUG) 77 78 reactor.connectTCP(self.server_host, self.server_port, factory) 79 80 ## return factory.replyDefer 81 82 83 ################################################################## 84 85 def send_command_and_print_response(self, command): 86 87 if self.DEBUG: 88 print "---> [Client] Sending:", 89 print command 90 91 92 d = self.send_command(command) 62 self.is_authorized = True 63 64 65 ################################################################## 66 67 def authorize_and_send_parameters(self,\ 68 thinkgear_parameters, \ 69 authorization_request): 70 71 if self.DEBUG: 72 print "----> [ThinkGear Client] Sending Authorization Request:", 73 print authorization_request 74 75 76 d = self.send_parameters(thinkgear_parameters, authorization_request) 77 d.addCallback(self.update_authorization) 78 79 80 ################################################################## 81 82 def update_authorization(self, authorization): 83 84 if self.DEBUG: 85 print "--> [ThinkGear Client] Authorization:", 86 print authorization 87 88 self.is_authorized = authorization 89 90 91 ################################################################## 92 93 def send_parameters(self, \ 94 parameters, \ 95 authentication=None, \ 96 max_connection_attempts=MAX_CONNECTION_ATTEMPTS): 97 98 self.factory = puzzlebox_brainstorms_client_thinkgear_factory(self.log, \ 99 parameters, \ 100 authentication, \ 101 self.server_host, \ 102 self.server_port, \ 103 max_connection_attempts, \ 104 self.DEBUG) 105 106 reactor.connectTCP(self.server_host, self.server_port, self.factory) 107 108 return self.factory.replyDefer 109 110 111 ################################################################## 112 113 def send_parameters_and_print_response(self, parameters): 114 115 if self.DEBUG: 116 print "----> [ThinkGear Client] Sending:", 117 print parameters 118 119 120 d = self.send_parameters(parameters) 93 121 d.addCallback(self.print_response) 94 95 96 ################################################################## 97 122 123 124 ################################################################## 125 98 126 def print_response(self, response): 99 127 100 128 if self.DEBUG: 101 print "-- -> [Client] Server Response:",129 print "--> [ThinkGear Client] Server Response:", 102 130 print response 103 131 … … 120 148 121 149 def connectionMade(self): 122 123 data = json.dumps(self.factory.command) 124 self.sendLine(data) 150 151 if self.DEBUG: 152 print "----> [ThinkGear Client] Connected to %s:%i" % \ 153 (self.factory.server_host, \ 154 self.factory.server_port) 155 156 157 if (self.factory.authentication != None): 158 self.send_data(self.factory.authentication) 159 160 else: 161 self.send_data(self.factory.parameters) 162 163 164 ################################################################## 165 166 def send_data(self, data): 167 168 packet = json.dumps(data) 169 170 if self.DEBUG: 171 print "----> [ThinkGear Client] Sending:", 172 print packet 173 174 175 self.sendLine(packet) 125 176 126 177 ## self.factory.noReply = reactor.callLater(NO_REPLY_WAIT, self.noReply) … … 136 187 if self.DEBUG: 137 188 print "noReply failed to call callback" 138 139 140 ################################################################## 141 142 143 144 145 146 147 148 149 150 151 if DEBUG:189 190 191 ################################################################## 192 193 def lineReceived(self, line): 194 195 # Ignore blank lines 196 if not line: 197 return 198 199 try: 200 data = json.loads(line) 201 except Exception, e: 202 if SELF.DEBUG: 152 203 print "Partial data received (or error:", 153 204 print e … … 157 208 print line 158 209 159 else: 160 if self.DEBUG: 161 if ('rawEeg' in data.keys()): 162 if self.DEBUG > 1: 163 print "data:", 164 print data 165 else: 166 print "data:", 167 print data 168 169 170 ################################################################## 171 172 def connectionLost(self, reason): 210 else: 211 212 if (type(data) == type({})): 213 214 self.factory.process_response(data) 215 ## self.factory.replyDefer.callback(data) 216 217 else: 218 if self.DEBUG: 219 print "data:", 220 print data 221 222 223 ################################################################## 224 225 def connectionLost(self, reason): 173 226 174 227 if self.DEBUG > 1: … … 194 247 195 248 def __init__(self, log, \ 196 command, \ 197 server_host=SERVER_HOST, \ 198 server_port=SERVER_PORT, \ 199 max_connection_attempts=MAX_CONNECTION_ATTEMPTS, \ 200 DEBUG=DEBUG): 249 parameters, \ 250 authentication, \ 251 server_host=SERVER_HOST, \ 252 server_port=SERVER_PORT, \ 253 max_connection_attempts=MAX_CONNECTION_ATTEMPTS, \ 254 DEBUG=DEBUG): 201 255 202 256 self.log = log … … 204 258 self.server_host = server_host 205 259 self.server_port = server_port 206 self.command = command 260 self.parameters = parameters 261 self.authentication = authentication 207 262 208 263 self.max_connection_attempts = max_connection_attempts … … 215 270 216 271 272 ################################################################## 273 274 def process_response(self, response): 275 276 if self.DEBUG: 277 if (('rawEeg' not in response.keys()) or \ 278 (self.DEBUG > 1)): 279 print "<-- [ThinkGear Client] Received:", 280 print response 281 282 283 if response.has_key('isAuthorized'): 284 285 if (response['isAuthorized'] == True): 286 self.replyDefer.callback(True) 287 else: 288 self.replyDefer.callback(False) 289 290 217 291 ##################################################################### 218 292 # Main … … 224 298 log = None 225 299 226 #authorization_request = AUTHORIZATION_REQUEST 227 authorization_request = None # ThinkGear authentication not working 300 authorization_request = AUTHORIZATION_REQUEST 228 301 thinkgear_parameters = THINKGEAR_PARAMETERS 229 302 230 303 thinkgear_client = puzzlebox_brainstorms_client_thinkgear( \ 231 304 log, \ 232 authorization_request, \233 305 server_host=SERVER_HOST, \ 234 306 server_port=SERVER_PORT, \ 235 307 DEBUG=DEBUG) 236 308 309 # Use ThinkGear authentication 237 310 reactor.callWhenRunning( \ 238 thinkgear_client.send_command, \ 239 thinkgear_parameters) 311 thinkgear_client.authorize_and_send_parameters, \ 312 thinkgear_parameters, \ 313 authorization_request) 314 315 # Do not use ThinkGear authentication 316 ## authorization_request = None 317 ## reactor.callWhenRunning( \ 318 ## thinkgear_client.send_parameters, \ 319 ## thinkgear_parameters, \ 320 ## authorization_request) 321 322 240 323 241 324 -
remote_control/puzzlebox_brainstorms_configuration.py
r56 r57 188 188 189 189 190 ##################################################################### 191 # Flash socket policy handling 190 192 ##################################################################### 191 193 … … 198 200 <allow-access-from domain="*" to-ports="%i" /> 199 201 </cross-domain-policy>%c''' % (THINKGEAR_SERVER_PORT, 0) 202 200 203 201 204 ##################################################################### -
remote_control/puzzlebox_brainstorms_server_thinkgear.py
r56 r57 12 12 # 13 13 ##################################################################### 14 # To do: 15 # - Server starts session sending data stream per command received 16 # instead of once per connection 17 ##################################################################### 14 18 15 19 import os, signal, sys, time … … 38 42 MESSAGE_FREQUENCY_TIMER = 1 # 1 Hz 39 43 BLINK_FREQUENCY_TIMER = 10 # 10 seconds 44 45 DEFAULT_AUTHORIZATION_MESSAGE = \ 46 {"isAuthorized": True} 47 # Tells the client whether the server has authorized 48 # access to the user's headset data. The value is 49 # either true or false. 40 50 41 51 DEFAULT_SIGNAL_LEVEL_MESSAGE = \ … … 47 57 48 58 DEFAULT_EEG_POWER_MESSAGE = \ 49 { 'eegPower': { \59 {"eegPower": { \ 50 60 'lowGamma': 0, \ 51 61 'highGamma': 0, \ … … 61 71 62 72 DEFAULT_ESENSE_MESSAGE = \ 63 { 'eSense': { \73 {"eSense": { \ 64 74 'meditation': 0, \ 65 75 'attention': 0, \ … … 70 80 # and 100 is an excess of that attribute. 71 81 72 DEFAULT_BLINK_MESSAGE = {"blinkStrength": 100}82 DEFAULT_BLINK_MESSAGE = {"blinkStrength": 255} 73 83 # The strength of a detected blink. This is 74 84 # an integer in the range of 0-255. … … 100 110 self.status_packet = DEFAULT_PACKET 101 111 self.client_connected = False 102 103 104 ################################################################## 112 113 114 ################################################################## 115 116 def validate_checksum(self, checksum): 117 118 '''The key used by the client application to identify 119 itself. This must be 40 hexadecimal characters, ideally generated 120 using an SHA-1 digest. The appKey is an identifier that is unique 121 to each application, rather than each instance of an application. 122 It is used by the server to bypass the authorization process if a 123 user had previously authorized the requesting client. To reduce 124 the chance of overlap with the appKey of other applications, 125 the appKey should be generated using an SHA-1 digest.''' 126 127 is_valid = True 128 129 hexadecimal_characters = '0123456789abcdef' 130 131 if len(checksum) != 40: 132 is_valid = False 133 else: 134 for character in checksum: 135 if character not in hexadecimal_characters: 136 is_valid = False 137 138 return(is_valid) 139 140 141 ################################################################## 142 143 def authorize_client(self, data): 144 145 '''The client must initiate an authorization request 146 and the server must authorize the client before the 147 server will start transmitting any headset data.''' 148 149 is_authorized = self.validate_checksum(data['appKey']) 150 151 # A human-readable name identifying the client 152 # application. This can be a maximum of 255 characters. 153 154 if len(data['appName']) > 255: 155 is_authorized = False 156 157 158 return(is_authorized) 159 160 161 ################################################################## 105 162 106 163 def process_data(self, data): 107 164 108 165 d = defer.Deferred() 109 110 166 111 167 # Special socket handling for Flash applications … … 113 169 response = FLASH_SOCKET_POLICY_FILE 114 170 115 else: 116 response = DEFAULT_RESPONSE_MESSAGE 171 elif (type(data) == type({}) and \ 172 data.has_key('appName') and \ 173 data.has_key('appKey')): 174 authorized = self.authorize_client(data) 175 176 response = DEFAULT_AUTHORIZATION_MESSAGE 177 178 response = {} 179 response['isAuthorized'] = authorized 180 181 182 else: 183 response = DEFAULT_RESPONSE_MESSAGE 117 184 118 185 119 186 if self.DEBUG: 120 print " -->[ThinkGear Emulator] Received:",187 print "<-- [ThinkGear Emulator] Received:", 121 188 print data 122 189 123 190 if response: 124 191 d.callback(response) 192 125 193 126 194 return d … … 130 198 131 199 def process_connection_lost(self): 132 133 print "--> [ThinkGear Emulator] Connection lost" 200 201 if self.DEBUG: 202 print "--> [ThinkGear Emulator] Connection lost" 203 134 204 self.client_connected = False 205 206 self.looping_timer.stop() 135 207 136 208 … … 172 244 173 245 if self.DEBUG: 174 print " -->[ThinkGear Emulator] Client connected"246 print "<-- [ThinkGear Emulator] Client connected" 175 247 176 248 self.factory.start_updating() … … 198 270 return 199 271 200 data = json.loads(line) 201 202 if self.DEBUG: 203 print "line received:", 204 print data 205 206 d = self.factory.process_data("%s" % data) 207 d.addCallback(self.send_response) 272 ## data = json.loads(line) 273 ## 274 ## if self.DEBUG: 275 ## print "line received:", 276 ## print data 277 ## 278 ## d = self.factory.process_data("%s" % data) 279 ## d.addCallback(self.send_response) 280 281 282 self.dataReceived(line) 208 283 209 284 … … 244 319 self.data_chunk = "" 245 320 246 d = self.factory.process_data( "%s" %data_to_process)321 d = self.factory.process_data(data_to_process) 247 322 d.addCallback(self.send_response) 248 323 … … 254 329 # Special socket handling for Flash applications 255 330 if (response == FLASH_SOCKET_POLICY_FILE): 256 self.sendLine(response) 257 ## self.transport.write(response) 331 ## self.transport.write(response) 332 self.sendLine(response) 333 258 334 else: 259 335 response = json.dumps(response) … … 262 338 if self.factory.client_connected: 263 339 if self.DEBUG: 264 print "-- > [ThinkGear Emulator] Sending:",340 print "----> [ThinkGear Emulator] Sending:", 265 341 print response 266 342
Note: See TracChangeset
for help on using the changeset viewer.