Merge branch 'master' of github.com:worklist/hifi

This commit is contained in:
Stephen Birarda 2013-02-12 13:34:14 -08:00
commit b2c6425f8a
3 changed files with 69 additions and 9 deletions

View file

@ -9,6 +9,7 @@
#include <iostream> #include <iostream>
#include "Agent.h" #include "Agent.h"
#include "Head.h" #include "Head.h"
#include "util.h"
// Structure to hold references to other agents that are nearby // Structure to hold references to other agents that are nearby
@ -17,6 +18,8 @@ const int MAX_AGENTS = 100;
struct AgentList { struct AgentList {
char address[255]; char address[255];
unsigned short port; unsigned short port;
timeval pingStarted;
int pingMsecs;
Head head; Head head;
} agents[MAX_AGENTS]; } agents[MAX_AGENTS];
@ -84,7 +87,7 @@ int add_agent(char * address, unsigned short port) {
//std::cout << "Checking for " << IP->c_str() << " "; //std::cout << "Checking for " << IP->c_str() << " ";
for (int i = 0; i < num_agents; i++) { for (int i = 0; i < num_agents; i++) {
if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) { if ((strcmp(address, agents[i].address) == 0) && (agents[i].port == port)) {
std::cout << "Found agent!\n"; //std::cout << "Found agent!\n";
return 0; 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 // 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; int sent_bytes;
//printf("broadcasting to %d agents\n", num_agents); //printf("broadcasting to %d agents\n", num_agents);
for (int i = 0; i < num_agents; i++) { for (int i = 0; i < num_agents; i++) {
@ -125,3 +128,27 @@ int broadcast_to_agents(UDPSocket *handle, char * data, int length) {
} }
return 1; 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;
}
}
}

View file

@ -13,6 +13,7 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <sys/time.h>
#include <fcntl.h> #include <fcntl.h>
#include <string.h> #include <string.h>
#include "UDPSocket.h" #include "UDPSocket.h"
@ -20,7 +21,9 @@
int update_agents(char * data, int length); int update_agents(char * data, int length);
int add_agent(char * address, unsigned short port); 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 update_agent(char * address, unsigned short port, char * data, int length);
void render_agents(); void render_agents();

View file

@ -100,7 +100,7 @@ Cloud cloud(0, // Particles
false // Wrap false // Wrap
); );
VoxelSystem voxels(0, box); VoxelSystem voxels(1000, box);
Lattice lattice(160,100); Lattice lattice(160,100);
Finger myFinger(WIDTH, HEIGHT); Finger myFinger(WIDTH, HEIGHT);
@ -230,6 +230,25 @@ void Timer(int extra)
int packet_size = strlen(output); int packet_size = strlen(output);
agentSocket.send(DOMAINSERVER_IP, DOMAINSERVER_PORT, output, packet_size); 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 // Send a message to ourselves
//char test[]="T"; //char test[]="T";
//agentSocket.send("127.0.0.1", AGENT_UDP_PORT, test, 1); //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; const int MAX_BROADCAST_STRING = 200;
char broadcast_string[MAX_BROADCAST_STRING]; char broadcast_string[MAX_BROADCAST_STRING];
int broadcast_bytes = myHead.getBroadcastData(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); //printf("-> %s\n", broadcast_string);
//dummyHead.recvBroadcastData(broadcast_string, broadcast_bytes); //dummyHead.recvBroadcastData(broadcast_string, broadcast_bytes);
@ -773,14 +792,25 @@ void read_network()
{ {
sockaddr_in senderAddress; sockaddr_in senderAddress;
int bytes_recvd; int bytes_recvd;
agentSocket.receive(&senderAddress, (void *)incoming_packet, &bytes_recvd); while (agentSocket.receive(&senderAddress, (void *)incoming_packet, &bytes_recvd))
if (bytes_recvd > 0)
{ {
packetcount++; packetcount++;
bytescount += bytes_recvd; bytescount += bytes_recvd;
// If packet is a Mouse data packet, copy it over // 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 // mouse location packet
// //