diff --git a/agent.cpp b/agent.cpp new file mode 100644 index 0000000000..f0f3fe9bc9 --- /dev/null +++ b/agent.cpp @@ -0,0 +1,80 @@ +// +// agent.cpp +// interface +// +// Created by Philip Rosedale on 11/20/12. +// Copyright (c) 2012 __MyCompanyName__. All rights reserved. +// + +#include +#include "agent.h" + +// Structure to hold references to other agents that are nearby + +const int MAX_AGENTS = 100; +struct AgentList { + in_addr sin_addr; + glm::vec3 position; +} agents[MAX_AGENTS]; +int num_agents = 0; + +// Process an incoming packet that lists the other agents in the area +void update_agents(char * data, int length) { + std::string packet(data, length); + //std::string packet("127.0.0.1,"); + //std::cout << " Update Agents, string: " << packet << "\n"; + size_t spot; + size_t start_spot = 0; + spot = packet.find_first_of (",", 0); + while (spot != std::string::npos) { + std::string IPstring = packet.substr(start_spot, spot-start_spot); + //std::cout << "Found " << num_agents << + //" with IP " << IPstring << " from " << start_spot << " to " << spot << "\n"; + // Add the IP address to the agent table + add_agent(&IPstring); + start_spot = spot + 1; + if (start_spot < packet.length()) + spot = packet.find_first_of (",", start_spot); + else spot = std::string::npos; + } +} + +int add_agent(std::string * IP) { + in_addr_t addr = inet_addr(IP->c_str()); + //std::cout << "Checking for " << IP->c_str() << " "; + for (int i = 0; i < num_agents; i++) { + if (agents[i].sin_addr.s_addr == addr) { + //std::cout << "Found!\n"; + return 0; + } + } + if (num_agents < MAX_AGENTS) { + agents[num_agents].sin_addr.s_addr = addr; + std::cout << "Added Agent # " << num_agents << " with IP " << + inet_ntoa(agents[num_agents].sin_addr) << "\n"; + num_agents++; + return 1; + } else { + std::cout << "Max agents reached!\n"; + return 0; + } +} + +// Broadcast data to all the other agents you are aware of, returns 1 for success +int broadcast(int handle, char * data, int length) { + sockaddr_in dest_address; + dest_address.sin_family = AF_INET; + dest_address.sin_port = htons( (unsigned short) UDP_PORT ); + + int sent_bytes; + for (int i = 0; i < num_agents; i++) { + dest_address.sin_addr.s_addr = agents[i].sin_addr.s_addr; + sent_bytes = sendto( handle, (const char*)data, length, + 0, (sockaddr*)&dest_address, sizeof(sockaddr_in) ); + if (sent_bytes != length) { + std::cout << "Broadcast packet fail!\n"; + return 0; + } + } + return 1; +} diff --git a/agent.h b/agent.h new file mode 100644 index 0000000000..896b76923f --- /dev/null +++ b/agent.h @@ -0,0 +1,23 @@ +// +// agent.h +// interface +// +// Created by Philip Rosedale on 11/20/12. +// Copyright (c) 2012 __MyCompanyName__. All rights reserved. +// + +#ifndef interface_agent_h +#define interface_agent_h + +#include "glm/glm.hpp" +#include +#include +#include +#include +#include +#include "network.h" + +void update_agents(char * data, int length); +int add_agent(std::string * IP); + +#endif diff --git a/interface.xcodeproj/project.pbxproj b/interface.xcodeproj/project.pbxproj index 910b2dbd70..18a6cffacf 100644 --- a/interface.xcodeproj/project.pbxproj +++ b/interface.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ B6BDADE415F44AC7002A07DF /* AudioUnit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B6BDADDC15F444D3002A07DF /* AudioUnit.framework */; }; B6BDAE4415F6BE53002A07DF /* particle.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B6BDAE4315F6BE53002A07DF /* particle.cpp */; }; D409B98A165849180099B0B3 /* cloud.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D409B989165849180099B0B3 /* cloud.cpp */; }; + D409B9A8165CA7BC0099B0B3 /* agent.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D409B9A7165CA7BB0099B0B3 /* agent.cpp */; }; D40BDFD513404BA300B0BE1F /* GLUT.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D40BDFD413404BA300B0BE1F /* GLUT.framework */; }; D40BDFD713404BB300B0BE1F /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D40BDFD613404BB300B0BE1F /* OpenGL.framework */; }; D40FD5FB164AF1C200878184 /* int-texture256-v2.png in CopyFiles */ = {isa = PBXBuildFile; fileRef = D40FD5FA164AF1A700878184 /* int-texture256-v2.png */; }; @@ -69,6 +70,8 @@ C6859E8B029090EE04C91782 /* test_c_plus.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = test_c_plus.1; sourceTree = ""; }; D409B988165849030099B0B3 /* cloud.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cloud.h; sourceTree = ""; }; D409B989165849180099B0B3 /* cloud.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cloud.cpp; sourceTree = ""; }; + D409B9A6165CA7A50099B0B3 /* agent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = agent.h; sourceTree = ""; }; + D409B9A7165CA7BB0099B0B3 /* agent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = agent.cpp; sourceTree = ""; }; D40BDFD413404BA300B0BE1F /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = /System/Library/Frameworks/GLUT.framework; sourceTree = ""; }; D40BDFD613404BB300B0BE1F /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = /System/Library/Frameworks/OpenGL.framework; sourceTree = ""; }; D40FD5FA164AF1A700878184 /* int-texture256-v2.png */ = {isa = PBXFileReference; lastKnownFileType = image.png; path = "int-texture256-v2.png"; sourceTree = SOURCE_ROOT; }; @@ -137,6 +140,8 @@ isa = PBXGroup; children = ( 08FB7796FE84155DC02AAC07 /* main.cpp */, + D409B9A6165CA7A50099B0B3 /* agent.h */, + D409B9A7165CA7BB0099B0B3 /* agent.cpp */, D409B988165849030099B0B3 /* cloud.h */, D409B989165849180099B0B3 /* cloud.cpp */, D4EE3BC015E746E900EE4C89 /* world.h */, @@ -267,6 +272,7 @@ F68135561648617D003040E3 /* texture.cpp in Sources */, F681358B1648896D003040E3 /* lodepng.cpp in Sources */, D409B98A165849180099B0B3 /* cloud.cpp in Sources */, + D409B9A8165CA7BC0099B0B3 /* agent.cpp in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/interface.xcodeproj/project.xcworkspace/xcuserdata/philip.xcuserdatad/UserInterfaceState.xcuserstate b/interface.xcodeproj/project.xcworkspace/xcuserdata/philip.xcuserdatad/UserInterfaceState.xcuserstate index b310c1ec89..13077bb321 100644 Binary files a/interface.xcodeproj/project.xcworkspace/xcuserdata/philip.xcuserdatad/UserInterfaceState.xcuserstate and b/interface.xcodeproj/project.xcworkspace/xcuserdata/philip.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/main.cpp b/main.cpp index bafb5383c1..9f4d612058 100644 --- a/main.cpp +++ b/main.cpp @@ -45,10 +45,10 @@ #include "head.h" #include "hand.h" #include "particle.h" - #include "texture.h" - #include "cloud.h" +#include "agent.h" + //TGAImg Img; @@ -100,7 +100,6 @@ Cloud cloud(300000, // Particles false // Wrap ); - #define RENDER_FRAME_MSECS 10 #define SLEEP 0 @@ -201,6 +200,7 @@ void Timer(int extra) // Send a message to the spaceserver telling it we are ALIVE notify_spaceserver(UDP_socket, location[0], location[1], location[2]); + } void display_stats(void) @@ -650,8 +650,9 @@ void read_network() ping_msecs = (float)diffclock(ping_start, check); } else if (incoming_packet[0] == 'S') { // Message from Spaceserver - std::cout << "Spaceserver: "; - outstring(incoming_packet, bytes_recvd); + //std::cout << "Spaceserver: "; + //outstring(incoming_packet, bytes_recvd); + update_agents(&incoming_packet[1], bytes_recvd - 1); } } } diff --git a/network.cpp b/network.cpp index def6845057..e386ac913d 100644 --- a/network.cpp +++ b/network.cpp @@ -11,13 +11,6 @@ #include "network.h" -const int UDP_PORT = 30001; -const char DESTINATION_IP[] = "127.0.0.1"; - -// Location of the spaceserver to talk to -const char SPACESERVER_IP[] = "127.0.0.1"; -const int SPACESERVER_PORT = 40000; - // Implementation of optional delay behavior using a ring buffer const int MAX_DELAY_PACKETS = 300; char delay_buffer[MAX_PACKET_SIZE*MAX_DELAY_PACKETS]; @@ -97,7 +90,7 @@ timeval network_send_ping(int handle) { int notify_spaceserver(int handle, float x, float y, float z) { char data[100]; sprintf(data, "%f,%f,%f", x, y, z); - std::cout << "sending: " << data << "\n"; + //std::cout << "sending: " << data << "\n"; int packet_size = strlen(data); int sent_bytes = sendto( handle, (const char*)data, packet_size, 0, (sockaddr*)&spaceserver_address, sizeof(sockaddr_in) ); diff --git a/network.h b/network.h index 751b8f0f87..e06f416b9e 100644 --- a/network.h +++ b/network.h @@ -16,7 +16,14 @@ #include #include "util.h" +// Port to use for communicating UDP with other nearby agents const int MAX_PACKET_SIZE = 1500; +const int UDP_PORT = 30001; +const char DESTINATION_IP[] = "127.0.0.1"; + +// Address and port of spaceserver process to advertise other agents +const char SPACESERVER_IP[] = "127.0.0.1"; +const int SPACESERVER_PORT = 40000; int network_init(); int network_send(int handle, char * packet_data, int packet_size);