def __init__(self,config,smart=False): self.config=config self.smart=smart self.messages={} self.sentmessages={} self.corr_conn={} self.messagelist=[] self.deadconnections=[] self.proxies=[] self.__correlationid__=0 self.connections=[] self.messagesignal={} self.events=[] self.eventregistry={} self.partitiontable=None firstConnection=HazelConnection(config.gethost(),config.getport(),self) self.connections.append(firstConnection) self.iothread=threading.Thread(target=self.ioloop) self.iothread.start() #get the first response from the server initialresponse=self.getPackageWithCorrelationId(self.__correlationid__-1,True) print "did we get here?" initialresponse=clientcodec.ClientAuthenticationCodec.decodeResponse(ClientMessage.decodeMessage(initialresponse)) self.uuid=initialresponse.uuid self.owneruuid=initialresponse.ownerUuid #if the client is smart, initialize other connections if initialresponse is not None: print "Connection has been initalized" else: print "There was an error connecting to the server!" if smart: msg=clientcodec.ClientGetPartitionsCodec.encodeRequest() self.adjustCorrelationId(msg) retryable=msg.retryable correlationid=msg.correlation self.sendPackage(msg) response=self.getPackageWithCorrelationId(correlationid,retryable) msg2=ClientMessage.decodeMessage(response) response=clientcodec.ClientGetPartitionsCodec.decodeResponse(msg2) self.updatePartitionTable(response.index) self.updateMemberList(response.members) #else: #raise Timeout Exception self.eventthreadflag=threading.Event() self.event_thread=threading.Thread(target= self.eventloop) self.event_thread.start()
def process_input(self,input): """ Process the input and put it in either received messages or events :param input: raw bytes received """ while len(input) > 0: clientmsg=ClientMessage.decodeMessage(input) msgsize=clientmsg.FRAME_SIZE currentinput=input[:msgsize] currentmsg=ClientMessage.decodeMessage(currentinput) if currentmsg.isEvent(): if not self.manager.eventthreadflag.is_set(): self.manager.eventthreadflag.set() self.manager.events.append(clientmsg) else: self.manager.messagesignal[currentmsg.correlation].set() self.manager.messages[currentmsg.correlation]=currentinput input=input[msgsize:]
def adjustPartitionId(self,clientmsg,opkey): msg=clientcodec.ClientGetPartitionsCodec.encodeRequest() self.adjustCorrelationId(msg) retryable=msg.retryable correlationid=msg.correlation self.sendPackage(msg) response=self.getPackageWithCorrelationId(correlationid,retryable) msg2=ClientMessage.decodeMessage(response) response=clientcodec.ClientGetPartitionsCodec.decodeResponse(msg2) newpartition=util.util.computepartitionid(response.index,opkey) clientmsg.partition=newpartition self.updatePartitionTable(response.index) self.updateMemberList(response.members)
def process_input(self,input): clientmsg=ClientMessage.decodeMessage(input) if clientmsg.isEvent(): self.manager.events.append(input) else: self.manager.messages[clientmsg.correlation]=input