Merge remote-tracking branch 'origin'

This commit is contained in:
Philip Rosedale 2013-02-13 20:40:38 -08:00
commit 49201efbb1
11 changed files with 385 additions and 109 deletions

View file

@ -12,4 +12,5 @@ file(GLOB HIFI_SHARED_SRCS ${HIFI_SHARED_DIR}/*.cpp ${HIFI_SHARED_DIR}/*.h)
add_subdirectory(interface)
add_subdirectory(domain)
add_subdirectory(mixer)
add_subdirectory(mixer)
add_subdirectory(space)

View file

@ -103,7 +103,7 @@ int addAgent(uint32_t ip, in_port_t port, char agentType, float x, float y, floa
// Search for agent in list and add if needed
int i = 0;
int is_new = 0;
while ((ip != agents[i].ip) && (i < num_agents)) {
while ((ip != agents[i].ip || port != agents[i].port) && (i < num_agents)) {
i++;
}
if ((i == num_agents) || (agents[i].active == false)) is_new = 1;

View file

@ -38,8 +38,9 @@ const short JITTER_BUFFER_SAMPLES = JITTER_BUFFER_LENGTH_MSECS * (SAMPLE_RATE /
const short NUM_AUDIO_SOURCES = 2;
const short ECHO_SERVER_TEST = 1;
char WORKCLUB_AUDIO_SERVER[] = "192.168.1.19";
char EC2_WEST_AUDIO_SERVER[] = "54.241.92.53";
char LOCALHOST_MIXER[] = "0.0.0.0";
char WORKCLUB_MIXER[] = "192.168.1.19";
char EC2_WEST_MIXER[] = "54.241.92.53";
const int AUDIO_UDP_LISTEN_PORT = 55444;
@ -88,7 +89,7 @@ int audioCallback (const void *inputBuffer,
// int16_t *inputRight = ((int16_t **) inputBuffer)[1];
if (inputLeft != NULL) {
data->audioSocket->send((char *) EC2_WEST_AUDIO_SERVER, 55443, (void *)inputLeft, BUFFER_LENGTH_BYTES);
data->audioSocket->send((char *) EC2_WEST_MIXER, 55443, (void *)inputLeft, BUFFER_LENGTH_BYTES);
//
// Measure the loudness of the signal from the microphone and store in audio object
//

View file

@ -63,7 +63,7 @@ const int DOMAINSERVER_PORT = 40102;
UDPSocket agentSocket(AGENT_UDP_PORT);
// For testing, add milliseconds of delay for received UDP packets
char* incoming_packet;
char* incomingPacket;
int packetcount = 0;
int packets_per_second = 0;
@ -777,53 +777,57 @@ void key(unsigned char k, int x, int y)
//
// Receive packets from other agents/servers and decide what to do with them!
//
void read_network()
void *networkReceive(void *args)
{
sockaddr_in senderAddress;
int bytes_recvd;
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] == '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
//
sscanf(incoming_packet, "M %d %d", &target_x, &target_y);
target_display = 1;
//printf("X = %d Y = %d\n", target_x, target_y);
} else if (incoming_packet[0] == 'D') {
//
// Message from domainserver
//
//printf("agent list received!\n");
nearbyAgents = update_agents(&incoming_packet[1], bytes_recvd - 1);
} else if (incoming_packet[0] == 'H') {
//
// Broadcast packet from another agent
//
update_agent(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port), &incoming_packet[1], bytes_recvd - 1);
} else if (incoming_packet[0] == 'T') {
// Received a self-test packet (to get one's own IP), copy it to local variable!
printf("My Address: %s:%u\n",
inet_ntoa(senderAddress.sin_addr),
senderAddress.sin_port);
int bytesRecvd;
while (true) {
if (agentSocket.receive(&senderAddress, (void *)incomingPacket, &bytesRecvd)) {
packetcount++;
bytescount += bytesRecvd;
// If packet is a Mouse data packet, copy it over
if (incomingPacket[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 (incomingPacket[0] == 'R') {
//
// Got Reply, record as appropriate
//
setAgentPing(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port));
} else if (incomingPacket[0] == 'M') {
//
// mouse location packet
//
sscanf(incomingPacket, "M %d %d", &target_x, &target_y);
target_display = 1;
//printf("X = %d Y = %d\n", target_x, target_y);
} else if (incomingPacket[0] == 'D') {
//
// Message from domainserver
//
//printf("agent list received!\n");
nearbyAgents = update_agents(&incomingPacket[1], bytesRecvd - 1);
} else if (incomingPacket[0] == 'H') {
//
// Broadcast packet from another agent
//
update_agent(inet_ntoa(senderAddress.sin_addr), ntohs(senderAddress.sin_port), &incomingPacket[1], bytesRecvd - 1);
} else if (incomingPacket[0] == 'T') {
// Received a self-test packet (to get one's own IP), copy it to local variable!
printf("My Address: %s:%u\n",
inet_ntoa(senderAddress.sin_addr),
senderAddress.sin_port);
}
}
}
pthread_exit(0);
}
void idle(void)
@ -852,8 +856,6 @@ void idle(void)
}
// Read network packets
read_network();
// Read serial data
if (serial_on) serialPort.readData();
if (SLEEP)
@ -938,7 +940,7 @@ void *poll_marker_capture(void *threadarg){
int main(int argc, char** argv)
{
// Create network socket and buffer
incoming_packet = new char[MAX_PACKET_SIZE];
incomingPacket = new char[MAX_PACKET_SIZE];
// Lookup the IP address of things we have hostnames
printf("need to look this one up\n");
@ -996,6 +998,10 @@ int main(int argc, char** argv)
printf("Serial Port Initialized\n");
serial_on = 1;
}
// create thread for receipt of data via UDP
pthread_t networkReceiveThread;
pthread_create(&networkReceiveThread, NULL, networkReceive, NULL);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
@ -1045,6 +1051,7 @@ int main(int argc, char** argv)
}
#endif
pthread_join(networkReceiveThread, NULL);
::terminate();
return EXIT_SUCCESS;
}

View file

@ -36,8 +36,7 @@ const long MIN_SAMPLE_VALUE = std::numeric_limits<int16_t>::min();
const int MAX_SOURCE_BUFFERS = 20;
sockaddr_in address, destAddress;
socklen_t destLength = sizeof(destAddress);
sockaddr_in agentAddress;
struct AgentList {
char *address;
@ -62,25 +61,29 @@ double usecTimestamp(timeval *time, double addedUsecs = 0) {
return (time->tv_sec * 1000000.0) + time->tv_usec + addedUsecs;
}
int addAgent(sockaddr_in agentAddress, void *audioData) {
int addAgent(sockaddr_in *newAddress, void *audioData) {
// Search for agent in list and add if needed
int is_new = 0;
int i = 0;
for (i = 0; i < numAgents; i++) {
if (strcmp(inet_ntoa(agentAddress.sin_addr), agents[i].address) == 0
&& agentAddress.sin_port == agents[i].port) {
for (i = 0; i < numAgents; i++) {
if (strcmp(inet_ntoa(newAddress->sin_addr), agents[i].address) == 0
&& ntohs(newAddress->sin_port) == agents[i].port) {
break;
}
}
if ((i == numAgents) || (agents[i].active == false)) {
is_new = 1;
agents[i].address = new char();
strcpy(agents[i].address, inet_ntoa(newAddress->sin_addr));
agents[i].bufferTransmitted = false;
}
agents[i].address = inet_ntoa(agentAddress.sin_addr);
agents[i].port = ntohs(agentAddress.sin_port);
agents[i].port = ntohs(newAddress->sin_port);
agents[i].active = true;
gettimeofday(&agents[i].time, NULL);
@ -112,7 +115,7 @@ struct sendBufferStruct {
UDPSocket *audioSocket;
};
void *sendBufferThread(void *args)
void *sendBuffer(void *args)
{
struct sendBufferStruct *bufferArgs = (struct sendBufferStruct *)args;
UDPSocket *audioSocket = bufferArgs->audioSocket;
@ -176,7 +179,6 @@ void *sendBufferThread(void *args)
? masterMix[as] - previousOutput[as]
: masterMix[as];
int16_t shortSample;
if (longSample < 0) {
@ -196,7 +198,7 @@ void *sendBufferThread(void *args)
}
audioSocket->send(agents[a].address, agents[a].port, clientMix, BUFFER_LENGTH_BYTES);
sentBytes = audioSocket->send(agents[a].address, agents[a].port, clientMix, BUFFER_LENGTH_BYTES);
if (sentBytes < BUFFER_LENGTH_BYTES) {
std::cout << "Error sending mix packet! " << sentBytes << strerror(errno) << "\n";
@ -220,26 +222,6 @@ void *sendBufferThread(void *args)
pthread_exit(0);
}
struct processArgStruct {
int16_t *packetData;
sockaddr_in destAddress;
};
void *processClientPacket(void *args)
{
struct processArgStruct *processArgs = (struct processArgStruct *) args;
sockaddr_in destAddress = processArgs->destAddress;
if (addAgent(destAddress, processArgs->packetData)) {
std::cout << "Added agent: " <<
inet_ntoa(destAddress.sin_addr) << " on " <<
ntohs(destAddress.sin_port) << "\n";
}
pthread_exit(0);
}
int main(int argc, const char * argv[])
{
timeval lastAgentUpdate;
@ -259,22 +241,21 @@ int main(int argc, const char * argv[])
struct sendBufferStruct sendBufferArgs;
sendBufferArgs.audioSocket = &audioSocket;
pthread_t bufferSendThread;
pthread_create(&bufferSendThread, NULL, sendBufferThread, (void *)&sendBufferArgs);
pthread_t sendBufferThread;
pthread_create(&sendBufferThread, NULL, sendBuffer, (void *)&sendBufferArgs);
while (true) {
if(audioSocket.receive(packetData, &receivedBytes)) {
struct processArgStruct args;
args.packetData = packetData;
args.destAddress = destAddress;
if(audioSocket.receive(&agentAddress, packetData, &receivedBytes)) {
pthread_t clientProcessThread;
pthread_create(&clientProcessThread, NULL, processClientPacket, (void *)&args);
pthread_join(clientProcessThread, NULL);
if (addAgent(&agentAddress, packetData)) {
std::cout << "Added agent: " <<
inet_ntoa(agentAddress.sin_addr) << " on " <<
ntohs(agentAddress.sin_port) << "\n";
}
}
}
pthread_join(bufferSendThread, NULL);
pthread_join(sendBufferThread, NULL);
return 0;
}

View file

@ -13,7 +13,6 @@
#include <cstdio>
sockaddr_in destSockaddr, senderAddress;
socklen_t addLength = sizeof(senderAddress);
UDPSocket::UDPSocket(int listeningPort) {
// create the socket
@ -41,13 +40,6 @@ UDPSocket::UDPSocket(int listeningPort) {
return;
}
// set socket as non-blocking
int nonBlocking = 1;
if (fcntl(handle, F_SETFL, O_NONBLOCK, nonBlocking) == -1) {
printf("Failed to set non-blocking socket\n");
return;
}
printf("Created UDP socket listening on port %d.\n", listeningPort);
}
@ -58,17 +50,16 @@ UDPSocket::~UDPSocket() {
// Receive data on this socket with retrieving address of sender
bool UDPSocket::receive(void *receivedData, int *receivedBytes) {
*receivedBytes = recvfrom(handle, receivedData, MAX_BUFFER_LENGTH_BYTES,
0, (sockaddr *)&senderAddress, &addLength);
return (*receivedBytes > 0);
return receive(&senderAddress, receivedData, receivedBytes);
}
// Receive data on this socket with the address of the sender
bool UDPSocket::receive(sockaddr_in *senderAddress, void *receivedData, int *receivedBytes) {
bool UDPSocket::receive(sockaddr_in *recvAddress, void *receivedData, int *receivedBytes) {
socklen_t addressSize = sizeof(&recvAddress);
*receivedBytes = recvfrom(handle, receivedData, MAX_BUFFER_LENGTH_BYTES,
0, (sockaddr *)senderAddress, &addLength);
0, (sockaddr *) recvAddress, &addressSize);
return (*receivedBytes > 0);
}

View file

@ -21,7 +21,7 @@ class UDPSocket {
~UDPSocket();
int send(char *destAddress, int destPort, const void *data, int byteLength);
bool receive(void *receivedData, int *receivedBytes);
bool receive(sockaddr_in *senderAddress, void *receivedData, int *receivedBytes);
bool receive(sockaddr_in *recvAddress, void *receivedData, int *receivedBytes);
private:
int handle;

7
space/CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 2.8)
project(space)
file(GLOB SPACE_SRCS src/*.cpp src/*.h)
add_executable(space ${SPACE_SRCS})

19
space/src/TreeNode.cpp Normal file
View file

@ -0,0 +1,19 @@
//
// TreeNode.cpp
// hifi
//
// Created by Stephen Birarda on 2/13/13.
//
//
#include "TreeNode.h"
std::string EMPTY_STRING = "";
TreeNode::TreeNode() {
for (int i = 0; i < CHILDREN_PER_NODE; ++i) {
child[i] = NULL;
}
hostname = &EMPTY_STRING;
nickname = &EMPTY_STRING;
}

26
space/src/TreeNode.h Normal file
View file

@ -0,0 +1,26 @@
//
// TreeNode.h
// hifi
//
// Created by Stephen Birarda on 2/13/13.
//
//
#ifndef __hifi__TreeNode__
#define __hifi__TreeNode__
#include <iostream>
const int CHILDREN_PER_NODE = 8;
class TreeNode {
public:
TreeNode();
TreeNode *child[CHILDREN_PER_NODE];
std::string *hostname;
std::string *nickname;
int domain_id;
};
#endif /* defined(__hifi__TreeNode__) */

243
space/src/main.cpp Normal file
View file

@ -0,0 +1,243 @@
//
// main.cpp
// space
//
// Created by Leonardo Murillo on 2/6/13.
// Copyright (c) 2013 HighFidelity, Inc. All rights reserved.
//
#include <iostream>
#include <fstream>
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <stack>
#include <bitset>
#include "TreeNode.h"
const char *CONFIG_FILE = "/etc/below92/spaceserver.data.txt";
const int UDP_PORT = 55551;
std::vector< std::vector<std::string> > configData;
sockaddr_in address, destAddress;
socklen_t destLength = sizeof(destAddress);
std::string ROOT_HOSTNAME = "root.highfidelity.co";
std::string ROOT_NICKNAME = "root";
std::string *LAST_KNOWN_HOSTNAME = new std::string();
const short PACKET_LENGTH_BYTES = 1024;
TreeNode rootNode;
void printBinaryValue(char element) {
std::bitset<8> x(element);
std::cout << "Printing binary value: " << x << std::endl;
}
int create_socket()
{
// Create socket
int handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (handle <= 0) {
printf("Failed to create socket: %d\n", handle);
return false;
}
return handle;
}
int network_init()
{
int handle = create_socket();
// Bind socket to port
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons( (unsigned short) UDP_PORT );
if (bind(handle, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0) {
printf( "failed to bind socket\n" );
return false;
}
return handle;
}
TreeNode *findOrCreateNode(unsigned long lengthInBits,
unsigned char *addressBytes,
std::string *hostname,
std::string *nickname,
int domain_id) {
TreeNode *currentNode = &rootNode;
for (int i = 0; i < lengthInBits; i += 3) {
unsigned char octetA;
unsigned char octetB;
unsigned char octet;
/*
* @TODO Put those shifts into a nice single statement, leaving as is for now
*/
if (i%8 < 6) {
octetA = addressBytes[i/8] << i%8;
octet = octetA >> (5);
} else {
octetA = addressBytes[i/8] << i;
octetA = octetA >> (11 - i);
octetB = addressBytes[i/8 + 1] >> (11 - i + 2);
octet = octetA | octetB;
}
if (currentNode->child[octet] == NULL) {
currentNode->child[octet] = new TreeNode;
} else if (!currentNode->child[octet]->hostname->empty()) {
LAST_KNOWN_HOSTNAME = currentNode->child[octet]->hostname;
}
currentNode = currentNode->child[octet];
}
if (currentNode->hostname->empty()) {
currentNode->hostname = hostname;
currentNode->nickname = nickname;
}
return currentNode;
};
bool loadSpaceData(void) {
std::ifstream configFile(CONFIG_FILE);
std::string line;
if (configFile.is_open()) {
while (getline(configFile, line)) {
std::istringstream iss(line);
std::vector<std::string> thisEntry;
copy(std::istream_iterator<std::string>(iss),
std::istream_iterator<std::string>(),
std::back_inserter(thisEntry));
configData.push_back(thisEntry);
thisEntry.clear();
}
} else {
std::cout << "Unable to load config file\n";
return false;
}
for (std::vector< std::vector<std::string> >::iterator it = configData.begin(); it != configData.end(); ++it) {
std::string *thisAddress = &(*it)[1];
std::string *thisHostname = &(*it)[2];
std::string *thisNickname = &(*it)[3];
char lengthByteString[8];
unsigned long lengthByte;
unsigned long bitsInAddress;
std::size_t bytesForAddress;
std::size_t lengthByteSlice = (*thisAddress).copy(lengthByteString, 8, 0);
lengthByteString[lengthByteSlice] = '\0';
lengthByte = strtoul(lengthByteString, NULL, 2);
bitsInAddress = lengthByte * 3;
bytesForAddress = (((bitsInAddress + 7) & ~7));
char *addressBitStream = new char();
std::size_t addressBitSlice = (*thisAddress).copy(addressBitStream, (*thisAddress).length(), 8);
if (bitsInAddress != addressBitSlice) {
std::cout << "[FATAL] Mismatching byte length: " << bitsInAddress
<< " and address bits: " << sizeof(addressBitStream) << std::endl;
return false;
}
char paddedBitString[bytesForAddress];
strcpy(paddedBitString, addressBitStream);
for (unsigned long i = addressBitSlice; i < bytesForAddress; ++i ) {
paddedBitString[i] = '0';
}
paddedBitString[bytesForAddress] = '\0';
std::string byteBufferHolder = *new std::string(paddedBitString);
unsigned char addressBytes[bytesForAddress / 8];
addressBytes[bytesForAddress / 8] = '\0';
int j = 0;
for (unsigned long i = 0; i < bytesForAddress; i += 8) {
char *byteHolder = new char;
unsigned long thisByte;
byteBufferHolder.copy(byteHolder, 8, i);
thisByte = strtoul(byteHolder, NULL, 2);
addressBytes[j] = thisByte;
++j;
}
findOrCreateNode(bitsInAddress, addressBytes, thisHostname, thisNickname, 0);
}
return true;
}
int main (int argc, const char *argv[]) {
int handle = network_init();
unsigned char packetData[PACKET_LENGTH_BYTES];
long received_bytes = 0;
if (!handle) {
std::cout << "[FATAL] Failed to create UDP listening socket" << std::endl;
return 0;
} else {
std::cout << "[DEBUG] Socket started" << std::endl;
}
rootNode.hostname = &ROOT_HOSTNAME;
rootNode.nickname = &ROOT_NICKNAME;
loadSpaceData();
std::cout << "[DEBUG] Listening for Datagrams" << std::endl;
while (true) {
received_bytes = recvfrom(handle, (unsigned char*)packetData, PACKET_LENGTH_BYTES, 0, (sockaddr*)&destAddress, &destLength);
if (received_bytes > 0) {
unsigned long lengthInBits;
lengthInBits = packetData[0] * 3;
unsigned char addressData[sizeof(packetData)-1];
for (int i = 0; i < sizeof(packetData)-1; ++i) {
addressData[i] = packetData[i+1];
}
std::string thisHostname;
std::string thisNickname;
std::string hostnameHolder;
int domain_id = 0;
long sentBytes;
TreeNode thisNode = *findOrCreateNode(lengthInBits, addressData, &thisHostname, &thisNickname, domain_id);
if (thisNode.hostname->empty()) {
hostnameHolder = *LAST_KNOWN_HOSTNAME;
} else {
hostnameHolder = *thisNode.hostname;
}
char hostname[hostnameHolder.size() + 1];
std::copy(hostnameHolder.begin(), hostnameHolder.end(), hostname);
hostname[hostnameHolder.size()] = '\0';
sentBytes = sendto(handle, &hostname, PACKET_LENGTH_BYTES,
0, (sockaddr*)&destAddress, sizeof(destAddress));
}
}
}