From a48d9ecd95a20484e218de71e21dfffa03262b46 Mon Sep 17 00:00:00 2001 From: Philip Rosedale Date: Tue, 12 Feb 2013 13:18:19 -0800 Subject: [PATCH] Added ping detection between agents --- interface/src/Agent.cpp | 31 ++++++++++++++++++++++++++++-- interface/src/Agent.h | 5 ++++- interface/src/main.cpp | 42 +++++++++++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 9 deletions(-) diff --git a/interface/src/Agent.cpp b/interface/src/Agent.cpp index 114a44adc3..ea358c1fbb 100644 --- a/interface/src/Agent.cpp +++ b/interface/src/Agent.cpp @@ -9,6 +9,7 @@ #include #include "Agent.h" #include "Head.h" +#include "util.h" // Structure to hold references to other agents that are nearby @@ -17,6 +18,8 @@ const int MAX_AGENTS = 100; struct AgentList { char address[255]; unsigned short port; + timeval pingStarted; + int pingMsecs; Head head; } agents[MAX_AGENTS]; @@ -84,7 +87,7 @@ int add_agent(char * address, unsigned short port) { //std::cout << "Checking for " << IP->c_str() << " "; for (int i = 0; i < num_agents; i++) { if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) { - std::cout << "Found agent!\n"; + //std::cout << "Found agent!\n"; return 0; } } @@ -104,7 +107,7 @@ int add_agent(char * address, unsigned short port) { // // Broadcast data to all the other agents you are aware of, returns 1 if success // -int broadcast_to_agents(UDPSocket *handle, char * data, int length) { +int broadcastToAgents(UDPSocket *handle, char * data, int length) { int sent_bytes; //printf("broadcasting to %d agents\n", num_agents); for (int i = 0; i < num_agents; i++) { @@ -125,3 +128,27 @@ int broadcast_to_agents(UDPSocket *handle, char * data, int length) { } return 1; } + +// Ping other agents to see how fast we are running +void pingAgents(UDPSocket *handle) { + char payload[] = "P"; + for (int i = 0; i < num_agents; i++) { + gettimeofday(&agents[i].pingStarted, NULL); + handle->send(agents[i].address, agents[i].port, payload, 1); + printf("\nSent Ping at %d usecs\n", agents[i].pingStarted.tv_usec); + } +} + +// On receiving a ping reply, save that with the agent record +void setAgentPing(char * address, unsigned short port) { + for (int i = 0; i < num_agents; i++) { + if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) { + timeval pingReceived; + gettimeofday(&pingReceived, NULL); + float pingMsecs = diffclock(&agents[i].pingStarted, &pingReceived); + printf("Received ping at %d usecs, Agent ping = %3.1f\n", pingReceived.tv_usec, pingMsecs); + agents[i].pingMsecs = pingMsecs; + } + } +} + diff --git a/interface/src/Agent.h b/interface/src/Agent.h index e629ae6427..e3b0585de1 100644 --- a/interface/src/Agent.h +++ b/interface/src/Agent.h @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include "UDPSocket.h" @@ -20,7 +21,9 @@ int update_agents(char * data, int length); int add_agent(char * address, unsigned short port); -int broadcast_to_agents(UDPSocket * handle, char * data, int length); +int broadcastToAgents(UDPSocket * handle, char * data, int length); +void pingAgents(UDPSocket *handle); +void setAgentPing(char * address, unsigned short port); void update_agent(char * address, unsigned short port, char * data, int length); void render_agents(); diff --git a/interface/src/main.cpp b/interface/src/main.cpp index 287d699d95..b1de646265 100644 --- a/interface/src/main.cpp +++ b/interface/src/main.cpp @@ -100,7 +100,7 @@ Cloud cloud(0, // Particles false // Wrap ); -VoxelSystem voxels(0, box); +VoxelSystem voxels(1000, box); Lattice lattice(160,100); Finger myFinger(WIDTH, HEIGHT); @@ -230,6 +230,25 @@ void Timer(int extra) int packet_size = strlen(output); agentSocket.send(DOMAINSERVER_IP, DOMAINSERVER_PORT, output, packet_size); + // Ping the agents we can see + pingAgents(&agentSocket); + + if (0) { + // Massive send packet speed test + timeval starttest, endtest; + gettimeofday(&starttest, NULL); + char junk[1000]; + junk[0] = 'J'; + for (int i = 0; i < 3000; i++) + { + agentSocket.send("192.168.1.38", AGENT_UDP_PORT, junk, 1000); + } + gettimeofday(&endtest, NULL); + float sendTime = diffclock(&starttest, &endtest); + printf("packet test = %4.1f\n", sendTime); + } + + // Send a message to ourselves //char test[]="T"; //agentSocket.send("127.0.0.1", AGENT_UDP_PORT, test, 1); @@ -520,7 +539,7 @@ void update_pos(float frametime) const int MAX_BROADCAST_STRING = 200; char broadcast_string[MAX_BROADCAST_STRING]; int broadcast_bytes = myHead.getBroadcastData(broadcast_string); - broadcast_to_agents(&agentSocket, broadcast_string, broadcast_bytes); + broadcastToAgents(&agentSocket, broadcast_string, broadcast_bytes); //printf("-> %s\n", broadcast_string); //dummyHead.recvBroadcastData(broadcast_string, broadcast_bytes); @@ -773,14 +792,25 @@ void read_network() { sockaddr_in senderAddress; int bytes_recvd; - agentSocket.receive(&senderAddress, (void *)incoming_packet, &bytes_recvd); - - if (bytes_recvd > 0) + while (agentSocket.receive(&senderAddress, (void *)incoming_packet, &bytes_recvd)) { packetcount++; bytescount += bytes_recvd; // If packet is a Mouse data packet, copy it over - if (incoming_packet[0] == 'M') { + if (incoming_packet[0] == 'P') { + // + // Got Ping, reply immediately! + // + printf("Replying to ping!\n"); + char reply[] = "R"; + agentSocket.send(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port), reply, 1); + } else if (incoming_packet[0] == 'R') { + // + // Got Reply, record as appropriate + // + setAgentPing(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port)); + + } else if (incoming_packet[0] == 'M') { // // mouse location packet //