From 3fdd9c5a3932345c4328a73db0aa9d748dd5d941 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 13:17:14 -0700 Subject: [PATCH 1/8] some refactoring for device parsing in pairing server --- pairing-server/src/main.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 6729c88ea9..3d7a36f14c 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -52,34 +52,33 @@ int main(int argc, const char* argv[]) { // create a new PairableDevice PairableDevice newDevice = {}; - int addressBytes[4]; + char deviceAddress[INET_ADDRSTRLEN] = {}; int socketPort = 0; - int numMatches = sscanf(senderData, "Available %s %d.%d.%d.%d:%d %s", + int numMatches = sscanf(senderData, "Available %s%[^:]:%d %s", newDevice.identifier, - &addressBytes[3], - &addressBytes[2], - &addressBytes[1], - &addressBytes[0], + deviceAddress, &socketPort, newDevice.name); - if (numMatches >= 6) { + if (numMatches >= 3) { // 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); + inet_pton(AF_INET, deviceAddress, &newDevice); 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); + printf("Adding device %s (%s) - %s:%d to list\n", + newDevice.identifier, + newDevice.name, + deviceAddress, + socketPort); + devices.push_back(newDevice); } } else if (senderData[0] == 'F') { @@ -90,6 +89,7 @@ int main(int argc, const char* argv[]) { int requestorPort = 0; int requestorMatches = sscanf(senderData, "Find %[^:]:%d", requestorAddress, &requestorPort); + printf("Find request from interface client at %s:%d\n", requestorAddress, requestorPort); if (requestorMatches == 2) { PairableDevice lastDevice = devices[devices.size() - 1]; @@ -102,5 +102,4 @@ int main(int argc, const char* argv[]) { } } } - } From 0f9d72e2c1ead7d0dbf61fad93f1eb404496d64f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 16:14:17 -0700 Subject: [PATCH 2/8] keep one last PairableDevice and one last RequestingClient --- pairing-server/src/main.cpp | 69 +++++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 25 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 3d7a36f14c..3c7c24c141 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -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,70 +43,80 @@ 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 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 = {}; char deviceAddress[INET_ADDRSTRLEN] = {}; int socketPort = 0; int numMatches = sscanf(senderData, "Available %s%[^:]:%d %s", - newDevice.identifier, + tempDevice.identifier, deviceAddress, &socketPort, - newDevice.name); + tempDevice.name); if (numMatches >= 3) { - // if we have fewer than 6 matches the packet wasn't properly formatted + // if we have fewer than 3 matches the packet wasn't properly formatted + // otherwise copy the tempDevice to the persisting lastDevice + *::lastDevice = tempDevice; // setup the localSocket for the pairing device - newDevice.localSocket.sin_family = AF_INET; - inet_pton(AF_INET, deviceAddress, &newDevice); - newDevice.localSocket.sin_port = socketPort; + ::lastDevice->localSocket.sin_family = AF_INET; + inet_pton(AF_INET, deviceAddress, &::lastDevice); + ::lastDevice->localSocket.sin_port = socketPort; // store this device's sending socket so we can talk back to it - newDevice.sendingSocket = senderSocket; + ::lastDevice->sendingSocket = senderSocket; // push this new device into the vector printf("Adding device %s (%s) - %s:%d to list\n", - newDevice.identifier, - newDevice.name, + ::lastDevice->identifier, + ::lastDevice->name, deviceAddress, socketPort); - devices.push_back(newDevice); + 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); - printf("Find request from interface client at %s:%d\n", 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 = tempClient; - char pairData[INET_ADDRSTRLEN + 6] = {}; - sprintf(pairData, "%s:%d", requestorAddress, requestorPort); + printf("Find request from interface client at %s:%d\n", + ::lastClient->address, + ::lastClient->port); - serverSocket.send((sockaddr*) &lastDevice.sendingSocket, pairData, strlen(pairData)); + if (::lastDevice) { + sendLastClientToLastDevice(); + } } } } From cc4492bad8088156fdc55b84473985b03da99891 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 16:29:43 -0700 Subject: [PATCH 3/8] fix memory breakages after previous change --- pairing-server/src/main.cpp | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 3c7c24c141..96a4606eae 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -66,7 +66,7 @@ int main(int argc, const char* argv[]) { char deviceAddress[INET_ADDRSTRLEN] = {}; int socketPort = 0; - int numMatches = sscanf(senderData, "Available %s%[^:]:%d %s", + int numMatches = sscanf(senderData, "Available %s %[^:]:%d %s", tempDevice.identifier, deviceAddress, &socketPort, @@ -74,23 +74,24 @@ int main(int argc, const char* argv[]) { if (numMatches >= 3) { // if we have fewer than 3 matches the packet wasn't properly formatted - // otherwise copy the tempDevice to the persisting lastDevice - *::lastDevice = tempDevice; // setup the localSocket for the pairing device - ::lastDevice->localSocket.sin_family = AF_INET; + tempDevice.localSocket.sin_family = AF_INET; inet_pton(AF_INET, deviceAddress, &::lastDevice); - ::lastDevice->localSocket.sin_port = socketPort; + tempDevice.localSocket.sin_port = socketPort; // store this device's sending socket so we can talk back to it - ::lastDevice->sendingSocket = senderSocket; + tempDevice.sendingSocket = senderSocket; // push this new device into the vector - printf("Adding device %s (%s) - %s:%d to list\n", - ::lastDevice->identifier, - ::lastDevice->name, + 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(); @@ -107,10 +108,10 @@ int main(int argc, const char* argv[]) { &tempClient.port); if (requestorMatches == 2) { - // good data, copy the tempClient to the persisting lastInterfaceClient - *::lastClient = tempClient; + // good data, copy the tempClient to the persisting lastInterfaceClient + ::lastClient = new RequestingClient(tempClient); - printf("Find request from interface client at %s:%d\n", + printf("New last client is at %s:%d\n", ::lastClient->address, ::lastClient->port); From 0b9b8a14d14b2763bbd08aa3b05f219b367eb453 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 16:36:55 -0700 Subject: [PATCH 4/8] couple of type squishes --- pairing-server/src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pairing-server/src/main.cpp b/pairing-server/src/main.cpp index 96a4606eae..0422251403 100644 --- a/pairing-server/src/main.cpp +++ b/pairing-server/src/main.cpp @@ -30,8 +30,8 @@ struct RequestingClient { }; UDPSocket serverSocket(PAIRING_SERVER_LISTEN_PORT); -PairableDevice *lastDevice = NULL; -RequestingClient *lastClient = NULL; +PairableDevice* lastDevice = NULL; +RequestingClient* lastClient = NULL; int indexOfFirstOccurenceOfCharacter(char* haystack, char needle) { int currentIndex = 0; @@ -57,7 +57,7 @@ int main(int argc, const char* argv[]) { ssize_t receivedBytes = 0; 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 From b6925edbe8d52fba1899ba25795d8470f75a3a34 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 17:34:34 -0700 Subject: [PATCH 5/8] add a PairingHandler to send pair requests to the pairing server --- interface/src/Application.cpp | 8 ++++-- interface/src/Application.h | 4 +-- interface/src/PairingHandler.cpp | 48 ++++++++++++++++++++++++++++++++ interface/src/PairingHandler.h | 19 +++++++++++++ 4 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 interface/src/PairingHandler.cpp create mode 100644 interface/src/PairingHandler.h diff --git a/interface/src/Application.cpp b/interface/src/Application.cpp index ff6c80d456..1def8e4b2e 100644 --- a/interface/src/Application.cpp +++ b/interface/src/Application.cpp @@ -7,6 +7,7 @@ #include #include +#include #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(); } diff --git a/interface/src/Application.h b/interface/src/Application.h index c47dc3e33a..0aab27329e 100644 --- a/interface/src/Application.h +++ b/interface/src/Application.h @@ -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__) */ diff --git a/interface/src/PairingHandler.cpp b/interface/src/PairingHandler.cpp new file mode 100644 index 0000000000..8a3b049807 --- /dev/null +++ b/interface/src/PairingHandler.cpp @@ -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 + +#include + +#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); + + printf("Sending a pair request to %s on port %d\n", inet_ntoa(pairingServerSocket.sin_addr), ntohs(pairingServerSocket.sin_port)); + + // send the pair request to the pairing server + agentSocket->send((sockaddr*) &pairingServerSocket, pairPacket, strlen(pairPacket)); +} diff --git a/interface/src/PairingHandler.h b/interface/src/PairingHandler.h new file mode 100644 index 0000000000..d201465898 --- /dev/null +++ b/interface/src/PairingHandler.h @@ -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 + +class PairingHandler { +public: + static void sendPairRequest(); +}; + +#endif /* defined(__hifi__PairingHandler__) */ From a01b35f04170e9e93b62288599780280cde640b2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 18:16:59 -0700 Subject: [PATCH 6/8] remove extra debugging --- interface/src/PairingHandler.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/interface/src/PairingHandler.cpp b/interface/src/PairingHandler.cpp index 8a3b049807..63f208c308 100644 --- a/interface/src/PairingHandler.cpp +++ b/interface/src/PairingHandler.cpp @@ -41,8 +41,6 @@ void PairingHandler::sendPairRequest() { memcpy(&pairingServerSocket.sin_addr, hostInfo->h_addr_list[0], hostInfo->h_length); pairingServerSocket.sin_port = htons(PAIRING_SERVER_PORT); - printf("Sending a pair request to %s on port %d\n", inet_ntoa(pairingServerSocket.sin_addr), ntohs(pairingServerSocket.sin_port)); - // send the pair request to the pairing server agentSocket->send((sockaddr*) &pairingServerSocket, pairPacket, strlen(pairPacket)); } From 2c66ec8520a74b9b7e3271f255637899dc791b40 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 18:19:19 -0700 Subject: [PATCH 7/8] include string for methods in PairingHandler --- interface/src/PairingHandler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/PairingHandler.cpp b/interface/src/PairingHandler.cpp index 63f208c308..f4b76a672e 100644 --- a/interface/src/PairingHandler.cpp +++ b/interface/src/PairingHandler.cpp @@ -7,6 +7,7 @@ // #include +#include #include From a3a91984d2f832e0a36c32f4071ad57c60157cd2 Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Mon, 13 May 2013 18:22:55 -0700 Subject: [PATCH 8/8] include stdio for sprintf --- interface/src/PairingHandler.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/interface/src/PairingHandler.cpp b/interface/src/PairingHandler.cpp index f4b76a672e..3023290708 100644 --- a/interface/src/PairingHandler.cpp +++ b/interface/src/PairingHandler.cpp @@ -8,6 +8,7 @@ #include #include +#include #include