2012-08-02 11:28:38 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <algorithm>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <comphelper/processfactory.hxx>
|
2012-08-02 12:27:19 +02:00
|
|
|
#include <rtl/strbuf.hxx>
|
2012-08-06 16:14:01 +02:00
|
|
|
#include <string.h>
|
2012-08-02 11:28:38 +02:00
|
|
|
|
|
|
|
#include "DiscoveryService.hxx"
|
|
|
|
|
2012-08-02 16:40:31 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
#include <winsock.h>
|
|
|
|
typedef int socklen_t;
|
|
|
|
#else
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2012-08-02 12:27:19 +02:00
|
|
|
using namespace osl;
|
|
|
|
using namespace rtl;
|
2012-08-02 11:28:38 +02:00
|
|
|
using namespace sd;
|
2012-08-02 12:27:19 +02:00
|
|
|
using namespace std;
|
2012-08-02 11:28:38 +02:00
|
|
|
|
|
|
|
DiscoveryService::DiscoveryService()
|
|
|
|
:
|
2012-08-02 16:40:31 +02:00
|
|
|
Thread( "sd::DiscoveryService" )
|
|
|
|
// mSocket()
|
2012-08-02 11:28:38 +02:00
|
|
|
{
|
2012-08-02 16:40:31 +02:00
|
|
|
mSocket = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
|
|
|
|
|
|
|
|
sockaddr_in aAddr;
|
|
|
|
aAddr.sin_family = AF_INET;
|
|
|
|
aAddr.sin_addr.s_addr = htonl(INADDR_ANY);
|
|
|
|
aAddr.sin_port = htons( PORT_DISCOVERY );
|
|
|
|
|
|
|
|
bind( mSocket, (sockaddr*) &aAddr, sizeof(sockaddr_in) );
|
|
|
|
|
|
|
|
struct ip_mreq multicastRequest;
|
|
|
|
|
|
|
|
multicastRequest.imr_multiaddr.s_addr = inet_addr( "239.0.0.1" );
|
|
|
|
multicastRequest.imr_interface.s_addr = htonl(INADDR_ANY);
|
|
|
|
|
|
|
|
setsockopt( mSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP,
|
2012-08-06 14:16:04 +02:00
|
|
|
#ifdef WNT
|
|
|
|
(const char*)
|
|
|
|
#endif
|
2012-08-02 16:40:31 +02:00
|
|
|
&multicastRequest, sizeof(multicastRequest));
|
2012-08-02 11:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DiscoveryService::~DiscoveryService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void DiscoveryService::execute()
|
|
|
|
{
|
2012-08-02 16:11:06 +02:00
|
|
|
fprintf( stderr, "Discovery service is listening\n" );;
|
2012-08-06 16:14:01 +02:00
|
|
|
|
|
|
|
char aBuffer[BUFFER_SIZE];
|
2012-08-02 11:28:38 +02:00
|
|
|
|
2012-08-02 12:27:19 +02:00
|
|
|
while ( true )
|
|
|
|
{
|
2012-08-06 16:14:01 +02:00
|
|
|
memset( aBuffer, 0, sizeof(char) * BUFFER_SIZE );
|
2012-08-02 16:40:31 +02:00
|
|
|
sockaddr_in aAddr;
|
|
|
|
socklen_t aLen = sizeof( aAddr );
|
2012-08-02 16:11:06 +02:00
|
|
|
fprintf( stderr, "DiscoveryService waiting for packet\n" );
|
2012-08-06 21:00:23 +02:00
|
|
|
recvfrom( mSocket, aBuffer, BUFFER_SIZE, 0, (sockaddr*) &aAddr, &aLen );
|
2012-08-02 16:11:06 +02:00
|
|
|
fprintf( stderr, "DiscoveryService received a packet.\n" );
|
2012-08-07 08:47:28 +02:00
|
|
|
|
|
|
|
OString aString( aBuffer, strlen( "LOREMOTE_SEARCH" ) );
|
|
|
|
if ( aString.compareTo( "LOREMOTE_SEARCH" ) == 0 )
|
|
|
|
{
|
|
|
|
OStringBuffer aStringBuffer("LOREMOTE_ADVERTISE\n");
|
|
|
|
aStringBuffer.append( OUStringToOString(
|
|
|
|
osl::SocketAddr::getLocalHostname(), RTL_TEXTENCODING_UTF8 ) )
|
|
|
|
.append( "\n\n" );
|
|
|
|
if ( sendto( mSocket, aStringBuffer.getStr(),
|
|
|
|
aStringBuffer.getLength(), 0, (sockaddr*) &aAddr,
|
|
|
|
sizeof(aAddr) ) <= 0 )
|
2012-08-06 16:14:01 +02:00
|
|
|
{
|
2012-08-07 08:47:28 +02:00
|
|
|
// Read error or closed socket -- we are done.
|
|
|
|
return;
|
2012-08-02 12:27:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 11:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DiscoveryService *sd::DiscoveryService::spService = NULL;
|
|
|
|
|
|
|
|
void DiscoveryService::setup()
|
|
|
|
{
|
|
|
|
if (spService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
spService = new DiscoveryService();
|
|
|
|
spService->launch();
|
|
|
|
}
|
|
|
|
|
2012-08-06 14:16:04 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|