dslinux/user/pixil/sys/syncagent/src Makefile main.c plugin.c plugin.h

amadeus dslinux_amadeus at user.in-berlin.de
Tue Oct 3 13:27:17 CEST 2006


Update of /cvsroot/dslinux/dslinux/user/pixil/sys/syncagent/src
In directory antilope:/tmp/cvs-serv11916/sys/syncagent/src

Added Files:
	Makefile main.c plugin.c plugin.h 
Log Message:
adding pristine copy of pixil to HEAD so I can branch from it

--- NEW FILE: main.c ---
/* Ugly?  Yes.  This is a very simple version of the agent, which doesn't do a lot more
   than introduce the agent architecture.  We don't bother messing with any of the sync
   code that already exists
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/select.h>

#include <ipc/colosseum.h>
#include <sync/msg_defs.h>

#include "plugin.h"

static int g_src = 0;
static plugin_t *g_plugin = 0;

static int parse_msgid(char *buffer) {
  int id = atoi(buffer);
  return id;
}

static void respond_ok(int id) {
  ClSendMessage(id, "200^", 4);
}

static void respond_error(int id, char *error) {
  char *str = (char *) calloc(strlen(error) + 5, 1);
  sprintf(str, "100^%s", error);

  ClSendMessage(id, error, strlen(error));
  free(str);
}

/* Buffer format:
   1000^plugin^arg1^arg2... 
*/

int handle_connect(char *buffer) {
  
  int argc = 0, ret = -1;

  char **argv = 0;
  char *end, *start = strchr(buffer, '^');
  char *plugin;
  char *ptr;

  if (!start) return -1;
  end = strchr(start + 1, '^');

  plugin = start + 1;

  if (end) {
    *end = 0;
    start = end + 1;
    
    while(1) {
      end = strchr(start, '^');
      if (!argv) argv = (char **) calloc(argc + 1, sizeof(char *));
      else argv = (char **) realloc(argv, (argc + 1 ) * sizeof(char *));
      argv[argc++] = start;
  
      if (!end) break;
      *end = 0;
      start = end + 1;
    }
  }

  /* Go lowercase on the plugin name */

  ptr = plugin;

  while(*ptr) 
    *ptr++ = tolower(*ptr);

  g_plugin = load_plugin(plugin);

  if (g_plugin) 
    ret = g_plugin->init(argc, argv);
  
  if (argv) free(argv);

  if (ret == -1 && g_plugin) {
    free_plugin(g_plugin);
    g_plugin = 0;
  }

  return (ret > 0) ? 0 : -1;
}

void handle_disconnect(void) {
  if (!g_plugin) return;
  g_plugin->close();

  free_plugin(g_plugin);
  g_plugin = 0;
}

int
handle_incoming(plugin_t * plugin)
{
    char *buffer = 0;
    int len = plugin->read(&buffer);
    
    if (len <= 0)
	return len;

    if (g_src)
      ClSendMessage(g_src, buffer, len);
    
    free(buffer);

    return 0;
}

int
handle_outgoing(plugin_t * plugin)
{
    int ret, size = CL_MAX_MSG_LEN;
    int msg_id;

    unsigned short src;

    char *buffer = (char *) calloc(CL_MAX_MSG_LEN, 1);
    if (!buffer)
	return;

    ret = ClGetMessage(buffer, &size, &src);

    if (ret < 0) {
      free(buffer);
      return -1;
    }

    /* Check to see if it is destined for us */
    msg_id = parse_msgid(buffer);

    if (msg_id == CONNECT) {
      printf("SYNAGENT:  Incoming connect request\n");

      if (handle_connect(buffer) == 0) 
	respond_ok(src);
      else
	respond_error(src, "Unable to connect to the remote agent\n");
    }

    else if (msg_id == DISCONNECT) {
      printf("SYNAGENT:  Incomming disconnect request\n");
      handle_disconnect();
      respond_ok(src);
    }
    else if (plugin) {
      g_src = src;
      plugin->write(buffer, size);
    }
    else  printf("SYNAGENT:  I don't know what to do with the incoming message [%d] [%s]\n", ret, buffer);
    free(buffer);
}

int
main(int argc, char **argv)
{

  int clsock, flags;

    clsock = ClRegister("syncagent", &flags);

    if (clsock < 0) {
	printf("SYNCAGENT:  Colosseum returned %d\n", clsock);
	exit(-1);
    }

    while (1) {
	fd_set fdset;
	int ret, max = clsock;

	FD_ZERO(&fdset);

	if (g_plugin) {
	  FD_SET(g_plugin->getfd(), &fdset);
	  if (g_plugin->getfd() > clsock) max = g_plugin->getfd();
	}

	FD_SET(clsock, &fdset);

	ret = select(max + 1, &fdset, 0, 0, 0);

	if (ret < 0) break;

	if (g_plugin && FD_ISSET(g_plugin->getfd(), &fdset))
	  if (handle_incoming(g_plugin) == -1)
	    break;

	if (FD_ISSET(clsock, &fdset))
	  if (handle_outgoing(g_plugin) == -1)
	    break;
    }

    printf("SYNCAGENT:  Shutting down\n");
    
    if (g_plugin->getfd()) 
      handle_disconnect();
      
    ClClose();    
    return 0;
}

--- NEW FILE: Makefile ---
#nanox/pixilwm/Makefile

SRC=${shell ls *.c} 
OBJS=${SRC:.c=.o}

TARGET=syncagent

ifeq ($(CONFIG_PAR),y)
LIBS+=-lpar
endif

ifeq ($(CONFIG_COLOSSEUM),y)
LIBS+=-lipc
endif

LIBS += -ldl

include $(BASE_DIR)/Rules.make

--- NEW FILE: plugin.c ---
/*                                                                       
 * Copyright (c) 2003 Century Software, Inc.   All Rights Reserved.     
 *                                                                       
 * This file is part of the PIXIL Operating Environment                 
 *                                                                       
 * The use, copying and distribution of this file is governed by one    
 * of two licenses, the PIXIL Commercial License, or the GNU General    
 * Public License, version 2.                                           
 *                                                                       
 * Licensees holding a valid PIXIL Commercial License may use this file 
 * in accordance with the PIXIL Commercial License Agreement provided   
 * with the Software. Others are governed under the terms of the GNU   
 * General Public License version 2.                                    
 *                                                                       
 * This file may be distributed and/or modified under the terms of the  
 * GNU General Public License version 2 as published by the Free        
 * Software Foundation and appearing in the file LICENSE.GPL included   
 * in the packaging of this file.                                      
 *                                                                       
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING  
 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A            
 * PARTICULAR PURPOSE.                                                  
 *                                                                       
 * RESTRICTED RIGHTS LEGEND                                             
 *                                                                     
 * Use, duplication, or disc1losure by the government is subject to      
 * restriction as set forth in paragraph (b)(3)(b) of the Rights in     
 * Technical Data and Computer Software clause in DAR 7-104.9(a).       
 *                                                                      
 * See http://www.pixil.org/gpl/ for GPL licensing       
 * information.                                                         
 *                                                                      
 * See http://www.pixil.org/license.html or              
 * email cetsales at centurysoftware.com for information about the PIXIL   
 * Commercial License Agreement, or if any conditions of this licensing 
 * are not clear to you.                                                
 */

#include <pixil_config.h>

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

#include <dlfcn.h>

#ifdef CONFIG_PAR
#include <par/par.h>
#endif

#include "plugin.h"

static plugin_t *g_plugin = 0;

/* Fixme:  Just a bogus path right for the moment */

#define PLUGIN_PATH "../tcpip/"

plugin_t *
load_plugin(char *filename)
{
  char *data = 0;

  char path[128];

#ifdef CONFIG_PAR
  db_handle *handle = db_openDB(db_getDefaultDB(), PAR_DB_MODE_RDONLY);
  
  if (handle) {
    int len = par_getCapability(handle, "syncagent", (void **) &data);
    db_closeDB(handle);
  }
#endif

  if (data)
    sprintf(path, "%s/%s.so", data, filename);
  else
    sprintf(path, "%s/%s.so", PLUGIN_PATH, filename);

  printf("Trying to load [%s]\n", path);

  if (g_plugin)
    return g_plugin;
  
  g_plugin = (plugin_t *) calloc(1, sizeof(plugin_t));

  if (!g_plugin)
    return 0;
  
  strcpy(g_plugin->name, basename(path));
    g_plugin->handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);

    if (g_plugin->handle) {
      g_plugin->init = (int (*)(int, char **))
	dlsym(g_plugin->handle, "pl_init");
      
      g_plugin->close = (int (*)(void))
	dlsym(g_plugin->handle, "pl_close");
      
      g_plugin->getfd = (int (*)(void))
	dlsym(g_plugin->handle, "pl_getfd");

      g_plugin->read = (int (*)(char **))
	dlsym(g_plugin->handle, "pl_read");
      
      g_plugin->write = (int (*)(char *, int))
	dlsym(g_plugin->handle, "pl_write");
      
      return g_plugin;
    }

    free(g_plugin);
    g_plugin = 0;
    
    return 0;
}

void free_plugin(plugin_t *plugin) {

  if (plugin != g_plugin) return;
  if (!g_plugin || !g_plugin->handle) return;

  dlclose(g_plugin->handle);
  free(g_plugin);

  g_plugin = 0;
}


--- NEW FILE: plugin.h ---
#ifndef PLUGIN_H_
#define PLUGIN_H_

typedef struct plugin_struct
{
    char name[32];
    void *handle;

    int (*init) (int, char **);
  int (*close) (void);
  int (*getfd)(void);
    int (*read) (char **);
    int (*write) (char *, int);

    struct plugin_struct *next;
}
plugin_t;

plugin_t *load_plugin(char *filename);
void free_plugin(plugin_t *);

#endif




More information about the dslinux-commit mailing list