slp.cpp 2.76 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * slp.cpp : SLP registration class
 ****************************************************************************
 * Copyright (C) 1998-2003 VideoLAN
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
5
 * $Id$
6
 *
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
7
 * Authors: Clément Stenac <zorglub@via.ecp.fr>
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

24 25 26
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
27 28 29 30 31 32 33 34 35

#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <vector>

36 37
using namespace std;

38
#include "sapserver.h"
39 40
#include "program.h"

41
#ifdef CONFIG_SLP
42 43 44
  #include <slp.h>
#endif

45
#include "lslp.h"
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67

using namespace std;

SLP::SLP()
{
    if( SLPOpen( NULL, SLP_FALSE, &slp_handle ) != SLP_OK)
    {
	fprintf(stderr,"- Unable to initialize SLP\n");
	abort();
    } 
}


SLP::~SLP(){return;}


void RegReport(SLPHandle slp_handle,SLPError slp_error,void* cookie)
{
}

char * BuildService(Program *p)
{
68 69 70
    string address = p->GetAddress();
    if (address.find(':') != string::npos)
        address = "[" + address + "]";
71

72
    string url = "udp:@" + address + ":" + p->GetPort();
73

74 75 76 77 78 79 80
#if 0
    /* This code is for the new naming scheme after vlc 0.7.0 */
    /* service:vlc.services.videolan.org://MRL//NAME */
    url += "//";
    url += p->GetName();
#endif

81
    string msg = "service:vlc.services.videolan.org://" + url;
82

83
    return strdup(msg.c_str());
84 85 86 87 88
}

int SLP::Register(Program *p)
{
    char *psz_msg = BuildService(p);
89
 
90 91 92 93 94
    string name;
    name += p->GetName();

    fprintf(stdout,"+ Registering %s (name: %s)\n",psz_msg,name.data());

95 96 97 98 99 100 101 102
    SLPReg (    slp_handle, 
                psz_msg,
                SLP_LIFETIME_MAXIMUM,
                NULL,
                name.data(),
                SLP_TRUE,
                RegReport,
                NULL );
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121

    if(psz_msg) free(psz_msg);

    return 0;
}

int SLP::UnRegister(Program *p)
{
    char *psz_msg = BuildService(p);
   
    fprintf(stdout,"+ Unregistering %s\n",psz_msg);

    SLPDereg (	slp_handle,
		psz_msg,
		RegReport,
		NULL );

    return 0;
}