dial spec 2.0

This commit is contained in:
jcli
2015-10-20 16:33:55 -07:00
parent 3454a59e99
commit 2477615a40
15 changed files with 656 additions and 362 deletions

View File

@@ -40,6 +40,7 @@
#include <vector>
#include "DialDiscovery.h"
#include <assert.h>
#include <ctime>
#define SSDP_TIMEOUT 30
#define SSDP_RESPONSE_TIMEOUT 3
@@ -97,6 +98,45 @@ static string getLocation(char *pResponse)
return retUrl;
}
static string getMac(char *pResponse)
{
string response(pResponse);
string mac("");
size_t index = response.find("WAKEUP:");
if (index==string::npos){
return mac;
}else{
size_t start = response.find("MAC=", index);
if (start==string::npos) return mac;
start+=4;
size_t end = response.find(";", start);
if (end==string::npos) return mac;
mac = response.substr(start, end-start);
return mac;
}
}
static int getTimeout(char *pResponse)
{
string response(pResponse);
int timeOut=0;
string timeOutStr;
size_t index = response.find("WAKEUP:");
if (index==string::npos){
return timeOut;
}else{
size_t start = response.find("Timeout=", index);
if (start==string::npos) return timeOut;
start+=8;
size_t end = response.find("\r\n", start);
if (end==string::npos) return timeOut;
timeOutStr = response.substr(start, end-start);
timeOut = (int)(strtol(timeOutStr.c_str(), NULL, 10));
return timeOut;
}
}
static size_t header_cb(void* ptr, size_t size, size_t nmemb, void* userdata)
{
if ((size * nmemb) != 0) {
@@ -132,7 +172,6 @@ static void getServerInfo( const string &server, string& appsUrl, string& ddxml
{
CURL *curl;
CURLcode res = CURLE_OK;
if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
fprintf(stderr, "curl_global_init() failed\n");
return;
@@ -146,6 +185,7 @@ static void getServerInfo( const string &server, string& appsUrl, string& ddxml
ATRACE("Sending ##%s##\n", server.c_str());
curl_easy_setopt(curl, CURLOPT_URL, server.c_str());
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1);
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, header_cb);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, &appsUrl);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, receiveData);
@@ -157,7 +197,7 @@ static void getServerInfo( const string &server, string& appsUrl, string& ddxml
//curl_global_cleanup();
}
void DialDiscovery::updateServerList(string& server)
void DialDiscovery::updateServerList(string& server, string mac, int timeOut)
{
ServerMap::const_iterator it;
it = mServerMap.find(server);
@@ -165,7 +205,7 @@ void DialDiscovery::updateServerList(string& server)
// not found, add it
string appsUrl, ddxml;
getServerInfo(server, appsUrl, ddxml);
mServerMap[server] = new DialServer(server, appsUrl, ddxml);
mServerMap[server] = new DialServer(server, appsUrl, ddxml, mac, timeOut);
} else {
// just mark that we found it
(*it).second->setFound(true);
@@ -181,9 +221,12 @@ void DialDiscovery::processServer(char *pResponse)
// parse for LOCATION header
server = getLocation(pResponse);
ATRACE("FOUND server: %s\n", server.c_str());
string mac=getMac(pResponse);
int timeOut=getTimeout(pResponse);
// save the appURL in the server
updateServerList(server);
updateServerList(server, mac, timeOut);
#ifndef DEBUG
}
#else
@@ -196,17 +239,39 @@ void DialDiscovery::processServer(char *pResponse)
void *DialDiscovery::receiveResponses(void *p)
{
search_conn *pConn = (search_conn*)p;
fd_set readfds;
struct timeval tv;
int selectRt;
// clear the set ahead of time
FD_ZERO(&readfds);
// add our descriptors to the set
FD_SET(pConn->sock, &readfds);
// wait until either socket has data ready to be recv()d (timeout 1.5 secs)
tv.tv_sec = 1;
tv.tv_usec = 500000;
int bytes;
char buf[4096] = {0,};
while (1) {
if (-1 == (bytes = recvfrom(pConn->sock, buf, sizeof(buf) - 1, 0,
(struct sockaddr *)&pConn->saddr, &pConn->addrlen))) {
perror("recvfrom");
time_t start=std::time(NULL);
while (std::time(NULL)-start<RESPONSE_TIMEOUT) {
// printf("beat %ld %ld %ld\n", std::time(NULL), start, std::time(NULL)-start);
selectRt=select(pConn->sock+1, &readfds, NULL, NULL, &tv);
if (selectRt==-1){
perror("select"); // error occurred in select()
break;
}else if (selectRt == 0){
// printf("Timeout occurred! No data after 1.5 seconds.\n");
}else{
if (-1 == (bytes = recvfrom(pConn->sock, buf, sizeof(buf) - 1, 0,
(struct sockaddr *)&pConn->saddr, &pConn->addrlen))) {
perror("recvfrom");
break;
}
buf[bytes] = 0;
ATRACE("Received [%s:%d] %s\n", __FUNCTION__, __LINE__, buf);
DialDiscovery::instance()->processServer(buf);
}
buf[bytes] = 0;
ATRACE("Received [%s:%d] %s\n", __FUNCTION__, __LINE__, buf);
DialDiscovery::instance()->processServer(buf);
}
return 0;
}
@@ -244,17 +309,17 @@ void DialDiscovery::resetDiscovery(void)
}
}
void * DialDiscovery::send_mcast(void *p)
void *DialDiscovery::send_mcast()
{
int one = 1, my_sock;
socklen_t addrlen;
//struct ip_mreq mreq;
char send_buf[strlen((char*)ssdp_msearch) + INET_ADDRSTRLEN + 256] = {0,};
int send_size;
pthread_attr_t attr;
search_conn connection;
// send_size = snprintf(send_buf, sizeof(send_buf), ssdp_msearch, ip_addr, my_port);
printf("Sending mcast for discovery. Please wait for %d seconds for the response.\n", RESPONSE_TIMEOUT);
send_size = snprintf(send_buf, sizeof(send_buf), ssdp_msearch);
ATRACE("[%s:%d] %s\n", __FUNCTION__, __LINE__, send_buf);
@@ -270,45 +335,38 @@ void * DialDiscovery::send_mcast(void *p)
saddr.sin_addr.s_addr = inet_addr("239.255.255.250");
saddr.sin_port = htons(1900);
while (1) {
addrlen = sizeof(saddr);
ATRACE("Sending SSDP M-SEARCH to %s:%d\n",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
if (-1 == sendto(my_sock, send_buf, send_size, 0, (struct sockaddr *)&saddr, addrlen)) {
perror("sendto");
continue;
}
// set all servers to not found
DialDiscovery::instance()->resetDiscovery();
// spawn a response thread
connection.saddr = saddr;
connection.sock = my_sock;
connection.addrlen = addrlen;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&DialDiscovery::instance()->_responseThread, &attr, DialDiscovery::receiveResponses, &connection);
// sleep SSDP_RESPONSE_TIMEOUT seconds to allow clients to response
sleep(SSDP_RESPONSE_TIMEOUT);
DialDiscovery::instance()->cleanServerList();
sleep(SSDP_TIMEOUT-SSDP_RESPONSE_TIMEOUT);
pthread_cancel(DialDiscovery::instance()->_responseThread);
addrlen = sizeof(saddr);
ATRACE("Sending SSDP M-SEARCH to %s:%d\n",
inet_ntoa(saddr.sin_addr), ntohs(saddr.sin_port));
if (-1 == sendto(my_sock, send_buf, send_size, 0, (struct sockaddr *)&saddr, addrlen)) {
perror("sendto");
return 0;
}
// set all servers to not found
DialDiscovery::instance()->resetDiscovery();
connection.saddr = saddr;
connection.sock = my_sock;
connection.addrlen = addrlen;
receiveResponses(&connection);
// // sleep SSDP_RESPONSE_TIMEOUT seconds to allow clients to response
// sleep(SSDP_RESPONSE_TIMEOUT);
DialDiscovery::instance()->cleanServerList();
return 0;
}
void DialDiscovery::init()
{
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&_mcastThread, &attr, DialDiscovery::send_mcast, (void*)ssdp_msearch );
send_mcast();
}
void DialDiscovery::getServerList( vector<DialServer*>& list )
{
list.clear();
for( ServerMap::iterator it = mServerMap.begin(); it != mServerMap.end(); ++it ) {
list.push_back( it->second );
}

View File

@@ -73,11 +73,11 @@ public:
const string& friendlyName,
DialServer &server );
void *send_mcast();
private:
DialDiscovery();
void updateServerList(string& server);
void updateServerList(string& server, string mac, int timeOut);
static void *receiveResponses(void *p);
static void *send_mcast(void *p);
void processServer(char *pResponse);
void cleanServerList();
void resetDiscovery();
@@ -89,6 +89,7 @@ private:
ServerMap mServerMap;
static DialDiscovery* sDiscovery;
static const int RESPONSE_TIMEOUT = 5;
};
#endif // DIALDISCOVERY_H

View File

@@ -185,6 +185,16 @@ bool DialServer::getUuid( string& uuid )
return retval;
}
string DialServer::getMacAddress()
{
return m_macAddr;
}
int DialServer::getWakeOnLanTimeout()
{
return m_wakeUpTimeout;
}
int DialServer::launchApplication(
string &application,
string &payload,
@@ -193,8 +203,8 @@ int DialServer::launchApplication(
{
ATRACE("%s: Launch %s\n", __FUNCTION__, application.c_str());
string appUrl = m_appsUrl;
sendCommand( appUrl.append(application), COMMAND_LAUNCH, payload, responseHeaders, responseBody);
return 0;
int status = sendCommand( appUrl.append(application), COMMAND_LAUNCH, payload, responseHeaders, responseBody);
return status;
}
int DialServer::getStatus(
@@ -205,8 +215,9 @@ int DialServer::getStatus(
ATRACE("%s: GetStatus %s\n", __FUNCTION__, application.c_str());
string emptyPayload;
string appUrl = m_appsUrl;
sendCommand( appUrl.append(application), COMMAND_STATUS, emptyPayload, responseHeaders, responseBody );
int status = sendCommand( appUrl.append(application), COMMAND_STATUS, emptyPayload, responseHeaders, responseBody );
if (!status) return 0;
ATRACE("Body: %s\n", responseBody.c_str());
unsigned found = responseBody.find("href=");
if( found != string::npos )
@@ -234,10 +245,10 @@ int DialServer::stopApplication(
// just call status to update the run endpoint
getStatus( application, responseHeaders, responseBody );
sendCommand(
(appUrl.append(application)).append("/"+m_stopEndPoint),
COMMAND_KILL, emptyPayload, responseHeaders, responseBody );
return 0;
int status = sendCommand(
(appUrl.append(application)).append("/"+m_stopEndPoint),
COMMAND_KILL, emptyPayload, responseHeaders, responseBody );
return status;
}
int DialServer::getHttpResponseHeader(

View File

@@ -50,11 +50,13 @@ public:
* empty string to find any server
*
*/
DialServer( string location, string appsUrl, string dd_xml ) :
DialServer( string location, string appsUrl, string dd_xml, string macAddr, int wakeUpTimeout) :
m_location(location),
m_appsUrl(appsUrl),
found(true),
m_ddxml(dd_xml)
m_ddxml(dd_xml),
m_macAddr(macAddr),
m_wakeUpTimeout(wakeUpTimeout)
{}
~DialServer() { ATRACE("%s\n", __FUNCTION__); }
@@ -94,6 +96,20 @@ public:
*/
bool getUuid( string& uuid );
/**
* Get the dial server mac address
*
* @return mac address if avaliable, '' if not.
*/
string getMacAddress();
/**
* Get the wake on lan timeout in seconds
*
* @return timeout value if avaliable, zero if not.
*/
int getWakeOnLanTimeout();
/**
* Launch a DIAL application
*
@@ -176,6 +192,9 @@ private:
bool found;
string m_ddxml; // information about the device
string m_stopEndPoint;
string m_macAddr;
int m_wakeUpTimeout;
};
#endif // DIALSERVER_H

View File

@@ -31,6 +31,11 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
using namespace std;
@@ -51,12 +56,15 @@ static void printServerList( vector<DialServer*> list )
vector<DialServer*>::iterator it;
for( i = 0, it = list.begin(); it < list.end(); it++, i++ )
{
string uuid, name;
string uuid, name, macAddr;
int wolTimeOut;
(*it)->getFriendlyName( name );
(*it)->getUuid( uuid );
printf("%Zu: Server IP[%s] UUID[%s] FriendlyName[%s] \n",
i+1, (*it)->getIpAddress().c_str(),
uuid.c_str(), name.c_str() );
macAddr =(*it)->getMacAddress();
wolTimeOut =(*it)->getWakeOnLanTimeout();
printf("%Zu: Server IP[%s] UUID[%s] FriendlyName[%s] MacAddress[%s] WakeOnLanTimeout[%d]\n",
i+1, (*it)->getIpAddress().c_str(),
uuid.c_str(), name.c_str(), macAddr.c_str(), wolTimeOut);
}
}
@@ -64,22 +72,19 @@ static DialServer* getServerFromUser( vector<DialServer*> list )
{
DialServer* pServer;
// show a list to the user
if( list.size() > 1 )
{
char buf[80] = {0,};
vector<DialServer*>::iterator it;
char buf[80] = {0,};
vector<DialServer*>::iterator it;
printf("Found Multiple servers\n");
printServerList(list);
printf("Enter server: ");
scanf("%s", buf);
unsigned int server = atoi(buf);
assert( server > 0 && server <= list.size() );
printf("Found Multiple servers\n");
printf("0: Rescan and list DIAL servers\n");
printServerList(list);
printf("Enter server: ");
scanf("%s", buf);
unsigned int server = atoi(buf);
if( server > 0 && server <= list.size()){
pServer = list[server-1];
}
else
{
pServer = list.front();
}else{
pServer = NULL;
}
return pServer;
}
@@ -141,85 +146,140 @@ static void runConformance()
}
}
static void sendMagic(string macAddr)
{
unsigned char tosend[102];
unsigned char mac[6];
/** first 6 bytes of 255 **/
for(int i = 0; i < 6; i++) {
tosend[i] = 0xFF;
}
/** store mac address **/
printf("sending magic packet to: ");
for (int i=0; i<6; i++){
mac[i]=(unsigned char)(strtoul(macAddr.substr(i*3, 2).c_str(), NULL, 16));
printf("%02x:", mac[i]);
}
printf("\n");
/** append it 16 times to packet **/
for(int i = 1; i <= 16; i++) {
memcpy(&tosend[i * 6], &mac, 6 * sizeof(unsigned char));
}
int udpSocket;
struct sockaddr_in udpClient, udpServer;
int broadcast = 1;
udpSocket = socket(AF_INET, SOCK_DGRAM, 0);
/** you need to set this so you can broadcast **/
if (setsockopt(udpSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof broadcast) == -1) {
perror("setsockopt (SO_BROADCAST)");
exit(1);
}
udpClient.sin_family = AF_INET;
udpClient.sin_addr.s_addr = INADDR_ANY;
udpClient.sin_port = 0;
bind(udpSocket, (struct sockaddr*)&udpClient, sizeof(udpClient));
/** set server end point (the broadcast addres)**/
udpServer.sin_family = AF_INET;
udpServer.sin_addr.s_addr = inet_addr("255.255.255.255");
udpServer.sin_port = htons(9);
/** send the packet **/
sendto(udpSocket, &tosend, sizeof(unsigned char) * 102, 0, (struct sockaddr*)&udpServer, sizeof(udpServer));
}
int handleUser(DialDiscovery *pDial) {
int processInput = 1;
int processInputOuter = 1;
char buf[80];
vector<DialServer*> list;
pDial->getServerList(list);
if( list.size() == 0 )
{
printf("No servers available\n");
return 1;
}
DialServer* pServer = getServerFromUser( list );
while(processInput)
{
string responseHeaders, responseBody, payload;
string netflix = "Netflix";
string youtube = "YouTube";
memset(buf, 0, 80);
printf("0. List DIAL servers\n");
printf("1. Launch Netflix\n");
printf("2. Kill Netflix\n");
printf("3. Netflix status\n");
printf("4. Launch YouTube\n");
printf("5. Kill YouTube\n");
printf("6. YouTube status\n");
printf("7. Run conformance tests\n");
printf("8. QUIT\n");
printf("Command (0:1:2:3:4:5:6:7:8): ");
scanf("%s", buf);
switch( atoi(buf) )
{
case 0:
{
printf("\n\n******** %Zu servers found ********\n\n", list.size());
for( unsigned int i = 0; i < list.size(); i++ )
{
string name;
list[i]->getFriendlyName(name);
printf("Server %Zu: %s\n", i+1, name.c_str());
}
printf("\n*********************************\n\n");
}break;
case 1:
printf("Launch Netflix\n");
pServer->launchApplication( netflix, payload, responseHeaders, responseBody );
break;
case 2:
printf("Kill Netflix\n");
pServer->stopApplication( netflix, responseHeaders );
break;
case 3:
printf("Netflix Status: \n");
pServer->getStatus( netflix, responseHeaders, responseBody );
printf("RESPONSE: \n%s\n", responseBody.c_str());
break;
case 4:
printf("Launch YouTube\n");
pServer->launchApplication( youtube, payload, responseHeaders, responseBody );
break;
case 5:
printf("Kill YouTube\n");
pServer->stopApplication( youtube, responseHeaders );
break;
case 6:
printf("YouTube Status: \n");
pServer->getStatus( youtube, responseHeaders, responseBody );
break;
case 7:
runConformance();
break;
case 8:
processInput = 0;
break;
default:
printf("Invalid, try again\n");
while (processInputOuter){
pDial->getServerList(list);
if( list.size() == 0 ){
printf("No servers available\n");
return 1;
}
}
DialServer* pServer = getServerFromUser( list );
if (pServer==NULL){
pDial->send_mcast();
continue;
}
int processInput = 1;
while(processInput){
string responseHeaders, responseBody, payload;
string netflix = "Netflix";
string youtube = "YouTube";
memset(buf, 0, 80);
printf("0. Rescan and list DIAL servers\n");
printf("1. Launch Netflix\n");
printf("2. Kill Netflix\n");
printf("3. Netflix status\n");
printf("4. Launch YouTube\n");
printf("5. Kill YouTube\n");
printf("6. YouTube status\n");
printf("7. Run conformance tests\n");
printf("8. Wake up on lan/wlan\n");
printf("9. QUIT\n");
printf("Command (0:1:2:3:4:5:6:7:8:9): ");
scanf("%s", buf);
switch( atoi(buf) )
{
case 0:
{
pDial->send_mcast();
processInput=0;
}break;
case 1:
printf("Launch Netflix\n");
pServer->launchApplication( netflix, payload, responseHeaders, responseBody );
break;
case 2:
printf("Kill Netflix\n");
pServer->stopApplication( netflix, responseHeaders );
break;
case 3:
printf("Netflix Status: \n");
pServer->getStatus( netflix, responseHeaders, responseBody );
printf("RESPONSE: \n%s\n", responseBody.c_str());
break;
case 4:
printf("Launch YouTube\n");
pServer->launchApplication( youtube, payload, responseHeaders, responseBody );
break;
case 5:
printf("Kill YouTube\n");
pServer->stopApplication( youtube, responseHeaders );
break;
case 6:
printf("YouTube Status: \n");
pServer->getStatus( youtube, responseHeaders, responseBody );
break;
case 7:
runConformance();
break;
case 8:
printf("Sending the magic packet\n");
sendMagic(pServer->getMacAddress());
break;
case 9:
processInput = 0;
processInputOuter = 0;
break;
default:
printf("Invalid, try again\n");
}
}
}
return 0;
}