int cl_endpoint_list_undefine_endpoint(cl_raw_list_t* list_p, cl_com_endpoint_t* endpoint) {
   int back = CL_RETVAL_UNKNOWN_ENDPOINT;
   int ret_val = CL_RETVAL_OK;
   cl_endpoint_list_elem_t* elem = NULL;
   
   if (list_p == NULL || endpoint == NULL) {
      return CL_RETVAL_PARAMS;
   }

   /* lock list */
   if ( (ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
      return ret_val;
   }

   elem = cl_endpoint_list_get_elem_endpoint(list_p, endpoint);
   if (elem && elem->is_static == false) {
      cl_endpoint_list_data_t* ldata = NULL;

      cl_raw_list_remove_elem(list_p, elem->raw_elem);
      cl_com_free_endpoint(&(elem->endpoint));
      sge_free(&elem);

      ldata = list_p->list_data;
      if (ldata->ht != NULL) {
         sge_htable_delete(ldata->ht, endpoint->hash_id);
      }
      back = CL_RETVAL_OK;
   }

   /* unlock list */
   if ( (ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
      return ret_val;
   }
   return back;
}
int cl_endpoint_list_cleanup(cl_raw_list_t** list_p) {
   cl_endpoint_list_data_t* ldata = NULL;
   cl_endpoint_list_elem_t* elem = NULL;
   
   if (list_p == NULL) {
      /* we expect an address of an pointer */
      return CL_RETVAL_PARAMS;
   }

   if (*list_p == NULL) {
      /* we expect an initalized pointer */
      return CL_RETVAL_PARAMS;
   }

   /* delete all entries in list */
   cl_raw_list_lock(*list_p);
   while ( (elem = cl_endpoint_list_get_first_elem(*list_p)) != NULL) {
      cl_raw_list_remove_elem(*list_p, elem->raw_elem);
      cl_com_free_endpoint(&(elem->endpoint));
      sge_free(&elem);
   }
   cl_raw_list_unlock(*list_p);

   /* clean list private data */
   ldata = (*list_p)->list_data;
   if (ldata != NULL) {
      if (ldata->ht != NULL) {
         sge_htable_destroy(ldata->ht);
      }
      sge_free(&ldata);
   }
   (*list_p)->list_data = NULL;

   return cl_raw_list_cleanup(list_p);
}
int cl_endpoint_list_define_endpoint(cl_raw_list_t* list_p, cl_com_endpoint_t* endpoint, int service_port, cl_xml_connection_autoclose_t autoclose, bool is_static) {

   int ret_val = CL_RETVAL_OK;
   struct timeval now;
   cl_com_endpoint_t* dup_endpoint = NULL;
   cl_endpoint_list_elem_t* new_elem = NULL;
   cl_endpoint_list_elem_t* elem = NULL;

   if (endpoint == NULL || list_p == NULL) {
      return CL_RETVAL_PARAMS;
   }

   /* lock the list and check for duplicate entry */
   if ( (ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
      return ret_val;
   }

   elem = cl_endpoint_list_get_elem_endpoint(list_p, endpoint);
   if (elem) {
      /* found matching endpoint */
      gettimeofday(&now,NULL);
      elem->last_used = now.tv_sec;
      elem->service_port = service_port;
      elem->autoclose = autoclose;
      if (elem->is_static == true && is_static == false ) {
         CL_LOG(CL_LOG_DEBUG,"can't set static element to non static");
      } else {
         elem->is_static = is_static;
      }
     
      /* unlock the list */
      if ((ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
         return ret_val;
      }
      return CL_RETVAL_OK;
   }

   /* unlock the list */
   if ((ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
      return ret_val;
   }

   /* create a copy of endpoint */
   dup_endpoint = cl_com_dup_endpoint(endpoint);
   if (dup_endpoint == NULL) {
      return CL_RETVAL_MALLOC;
   }

   /* add new element list */
   new_elem = (cl_endpoint_list_elem_t*) malloc(sizeof(cl_endpoint_list_elem_t));
   if (new_elem == NULL) {
      cl_com_free_endpoint(&dup_endpoint);
      return CL_RETVAL_MALLOC;
   }

   gettimeofday(&now,NULL);
   new_elem->endpoint = dup_endpoint;
   new_elem->service_port = service_port;
   new_elem->autoclose = autoclose;
   new_elem->is_static = is_static;
   new_elem->last_used = now.tv_sec;

   /* lock the list */
   if ((ret_val = cl_raw_list_lock(list_p)) != CL_RETVAL_OK) {
      sge_free(&new_elem);
      return ret_val;
   }
   new_elem->raw_elem = cl_raw_list_append_elem(list_p, (void*) new_elem);
   if ( new_elem->raw_elem == NULL) {
      cl_raw_list_unlock(list_p);
      cl_com_free_endpoint(&dup_endpoint);
      sge_free(&new_elem);
      return CL_RETVAL_MALLOC;
   } else {
      cl_endpoint_list_data_t* ldata = list_p->list_data;
      if (ldata->ht != NULL) {
         sge_htable_store(ldata->ht, dup_endpoint->hash_id, new_elem);
      }
   }

   /* unlock the list */
   if ((ret_val = cl_raw_list_unlock(list_p)) != CL_RETVAL_OK) {
      return ret_val;
   }

   return CL_RETVAL_OK;
}
Пример #4
0
/*
 *  main()
 */
extern int main(int argc, char** argv) {
   cl_com_handle_t*    handle        = NULL; 
   cl_com_message_t*   message       = NULL;
   cl_com_endpoint_t*  sender        = NULL;
   cl_ssl_setup_t      ssl_config;
   cl_framework_t      framework     = CL_CT_TCP;
   int                 ret_val       = CL_RETVAL_OK;
   const int           fd_in         = 0;
   const int           fd_out        = 1;
   char                fd_in_data[255];
   char                fd_out_data[255];
   char*               reg           = NULL; 
   int                 error         = CL_RETVAL_OK;   

   reg = "reg";

   fd_in_data[0] = '\0';
   fd_out_data[0] = '\0';
 
   /* check command line argument count */
   if (argc <= 3) {
      usage();
      exit(1);
   }

   /* get service host and port as also the connection mode from command line */
   server_host = argv[1];
   handle_port = atoi(argv[2]);
   if (handle_port <= 0) {
      fprintf(stderr,"need a port number > 0\n");
      usage();
      exit(1);
   }

   if (argv[3]) {
      if (strcmp(argv[3], "SSL") == 0) {
         framework=CL_CT_SSL;
         printf("using SSL framework\n");
      }
   }else{
      printf("using TCP framework\n");
   }
 
   /* set the ssl environment */
   if(framework == CL_CT_SSL){
      memset(&ssl_config, 0, sizeof(ssl_config));
      ssl_config.ssl_method           = CL_SSL_v23;                 /*  v23 method                                  */
      ssl_config.ssl_CA_cert_pem_file = getenv("SSL_CA_CERT_FILE"); /*  CA certificate file                         */
      ssl_config.ssl_CA_key_pem_file  = NULL;                       /*  private certificate file of CA (not used)   */
      ssl_config.ssl_cert_pem_file    = getenv("SSL_CERT_FILE");    /*  certificates file                           */
      ssl_config.ssl_key_pem_file     = getenv("SSL_KEY_FILE");     /*  key file                                    */
      ssl_config.ssl_rand_file        = getenv("SSL_RAND_FILE");    /*  rand file (if RAND_status() not ok)         */
      ssl_config.ssl_crl_file         = getenv("SSL_CRL_FILE");     /*  revocation list file                        */
      ssl_config.ssl_reconnect_file   = NULL;                       /*  file for reconnect data    (not used)       */
      ssl_config.ssl_refresh_time     = 0;                          /*  key alive time for connections (not used)   */
      ssl_config.ssl_password         = NULL;                       /*  password for encrypted keyfiles (not used)  */
      ssl_config.ssl_verify_func      = my_ssl_verify_func;         /*  function callback for peer user/name check  */

      if (ssl_config.ssl_CA_cert_pem_file == NULL ||
          ssl_config.ssl_cert_pem_file    == NULL ||
          ssl_config.ssl_key_pem_file     == NULL ||
          ssl_config.ssl_rand_file        == NULL) {
         printf("please set the following environment variables:\n");
         printf("SSL_CA_CERT_FILE         = CA certificate file\n");
         printf("SSL_CERT_FILE            = certificates file\n");
         printf("SSL_KEY_FILE             = key file\n");
         printf("(optional) SSL_RAND_FILE = rand file (if RAND_status() not ok)\n");
      }
   }

   /* setup signalhandling */
   setup_signal_handler();
 
   /* setup commlib */
   error = cl_com_setup_commlib(CL_RW_THREAD, CL_LOG_OFF, NULL);
 
   if(error != CL_RETVAL_OK){
      fprintf(stderr, "cl_com_setup_commlib failed with: %s\n", cl_get_error_text(error));
   }
   /* setup commlib error function callback */
   cl_com_set_error_func(on_communication_error);
 

   if (framework == CL_CT_SSL) {
      cl_com_specify_ssl_configuration(&ssl_config);
   }

   /* create communication handle */
   handle=cl_com_create_handle(&error,
                               framework,
                               CL_CM_CT_MESSAGE,
                               false,
                               handle_port,
                               CL_TCP_DEFAULT,
                               "client", 0,
                               5, 0 );
   if (handle == NULL) {
      fprintf(stderr, "could not create communication handle with error: %s\n", cl_get_error_text(error));
      cl_com_cleanup_commlib();
      exit(1);
   }
 
   /* print out some info output */
   printf("client running:\n");
   printf("host: \"%s\"\n",  handle->local->comp_host);
   printf("name: \"%s\"\n",  handle->local->comp_name);
   printf("id:   \"%ld\"\n", handle->local->comp_id);
    
   ret_val = cl_com_external_fd_register(handle, fd_in, fd_in_cb, CL_R_SELECT, fd_in_data);
   if (ret_val != CL_RETVAL_OK) {
      printf("could not register stdin-fd: %s\n", cl_get_error_text(ret_val));
      do_shutdown = 1;
   }else{
      printf("stdin-fd was registered\n");
   }

   ret_val = cl_com_external_fd_register(handle, fd_out, fd_out_cb, CL_W_SELECT, fd_out_data);
   if (ret_val != CL_RETVAL_OK) {
      printf("could not register stdout-fd: %s\n", cl_get_error_text(ret_val));
      do_shutdown = 1;
   }else{
      printf("stdout-fd was registered\n");
   }

   /* register at the server */
   ret_val = cl_commlib_send_message(handle,
                                     server_host, "server", 1, 
                                     CL_MIH_MAT_NAK, 
                                     (cl_byte_t**)&reg, strlen(reg)+1,
                                     NULL, 0, 0,
                                     true, false);
                                     
   if (ret_val != CL_RETVAL_OK) {
      do_shutdown = 1;
   }
   /* application main loop */
   while ( do_shutdown == 0 ) {
      CL_LOG(CL_LOG_WARNING, "App: start looping");
      cl_commlib_trigger(handle, 1);
      CL_LOG(CL_LOG_WARNING, "App: trigger was done");
      ret_val = cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);

      if (message != NULL) {
         CL_LOG(CL_LOG_WARNING, "App: std_out received");
         pthread_mutex_lock(&data_mutex);
         CL_LOG(CL_LOG_WARNING, "App: std_out got mutex");
         strcat(fd_out_data, (const char*)message->message);
         pthread_mutex_unlock(&data_mutex);
         CL_LOG(CL_LOG_WARNING, "App: std_out released mutex");
         cl_com_external_fd_set_write_ready(handle, fd_out);
         CL_LOG(CL_LOG_WARNING, "App: std_out set write ready flag");

         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
      }
      pthread_mutex_lock(&data_in_mutex);
      if(ready == 1) {
         cl_byte_t* bp = NULL; 
         CL_LOG(CL_LOG_WARNING, "App: std_in send");
         bp = (cl_byte_t*) fd_in_data;
         cl_commlib_send_message(handle,
                              server_host, "server", 1,
                              CL_MIH_MAT_NAK,
                              &bp, strlen(fd_in_data)+1,
                              NULL, 0, 0,
                              true, false);
         ready = 0;
         fd_in_data[0] = '\0';
      }
      pthread_mutex_unlock(&data_in_mutex);
   }
 
  
   printf("shutting down ...\n");
   cl_com_external_fd_unregister(handle, fd_in);
   cl_com_external_fd_unregister(handle, fd_out);
 
   /* here the application goes down - shutdown communication lib */
   while ( cl_commlib_shutdown_handle(handle, true) == CL_RETVAL_MESSAGE_IN_BUFFER) {
      message = NULL;
      cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);
 
      if (message != NULL) {
         printf("ignoring message from \"%s\"\n", sender->comp_host); 
         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
         message = NULL;
      }
   }

   /* cleanup commlib */
   cl_com_cleanup_commlib();
   
   return 0;
 }
extern int main(int argc, char** argv)
{
  struct sigaction sa;
  struct timeval now;
  time_t last_time = 0;
  time_t shutdown_time = 0;
  int i,first_message_sent = 0;
  int no_output = 0;
  cl_byte_t *reference = NULL;

  prof_mt_init();

  if (argc < 4) {
      printf("syntax: debug_level vmaster_port vmaster_host [no_output]\n");
      exit(1);
  }

  if (argc >= 5) {
     if (strcmp(argv[4],"no_output") == 0) {
        printf("virtual event client: no_output option set\n");
        no_output = 1;
     }
  }


  /* setup signalhandling */
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = sighandler_client;  /* one handler for all signals */
  sigemptyset(&sa.sa_mask);
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGTERM, &sa, NULL);
  sigaction(SIGHUP, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);

  gettimeofday(&now,NULL);
  shutdown_time = now.tv_sec + SGE_TEST_VIRTUAL_CLIENT_SHUTDOWN_TIMEOUT;




  printf("startup commlib ...\n");
  cl_com_setup_commlib(CL_NO_THREAD , (cl_log_t)atoi(argv[1]), NULL);

  printf("setting up handle for connect port %d\n", atoi(argv[2]) );
  handle=cl_com_create_handle(NULL,CL_CT_TCP,CL_CM_CT_MESSAGE , CL_FALSE, atoi(argv[2]) , CL_TCP_DEFAULT,"virtual_event_client", 0, 1,0 );
  if (handle == NULL) {
     printf("could not get handle\n");
     exit(1);
  }

  printf("local hostname is \"%s\"\n", handle->local->comp_host);
  printf("local component is \"%s\"\n", handle->local->comp_name);
  printf("local component id is \"%ld\"\n", handle->local->comp_id);

  cl_com_get_connect_port(handle, &i);
  printf("connecting to port \"%d\" on host \"%s\"\n", i, argv[3]);

  printf("virtual event client is running ...\n");
  while(do_shutdown == 0) {
     int                retval  = 0;
     cl_com_message_t*  message = NULL;
     cl_com_endpoint_t* sender  = NULL;

     gettimeofday(&now,NULL);
     if (now.tv_sec != last_time && !no_output) {
        printf("virtual event client["pid_t_fmt"] message count[sent |%d|] events[received |%d|]...\n",
           getpid(), snd_messages, events_received);
        last_time = now.tv_sec;
     }

     if (now.tv_sec > shutdown_time ) {
        printf("shutting down test - timeout\n");
        do_shutdown = 1;
     }

     retval = cl_commlib_receive_message(handle, NULL, NULL, 0,      /* handle, comp_host, comp_name , comp_id, */
                                         CL_TRUE, 0,                 /* syncron, response_mid */
                                         &message, &sender );
     if ( retval != CL_RETVAL_OK) {
        if ( retval == CL_RETVAL_CONNECTION_NOT_FOUND ) {

#if 0
            printf("opening connection to %s/%s/%d\n", argv[3], "virtual_master", 1);
#endif


            /* shutdown when virtual qmaster is not running anymore */
            if (events_received > 0) {
               do_shutdown = 1;
               printf("lost connection to  %s/%s/%d\n", argv[3], "virtual_master", 1);
            }
            retval = cl_commlib_open_connection(handle, argv[3], "virtual_master", 1);
            if (retval == CL_RETVAL_OK) {
               first_message_sent = 0; 
            } 
        }
     } else {
#if 0
        printf("received message from %s/%s/%ld: \"%s\" (%ld bytes)\n", sender->comp_host,sender->comp_name,sender->comp_id, message->message, message->message_length);
#endif
        gettimeofday(&now,NULL);
        shutdown_time = now.tv_sec + SGE_TEST_VIRTUAL_CLIENT_SHUTDOWN_TIMEOUT;
        events_received++;
        cl_com_free_message(&message);
        cl_com_free_endpoint(&sender);
     }
#if 0
     printf("status: %s\n",cl_get_error_text(retval));
#endif

     if (  first_message_sent == 0) {
        char data[6] = "event";
        reference = (cl_byte_t *)data;
        first_message_sent = 1;
        retval = cl_commlib_send_message(handle, argv[3], "virtual_master", 1,
                                         CL_MIH_MAT_ACK,
                                         &reference, 6,
                                         NULL, 0, 0 , CL_TRUE, CL_TRUE );
        if (retval == CL_RETVAL_OK) {
           snd_messages++;
        }
#if 0
        printf("sending a event hello message: %s\n",cl_get_error_text(retval) );
#endif
     }
  }

  printf("shutdown commlib ...\n");
  cl_com_cleanup_commlib();

  printf("main done\n");
  return 0;
}
extern int main(int argc, char** argv)
{
  struct sigaction sa;


  cl_com_handle_t* handle = NULL; 
  cl_com_message_t* message = NULL;
  cl_com_endpoint_t* sender = NULL;
#if 0
  cl_com_endpoint_t* clients[10] = { NULL, NULL, NULL, NULL, NULL,
                                     NULL, NULL, NULL, NULL, NULL };
#endif
  int i;
  unsigned long max_connections;
  
  if (argc != 4) {
      printf("please enter  debug level, port and nr. of max connections\n");
      exit(1);
  }

  /* setup signalhandling */
  memset(&sa, 0, sizeof(sa));
  sa.sa_handler = sighandler_server;  /* one handler for all signals */
  sigemptyset(&sa.sa_mask);
  sigaction(SIGINT, &sa, NULL);
  sigaction(SIGTERM, &sa, NULL);
  sigaction(SIGHUP, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);


  printf("commlib setup ...\n");
  cl_com_setup_commlib(CL_RW_THREAD, (cl_log_t)atoi(argv[1]), NULL);

  printf("setting up service on port %d\n", atoi(argv[2]) );
  handle=cl_com_create_handle(NULL,CL_CT_TCP,CL_CM_CT_MESSAGE , CL_TRUE, atoi(argv[2]) , CL_TCP_DEFAULT,"server", 1, 2, 0 );
  if (handle == NULL) {
     printf("could not get handle\n");
     exit(-1);
  }

  cl_com_get_service_port(handle,&i), 
  printf("server running on host \"%s\", port %d, component name is \"%s\", id is %ld\n", 
         handle->local->comp_host, 
         i, 
         handle->local->comp_name,  
         handle->local->comp_id);

  cl_com_set_max_connections(handle,atoi(argv[3]));
  cl_com_get_max_connections(handle,&max_connections);
  printf("max open connections is set to %lu\n", max_connections);

  printf("enable max connection close\n");
  cl_com_set_max_connection_close_mode(handle, CL_ON_MAX_COUNT_CLOSE_AUTOCLOSE_CLIENTS);

  while(do_shutdown != 1) {
     unsigned long mid;
     int ret_val;
     struct timeval now;
     

     CL_LOG(CL_LOG_INFO,"main()");

     gettimeofday(&now,NULL);
     cl_commlib_trigger(handle, 1);
     ret_val = cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);
     if (message != NULL ) {
        ret_val = cl_commlib_send_message(handle, 
                                sender->comp_host, 
                                sender->comp_name, 
                                sender->comp_id, CL_MIH_MAT_NAK,  
                                &message->message, 
                                message->message_length, 
                                &mid, message->message_id,0, 
                                CL_FALSE, CL_FALSE);
        if (ret_val != CL_RETVAL_OK) {
/*
           printf("cl_commlib_send_message() returned: %s\n",cl_get_error_text(ret_val));
*/
        } 
        

/*        printf("received message from \"%s\": size of message: %ld\n", sender->comp_host, message->message_length); */

        cl_com_free_message(&message);
        cl_com_free_endpoint(&sender);
        message = NULL;
     } 
  }


  cl_com_ignore_timeouts(CL_TRUE); 
  cl_com_get_ignore_timeouts_flag();

  printf("shutting down server ...\n");
  handle = cl_com_get_handle( "server", 1 );
  if (handle == NULL) {
     printf("could not find handle\n");
     exit(1);
  } else {
     printf("found handle\n");
  }

  while ( cl_commlib_shutdown_handle(handle, CL_TRUE) == CL_RETVAL_MESSAGE_IN_BUFFER) {
     message = NULL;
     cl_commlib_receive_message(handle, NULL, NULL, 0, CL_FALSE, 0, &message, &sender);

     if (message != NULL) {
        printf("ignoring message from \"%s\": size of message: %ld\n", sender->comp_host, message->message_length); 
        cl_com_free_message(&message);
        cl_com_free_endpoint(&sender);
        message = NULL;
     } else {
        break;
     }
  }

  printf("commlib cleanup ...\n");
  cl_com_cleanup_commlib();
  
  printf("main done\n");
  return 0;
}
Пример #7
0
/*
 *  main()
 */
extern int main(int argc, char** argv) {

   int                handle_port = 0;
   cl_com_handle_t*   handle      = NULL; 
   cl_com_message_t*  message     = NULL;
   cl_com_endpoint_t* sender      = NULL;
   int                i           = 0;
 
 
   /* check command line argument count */
   if (argc != 2) {
      usage();
      exit(1);
   }
 
   /* get server port from command line */
   handle_port = atoi(argv[1]);
   if (handle_port <= 0) {
      fprintf(stderr,"need a port number > 0\n");
      usage();
      exit(1);
   }
 
 
   /* setup signalhandling */
   setup_signal_handler();
 
   /* setup commlib */
   cl_com_setup_commlib(CL_RW_THREAD, CL_LOG_WARNING, on_communication_log);
 
   /* setup commlib error function callback */
   cl_com_set_error_func(on_communication_error);
 
   /* create communication handle */
   handle=cl_com_create_handle(NULL, 
                               CL_CT_TCP, 
                               CL_CM_CT_MESSAGE,
                               true,
                               handle_port,
                               CL_TCP_DEFAULT,
                               "server", 1,
                               1, 0 );
 
   if (handle == NULL) {
      fprintf(stderr, "could not create communication handle\n");
      cl_com_cleanup_commlib();
      exit(1);
   }
 
   /* print out some info output */
   printf("server running:\n");
   printf("host: \"%s\"\n", handle->local->comp_host);
   cl_com_get_service_port(handle,&i);
   printf("port: %d\n",     i);
   printf("name: \"%s\"\n", handle->local->comp_name);
   printf("id:   %ld\n",    handle->local->comp_id);
     
   /* application main loop */
   while(do_shutdown != 1) {
 
      cl_commlib_trigger(handle, 1);
 
      cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);
 
      if (message != NULL) {
         char response_message[1024];
         sprintf(response_message,"Welcome to demo service at host \"%s\"!", handle->local->comp_host);
         printf("received message from \"%s/%s/%ld\": %s\n", sender->comp_host, sender->comp_name, sender->comp_id, message->message);
 
         cl_commlib_send_message(handle, 
                                 sender->comp_host, 
                                 sender->comp_name, 
                                 sender->comp_id, CL_MIH_MAT_NAK,  
                                 (cl_byte_t**)&response_message, 
                                 strlen(response_message)+1, 
                                 NULL, 0, 0, 
                                 true,false);
         message->message = NULL;
         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
         message = NULL;
      }
   }

   printf("shutting down ...\n");

   /* here the application goes down - shutdown communication lib */
   while ( cl_commlib_shutdown_handle(handle, true) == CL_RETVAL_MESSAGE_IN_BUFFER) {
      message = NULL;
      cl_commlib_receive_message(handle,NULL, NULL, 0, false, 0, &message, &sender);
 
      if (message != NULL) {
         printf("ignoring message from \"%s\"\n", sender->comp_host); 
         cl_com_free_message(&message);
         cl_com_free_endpoint(&sender);
         message = NULL;
      }
   }
 
   /* cleanup commlib */
   cl_com_cleanup_commlib();

  return 0;
}
extern int main(int argc, char** argv)
{
    struct sigaction sa;
    static int runs = 100;


    cl_com_handle_t* handle = NULL;
    cl_com_message_t* message = NULL;
    cl_com_endpoint_t* sender = NULL;

    int bytes_received = 0;
    unsigned long total_bytes_sent = 0;
    unsigned long total_bytes_received = 0;
    unsigned long total_connections = 0;
    unsigned long total_connection_sum = 0;
#if 1
    char* welcome_text = "Welcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\nWelcome to the tcp framework module!Welcome to the tcp framework module!Welcome to the tcp framework module!\n";
#else
    char* welcome_text = "This message is from thread 1";
#endif

    struct timeval now;
    long start_time;
    long appl_start_time;
    long end_time;
    int retval = CL_RETVAL_PARAMS;
    int welcome_text_size;
    int close_connection = 0;
    unsigned long last_mid = 0;
    int i;
    cl_framework_t framework = CL_CT_TCP;
    cl_xml_connection_autoclose_t autoclose;

    /* setup signalhandling */
    memset(&sa, 0, sizeof(sa));
    sa.sa_handler = sighandler_client;  /* one handler for all signals */
    sigemptyset(&sa.sa_mask);
    sigaction(SIGINT, &sa, NULL);
    sigaction(SIGTERM, &sa, NULL);
    sigaction(SIGHUP, &sa, NULL);
    sigaction(SIGPIPE, &sa, NULL);


    if (argc < 7) {
        printf("wrong parameters, param1 = server host, param2 = port number, param3 = client id, param4=debug_level, param5=sleep time, param6=do_close, [param7=framework(TCP/SSL)]\n");
        exit(1);
    }
    prof_mt_init();
    cl_com_setup_commlib(CL_NO_THREAD, (cl_log_t)atoi(argv[4]), NULL);
    if (atoi(argv[6]) != 0) {
        close_connection = 1;
    }
    if (argv[7]) {
        framework = CL_CT_UNDEFINED;
        if (strcmp(argv[7], "TCP") == 0) {
            framework=CL_CT_TCP;
            printf("using TCP framework\n");
        }
        if (strcmp(argv[7], "SSL") == 0) {
            framework=CL_CT_SSL;
            printf("using SSL framework\n");
        }
        if (framework == CL_CT_UNDEFINED) {
            printf("unexpected framework type\n");
            exit(1);
        }
    }

    cl_com_set_alias_file("./alias_file");
    if ( framework == CL_CT_SSL) {
        cl_ssl_setup_t ssl_config;
        ssl_config.ssl_method           = CL_SSL_v23;                 /*  v23 method                                  */
        ssl_config.ssl_CA_cert_pem_file = getenv("SSL_CA_CERT_FILE"); /*  CA certificate file                         */
        ssl_config.ssl_CA_key_pem_file  = NULL;                       /*  private certificate file of CA (not used)   */
        ssl_config.ssl_cert_pem_file    = getenv("SSL_CERT_FILE");    /*  certificates file                           */
        ssl_config.ssl_key_pem_file     = getenv("SSL_KEY_FILE");     /*  key file                                    */
        ssl_config.ssl_rand_file        = getenv("SSL_RAND_FILE");    /*  rand file (if RAND_status() not ok)         */
        ssl_config.ssl_crl_file         = getenv("SSL_CRL_FILE");     /*  revocation list file                        */
        ssl_config.ssl_reconnect_file   = NULL;                       /*  file for reconnect data    (not used)       */
        ssl_config.ssl_refresh_time     = 0;                          /*  key alive time for connections (not used)   */
        ssl_config.ssl_password         = NULL;                       /*  password for encrypted keyfiles (not used)  */
        ssl_config.ssl_verify_func      = NULL;                       /*  function callback for peer user/name check  */

        if (ssl_config.ssl_CA_cert_pem_file == NULL ||
                ssl_config.ssl_cert_pem_file    == NULL ||
                ssl_config.ssl_key_pem_file     == NULL ||
                ssl_config.ssl_rand_file        == NULL) {
            printf("please set the following environment variables:\n");
            printf("SSL_CA_CERT_FILE         = CA certificate file\n");
            printf("SSL_CERT_FILE            = certificates file\n");
            printf("SSL_KEY_FILE             = key file\n");
            printf("(optional) SSL_RAND_FILE = rand file (if RAND_status() not ok)\n");
        }
        cl_com_specify_ssl_configuration(&ssl_config);
    }
    CL_LOG_STR(CL_LOG_INFO,"connection to server on host", argv[1]);
    CL_LOG_INT(CL_LOG_INFO,"using port",atoi(argv[2]));

    CL_LOG_STR(CL_LOG_INFO,"component is","client");
    CL_LOG_INT(CL_LOG_INFO,"id ist",atoi(argv[3]));
#define SELECT_TIMEOUT 1
#if 0
#define CREATE_SERVICE
#endif

#ifdef CREATE_SERVICE
    cl_com_append_known_endpoint_from_name(argv[1], "server", 1,atoi(argv[2]),CL_CM_AC_DISABLED , 1);
    handle=cl_com_create_handle(NULL,framework,CL_CM_CT_MESSAGE , CL_TRUE, 0 , CL_TCP_DEFAULT, "client", atoi(argv[3]),SELECT_TIMEOUT,0 );
    if (handle == NULL) {
        printf("could not get handle\n");
        exit(1);
    } else {
        int my_port;
        cl_com_get_service_port(handle,&my_port);
        printf("I'm reachable at port %d!\n", my_port);
    }
#else
    handle=cl_com_create_handle(NULL,framework,CL_CM_CT_MESSAGE , CL_FALSE, atoi(argv[2]) , CL_TCP_DEFAULT,"client", atoi(argv[3]),SELECT_TIMEOUT,0 );
    if (handle == NULL) {
        printf("could not get handle\n");
        exit(1);
    }
#endif

    cl_com_set_auto_close_mode(handle,CL_CM_AC_ENABLED );
    cl_com_get_auto_close_mode(handle,&autoclose);
    if (autoclose != CL_CM_AC_ENABLED ) {
        printf("could not enable autoclose\n");
        exit(1);
    }

    printf("local hostname is \"%s\"\n", handle->local->comp_host);
    printf("local component is \"%s\"\n", handle->local->comp_name);
    printf("local component id is \"%ld\"\n", handle->local->comp_id);

#ifdef CREATE_SERVICE
    cl_com_get_known_endpoint_port_from_name(argv[1], "server", 1, &i);
    printf("connecting to port \"%d\" on host \"%s\"\n", i, argv[1]);
#else
    cl_com_get_connect_port(handle, &i);
    printf("connecting to port \"%d\" on host \"%s\"\n", i, argv[1]);
#endif


    gettimeofday(&now,NULL);
    start_time =    now.tv_sec;
    appl_start_time = start_time;
    welcome_text_size = strlen(welcome_text) + 1;

    if (getenv("CL_RUNS")) {
        runs = atoi(getenv("CL_RUNS"));
    } else {
        runs = -1;  /* disable runs shutdown */
    }
    while(do_shutdown != 1) {
        unsigned long mid = 0;
        int my_sent_error = 0;

        CL_LOG(CL_LOG_INFO,"main loop");
        if (runs > 0) {
            runs--;
        }

        /* printf("sending to \"%s\" ...\n", argv[1]);  */

        /*     CL_LOG(CL_LOG_ERROR,"sending ack message ..."); */

        my_sent_error = cl_commlib_send_message(handle, argv[1], "server", 1, CL_MIH_MAT_ACK, (cl_byte_t**)&welcome_text , welcome_text_size, &mid ,0,0, CL_TRUE, CL_FALSE);
        if ( retval == CL_RETVAL_CONNECTION_NOT_FOUND ) {
            CL_LOG(CL_LOG_ERROR,"after new connection");
        }

        if (last_mid >= mid || mid == 1) {
            total_connections++;
            total_connection_sum++;
        }
        last_mid = mid;

#if 1
        if (my_sent_error != CL_RETVAL_OK) {
            printf("cl_commlib_send_message() returned %s\n", cl_get_error_text(my_sent_error));
#ifdef CREATE_SERVICE
            cl_com_get_known_endpoint_port_from_name(argv[1], "server", 1, &i);
            printf("connecting to port \"%d\" on host \"%s\"\n", i, argv[1]);
#else
            cl_com_get_connect_port(handle, &i);
            printf("connecting to port \"%d\" on host \"%s\"\n", i, argv[1]);
#endif

            /* exit(1); */
#if CL_DO_SLOW
            sleep(atoi(argv[5]));
#endif
            cl_commlib_trigger(handle,1);
            continue;
        }
#endif
#if 1
        retval = CL_RETVAL_PARAMS;
        while (retval != CL_RETVAL_OK ) {


            while ( (retval=cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender)) == CL_RETVAL_OK) {
                if (message != NULL) {
                    cl_com_free_endpoint(&sender);
                    cl_com_free_message(&message);
                } else {
                    break;
                }
            }

            if ( retval == CL_RETVAL_CONNECTION_NOT_FOUND ) {
                CL_LOG(CL_LOG_ERROR,"connection not found (1)");
                break;
            }

            retval = cl_commlib_check_for_ack(handle, argv[1], "server", 1, mid, CL_TRUE );
            if (retval != CL_RETVAL_MESSAGE_WAIT_FOR_ACK && retval != CL_RETVAL_OK) {
                printf("retval of cl_commlib_check_for_ack(%ld) is %s\n",mid,cl_get_error_text(retval));
                /* exit(1);  */
                break;
            }
            if (retval == CL_RETVAL_OK) {
                CL_LOG_INT(CL_LOG_INFO,"received ack for message mid", (int)mid);
            } else {
                cl_commlib_trigger(handle, 1);
            }

            if ( retval == CL_RETVAL_CONNECTION_NOT_FOUND ) {
                CL_LOG(CL_LOG_ERROR,"connection not found (2)");
                break;
            }



            /*        printf("retval of cl_commlib_check_for_ack(%ld) is %s\n",mid,cl_get_error_text(retval));  */
        }
        if (retval == CL_RETVAL_CONNECTION_NOT_FOUND) {
            cl_commlib_trigger(handle, 1);
            continue;
        }
#endif


        total_bytes_sent  = total_bytes_sent + welcome_text_size;
        CL_LOG_INT(CL_LOG_INFO,"bytes sent:", welcome_text_size);

        bytes_received = 0;
        while (bytes_received != welcome_text_size ) {

            cl_commlib_trigger(handle, 1);


            CL_LOG_INT(CL_LOG_INFO,"waiting for mid .... ", (int)mid);
            retval = cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, mid, &message, &sender);


            CL_LOG_STR(CL_LOG_INFO,"waiting for bytes ...", cl_get_error_text(retval));
            if (retval == CL_RETVAL_CONNECTION_NOT_FOUND) {
#if CL_DO_SLOW
                /*           CL_LOG(CL_LOG_ERROR,"connection not found"); */

                if (atoi(argv[5]) > 0) {
                    printf("sleeping...\n");
                    sleep(atoi(argv[5]));
                }
#endif
                break;
            }
            if (message != NULL) {

                /*   printf("received message from \"%s\"\n", sender->comp_host); */
                CL_LOG_INT(CL_LOG_INFO,"bytes received:", (int)message->message_length);
                if (strcmp((char*)message->message, welcome_text) != 0) {
                    printf("------------------------> message transfer error\n");
                }
                total_bytes_received = total_bytes_received + message->message_length;
                bytes_received = bytes_received + (int)message->message_length;
                cl_com_free_endpoint(&sender);
                cl_com_free_message(&message);
            }

            while ( (retval = cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender)) == CL_RETVAL_OK) {
                if (message != NULL) {
                    cl_com_free_endpoint(&sender);
                    cl_com_free_message(&message);
                } else {
                    break;
                }
            }

            if (retval == CL_RETVAL_CONNECTION_NOT_FOUND) {
                CL_LOG(CL_LOG_ERROR,"connection not found (3)");
                break;

            }


#if CL_DO_SLOW
            sleep(atoi(argv[5]));
#endif
            if (do_shutdown == 1) {
                break;
            }
        }


#if CL_DO_SLOW
        sleep(atoi(argv[5]));
#endif
        gettimeofday(&now,NULL);
        end_time =    now.tv_sec;
        if (end_time - start_time >= 2 ) {
            cl_com_connection_t* con = NULL;
            cl_connection_list_elem_t* elem = NULL;
            /*        printf("Kbit/s sent: %.2f   ", ((total_bytes_sent * 8.0)/1024.0) /  (double)(end_time - start_time));
                    printf("Kbit/s read: %.2f   ", ((total_bytes_received * 8.0)/1024.0) /  (double)(end_time - start_time)); */
            printf("KBit/s     : %.2f   ", (((total_bytes_received + total_bytes_sent) * 8.0)/1024.0) /  (double)(end_time - start_time));
            printf("connections/s: %.2f ", (double) total_connections / (double)(end_time - start_time));
            printf("avg. connections/s: %.2f", (double) total_connection_sum / (double)(end_time - appl_start_time));
            cl_raw_list_lock(handle->connection_list);
            elem = cl_connection_list_get_first_elem(handle->connection_list);
            if (elem != NULL) {
                con = elem->connection;
                if (elem->connection->local) {
                    printf("[for comp host, comp name, comp id: \"%s\", \"%s\", \"%ld\"]    \n",con->local->comp_host, con->local->comp_name , con->local->comp_id);
                }
            } else {
                printf("     \n");
            }
            cl_raw_list_unlock(handle->connection_list);


            start_time =    now.tv_sec;
            total_bytes_sent = 0;
            total_bytes_received = 0;
            total_connections = 0;
            fflush(stdout);
        }
        if (close_connection != 0) {

            while (cl_commlib_shutdown_handle(handle, CL_TRUE) == CL_RETVAL_MESSAGE_IN_BUFFER) {
                printf("got message\n");
                message = NULL;
                cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);
                if (message != NULL) {
                    cl_com_free_endpoint(&sender);
                    cl_com_free_message(&message);
                } else {
                    printf("error shutdown handle");
                    exit(-1);
                    break;
                }
            }
#ifdef CREATE_SERVICE
            handle=cl_com_create_handle(NULL,framework,CL_CM_CT_MESSAGE , CL_TRUE, 0 , CL_TCP_DEFAULT, "client", atoi(argv[3]),SELECT_TIMEOUT,0 );
            if (handle == NULL) {
                printf("could not get handle\n");
                exit(-1);
            } else {
                int my_port;
                cl_com_get_service_port(handle,&my_port);
                printf("I'm reachable at port %d!\n", my_port);
            }
#else
            handle=cl_com_create_handle(NULL,framework,CL_CM_CT_MESSAGE , CL_FALSE, atoi(argv[2]) , CL_TCP_DEFAULT, "client", atoi(argv[3]), SELECT_TIMEOUT,0 );
            if (handle == NULL) {
                printf("could not get handle\n");
                exit(-1);
            }
#endif
            cl_com_set_auto_close_mode(handle,CL_CM_AC_ENABLED );
            cl_com_get_auto_close_mode(handle,&autoclose);
            if (autoclose != CL_CM_AC_ENABLED ) {
                printf("could not enable autoclose\n");
                exit(1);
            }
        }
        if (runs == 0) {
            do_shutdown = 1;
        }
    }
    printf("do_shutdown received\n");
    fflush(stdout);
    while (cl_commlib_shutdown_handle(handle, CL_TRUE) == CL_RETVAL_MESSAGE_IN_BUFFER) {
        printf("got message\n");
        message = NULL;

        cl_commlib_receive_message(handle,NULL, NULL, 0, CL_FALSE, 0, &message, &sender);
        if (message != NULL) {
            cl_com_free_endpoint(&sender);
            cl_com_free_message(&message);
        } else {
            break;
        }
    }
    printf("cleanup commlib ...\n");
    cl_com_cleanup_commlib();

    printf("main done\n");
    fflush(stdout);
    return 0;
}