Exemplo 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)
Exemplo n.º 2
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)