def askVote(self, request, context): print "Receive ask vote request" if self.flag == 0: self.flag += 1 res = inner_data_pb2.Response() res.msg = 'ok' return res else: self.flag += 1 if self.flag == 3: self.flag = 0 res = inner_data_pb2.Response() res.msg = 'wait' return res
def setLeader(self, request, context): print "Receive set leader request" print request self.role = 'leader' with open('info.txt','w') as f: f.write(request.fromSender) res = inner_data_pb2.Response() res.msg = 'success' return res
def ping(self, request, context): print "Receive ping request" fromSender = request.fromSender toReceiver = request.toReceiver originalSender = request.originalSender # print(fromSender,toReceiver,originalSender) response = inner_data_pb2.Response() response.msg = 'Ping Success' #status_code = data_pb2.StatusCode() status_code = 1 response.Code = status_code # print "Send Ping response" return response
def putHandler(self, request_iterator, context): if self.role == 'leader': for i in self.nodes: if i != '0.0.0.0:8080': print i host = i.split(':')[0] port = int(i.split(':')[1]) cli = Client(host, port) cli.stub.putHandler(request_iterator) print "Receive put request" buffer = [] timestamp = '' for request in request_iterator: print request fromSender = request.fromSender toReceiver = request.toReceiver originalSender = request.originalSender putRequest = request.putRequest metaData = putRequest.metaData DatFragment = putRequest.datFragment if DatFragment.timestamp_utc: timestamp = DatFragment.timestamp_utc data = DatFragment.data #if timestamp != '': # No timestamp, mesonet data # data = self.normalize_data_mesonet(DatFragment.data, timestamp) #else: # Mesowest data # data = self.normalize_data_mesowest(DatFragment.data) buffer.append(data) # Save buffer to my mongo database print "Saving buffer to mongodb...." print "Timestamp is " + str(timestamp) client = MongoClient('localhost', 27017) db = client.pymongo_test #if timestamp: # insert_bulk_mongo_mesonet(db, buffer, timestamp) #else: insert_bulk_mongo(db, buffer) print "Finish saving buffer to mongodb" # # Save buffer to File # #timestamp_utc = DatFragment.timestamp_utc # file_name = 'timestamp_utc' + '.txt' # with open(file_name, 'w') as file: # file.write(str(buffer)) # # Call comand to send file to clsuter # call(['../ProjectCluster/client.sh', ' 1 -write -' + file_name]) # #os.system('sh ../ProjectCluster/client.sh 1 -write -' + file_name) # Reponse to server response = inner_data_pb2.Response() status_code = 1 response.Code = status_code response.msg = 'Put Request success' datFragment = inner_data_pb2.DatFragment() datFragment.data = "Success".encode() response.datFragment.data = "Success".encode() #dataFragment.data = "Success".encode() #response.datFragment=DatFragment(data="Success".encode()) line = "Success" #response=Response( # Code=1, ## datFragment=DatFragment(timestamp_utc="0/0/0", data=line) #) print response return response