def marsh_err(err): m_err = pb.Err() m_err.err = err msg = pb.Msg() msg.err.CopyFrom(m_err) encoded = msg.SerializeToString() return encoded
def marsh_get(rep_id, key, consistency): m_get = pb.Get() m_get.id = rep_id m_get.key = key m_get.consistencylevel = consistency msg = pb.Msg() msg.Get.CopyFrom(m_get) encoded = msg.SerializeToString() return encoded
def marsh_putresp(rep_id, key, status): m_putresp = pb.PutResponse() m_putresp.id = rep_id m_putresp.key = key m_putresp.status = status msg = pb.Msg() msg.PutRequest.CopyFrom(m_putresp) encoded = msg.SerializeToString() return encoded
def marsh_put(rep_id, key, value, consistency): m_put = pb.Put() m_put.id = rep_id m_put.key = key m_put.value = value m_put.consistencylevel = consistency msg = pb.Msg() msg.Put.CopyFrom(m_put) encoded = msg.SerializeToString() return encoded
def marsh_getresp(resp_lst): resp = pb.GetResponseLst() for i in resp_lst: m_getresp = resp.response.add() m_getresp.id = i[0] m_getresp.value = i[1] m_getresp.version = i[2] msg = pb.Msg() msg.GetRequestLst.CopyFrom(resp) encoded = msg.SerializeToString() return encoded
def getReqHandler(self, key, consistency, s): msg = pb.Msg() msg.Get.id = -1 msg.Get.key = key msg.Get.consistencylevel = consistency print("Sending get msg") s.sendall(msg.SerializeToString()) recv = s.recv(1000) pb_msg = pb.Msg() pb_msg.ParseFromString(recv) print(pb_msg)
def putReqHandler(self, key, value, consistency, s): msg = pb.Msg() msg.Put.id = -1 msg.Put.key = key msg.Put.value = value msg.Put.consistencylevel = consistency s.sendall(msg.SerializeToString()) recv = s.recv(10000) pb_msg = pb.Msg() pb_msg.ParseFromString(recv) print(pb_msg)
def handle(self): counter = 0 p_ip = self.client_address[0] p_port = self.client_address[1] print("Handling message from "+str(p_ip)+':'+str(p_port)) while 1: if counter > 10: print('infinite while loop, break') break message = self.request.recv(10000) pb_msg = pb.Msg() pb_msg.ParseFromString(message) # Read from replica if pb_msg.HasField('Get'): rep_id = pb_msg.Get.id key = pb_msg.Get.key consistency = pb_msg.Get.consistencylevel if rep_id == -1: self.server.replica.read(key, consistency, self.request) else: self.server.replica.get_update(key, self.request) # Read response if pb_msg.HasField('GetRequestLst'): rep_id = pb_msg.GetResponse.id value = pb_msg.GetResponse.value version = pb_msg.GetResponse.version status = pb_msg.GetResponse.status ''' Process response in local replica obj ''' # Write value to replica if pb_msg.HasField('Put'): rep_id = pb_msg.Put.id key = pb_msg.Put.key value = pb_msg.Put.value consistensy = pb_msg.Put.consistencylevel if rep_id == -1: self.server.replica.write(key, value, consistensy, self.request) else: # get hint from local replica if rep_id in self.server.replica.hint: print("Hint handoff") # send lost value back to coord h = self.server.replica.hint[rep_id] for i in h: print("Send hint back to coord") print(i) msg = util.marsh_put( rep_id, i[0], i[1], consistensy ) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # TCP info = self.server.replica.rep_lst[rep_id-1] sock.connect((info[1], info[2])) sleep(1) sock.sendall(msg) sock.close() del self.server.replica.hint[rep_id] print("Get hint") self.server.replica.update(key, value, self.request) ''' # Write response if pb_msg.HasField('PutResponse'): rep_id = pb_msg.PutResponse.id key = pb_msg.PutResponse.key status = pb_msg.PutResponse.status ''' counter += 1