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

This commit is contained in:
ZappoMan 2013-05-14 09:04:58 -07:00
commit 4f826e96ee
5 changed files with 126 additions and 40 deletions

View file

@ -7,6 +7,7 @@
#include <QMenuBar>
#include <QtDebug>
#include <PairingHandler.h>
#include "Application.h"
@ -14,9 +15,10 @@ Application::Application(int& argc, char** argv) : QApplication(argc, argv) {
// simple menu bar (will only appear on OS X, for now)
QMenuBar* menuBar = new QMenuBar();
QMenu* fileMenu = menuBar->addMenu("File");
fileMenu->addAction("Test", this, SLOT(testSlot()));
fileMenu->addAction("Pair", this, SLOT(pair()));
}
void Application::testSlot() {
qDebug() << "Hello world.";
void Application::pair() {
PairingHandler::sendPairRequest();
}

View file

@ -15,12 +15,10 @@ class Application : public QApplication {
Q_OBJECT
public:
Application(int& argc, char** argv);
public slots:
void testSlot();
void pair();
};
#endif /* defined(__interface__Application__) */

View file

@ -0,0 +1,48 @@
//
// PairingHandler.cpp
// hifi
//
// Created by Stephen Birarda on 5/13/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#include <arpa/inet.h>
#include <string.h>
#include <stdio.h>
#include <AgentList.h>
#include "PairingHandler.h"
const char PAIRING_SERVER_HOSTNAME[] = "pairing.highfidelity.io";
const int PAIRING_SERVER_PORT = 7247;
void PairingHandler::sendPairRequest() {
// grab the agent socket from the AgentList singleton
UDPSocket *agentSocket = &AgentList::getInstance()->getAgentSocket();
// prepare the pairing request packet
// use the getLocalAddress helper to get this client's listening address
int localAddress = getLocalAddress();
char pairPacket[24] = {};
sprintf(pairPacket, "Find %d.%d.%d.%d:%d",
localAddress & 0xFF,
(localAddress >> 8) & 0xFF,
(localAddress >> 16) & 0xFF,
(localAddress >> 24) & 0xFF,
AGENT_SOCKET_LISTEN_PORT);
sockaddr_in pairingServerSocket;
pairingServerSocket.sin_family = AF_INET;
// lookup the pairing server IP by the hostname
struct hostent* hostInfo = gethostbyname(PAIRING_SERVER_HOSTNAME);
memcpy(&pairingServerSocket.sin_addr, hostInfo->h_addr_list[0], hostInfo->h_length);
pairingServerSocket.sin_port = htons(PAIRING_SERVER_PORT);
// send the pair request to the pairing server
agentSocket->send((sockaddr*) &pairingServerSocket, pairPacket, strlen(pairPacket));
}

View file

@ -0,0 +1,19 @@
//
// PairingHandler.h
// hifi
//
// Created by Stephen Birarda on 5/13/13.
// Copyright (c) 2013 High Fidelity, Inc. All rights reserved.
//
#ifndef __hifi__PairingHandler__
#define __hifi__PairingHandler__
#include <iostream>
class PairingHandler {
public:
static void sendPairRequest();
};
#endif /* defined(__hifi__PairingHandler__) */

View file

@ -24,6 +24,15 @@ struct PairableDevice {
sockaddr_in localSocket;
};
struct RequestingClient {
char address[INET_ADDRSTRLEN];
int port;
};
UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT);
PairableDevice* lastDevice = NULL;
RequestingClient* lastClient = NULL;
int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) {
int currentIndex = 0;
@ -34,73 +43,83 @@ int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) {
return currentIndex;
}
void sendLastClientToLastDevice() {
char pairData[INET_ADDRSTRLEN + 6] = {};
int bytesWritten = sprintf(pairData, "%s:%d", ::lastClient->address, ::lastClient->port);
::serverSocket.send((sockaddr*) &::lastDevice->sendingSocket, pairData, bytesWritten);
}
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 (::serverSocket.receive((sockaddr*) &senderSocket, &senderData, &receivedBytes)) {
if (senderData[0] == 'A') {
// this is a device reporting itself as available
// create a new PairableDevice
PairableDevice newDevice = {};
PairableDevice tempDevice = {};
int addressBytes[4];
char deviceAddress[INET_ADDRSTRLEN] = {};
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],
int numMatches = sscanf(senderData, "Available %s %[^:]:%d %s",
tempDevice.identifier,
deviceAddress,
&socketPort,
newDevice.name);
tempDevice.name);
if (numMatches >= 6) {
// if we have fewer than 6 matches the packet wasn't properly formatted
if (numMatches >= 3) {
// if we have fewer than 3 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;
tempDevice.localSocket.sin_family = AF_INET;
inet_pton(AF_INET, deviceAddress, &::lastDevice);
tempDevice.localSocket.sin_port = socketPort;
// store this device's sending socket so we can talk back to it
newDevice.sendingSocket = senderSocket;
tempDevice.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);
printf("New last device is %s (%s) at %s:%d\n",
tempDevice.identifier,
tempDevice.name,
deviceAddress,
socketPort);
// copy the tempDevice to the persisting lastDevice
::lastDevice = new PairableDevice(tempDevice);
if (::lastClient) {
sendLastClientToLastDevice();
}
}
} 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;
RequestingClient tempClient = {};
int requestorMatches = sscanf(senderData, "Find %[^:]:%d", requestorAddress, &requestorPort);
int requestorMatches = sscanf(senderData, "Find %[^:]:%d",
tempClient.address,
&tempClient.port);
if (requestorMatches == 2) {
PairableDevice lastDevice = devices[devices.size() - 1];
// good data, copy the tempClient to the persisting lastInterfaceClient
::lastClient = new RequestingClient(tempClient);
char pairData[INET_ADDRSTRLEN + 6] = {};
sprintf(pairData, "%s:%d", requestorAddress, requestorPort);
printf("New last client is at %s:%d\n",
::lastClient->address,
::lastClient->port);
serverSocket.send((sockaddr*) &lastDevice.sendingSocket, pairData, strlen(pairData));
if (::lastDevice) {
sendLastClientToLastDevice();
}
}
}
}
}
}