def run(self): """ Entry point for UploadData thread. Details about the uploading method :func:`here <connections.uploadData.UploadData.sendPostRequest>`. """ logging.log('I', 'Starting Post Request') self.sendPostRequest() logging.log('I', 'Exiting From the Post Request Thread')
def run(self): logging.log('I', 'Starting Listening to Redis Channel for HTTP Post Requests') while (True): shouldEnd = self.listenToChannel(self.ps, self.r) if (shouldEnd): break logging.log('I', 'Exiting Redis Thread for HTTP Post requests')
def run(self): logging.log('I', 'Starting Socket Connection Thread') self.setupSocketIO() logging.log('I', 'Exiting Socket Connection Thread') local_server.server.stop() print 'Exit Program by pressing Control + C'
def run(self): logging.log('I', 'Listening to Redis Channel') while (True): shouldEnd = self.listenToChannel(self._pubsub_obj, self._redis_obj) if (shouldEnd): break logging.log('I', 'Ending Listing to Redis Channel')
def run(self): logging.log("I", "Listening to Redis Channel") while True: shouldEnd = self.listenToChannel(self._pubsub_obj, self._redis_obj) if shouldEnd: break logging.log("I", "Ending Listening to Redis Channel")
def run(self): logging.log( 'I', 'Starting Listening to Redis Channel for HTTP Post Requests') while (True): shouldEnd = self.listenToChannel(self.ps, self.r) if (shouldEnd): break logging.log('I', 'Exiting Redis Thread for HTTP Post requests')
def run(self): """ An entry point for the Socket Connection thread. """ logging.log('I', 'Starting Socket Connection Thread') self.setupSocketIO() logging.log('I', 'Exiting Socket Connection Thread')
def run(self): """ An entry point for the Socket Connection thread. """ logging.log("I", "Starting Socket Connection Thread") self.setupSocketIO() logging.log("I", "Exiting Socket Connection Thread")
def run(self): """ Entry point for Redis channel listening thread. Thread is ended depending on the current message across the channel. """ logging.log('I', 'Starting Listening to Redis Channel for HTTP Post Requests') while (True): shouldEnd = self.listenToChannel(self.ps, self.r) if (shouldEnd): break logging.log('I', 'Exiting Redis Thread for HTTP Post requests')
def getRequest(self, url): token = '' try: data = requests.get(url) for line in data: m = re.search('(META:{\'CSRF_COOKIE\': \')((\\w)+)(\',)', line) if m is not None: token = m.group(2) except Exception as e: logging.log('W', e) return token
def setupSocketIO(self): global socketio try: self._socket_io = SocketIO('godel.ece.vt.edu', 8000) self._socket_io.on('connect', self.connection) self._socket_io.on('message', self.on_aaa_response) self._socket_io.on('error', self.on_error_response) socketio = self._socket_io self._socket_io.wait() except Exception as e: logging.log('W', e) raise SystemExit
def setupSocketIO(self): """ Establishes a socket connection and adds event handlers for ``connet``, ``message``, ``error`` events. """ global socketio try: self._socket_io = SocketIO(conf.SOCKET_URL, 80) self._socket_io.on('connect', self.connection) self._socket_io.on('message', self.on_aaa_response) self._socket_io.on('error', self.on_error_response) socketio = self._socket_io self._socket_io.wait() print 'Socket waiting finished. \n' except Exception as e: logging.log('W', e) raise SystemExit
def setupSocketIO(self): """ Establishes a socket connection and adds event handlers for ``connet``, ``message``, ``error`` events. """ global socketio try: self._socket_io = SocketIO(conf.SOCKET_URL, 80) self._socket_io.on("connect", self.connection) self._socket_io.on("message", self.on_aaa_response) self._socket_io.on("error", self.on_error_response) socketio = self._socket_io self._socket_io.wait() print "Socket waiting finished. \n" except Exception as e: logging.log("W", e) raise SystemExit
def outputToFile(self): """ Creates a output file with appropriate permissions and writes obtained output to it. """ try: if not os.path.exists(self.resultpath): os.makedirs(self.resultpath) os.chmod(self.resultpath, 0776) if self.output != '': f = open(self.resultpath + '/output.txt', 'w') f.write(self.output) f.close() print 'Output Written to File: ' + self.resultpath + '/output.txt' else: print 'No Output Present' except OSError as oserror: logging.log('W', 'Check Output Path in your config. This is usually caused while trying to write in a directory with limited permissions') logging.log('W', str(traceback.format_exc())) except Exception as e: logging.log('W', 'Error Writing Output.') logging.log('W', str(traceback.format_exc()))
def on_aaa_response(self, *args): """ Handler for messages received during the socket connection. Depending on the message received this method can assign a ``jobID``, ``socketID``, provides job completion acknowledgment, gives output of a job. At the end, the resultant image/text file is downloaded from the message received. """ message = args[0] if "socketid" in message: self._redis_obj.publish("intercomm", message["socketid"]) self._socketid = message["socketid"] if "jobid" in message: print "Received JobID: " + message["jobid"] job.job.jobid = message["jobid"] if "name" in message: logging.log("O", message["name"]) # self._socket_io.emit('send_message', self._executable) if "done" in message: self._socket_io.emit("send_message", self._executable) if "jobinfo" in message: # logging.log('O', 'Received information regarding the current job') print message["jobinfo"] job.job.jobinfo = message["jobinfo"] if "data" in message: logging.log("O", message["data"]) job.job.output = message["data"] if job.job.jobid is None: job.job.jobid = "" resultpath = self._imagepath.rstrip("/") + "/" + job.job.jobid job.job.resultpath = resultpath job.job.executable = self._executable print "Data Received from Server" self._redis_obj.publish("intercomm", "***end***") if "picture" in message: logging.log("D", message["picture"]) resultpath = self._imagepath.rstrip("/") + "/" + message["jobid"] job.job.setJobID(message["jobid"]) job.job.resultpath = resultpath job.job.executable = self._executable try: if not os.path.exists(resultpath): os.makedirs(resultpath) os.chmod(resultpath, 0775) i = 0 while i < 10: try: file = requests.get(os.path.join(conf.BASE_URL + message["picture"])) file_name = basename(urlparse(message["picture"]).path) f = open(resultpath + "/" + file_name, "wb") f.write(file.content) f.close() job.job.addFiles(resultpath + "/" + file_name) logging.log("D", "File Saved: " + resultpath + "/" + file_name) break except Exception as e: print "Error Connecting to CloudCV. Will try again" i += 1 except Exception as e: logging.log("W", str(traceback.format_exc())) logging.log("W", str("possible reason: Output format improper")) if "mat" in message: logging.log("D", message["mat"]) file = requests.get(os.path.join(conf.BASE_URL, message["mat"])) with open(self._imagepath + "/results" + self._socketid + ".txt", "wb") as f: f.write(file.content) logging.log("D", "Results Saved: " + self._imagepath + "/results" + self._socketid + ".txt") if "request_data" in message: print "Data request from Server" self._socket_io.emit("send_message", "data") if "exit" in message: logging.log("W", message["exit"]) self._redis_obj.publish("intercomm", "***end***")
def run(self): logging.log('I', 'Starting Post Request') self.sendPostRequest() logging.log('I', 'Exiting From the Post Request Thread')
def on_aaa_response(self, *args): message = args[0] if ('socketid' in message): self._redis_obj.publish('intercomm', message['socketid']) self._socketid = message['socketid'] if ('name' in message): logging.log('O', message['name']) self._socket_io.emit('send_message', self._executable) if ('data' in message): logging.log('O', message['data']) job.job.output = message['data'] self._redis_obj.publish('intercomm', '***end***') if ('picture' in message): logging.log('D', message['picture']) resultpath = self._imagepath.rstrip('/') + '/' + message['jobid'] job.job.setJobID(message['jobid']) job.job.resultpath = resultpath job.job.executable = self._executable try: if not os.path.exists(resultpath): os.makedirs(resultpath) os.chmod(resultpath, 0776) i = 0 while i < 10: try: file = requests.get(message['picture']) break except Exception as e: print 'Error Connecting to CloudCV. Will try again' i += 1 file_name = basename(urlparse(message['picture']).path) f = open(resultpath + '/' + file_name, 'wb') f.write(file.content) f.close() job.job.addFiles(resultpath + '/' + file_name) except Exception as e: logging.log('W', str(traceback.format_exc())) logging.log('W', str('possible reason: Output format improper')) logging.log('D', 'File Saved: ' + resultpath + '/' + file_name) if ('mat' in message): logging.log('D', message['mat']) file = requests.get(message['mat']) with open(self._imagepath + '/results' + self._socketid + '.txt', 'wb') as f: f.write(file.content) logging.log( 'D', 'Results Saved: ' + self._imagepath + '/results' + self._socketid + '.txt') if ('request_data' in message): self._socket_io.emit('send_message', 'data')
def on_aaa_response(self, *args): """ Handler for messages received during the socket connection. Depending on the message received this method can assign a ``jobID``, ``socketID``, provides job completion acknowledgment, gives output of a job. At the end, the resultant image/text file is downloaded from the message received. """ message = args[0] if ('socketid' in message): self._redis_obj.publish('intercomm', message['socketid']) self._socketid = message['socketid'] if ('jobid' in message): print 'Received JobID: ' + message['jobid'] job.job.jobid = message['jobid'] if ('name' in message): logging.log('O', message['name']) # self._socket_io.emit('send_message', self._executable) if ('done' in message): self._socket_io.emit('send_message', self._executable) if ('jobinfo' in message): # logging.log('O', 'Received information regarding the current job') print message['jobinfo'] job.job.jobinfo = message['jobinfo'] if ('data' in message): logging.log('O', message['data']) job.job.output = message['data'] if (job.job.jobid is None): job.job.jobid = '' resultpath = self._imagepath.rstrip('/') + '/' + job.job.jobid job.job.resultpath = resultpath job.job.executable = self._executable print "Data Received from Server" self._redis_obj.publish('intercomm', '***end***') if ('picture' in message): logging.log('D', message['picture']) resultpath = self._imagepath.rstrip('/') + '/' + message['jobid'] job.job.setJobID(message['jobid']) job.job.resultpath = resultpath job.job.executable = self._executable try: if not os.path.exists(resultpath): os.makedirs(resultpath) os.chmod(resultpath, 0775) i = 0 while i < 10: try: file = requests.get( os.path.join(conf.BASE_URL + message['picture'])) file_name = basename(urlparse(message['picture']).path) f = open(resultpath + '/' + file_name, 'wb') f.write(file.content) f.close() job.job.addFiles(resultpath + '/' + file_name) logging.log( 'D', 'File Saved: ' + resultpath + '/' + file_name) break except Exception as e: print 'Error Connecting to CloudCV. Will try again' i += 1 except Exception as e: logging.log('W', str(traceback.format_exc())) logging.log('W', str('possible reason: Output format improper')) if ('mat' in message): logging.log('D', message['mat']) file = requests.get(os.path.join(conf.BASE_URL, message['mat'])) with open(self._imagepath + '/results' + self._socketid + '.txt', 'wb') as f: f.write(file.content) logging.log( 'D', 'Results Saved: ' + self._imagepath + '/results' + self._socketid + '.txt') if ('request_data' in message): print 'Data request from Server' self._socket_io.emit('send_message', 'data') if ('exit' in message): logging.log('W', message['exit']) self._redis_obj.publish('intercomm', '***end***')
def on_aaa_response(self, *args): message = args[0] if ('socketid' in message): self._redis_obj.publish('intercomm', message['socketid']) self._socketid = message['socketid'] if ('name' in message): logging.log('O', message['name']) self._socket_io.emit('send_message', self._executable) if ('data' in message): logging.log('O', message['data']) job.job.output = message['data'] self._redis_obj.publish('intercomm', '***end***') if ('picture' in message): logging.log('D', message['picture']) resultpath = self._imagepath.rstrip('/') + '/' + message['jobid'] job.job.setJobID(message['jobid']) job.job.resultpath = resultpath job.job.executable = self._executable try: if not os.path.exists(resultpath): os.makedirs(resultpath) os.chmod(resultpath, 0776) i =0 while i<10: try: file = requests.get(message['picture']) break except Exception as e: print 'Error Connecting to CloudCV. Will try again' i+=1 file_name = basename(urlparse(message['picture']).path) f = open(resultpath + '/' + file_name, 'wb') f.write(file.content) f.close() job.job.addFiles(resultpath + '/' + file_name) except Exception as e: logging.log('W', str(traceback.format_exc())) logging.log('W', str('possible reason: Output format improper')) logging.log('D', 'File Saved: ' + resultpath + '/' + file_name) if ('mat' in message): logging.log('D', message['mat']) file = requests.get(message['mat']) with open(self._imagepath + '/results' + self._socketid + '.txt', 'wb') as f: f.write(file.content) logging.log('D', 'Results Saved: ' + self._imagepath + '/results' + self._socketid + '.txt') if ('request_data' in message): self._socket_io.emit('send_message', 'data')
def sendPostRequest(self): """ Sends POST request containing the images to the server. For the payload data refer ``params_data`` parameter of :func:`this <connections.uploadData.UploadData.addFileParameters>` function. """ try: params_for_request = {} params_data = {} token = self.getRequest('http://cloudcv.org/api') print token if token is None: logging.log('W', 'token not found') raise SystemExit source, source_path = self.identifySourcePath() self.addAccountParameters(params_data, source) self.addFileParameters(source, source_path, params_data, params_for_request) job.job.imagepath = source_path params_data['token'] = token params_data['socketid'] = '' params_data['executable'] = self.exec_name params_data['exec_params'] = str(self.params) logging.log('D', 'Source Path: ' + self.source_path) logging.log('D', 'Executable: ' + params_data['executable']) logging.log('D', 'Executable Params: ' + params_data['exec_params']) while True: if self.socketid != '' and self.socketid is not None: params_data['socketid'] = (self.socketid) print 'SocketID: ', (self.socketid) break else: self._redis_obj.publish('intercomm2', 'getsocketid') time.sleep(3) logging.log('W', 'Waiting for Socket Connection to complete') # for k,v in params_for_request.items(): # params_data[k] = v logging.log('D', 'Starting The POST request') for i in range(1,5): try: request = requests.post("http://cloudcv.org/api/", data=params_data, files=params_for_request) # logging.log('D', 'Response: ' + request.text) logging.log('D', 'Info: ' + 'Please wait while CloudCV runs your job request') break except Exception as e: logging.log('W', 'Error in sendPostRequest' + str(traceback.format_exc())) except Exception as e: logging.log('W', str(traceback.format_exc())) self._redis_obj.publish('intercomm', '***end***')