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

simple_client.c

/*
 *   LADCCA
 *    
 *   Copyright (C) 2002 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.
 */

#define _GNU_SOURCE

#include <stdio.h>
#include <unistd.h>
#include <alloca.h>

#include <jack/jack.h>
#include <alsa/asoundlib.h>

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

int main (int argc, char ** argv)
{
  cca_event_t * event;
  cca_config_t * config;
  cca_client_t * client;
  jack_client_t * jack_client;
  snd_seq_t * aseq;
  snd_seq_client_info_t * aseq_client_info;
  char client_name [64];
  int err;
  int done = 0;
  
  sprintf (client_name, "lsec_%d", getpid ());
  printf ("%s: client name: '%s'\n", __FUNCTION__, client_name);
  
  printf ("%s: attempting to initialise ladcca\n", __FUNCTION__);

  client = cca_init (cca_extract_args (&argc, &argv), "LADCCA Simple Client",
                     CCA_Config_Data_Set|CCA_Config_File,
                     CCA_PROTOCOL (2,0));
  
  if (!client)
    {
      fprintf (stderr, "%s: could not initialise ladcca\n", __FUNCTION__);
      exit (1);
    }
  
  printf ("%s: initialised\n", __FUNCTION__);
  
  event = cca_event_new_with_type (CCA_Client_Name);
  cca_event_set_string (event, client_name);
  cca_send_event (client, event);
  
  /*
   * jack ports
   */
  printf ("%s: connecting to jack server\n", __FUNCTION__);
  jack_client = jack_client_new (client_name);
  if (!jack_client)
    {
      printf ("%s: could not connect to jack server", __FUNCTION__);
      exit (1);
    }
  printf ("%s: connected to jack with client name '%s'\n", __FUNCTION__, client_name);
  
  
  printf ("%s: opening alsa sequencer\n", __FUNCTION__);
  err = snd_seq_open (&aseq,
                      "default",
                      SND_SEQ_OPEN_DUPLEX,
                      0);
  if (err)
    {
      printf("%s: could not open alsa sequencer\n", __FUNCTION__);
      exit (1);
    }
  snd_seq_client_info_alloca (&aseq_client_info);
  snd_seq_get_client_info (aseq, aseq_client_info);
  snd_seq_client_info_set_name (aseq_client_info, client_name);
  snd_seq_set_client_info (aseq, aseq_client_info);
  printf ("%s: opened alsa sequencer with id %d, name '%s'\n",
          __FUNCTION__, snd_seq_client_id (aseq), client_name);
  

  
  event = cca_event_new_with_type (CCA_Jack_Client_Name);
  cca_event_set_string (event, client_name);
  cca_send_event (client, event);
  
  event = cca_event_new_with_type (CCA_Alsa_Client_ID);
  client_name[0] = (char) snd_seq_client_id (aseq);
  client_name[1] = '\0';
  CCA_DEBUGARGS ("alsa client id: %d", snd_seq_client_id (aseq));
  cca_event_set_string (event, client_name); 
  cca_send_event (client, event);
  
  

  while (!done)
    {
      printf ("%s: trying to get events and configs\n", __FUNCTION__);
      
      event = cca_get_event (client);
      if (event)
        {
          printf ("%s: got event of type %d with string '%s'\n",
                  __FUNCTION__, cca_event_get_type (event), cca_event_get_string (event));
          switch (cca_event_get_type (event))
          {
          case CCA_Save_Data_Set:
            config = cca_config_new_with_key ("test config");
            cca_config_set_value_string (config, "this is some configuration data");
            cca_send_config (client, config);
            cca_send_event (client, event);
            break;
          case CCA_Save_File:
          {
            FILE * config_file;
            config_file = fopen (cca_get_fqn (cca_event_get_string (event), "simple-client.data"), "w");
            fprintf (config_file, "ladcca simple client data");
            fclose (config_file);
            CCA_DEBUG ("wrote config file");
            cca_send_event (client, event);
          }
          case CCA_Restore_File:
          {
            FILE * config_file;
            char * buf = NULL;
            size_t buf_size = 0;
            
            config_file = fopen (cca_get_fqn (cca_event_get_string (event), "simple-client.data"), "r");
            if (config_file)
              {
                getline (&buf, &buf_size, config_file);
                fclose (config_file);
              }
            
            CCA_DEBUGARGS ("read data from config file: '%s'", buf);
            if (buf)
              free (buf);
            
            cca_send_event (client, event);
            break;
          }
        case CCA_Server_Lost:
          printf ("server connection lost; exiting\n");
          exit (0);
          break;

          default:
            fprintf (stderr, "%s: recieved unknown LADCCA event of type %d\n",
                     __FUNCTION__, cca_event_get_type (event));
            cca_event_destroy (event);
            break;
          }
        }

      config = cca_get_config (client);
      if (config)
        {
          printf ("%s: got config with key '%s', value_size %d\n",
                  __FUNCTION__, cca_config_get_key (config), cca_config_get_value_size (config));
          cca_config_free (config);
          free (config);
        }
      
      sleep (5);
    }
  
  return 0;
}


Generated by  Doxygen 1.6.0   Back to index