def receive(self,addr_source, port_source):
        code=0
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        s.bind((addr_source, port_source)) 
	s.setblocking(0)
        msgHandler=MsgHandler()
        #a=time.time() 
        #time.sleep(5)
        #b=time.time()
        #print b-a
        lastSend=time.time()
	start_time=lastSend
        number_node_activated=0
        while True:
		try:
		    #time.sleep(1)
		    #print str(time.time()-lastSend)
		    if (time.time()-lastSend)>COMMUNICATION_TIMEOUT and self.HasInit==True and lastSend!=start_time:
			print 'lost connection with cluster head for : ' + str(time.time()-lastSend)
		        self.Change_to_spare_CH()
			print 'change to spare cluster head: '+ str(self.clusterHead)
		        msg=msgHandler.Encode_CH_Change_Msg(self.clusterHead[0],self.clusterHead[1],self.clusterHead[2])
		        for eachNode in self.network:
		            code=self.send(eachNode[0], PORT, msg)
			lastSend=time.time()
                        print 'Reset lastSend time: '  +str (lastSend)
		    data, addr = s.recvfrom(1024)
		    
		    #print 'received' + data
		    type_msg=msgHandler.Decode(data)
		    if type_msg==1:
		        temp,code=msgHandler.Decode_CH_Change_Msg(data)
		        if code==0:
		            self.clusterHead=temp
		            self.RefreshNetwork(temp)
		            print '******************************'
		            print 'received: ' + str(temp)
		            print 'CH changed:'
		            print self.clusterHead
		        else:
		            print "error in decoding CH change msg."
		    elif type_msg==2:
		        temp,code=msgHandler.Decode_List_Info_Msg(data)
		        if code==0:
		            self.network=temp
			        self.clusterHead=temp[0]
                    lastSend=time.time()
		            print 'Received network info: '
                    print str(self.network)
                    print ' '
		        else:
Example #2
0
    def receive(self, addr_source, port_source):
        print 'thread start'
        code = 0
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.bind((addr_source, port_source))
        msgHandler = MsgHandler()
        while True:
            data, addr = s.recvfrom(1024)
            print 'received' + data
            type_msg = msgHandler.Decode(data)
            if type_msg == 1:
                temp, code = msgHandler.Decode_CH_Change_Msg(data)
                if code == 0:
                    self.clusterHead = temp
                    self.RefreshNetwork(temp)
                    print 'CH changed:' + self.clusterHead
                else:
                    print "error in decoding CH change msg."
            elif type_msg == 2:
                temp, code = msgHandler.Decode_List_Info_Msg(data)
                if code == 0:
                    self.network = temp
                    print self.network
                else:
                    print "error in decoding list of info msg"
            elif type_msg == 3:
                temp, code = msgHandler.Decode_Info_Msg(data)
                if code == 0:
                    print '-----------------3.--------'
                    Ischanged = self.RefreshNode(temp)

                    print temp
                    print Ischanged
                else:
                    print 'Error in decoding info msg'
            elif type_msg == 4:
                temp, code = msgHandler.Decode_Sensor_Data(data)
                if code == 0:
                    self.allSensorData = self.allSensorData + temp
                    with self.networkLock:
                        for i in range(len(self.network)):
                            if self.network[i][0][0] == self.addrCH:
                                self.network[i][0][3] = time.time()
                    print temp
                else:
                    print 'Error in decoding sensor data'

            elif type_msg == 0:
                print 'error in decode message'
    def receive(self,addr_source, port_source):
        code=0
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        s.bind((addr_source, port_source)) 
        msgHandler=MsgHandler()
        while True:                                          
            data, addr = s.recvfrom(1024)
            #print 'received' + data
            type_msg=msgHandler.Decode(data)
            if type_msg==1:
                temp,code=msgHandler.Decode_CH_Change_Msg(data)
                if code==0:
                    judge = self.calculateCoor(temp[2], self.coordinate)
                    if judge:
                        self.clusterHead=temp
                        self.RefreshNetwork(temp)
                        print '******************************'
                        print 'Received: CH change message: ' + str(temp)
                    else:
                        newInfo = [self.addr, self.energy, self.coor]
                        self.clusterHead = newInfo
                        self.RefreshNetwork(newInfo)
                        print '******************************'
                        print 'Received: CH change message: ' + str(newInfo)
                        print ' '

                    print 'CH is changed to: '  + str(self.clusterHead)
                    print ' '
                else:
                    print "Error in decoding CH change msg."
                    print ' '
            elif type_msg==2:
                temp,code=msgHandler.Decode_List_Info_Msg(data)
                if code==0:
                    self.network=temp
                    print 'Received: ' + addr_source
                    print self.network
                else:
                    print "error in decoding list of info msg"
            elif type_msg==3:
                temp,code=msgHandler.Decode_Info_Msg(data)
                if code==0:                 
                    Ischanged=self.RefreshNetwork(temp)
                    print '******************************'
                    #print temp[0]
                    print 'Received Info msg from:  ' + str(temp[0])
                    print ' '
                    #print temp
                    #print Ischanged
                else:
                    print 'Error in decoding info msg'
                    print ' '
            elif type_msg==4:
                temp,code=msgHandler.Decode_Sensor_Data(data)
                if code==0:
                    self.allSensorData=self.allSensorData+temp  
                    print '******************************'
                    print 'Received sensor data: '  + str(temp)
                    print ' '
                else:
                    print 'Error in decoding sensor data' 
                    print ' '              
                #TODO: boradcast response 
            elif type_msg==5:
                 addr_des,code=msgHandler.Decode_Broadcast_msg(data)
                 if code==0:
                    print '******************************'
                    print 'Received broadcast from:' + addr_des
                    print ' '
                    msg=msgHandler.Encode_Info_Msg(self.addr,self.energy,self.coordinate)
                    IsSent = self.send(addr_des, PORT, msg)
                    #print 'IsSent; ' + str(IsSent)
                 else:
                    print 'Error in decoding broadcast message.' 
                    print ' '  
            elif type_msg==0:
                print 'Error in decode message: ' + data
                print ' '
        #analyze data
        # connect to the speicified address and port
        # receive message
        # analyze message
        # store important information into the msg

        # return action status code
        # 0: success
        # 1: fail
        return code