Merge pull request #189 from birarda/pairing

basic version of the pairing server
This commit is contained in:
ZappoMan 2013-05-03 12:24:29 -07:00
commit f78c59f828
4 changed files with 118 additions and 2 deletions

View file

@ -8,5 +8,6 @@ add_subdirectory(domain-server)
add_subdirectory(eve)
add_subdirectory(interface)
add_subdirectory(injector)
add_subdirectory(pairing-server)
add_subdirectory(space-server)
add_subdirectory(voxel-server)

View file

@ -1817,8 +1817,6 @@ void mouseoverFunc( int x, int y)
{}
}
void attachNewHeadToAgent(Agent *newAgent) {
if (newAgent->getLinkedData() == NULL) {
newAgent->setLinkedData(new Avatar(false));

View file

@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 2.8)
set(ROOT_DIR ..)
set(MACRO_DIR ${ROOT_DIR}/cmake/macros)
set(TARGET_NAME pairing-server)
include(${MACRO_DIR}/SetupHifiProject.cmake)
setup_hifi_project(${TARGET_NAME})
# link the shared hifi library
include(${MACRO_DIR}/LinkHifiLibrary.cmake)
link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR})

104
pairing-server/src/main.cpp Normal file
View file

@ -0,0 +1,104 @@
//
// main.cpp
// pairing-server
//
// Created by Stephen Birarda on 5/1/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#include <iostream>
#include <vector>
#include <UDPSocket.h>
#include <arpa/inet.h>
const int PAIRING_SERVER_LISTEN_PORT = 52934;
const int MAX_PACKET_SIZE_BYTES = 1400;
struct PairableDevice {
char identifier[64];
char name[64];
sockaddr_in sendingSocket;
sockaddr_in localSocket;
};
int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) {
int currentIndex = 0;
while (haystack[currentIndex] != '\0' && haystack[currentIndex] != needle) {
currentIndex++;
}
return currentIndex;
}
int main(int argc, const char* argv[]) {
UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT);
sockaddr_in senderSocket;
char senderData[MAX_PACKET_SIZE_BYTES] = {};
ssize_t receivedBytes = 0;
std::vector<PairableDevice> devices;
while (true) {
if (serverSocket.receive((sockaddr *)&senderSocket, &senderData, &receivedBytes)) {
if (senderData[0] == 'A') {
// this is a device reporting itself as available
// create a new PairableDevice
PairableDevice newDevice = {};
int addressBytes[4];
int socketPort = 0;
int numMatches = sscanf(senderData, "Available %s %d.%d.%d.%d:%d %s",
newDevice.identifier,
&addressBytes[3],
&addressBytes[2],
&addressBytes[1],
&addressBytes[0],
&socketPort,
newDevice.name);
if (numMatches >= 6) {
// if we have fewer than 6 matches the packet wasn't properly formatted
// setup the localSocket for the pairing device
newDevice.localSocket.sin_family = AF_INET;
newDevice.localSocket.sin_addr.s_addr = (addressBytes[3] |
addressBytes[2] << 8 |
addressBytes[1] << 16 |
addressBytes[0] << 24);
newDevice.localSocket.sin_port = socketPort;
// store this device's sending socket so we can talk back to it
newDevice.sendingSocket = senderSocket;
// push this new device into the vector
printf("Adding device %s (%s) to list\n", newDevice.identifier, newDevice.name);
devices.push_back(newDevice);
}
} else if (senderData[0] == 'F') {
// this is a client looking to pair with a device
// send the most recent device this address so it can attempt to pair
char requestorAddress[INET_ADDRSTRLEN] = {};
int requestorPort = 0;
int requestorMatches = sscanf(senderData, "Find %[^:]:%d", requestorAddress, &requestorPort);
if (requestorMatches == 2) {
PairableDevice lastDevice = devices[devices.size() - 1];
char pairData[INET_ADDRSTRLEN + 6] = {};
sprintf(pairData, "%s:%d", requestorAddress, requestorPort);
serverSocket.send((sockaddr*) &lastDevice.sendingSocket, pairData, strlen(pairData));
}
}
}
}
}