Update of /cvsroot/playerstage/code/player/libplayersd
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3525/libplayersd

Modified Files:
        Makefile.am playersd.h playersd_mdns.c 
Added Files:
        playersd.c 
Log Message:
sd enhancements

Index: playersd_mdns.c
===================================================================
RCS file: /cvsroot/playerstage/code/player/libplayersd/playersd_mdns.c,v
retrieving revision 1.8
retrieving revision 1.9
diff -C2 -d -r1.8 -r1.9
*** playersd_mdns.c     26 Sep 2007 17:36:58 -0000      1.8
--- playersd_mdns.c     26 Sep 2007 19:53:45 -0000      1.9
***************
*** 78,82 ****
    uint8_t fail;
    // Identifying information, as provided by user when registering
!   player_sd_device_t sdDev;
    // Index appended to sdDev.name to make it unique
    int nameIdx;
--- 78,82 ----
    uint8_t fail;
    // Identifying information, as provided by user when registering
!   player_sd_dev_t sdDev;
    // Index appended to sdDev.name to make it unique
    int nameIdx;
***************
*** 121,124 ****
--- 121,134 ----
                       void *context);
  
+ static void resolveCB(DNSServiceRef sdRef, 
+                       DNSServiceFlags flags, 
+                       uint32_t interfaceIndex, 
+                       DNSServiceErrorType errorCode, 
+                       const char *fullname, 
+                       const char *hosttarget, 
+                       uint16_t port, 
+                       uint16_t txtLen, 
+                       const char *txtRecord, 
+                       void *context);  
  
  player_sd_t* 
***************
*** 130,133 ****
--- 140,146 ----
    sd = (player_sd_t*)malloc(sizeof(player_sd_t));
    assert(sd);
+   sd->devs = NULL;
+   sd->devs_len = 0;
+ 
    mdns = (player_sd_mdns_t*)malloc(sizeof(player_sd_mdns_t));
    assert(mdns);
***************
*** 326,330 ****
                                 NULL,
                                 browseCB,
!                                (void*)mdns)) != kDNSServiceErr_NoError)
    {
      PLAYER_ERROR1("DNSServiceBrowse returned error: %d", sdErr);
--- 339,343 ----
                                 NULL,
                                 browseCB,
!                                (void*)sd)) != kDNSServiceErr_NoError)
    {
      PLAYER_ERROR1("DNSServiceBrowse returned error: %d", sdErr);
***************
*** 336,340 ****
  
    // Should we wait here for responses?
!   if(timeout > 0.0)
    {
      // Record the current time
--- 349,353 ----
  
    // Should we wait here for responses?
!   if(timeout != 0.0)
    {
      // Record the current time
***************
*** 342,346 ****
      starttime = currtime = curr.tv_sec + curr.tv_usec / 1e6;
  
!     while((currtime - starttime) < timeout)
      {
        // Set up to poll on the DNSSD socket
--- 355,359 ----
      starttime = currtime = curr.tv_sec + curr.tv_usec / 1e6;
  
!     while((timeout < 0.0) || ((currtime - starttime) < timeout))
      {
        // Set up to poll on the DNSSD socket
***************
*** 410,412 ****
--- 423,502 ----
           void *context)
  {
+   player_sd_t* sd = (player_sd_t*)context;
+   player_sd_dev_t* sddev;
+   DNSServiceRef resolveRef;
+   DNSServiceErrorType sdErr;
+ 
+   // Got a browse event.  
+   if(flags & kDNSServiceFlagsAdd)
+   {
+     // A new service was added.  First check whether we already have an
+     // entry for a device with this name.
+     if(!(sddev = player_sd_get_device(sd, serviceName)))
+     {
+       // No existing entry.  Add one
+       sddev = _player_sd_add_device(sd, serviceName);
+     }
+ 
+     // Record the name
+     sddev->valid = 1;
+     sddev->addr_valid = 0;
+     memset(sddev->name,0,sizeof(sddev->name));
+     strncpy(sddev->name,serviceName,sizeof(sddev->name)-1);
+ 
+     printf("Resolving %s\n", serviceName);
+     // Resolve its address
+     if((sdErr = DNSServiceResolve(&resolveRef,
+                                   0,
+                                   interfaceIndex, 
+                                   serviceName, 
+                                   regtype, 
+                                   replyDomain,
+                                   resolveCB,
+                                   (void*)sddev)) != kDNSServiceErr_NoError)
+     {
+       PLAYER_ERROR1("DNSServiceResolve returned error: %d\n", sdErr);
+       return;
+     }
+ 
+     puts("Calling DNSServiceProcessResult");
+     
+     // Wait for the resolution response.
+     // TODO: test whether this can block for a long time
+     if((sdErr = DNSServiceProcessResult(resolveRef)) != 
kDNSServiceErr_NoError)
+     {
+       PLAYER_ERROR1("DNSServiceProcessResult returned error: %d\n", sdErr);
+       return;
+     }
+     puts("done");
+   }
+   else
+   {
+     // An existing service was removed.  Delete our records for it.
+     if((sddev = player_sd_get_device(sd, serviceName)))
+     {
+       sddev->valid = 0;
+     }
+   }
+ 
+   puts("***************************************************");
+   puts("Device cache:");
+   player_sd_printcache(sd);
+   puts("***************************************************");
+ }
+ 
+ void 
+ resolveCB(DNSServiceRef sdRef, 
+           DNSServiceFlags flags, 
+           uint32_t interfaceIndex, 
+           DNSServiceErrorType errorCode, 
+           const char *fullname, 
+           const char *hosttarget, 
+           uint16_t port, 
+           uint16_t txtLen, 
+           const char *txtRecord, 
+           void *context)
+ {
+   // Handle resolution result
+   printf("resolveCB: %s\n", fullname);
  }

Index: Makefile.am
===================================================================
RCS file: /cvsroot/playerstage/code/player/libplayersd/Makefile.am,v
retrieving revision 1.1
retrieving revision 1.2
diff -C2 -d -r1.1 -r1.2
*** Makefile.am 25 Sep 2007 23:53:50 -0000      1.1
--- Makefile.am 26 Sep 2007 19:53:45 -0000      1.2
***************
*** 18,22 ****
  CLEANFILES = playersd.pc
  
! libplayersd_la_SOURCES = playersd_mdns.c playersd.h 
  
  libplayersd_la_LDFLAGS = $(PLAYER_VERSION_INFO) -rpath $(libdir) 
$(top_builddir)/libplayercore/libplayererror.la 
$(top_builddir)/libplayercore/libplayerutils.la
--- 18,22 ----
  CLEANFILES = playersd.pc
  
! libplayersd_la_SOURCES = playersd_mdns.c playersd.h playersd.c
  
  libplayersd_la_LDFLAGS = $(PLAYER_VERSION_INFO) -rpath $(libdir) 
$(top_builddir)/libplayercore/libplayererror.la 
$(top_builddir)/libplayercore/libplayerutils.la

--- NEW FILE: playersd.c ---
/*
 *  Player - One Hell of a Robot Server
 *  Copyright (C) 2007
 *     Brian Gerkey
 *                      
 *
 *  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-1307  USA
 *
 */
/*
 *  Player - One Hell of a Robot Server
 *  Copyright (C) 2007
 *     Brian Gerkey
 *                      
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/*
 * Common (i.e., implementation-independent) libplayersd functions.
 *
 * $Id: playersd.c,v 1.1 2007/09/26 19:53:45 gerkey Exp $
 */

#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdio.h>

#include <libplayercore/addr_util.h>
#include <libplayercore/interface_util.h>
#include "playersd.h"

#define PLAYER_SD_DEVS_LEN_INITIAL 4
#define PLAYER_SD_DEVS_LEN_MULTIPLIER 2

player_sd_dev_t* 
player_sd_get_device(player_sd_t* sd, const char* name)
{
  int i;

  for(i=0;i<sd->devs_len;i++)
  {
    if(sd->devs[i].valid && !strcmp(sd->devs[i].name,name))
      return(sd->devs + i);
  }
  
  return(NULL);
}

player_sd_dev_t*
_player_sd_add_device(player_sd_t* sd, const char* name)
{
  int i,j;

  // Look for an empty spot.
  for(i=0;i<sd->devs_len;i++)
  {
    if(!sd->devs[i].valid)
      break;
  }

  // Grow the list if necessary
  if(i == sd->devs_len)
  {
    if(!sd->devs_len)
      sd->devs_len = PLAYER_SD_DEVS_LEN_INITIAL;
    else
      sd->devs_len *= PLAYER_SD_DEVS_LEN_MULTIPLIER;

    sd->devs = 
            (player_sd_dev_t*)realloc(sd->devs,
                                      sizeof(player_sd_dev_t) * sd->devs_len);
    assert(sd->devs);

    for(j=i;j<sd->devs_len;j++)
      sd->devs[j].valid = 0;
  }

  return(sd->devs + i);
}

void
player_sd_printcache(player_sd_t* sd)
{
  char ip[16];
  int i;

  puts("Device cache:");
  for(i=0;i<sd->devs_len;i++)
  {
    if(sd->devs[i].valid)
    {
      printf("  name:%s\n", sd->devs[i].name);
      if(sd->devs[i].addr_valid)
      {
        packedaddr_to_dottedip(ip,sizeof(ip),sd->devs[i].addr.host);
        printf("    host:    %s\n"
               "    robot:   %d\n"
               "    interf:  %s\n"
               "    index:   %d\n",
               ip, 
               sd->devs[i].addr.robot, 
               interf_to_str(sd->devs[i].addr.interf), 
               sd->devs[i].addr.index);
      }
    }
  }
}


Index: playersd.h
===================================================================
RCS file: /cvsroot/playerstage/code/player/libplayersd/playersd.h,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** playersd.h  26 Sep 2007 17:36:58 -0000      1.7
--- playersd.h  26 Sep 2007 19:53:45 -0000      1.8
***************
*** 72,78 ****
  typedef struct
  {
    char name[PLAYER_SD_NAME_MAXLEN];
    player_devaddr_t addr;
! } player_sd_device_t;
  
  /// Service discovery object
--- 72,84 ----
  typedef struct
  {
+   // Is this entry valid?
+   uint8_t valid;
+   // Name of the device
    char name[PLAYER_SD_NAME_MAXLEN];
+   // Is the address valid?
+   uint8_t addr_valid;
+   // Address of the device
    player_devaddr_t addr;
! } player_sd_dev_t;
  
  /// Service discovery object
***************
*** 83,89 ****
    void* sdRef;
    /// List of devices discovered by browsing
!   player_sd_device_t* devs;
    /// Number of devices discovered
!   size_t numdevs;
  } player_sd_t;
  
--- 89,95 ----
    void* sdRef;
    /// List of devices discovered by browsing
!   player_sd_dev_t* devs;
    /// Number of devices discovered
!   size_t devs_len;
  } player_sd_t;
  
***************
*** 135,138 ****
--- 141,158 ----
  int player_sd_browse_stop(player_sd_t* sd);
  
+ ////////////////////////////////////////////////////////////////////////////
+ // Common functions, implemented in playersd.c
+ 
+ /// Look up a device by name.  This functions only consults the local cache 
+ /// of registered devices, which was filled by player_sd_browse().
+ player_sd_dev_t* player_sd_get_device(player_sd_t* sd, const char* name);
+ 
+ /// Add a device to the local cache.  The user should not call this
+ /// function.  It will be invoked by the playersd implementation.
+ player_sd_dev_t* _player_sd_add_device(player_sd_t* sd, const char* name);
+ 
+ /// Print the contents of the local device cache to the console.
+ void player_sd_printcache(player_sd_t* sd);
+ 
  #ifdef __cplusplus
  }


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Playerstage-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/playerstage-commit

Reply via email to