Changeset 68 for remote_control/puzzlebox_thinkgear_client.py
- Timestamp:
- 06/27/10 05:37:11 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
remote_control/puzzlebox_thinkgear_client.py
r64 r68 49 49 50 50 def __init__(self, log, \ 51 52 53 51 server_host=SERVER_HOST, \ 52 server_port=SERVER_PORT, \ 53 DEBUG=DEBUG): 54 54 55 55 self.log = log … … 59 59 self.server_port = server_port 60 60 self.max_connection_attempts = MAX_CONNECTION_ATTEMPTS 61 62 63 64 65 ################################################################## 66 61 62 self.is_authorized = True 63 64 65 ################################################################## 66 67 67 def authorize_and_send_parameters(self,\ 68 68 thinkgear_parameters, \ 69 69 authorization_request): 70 70 71 71 if self.DEBUG: 72 72 print "--> [ThinkGear Client] Sending Authorization Request:", 73 73 print authorization_request 74 75 74 75 76 76 d = self.send_parameters(thinkgear_parameters, authorization_request) 77 77 d.addCallback(self.update_authorization) 78 79 80 ################################################################## 81 78 79 80 ################################################################## 81 82 82 def update_authorization(self, authorization): 83 83 84 84 if self.DEBUG: 85 85 print "--> [ThinkGear Client] Authorization:", 86 86 print authorization 87 87 88 88 self.is_authorized = authorization 89 90 91 ################################################################## 92 89 90 91 ################################################################## 92 93 93 def send_parameters(self, \ 94 parameters, \ 95 authentication=None, \ 96 max_connection_attempts=MAX_CONNECTION_ATTEMPTS): 97 98 self.factory = puzzlebox_thinkgear_client_factory(self.log, \ 99 parameters, \ 100 authentication, \ 101 self.server_host, \ 102 self.server_port, \ 103 max_connection_attempts, \ 104 self.DEBUG) 94 parameters, \ 95 authentication=None, \ 96 max_connection_attempts=MAX_CONNECTION_ATTEMPTS): 97 98 self.factory = puzzlebox_thinkgear_client_factory( \ 99 self.log, \ 100 parameters, \ 101 authentication, \ 102 self.server_host, \ 103 self.server_port, \ 104 max_connection_attempts, \ 105 self.DEBUG) 105 106 106 107 reactor.connectTCP(self.server_host, self.server_port, self.factory) 107 108 108 109 return self.factory.replyDefer 109 110 111 ################################################################## 112 110 111 112 ################################################################## 113 113 114 def send_parameters_and_print_response(self, parameters): 114 115 115 116 if self.DEBUG: 116 117 print "----> [ThinkGear Client] Sending:", 117 118 print parameters 118 119 119 120 120 121 d = self.send_parameters(parameters) 121 122 d.addCallback(self.print_response) 122 123 124 ################################################################## 125 123 124 125 ################################################################## 126 126 127 def print_response(self, response): 127 128 … … 129 130 print "--> [ThinkGear Client] Server Response:", 130 131 print response 131 132 132 133 133 134 ##################################################################### … … 135 136 ##################################################################### 136 137 137 class puzzlebox_thinkgear_client_protocol( \ 138 basic.LineReceiver): 139 140 delimiter = THINKGEAR_DELIMITER 138 class puzzlebox_thinkgear_client_protocol(basic.LineReceiver): 139 140 delimiter = THINKGEAR_DELIMITER 141 141 142 142 def __init__(self): 143 143 144 144 self.DEBUG = DEBUG 145 145 146 146 147 147 ################################################################## 148 148 149 149 def connectionMade(self): 150 151 152 153 154 155 156 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 157 if (self.factory.authentication != None): 158 158 self.send_data(self.factory.authentication) 159 159 160 160 else: 161 161 self.send_data(self.factory.parameters) 162 163 164 165 166 167 162 163 164 ################################################################## 165 166 def send_data(self, data): 167 168 168 packet = json.dumps(data) 169 169 170 170 if self.DEBUG: 171 171 print "----> [ThinkGear Client] Sending:", 172 172 print packet 173 173 174 174 175 175 self.sendLine(packet) 176 176 177 ##self.factory.noReply = reactor.callLater(NO_REPLY_WAIT, self.noReply)177 #self.factory.noReply = reactor.callLater(NO_REPLY_WAIT, self.noReply) 178 178 179 179 … … 187 187 if self.DEBUG: 188 188 print "noReply failed to call callback" 189 190 191 ################################################################## 192 189 190 191 ################################################################## 192 193 193 def lineReceived(self, line): 194 194 195 195 # Ignore blank lines 196 196 if not line: 197 197 return 198 198 199 199 try: 200 200 data = json.loads(line) … … 204 204 print e 205 205 print ")." 206 206 207 207 print "line:", 208 208 print line 209 209 210 210 # We've received back an unrecognized response 211 211 # so we'll attempt to reset the server's 212 212 # parameters for JSON communication 213 213 self.send_data(self.factory.parameters) 214 215 else: 216 217 if (type(data) == type({})): 218 219 self.factory.process_response(data) 220 #self.factory.replyDefer.callback(data) 214 221 215 else:216 217 if (type(data) == type({})):218 219 self.factory.process_response(data)220 ## self.factory.replyDefer.callback(data)221 222 222 else: 223 223 if self.DEBUG: 224 224 print "data:", 225 225 print data 226 227 228 ################################################################## 229 226 227 228 ################################################################## 229 230 230 def connectionLost(self, reason): 231 231 232 232 if self.DEBUG > 1: 233 233 print "Connection lost:", 234 234 print reason 235 236 237 238 239 240 235 236 237 ################################################################## 238 239 def connectionDone(self, reason): 240 241 241 if self.DEBUG > 1: 242 242 print "Connection done:", 243 243 print reason 244 244 245 245 246 246 ##################################################################### 247 247 # ThinkGear Client Factory class … … 252 252 def __init__(self, log, \ 253 253 parameters, \ 254 254 authentication, \ 255 255 server_host=SERVER_HOST, \ 256 256 server_port=SERVER_PORT, \ … … 272 272 273 273 self.replyDefer = defer.Deferred() 274 275 276 ################################################################## 277 274 275 276 ################################################################## 277 278 278 def process_response(self, response): 279 279 280 280 if self.DEBUG: 281 281 if (('rawEeg' not in response.keys()) or \ … … 283 283 print "<-- [ThinkGear Client] Received:", 284 284 print response 285 286 285 286 287 287 if response.has_key('isAuthorized'): 288 288 289 289 if (response['isAuthorized'] == True): 290 290 self.replyDefer.callback(True) 291 291 else: 292 292 self.replyDefer.callback(False) 293 294 293 294 295 295 ################################################################## 296 296 … … 347 347 server_port=SERVER_PORT, \ 348 348 DEBUG=DEBUG) 349 350 349 350 351 351 if ENABLE_THINKGEAR_AUTHORIZATION: 352 352 # Use ThinkGear authentication … … 355 355 thinkgear_parameters, \ 356 356 authorization_request) 357 357 358 358 else: 359 359 # Do not use ThinkGear authentication … … 363 363 thinkgear_parameters, \ 364 364 authorization_request) 365 366 365 366 367 367 reactor.run() 368 368
Note: See TracChangeset
for help on using the changeset viewer.