def __handleConnectionMessages(self, state, msgObj=None): if state == 0: self.__state = 0 self.__connected = 0 self.__handshake = 1 self.__currSeq = random.getrandbits(32) ##print "Preparing to send a SYN Message" ##print "The sequence number I am sending is :", self.__currSeq responseMessageBuilder = MessageData.GetMessageBuilder(PTCLMessage) responseMessageBuilder["Hash"].setData('0') responseMessageBuilder["MessageType"].setData("SYN") responseMessageBuilder["MessageSeq"].setData(self.__currSeq) responseMessageBuilder["AckSeq"].setData(0) responseMessageBuilder["Data"].setData("") # TODO Calculate the hash of the whole packet created above # and fill it in the HASH field of the packet msg_hash = self.hash_it(responseMessageBuilder) responseMessageBuilder["Hash"].setData(msg_hash) self.__waiting = 1 self.transport.writeMessage(responseMessageBuilder) Timer.callLater(0.1, lambda: self.checkState(0)) elif state == 1: # Server state where server received a SYN packet and will now send an SYNACK if (self.__state == 3 or self.__state == 2): ##print "Already Connected" return ##print "I have got a SYN and will now Send a SYNACK" self.__state = 1 self.__currSeq = random.getrandbits(32) #self.__curr_ackSeq = msgObj.MessageSeq ##print "The sequence number I got in the SYN is : ",msgObj.MessageSeq,"and the sequence number I am sending in the SYNACK is: ", self.__currSeq if (msgObj != None): self.__curr_ackSeq = msgObj.MessageSeq logger.debug( "Harsh: %s Hey! We got our SYN, setting curr_ackSeq to %d" % (self._addr, self.__curr_ackSeq)) #Prepare and send a SYNACK Message responseMessageBuilder = MessageData.GetMessageBuilder( PTCLMessage) responseMessageBuilder["Hash"].setData('0') responseMessageBuilder["MessageType"].setData("SYNACK") responseMessageBuilder["MessageSeq"].setData(self.__currSeq) responseMessageBuilder["AckSeq"].setData(self.__curr_ackSeq) responseMessageBuilder["Data"].setData("") msg_hash = self.hash_it(responseMessageBuilder) responseMessageBuilder["Hash"].setData(msg_hash) self.transport.writeMessage(responseMessageBuilder) Timer.callLater(0.1, lambda: self.checkState(1)) elif state == 2: if (self.__state >= 2): ##print "Multiple SYNACK, Discard" return self.__state = 2 ##print "I have got a SYNACK and will now send an ACK followed by data" ##print "The sequence number I got is : ",msgObj.MessageSeq, "and the sequence number i am sending is : ", self.__currSeq # Extract sequence number and set state variable self.__curr_ackSeq = msgObj.MessageSeq logger.debug( "Harsh: %s Hey! We got our SYNACK, setting curr_ackSeq to %d" % (self._addr, self.__curr_ackSeq)) #Prepare and send an ACK Message responseMessageBuilder = MessageData.GetMessageBuilder(PTCLMessage) responseMessageBuilder["Hash"].setData('0') responseMessageBuilder["MessageType"].setData("ACK") responseMessageBuilder["MessageSeq"].setData(0) responseMessageBuilder["AckSeq"].setData(msgObj.MessageSeq) responseMessageBuilder["Data"].setData("") #TODO Calculate the hash of the whole packet created above # and fill it in the HASH field of the packet msg_hash = self.hash_it(responseMessageBuilder) responseMessageBuilder["Hash"].setData(msg_hash) self.transport.writeMessage(responseMessageBuilder) ##print "sent an ACK , we are connected ,now sending Data:" ##print "Setting connected in client" self.__handshake = 1 self.__connected = 1 #self.__last_contig = self.__curr_ackSeq #set the timer #No timer because now data has to be sent, after sending ACK you don't # wait and start pushing data directly ##print "Sent Data" #print "Setting hb timer on receiver" self.__hb_timer = Timer.OneshotTimer(lambda: self.checkData()) self.__hb_timer.run(10) Timer.callLater(0.1, lambda: self.processQueue()) self.__session_timer = Timer.OneshotTimer( lambda: self.checkSession()) self.__session_timer.run(35) #Timer.callLater(0.1,lambda:self.checkState(2,msgObj)) #Check if the state has looped back to the same state. Store the current time # and check if it has changed after timeout elif state == 3: # Server side state, once server has received ACK from Client in response to SYNACK self.__state = 3 # Extract sequence number, check and set state variable ##print "I have got an ACK" #self.__currSeq = self.__currSeq +1 ##print "Setting connected in server" self.__connected = 1 self.__handshake = 1 self.__last_contig = msgObj.MessageSeq ##print "The sequence number I got is: ", msgObj.MessageSeq #print "Setting hb timer on sender" self.__hb_timer = Timer.OneshotTimer(lambda: self.checkData()) self.__hb_timer.run(10) self.__session_timer = Timer.OneshotTimer( lambda: self.checkSession()) self.__session_timer.run(35) elif state == 5: self.__state = 5 # Extract sequence number and set state variable ##print "I am going to send a FIN Message" self.__currSeq = self.__currSeq + 1 #Prepare and send an FIN Message responseMessageBuilder = MessageData.GetMessageBuilder(PTCLMessage) responseMessageBuilder["Hash"].setData('0') responseMessageBuilder["MessageType"].setData("FIN") responseMessageBuilder["MessageSeq"].setData(self.__currSeq) responseMessageBuilder["AckSeq"].setData(0) responseMessageBuilder["Data"].setData("") #TODO Calculate the hash of the whole packet created above # and fill it in the HASH field of the packet msg_hash = self.hash_it(responseMessageBuilder) responseMessageBuilder["Hash"].setData(msg_hash) self.transport.writeMessage(responseMessageBuilder) #set the timer self.__timely = Timer.callLater(0.1, lambda: self.checkState(5)) elif state == 6: ##print "I got a FIN Message and I will send a FINACK" self.__finhandshake = 1 self.__state = 6 self.__curr_ackSeq = msgObj.MessageSeq #Prepare and send an FINACK Message responseMessageBuilder = MessageData.GetMessageBuilder(PTCLMessage) responseMessageBuilder["Hash"].setData('0') responseMessageBuilder["MessageType"].setData("FINACK") responseMessageBuilder["MessageSeq"].setData(0) responseMessageBuilder["AckSeq"].setData(self.__curr_ackSeq) responseMessageBuilder["Data"].setData("") #TODO Calculate the hash of the whole packet created above # and fill it in the HASH field of the packet msg_hash = self.hash_it(responseMessageBuilder) responseMessageBuilder["Hash"].setData(msg_hash) ##print "Sending FINACK now: " self.transport.writeMessage(responseMessageBuilder) Timer.callLater( 0.1, lambda: self.getHigherProtocol().connectionLost( "Received FIN")) self.transport.loseConnection() self.__connected = 0 elif state == 7: ###print "I am suppose to send an ACK for data received:" responseMessageBuilder = MessageData.GetMessageBuilder(PTCLMessage) responseMessageBuilder["Hash"].setData('0') responseMessageBuilder["MessageType"].setData("ACK") responseMessageBuilder["MessageSeq"].setData(0) logger.info("Harsh: %s Sending ACK with ACK Sequence %d" % (self._addr, self.__curr_ackSeq)) responseMessageBuilder["AckSeq"].setData(self.__curr_ackSeq) # There is no data being piggy bagged on the ACK , if required put the data here from the # buffer where it is stored. responseMessageBuilder["Data"].setData("") responseMessageBuilder["Hash"].setData( self.hash_it(responseMessageBuilder)) self.transport.writeMessage(responseMessageBuilder) elif state == 8: self.__state = 8 self.__currSeq = 0 self.__curr_ackSeq = 0 ##print "I received a FINACK - Trying to terminate connection" ##print "dataList and rcvWindow are both empty, safe to terminate, connectionLost() called" Timer.callLater( 0.2, lambda: self.getHigherProtocol().connectionLost( "Received FINACK")) self.transport.loseConnection() if self.__timely: self.__timely.cancel() self.__connected = 0 else: pass