mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-09 01:22:21 +02:00
Spaceserver agent list, broadcast routines for discovered agents
This commit is contained in:
parent
3ddea45e2c
commit
5daf15ac2d
7 changed files with 123 additions and 13 deletions
80
agent.cpp
Normal file
80
agent.cpp
Normal file
|
@ -0,0 +1,80 @@
|
|||
//
|
||||
// agent.cpp
|
||||
// interface
|
||||
//
|
||||
// Created by Philip Rosedale on 11/20/12.
|
||||
// Copyright (c) 2012 __MyCompanyName__. All rights reserved.
|
||||
//
|
||||
|
||||
#include <iostream>
|
||||
#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;
|
||||
}
|
23
agent.h
Normal file
23
agent.h
Normal file
|
@ -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 <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include "network.h"
|
||||
|
||||
void update_agents(char * data, int length);
|
||||
int add_agent(std::string * IP);
|
||||
|
||||
#endif
|
|
@ -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 = "<group>"; };
|
||||
D409B988165849030099B0B3 /* cloud.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = cloud.h; sourceTree = "<group>"; };
|
||||
D409B989165849180099B0B3 /* cloud.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = cloud.cpp; sourceTree = "<group>"; };
|
||||
D409B9A6165CA7A50099B0B3 /* agent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = agent.h; sourceTree = "<group>"; };
|
||||
D409B9A7165CA7BB0099B0B3 /* agent.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = agent.cpp; sourceTree = "<group>"; };
|
||||
D40BDFD413404BA300B0BE1F /* GLUT.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GLUT.framework; path = /System/Library/Frameworks/GLUT.framework; sourceTree = "<absolute>"; };
|
||||
D40BDFD613404BB300B0BE1F /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = /System/Library/Frameworks/OpenGL.framework; sourceTree = "<absolute>"; };
|
||||
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;
|
||||
};
|
||||
|
|
Binary file not shown.
11
main.cpp
11
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) );
|
||||
|
|
|
@ -16,7 +16,14 @@
|
|||
#include <sys/time.h>
|
||||
#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);
|
||||
|
|
Loading…
Reference in a new issue