Esempio n. 1
0
def main():
	global terminated
	
	print 'Starting chat'
    
	print 'Calling repaOpen()'
	sock = repa.repaOpen()
	
	interest = 'app-chat://message'
	print 'Registering interest "%s"' % (interest)
	repa.registerInterest(sock, interest)
	
	prefix = repa.prefixToString(repa.getRepaNodeAddress()) 
	print 'NodePrefix: %s' % prefix
    
	thread.start_new_thread( handle_message, ( "Thread-1", sock, ) )
	
	text = raw_input('Message: ').decode('utf8')
	while text != 'exit':
		send_data = text.encode('utf8')
		data_len = len(send_data)
		if data_len > 0:
			result = repa.repaSend(sock, interest, send_data, 0)
			if result > 0:
				print '\tMessage sent I: "%s" D: "%s"' % (interest.decode('utf8'), text.decode('utf8')) 
			else:
				print 'CHATERROR: Send message (%d)' % (result)
    
		text = raw_input('Message: ').decode('utf8')
    
	print 'Exiting...'
	terminated = True;
	repa.repaClose(sock)
Esempio n. 2
0
def handle_message( threadName, sock ):
	global terminated
	
	read_len = 0
	terminated = False
	while not terminated:
		try:
			time.sleep( 0.001 ) # It is like a Thread yield, sleep for 1ms
			message = repa.repaRecv(sock,1) # Wait for 1us
			if message is not None:
				if len(message[1]) > 0:
					prefix = repa.prefixToString(message[2]) 
					print '\n\tReceive %s from %s I: "%s" D: "%s"\n' % (len(message[1].decode('utf8')),prefix,message[0].decode('utf8'),str(message[1]).decode('utf8'))
		except:
			pass
	return
Esempio n. 3
0
def main():
    global terminated
    global sock
    
    print 'Request Info'
    
    print 'Calling repaOpen()'
    sock = repa.repaOpen()
    
    text = raw_input('Request: ').decode('utf8')
    while text != 'exit':
        if text == 'info':
            repa.registerInterest(sock, INT_SND_INFO)
            
            if repa.repaSend(sock, INT_RQT_SERVER, RQT_INFO, 0) > 0:
                msg = repa.repaRecv(sock, 1000000)
                while msg is not None:
                    prefix = repa.prefixToString(msg[2]) 
                    print "\t%s <%s>" % (prefix,msg[1])
                    msg = repa.repaRecv(sock, 1000000)
                    
            repa.unregisterInterest(sock, INT_SND_INFO)
        elif text == 'image':
            repa.registerInterest(sock, INT_SND_IMAGE)
            
            with open('receive-image.jpg', 'wb') as f:
                if repa.repaSend(sock, INT_RQT_SERVER, RQT_IMAGE, 0) > 0:
                    packet_num = INT8_C(0)
                    msg = repa.repaRecv(sock, 5000000)
                    while (msg is not None) and (len(msg[1]) > 1):
                        recv_packet_num = ord(msg[1][0])
                        if recv_packet_num == packet_num:
                            if repa.repaSend(sock, INT_SND_IMAGE_ACK, chr(packet_num), 0) <= 0:
                                print "Error on sent ack!"
                            packet_num += 1
                        f.write(msg[1][1:])
                        sys.stdout.write("Download progress: %d%%   \r" % (progress) )
                        sys.stdout.flush()
                        msg = repa.repaRecv(sock, 5000000)
                    print "Image received!"
                else:
                    print "Request was not sent!"
                
            repa.unregisterInterest(sock, INT_SND_IMAGE)
        elif text == 'ds18b20':
            repa.registerInterest(sock, INT_SND_TEMPERATURE)
            
            if repa.repaSend(sock, INT_RQT_SERVER, RQT_TEMPERATURE, 0) > 0:
                msg = repa.repaRecv(sock, 1000000)
                while msg is not None:
                    prefix = repa.prefixToString(msg[2]) 
                    values = array.array("I", msg[1])
                    print "\t%s <Temperatura %gº>" % (prefix,float(values[0])/1000)
                    msg = repa.repaRecv(sock, 1000000)
                
            repa.unregisterInterest(sock, INT_SND_TEMPERATURE)
        elif text == 'dht11':
            repa.registerInterest(sock, INT_SND_HUMIDITY)
            
            if repa.repaSend(sock, INT_RQT_SERVER, RQT_HUMIDITY, 0) > 0:
                msg = repa.repaRecv(sock, 1000000)
                while msg is not None:
                    prefix = repa.prefixToString(msg[2]) 
                    values = array.array("I", msg[1])
                    print "\t%s <Temperatura %gº\tHumidade %g>" % (prefix,float(values[0])/1000, float(values[1])/1000)
                    msg = repa.repaRecv(sock, 1000000)
                    
            repa.unregisterInterest(sock, INT_SND_HUMIDITY)
            
        text = raw_input('Request: ').decode('utf8')
    print 'Exiting...'
    repa.repaClose(sock)