Logo Search packages:      
Sourcecode: ladcca version File versions  Download package

comm_buffer.c

/*
 *   LADCCA
 *    
 *   Copyright (C) 2002, 2003 Robert Ham <rah@bash.sh>
 *    
 *   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., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <netinet/in.h>

#include <ladcca/ladcca.h>
#include <ladcca/internal_headers.h>

void 
cca_buffer_from_comm_event_connect (char ** buf_ptr, size_t * buf_size_ptr, cca_connect_params_t *params)
{
  size_t buf_size;
  char * buf;
  size_t proj_name_size;
  size_t working_dir_size;
  size_t class_size;
  size_t arg_size;
  int i;
  uint32_t * iptr;
  char * ptr;
  char id_str[37];

  if (!buf_ptr)
    return;
  
  buf_size = sizeof (uint32_t) * 5;
  buf_size += proj_name_size = (params->project ? strlen (params->project) : 0) + 1;
  buf_size += working_dir_size = strlen (params->working_dir) + 1;
  buf_size += class_size = strlen (params->class) + 1;
  buf_size += sizeof (id_str);
  
  for (i = 0; i < params->argc; i++)
    buf_size += strlen (params->argv[i]) + 1;
  
  buf = cca_malloc (buf_size);

  iptr = (uint32_t *) buf;  
  *iptr = htonl (CCA_Comm_Event_Connect);
  iptr++;

  *iptr = htonl (CCA_COMM_PROTOCOL_VERSION);
  iptr++;

  *iptr = htonl (params->protocol_version);
  iptr++;

  *iptr = htonl (params->flags);
  iptr++;
  
  ptr = (char *) iptr;
  if (params->project)
    memcpy (ptr, params->project, proj_name_size);
  else
    *ptr = 0;
  ptr += proj_name_size;
  
  memcpy (ptr, params->working_dir, working_dir_size);
  ptr += working_dir_size;

  memcpy (ptr, params->class, class_size);
  ptr += class_size;
  
  uuid_unparse (params->id, id_str);
  memcpy (ptr, id_str, sizeof (id_str));
  ptr += sizeof (id_str);
    
  iptr = (uint32_t *) ptr;
  *iptr = htonl (params->argc);
  iptr++;
  
  ptr = (char *) iptr;
  for (i = 0; i < params->argc; i++)
    {
      arg_size = strlen (params->argv[i]) + 1;
      memcpy (ptr, params->argv[i], arg_size);
      ptr += arg_size;
    }

  *buf_ptr = buf;
  *buf_size_ptr = buf_size;
}

int
cca_comm_event_from_buffer_connect (char * buf, size_t buf_size, cca_comm_event_t * event)
{
  cca_connect_params_t *params;
  uint32_t * iptr;
  char * ptr;
  int i;

  CCA_DEBUGARGS ("buf_size: %d", buf_size);
  
  iptr = (uint32_t *) buf;
  event->type = ntohl (*iptr);
  iptr++;

  if (ntohl (*iptr) != CCA_COMM_PROTOCOL_VERSION)
    return -1;
  iptr++;

  params = cca_connect_params_new ();

  params->protocol_version = ntohl (*iptr);
  iptr++;
  
  params->flags = ntohl (*iptr);
  iptr++;
  
  ptr = (char *) iptr;
  if (*ptr)
    cca_connect_params_set_project (params, ptr);
  ptr += strlen (ptr) + 1;

  cca_connect_params_set_working_dir (params, ptr);
  ptr += strlen (ptr) + 1;

  cca_connect_params_set_class (params, ptr);
  ptr += strlen (ptr) + 1;

  uuid_parse (ptr, params->id);
  ptr += sizeof (char[37]);
  
  iptr = (uint32_t *) ptr;
  params->argc = ntohl (*iptr);
  iptr++;
  
  params->argv = cca_malloc (params->argc * sizeof (char *));
  
  /* set the argv array */
  ptr = (char *) iptr;
  for (i = 0; i < params->argc; i++)
    {
      CCA_DEBUGARGS ("recieving argv[%d] == '%s'", i, ptr);
      params->argv[i] = strdup (ptr);
      ptr += strlen (ptr) + 1;
    }

  event->event_data.connect = params;

  return 0;
}




void
cca_buffer_from_comm_event_event (char ** buf_ptr, size_t * buf_size_ptr, cca_event_t * event)
{
  size_t buf_size;
  char * buf;
  size_t string_size = 0;
  size_t project_size = 0;
  uint32_t * iptr;
  char *ptr;

  buf_size = sizeof (uint32_t) * 2;
  buf_size += sizeof (char[37]);
  
  if (event->string)
    buf_size += string_size = strlen (event->string) + 1;
  else
    buf_size += 1;

  if (event->project)
    buf_size += project_size = strlen (event->project) + 1;
  else
    buf_size += 1;
  
  buf = cca_malloc (buf_size);
  
  
  iptr = (uint32_t *) buf;
  *iptr = htonl (CCA_Comm_Event_Event);
  iptr++;
  
  *iptr = htonl (event->type);
  iptr++;

  ptr = (char *) iptr;
  uuid_unparse (event->client_id, ptr);
  ptr += sizeof (char[37]);
  
  if (event->string)
    {
      memcpy (ptr, event->string, string_size);
      ptr += string_size;
    }
  else
    {
      *ptr = '\0';
      ptr++;
    }

  if (event->project)
    {
      memcpy (ptr, event->project, project_size);
      ptr += project_size;
    }
  else
    {
      *ptr = '\0';
    }

  *buf_ptr = buf;
  *buf_size_ptr = buf_size;
}

void
cca_comm_event_from_buffer_event (char * buf, size_t buf_size, cca_comm_event_t * comm_event)
{
  uint32_t * iptr;
  char * ptr;
  cca_event_t * event;
  
  comm_event->type = CCA_Comm_Event_Event;
  iptr = (uint32_t *) buf;
  iptr++;
  
  event = cca_event_new ();
  
  cca_event_set_type (event, ntohl (*iptr));
  iptr++;

  ptr = (char *) iptr;
  uuid_parse (ptr, event->client_id);
  ptr += sizeof (char[37]);

  if (*ptr == '\0')
    ptr++;
  else
    {
      cca_event_set_string (event, ptr);
      ptr += strlen (event->string) + 1;
    }

  if (*ptr != '\0')
    cca_event_set_project (event, ptr);
  
  comm_event->event_data.event = event;
}






void
cca_buffer_from_comm_event_config (char ** buf_ptr, size_t * buf_size_ptr, cca_config_t * config)
{
  size_t buf_size = sizeof (uint32_t);
  char * buf;
  size_t key_size;
  uint32_t * iptr;
  char * ptr;
  
  buf_size += key_size = strlen (config->key) + 1;
  if (config->value)
    {
      buf_size += sizeof (uint32_t);
      buf_size += config->value_size;
    }
  
  buf = cca_malloc (buf_size);
  
  
  iptr = (uint32_t *) buf;
  *iptr = htonl (CCA_Comm_Event_Config);
  iptr++;
  
  memcpy (iptr, config->key, key_size);
  ptr = (char *) iptr;
  ptr += key_size;
  
  if (config->value)
    {
      iptr = (uint32_t *) ptr;
      *iptr = htonl (config->value_size);
      iptr++;
      
      memcpy (iptr, config->value, config->value_size);
    }

  *buf_ptr = buf;
  *buf_size_ptr = buf_size;
}

void
cca_comm_event_from_buffer_config (char * buf, size_t buf_size, cca_comm_event_t * event)
{
  uint32_t * iptr;
  char * ptr;
  cca_config_t * config;
  size_t key_size;
  size_t value_size;
  
  event->type = CCA_Comm_Event_Config;
  iptr = (uint32_t *) buf;
  iptr++;

  config = cca_config_new ();

  ptr = (char *) iptr;
  cca_config_set_key (config, ptr);
  
  key_size = strlen (ptr) + 1;
  if (buf_size > key_size + sizeof (uint32_t))
    {
      ptr += key_size;
      
      iptr = (uint32_t *) ptr;
      value_size = ntohl (*iptr);
      iptr++;
      
      cca_config_set_value (config, iptr, value_size);
    }
  
  event->event_data.config = config;
}

void
cca_buffer_from_comm_event_protocol_mismatch (char ** buf_ptr, size_t * buf_size_ptr, cca_protocol_t protocol)
{
  size_t buf_size = sizeof (uint32_t) + sizeof (cca_protocol_t);
  char * buf;
  uint32_t * iptr;
  
  buf = cca_malloc (buf_size);
  
  iptr = (uint32_t *) buf;
  *iptr = htonl (CCA_Comm_Event_Protocol_Mismatch);
  iptr++;
  
  *iptr = htonl (protocol);
  /* iptr++ */

  *buf_ptr = buf;
  *buf_size_ptr = buf_size;
}

void
cca_comm_event_from_buffer_protocol_mismatch (char * buf, size_t buf_size, cca_comm_event_t * event)
{
  uint32_t * iptr;
  
  iptr = (uint32_t *) buf;
  iptr++;

  cca_comm_event_set_protocol_mismatch (event, ntohl (*iptr));
}

void
cca_buffer_from_comm_event_exec (char ** buf_ptr, size_t * buf_size_ptr, cca_exec_params_t *params)
{
  uint32_t *iptr;
  char *ptr;
  char *buf;
  size_t working_dir_size;
  size_t server_size;
  size_t project_size;
  size_t argv_size;
  size_t buf_size;
  int i;
  

  /* see how big our buffer will be */
  buf_size = sizeof (uint32_t) * 3;
  buf_size += sizeof (char[37]);
  buf_size += working_dir_size = strlen (params->working_dir) + 1;
  buf_size += server_size      = strlen (params->server) + 1;
  buf_size += project_size     = strlen (params->project) + 1;

  for (i = 0; i < params->argc; i++)
    buf_size += strlen (params->argv[i]) + 1;

  
  /* create it */

  buf = cca_malloc (buf_size);

  iptr = (uint32_t *) buf;
  *iptr = htonl (CCA_Comm_Event_Exec);
  iptr++;

  *iptr = htonl (params->flags);
  iptr++;

  *iptr = htonl (params->argc);
  iptr++;

  ptr = (char *) iptr;
  uuid_unparse (params->id, ptr);
  ptr += 37;

  memcpy (ptr, params->working_dir, working_dir_size);
  ptr += working_dir_size;

  memcpy (ptr, params->server, server_size);
  ptr += server_size;

  memcpy (ptr, params->project, project_size);
  ptr += project_size;

  for (i = 0; i < params->argc; i++)
    {
      argv_size = strlen (params->argv[i]) + 1;
      memcpy (ptr, params->argv[i], argv_size);
      ptr += argv_size;
    }

  /* done */

  *buf_ptr = buf;
  *buf_size_ptr = buf_size;
}

void
cca_comm_event_from_buffer_exec (char * buf, size_t buf_size, cca_comm_event_t * event)
{
  uint32_t *iptr;
  char *ptr;
  cca_exec_params_t *params;
  int i;

  iptr = (uint32_t *) buf;
  event->type = CCA_Comm_Event_Exec;
  iptr++;

  params = cca_exec_params_new ();

  params->flags = ntohl (*iptr);
  iptr++;

  params->argc  = ntohl (*iptr);
  iptr++;

  ptr = (char *) iptr;
  uuid_parse (ptr, params->id);
  ptr += 37;

  cca_exec_params_set_working_dir (params, ptr);
  ptr += strlen (ptr) + 1;

  cca_exec_params_set_server (params, ptr);
  ptr += strlen (ptr) + 1;

  cca_exec_params_set_project (params, ptr);
  ptr += strlen (ptr) + 1;

  params->argv = cca_malloc (sizeof (char *) * params->argc);
  for (i = 0; i < params->argc; i++)
    {
      params->argv[i] = cca_strdup (ptr);
      ptr += strlen (ptr) + 1;
    }

#ifdef LADCCA_DEBUG
  CCA_DEBUGARGS ("created exec comm event with: flags=%d, working_dir=%s, server=%s, project=%s, argc=%d, args:",
             params->flags, params->working_dir, params->server, params->project, params->argc);
  for (i = 0; i < params->argc; i++)
    {
      CCA_DEBUGARGS ("  argv[%d]: '%s'", i, params->argv[i]);
    }
#endif

  cca_comm_event_set_exec (event, params);
}

/* for ping, pong and close */
void
cca_buffer_from_comm_event (char ** buf_ptr, size_t * buf_size_ptr, cca_comm_event_t * event)
{
  size_t buf_size = sizeof (uint32_t);
  char * buf;
  uint32_t * iptr;
  
  buf = cca_malloc (buf_size);
  
  iptr = (uint32_t *) buf;
  *iptr = htonl (event->type);
  iptr++;
  
  
  *buf_ptr = buf;
  *buf_size_ptr = buf_size;
}

void
cca_comm_event_from_buffer  (char * buf, size_t buf_size, cca_comm_event_t * event)
{
  event->type = ntohl (*((uint32_t *) buf));
}


/* EOF */

Generated by  Doxygen 1.6.0   Back to index