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-02 11:28:38 +02:00
|
|
|
|
|
|
|
#include "DiscoveryService.hxx"
|
|
|
|
|
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()
|
|
|
|
:
|
|
|
|
Thread( "sd::DiscoveryService" ),
|
|
|
|
mSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DiscoveryService::~DiscoveryService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-02 12:27:19 +02:00
|
|
|
void DiscoveryService::replyTo( SocketAddr& rAddr )
|
|
|
|
{
|
|
|
|
SocketAddr aLocalAddr;
|
|
|
|
mSocket.getLocalAddr( aLocalAddr );
|
|
|
|
OString aAddrString = OUStringToOString( aLocalAddr.getHostname(),
|
|
|
|
RTL_TEXTENCODING_UTF8 );
|
|
|
|
OStringBuffer aBuffer( "LOREMOTE_ADVERTISE\n" );
|
|
|
|
aBuffer.append( aAddrString ).append( "\n" );
|
|
|
|
mSocket.sendTo( rAddr, aBuffer.getStr(), aBuffer.getLength() );
|
|
|
|
}
|
2012-08-02 11:28:38 +02:00
|
|
|
|
|
|
|
void DiscoveryService::execute()
|
|
|
|
{
|
2012-08-02 16:11:06 +02:00
|
|
|
fprintf( stderr, "Discovery service is listening\n" );;
|
2012-08-02 12:27:19 +02:00
|
|
|
sal_uInt64 aRet, aRead;
|
|
|
|
vector<char> aBuffer;
|
|
|
|
aRead = 0;
|
2012-08-02 11:28:38 +02:00
|
|
|
|
2012-08-02 16:11:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
SocketAddr aListenAddr( "239.0.0.1", PORT_DISCOVERY );
|
|
|
|
mSocket.bind( aListenAddr );
|
|
|
|
|
2012-08-02 12:27:19 +02:00
|
|
|
SocketAddr aAddr;
|
|
|
|
while ( true )
|
|
|
|
{
|
|
|
|
aBuffer.resize( aRead + 100 );
|
2012-08-02 16:11:06 +02:00
|
|
|
|
|
|
|
fprintf( stderr, "DiscoveryService waiting for packet\n" );
|
2012-08-02 12:27:19 +02:00
|
|
|
aRet = mSocket.recvFrom( &aBuffer[aRead], 100 );
|
2012-08-02 16:11:06 +02:00
|
|
|
fprintf( stderr, "DiscoveryService received a packet.\n" );
|
2012-08-02 12:27:19 +02:00
|
|
|
if ( aRet == 0 )
|
|
|
|
{
|
|
|
|
fprintf( stderr, "Socket returned 0\n" );
|
|
|
|
// break; // I.e. transmission finished.
|
|
|
|
}
|
|
|
|
aRead += aRet;
|
|
|
|
vector<char>::iterator aIt;
|
|
|
|
while ( (aIt = find( aBuffer.begin(), aBuffer.end(), '\n' ))
|
|
|
|
!= aBuffer.end() )
|
|
|
|
{
|
|
|
|
sal_uInt64 aLocation = aIt - aBuffer.begin();
|
|
|
|
OString aString( &(*aBuffer.begin()), aLocation );
|
|
|
|
if ( aString.compareTo( "LOREMOTE_SEARCH" ) == 0 ) {
|
|
|
|
replyTo( aAddr );
|
|
|
|
}
|
|
|
|
aBuffer.erase( aBuffer.begin(), aIt + 1 ); // Also delete the newline
|
|
|
|
aRead -= (aLocation + 1);
|
|
|
|
}
|
|
|
|
}
|
2012-08-02 11:28:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DiscoveryService *sd::DiscoveryService::spService = NULL;
|
|
|
|
|
|
|
|
void DiscoveryService::setup()
|
|
|
|
{
|
|
|
|
if (spService)
|
|
|
|
return;
|
|
|
|
|
|
|
|
spService = new DiscoveryService();
|
|
|
|
spService->launch();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|