Пример #1
0
int main (int argc, char **argv) {
    GMainLoop  *loop;
    GIOChannel *sock;

    /* read nocat.conf */
    read_conf_file( NC_CONF_PATH "/nocat.conf" );

    if (argc < 2 || strncmp(argv[1], "-D", 2) != 0)
        daemonize();

    /* initalize the log */
    initialize_log();

    /* set network parameters */
    set_network_defaults( nocat_conf );

    /* initialize the gateway type driver */
    initialize_driver();

    /* initialize the firewall */
    fw_init( nocat_conf );

    /* initialize the peer table */
    peer_tab = g_hash_new();

    /* initialize the listen socket */
    sock = http_bind_socket(
               CONF("GatewayAddr"), CONFd("GatewayPort"), CONFd("ListenQueue") );

    /* initialize the main loop and handlers */
    loop = g_main_new(FALSE);
    g_io_add_watch( sock, G_IO_IN,  (GIOFunc) handle_accept, NULL );
    g_timeout_add( 30000, (GSourceFunc) check_peers, NULL );
    g_timeout_add( 1000, (GSourceFunc) check_exit_signal, loop );

    /* Go! */
    g_message("starting main loop");
    g_main_run( loop );
    g_message("exiting main loop");
    return 0;
}
static void setup_signals()
{
	struct sigaction sigact = {
		.sa_handler = terminate,
		.sa_flags = 0
	};
	sigemptyset(&sigact.sa_mask);
	sigaction(SIGTERM, &sigact, 0);
	sigaction(SIGINT, &sigact, 0);

	signal(SIGHUP, SIG_IGN);
	signal(SIGPIPE,SIG_IGN);
}

// main function
int main()
{
	if (!ensure_singleton(SINGLETON_LOCKFILE))
		return 1;

	if (initialize_log() != 0) {
		LOGE("Init log failed. uninit\n");
		terminate0();
		LOGE("Daemon terminated\n");
		exit(0);
	}

	LOGI("da_started\n");
	atexit(terminate0);


	//for terminal exit
	setup_signals();
	daemon(0, 1);
	LOGI("--- daemonized (pid %d) ---\n", getpid());

	FILE *portfile = fopen(PORTFILE, "w");
	if (!portfile) {
		LOGE("cannot create portfile");
		return 1;
	}

	int err = initializeManager(portfile);
	fclose(portfile);
	if (err)
		return 1;

	//init all file descriptors
	init_system_file_descriptors();
	//daemon work
	//FIX ME remove samplingThread it is only for debug
	//samplingThread(NULL);
	daemonLoop();
	LOGI("daemon loop finished\n");
	stop_all();
	finalizeManager();

	close_system_file_descriptors();

	//DO NOT USE THIS FUNCTION FOR RELEASE IT IS TOO SLOW
#ifdef MALLOC_DEBUG_LEVEL
	msg_swap_free_all_data(&prof_session.user_space_inst);
#endif

	LOGI("main finished\n");
	print_malloc_list(NULL, 0);
	return 0;
}
Пример #3
0
int main (int argc, char *argv[])
{
  char log_name[100];
  char * cod_func = "int i;\n\
     float total = 0;\n\
     for(i = 0; i < the_size; ++i)\n\
     {\n\
       float * temp_float_ptr;\n\
       entity_float_data_change_event * old_event;\n\
       old_event = EVdata_entity_float_data_change_event(i);\n\
       temp_float_ptr = &(old_event->float_data.data[0]);\n\
       total = total + (*temp_float_ptr);\n\ 
     }\n\
     float average;\n\
     average = total / the_size;\n\
     printf(\"The value of average is: \\%f\\n\", average);\n\
     entity_float_data_change_event new_event;\n\
     entity_float_data_change_event * old_event = EVdata_entity_float_data_change_event(0);\n\
     new_event.float_data.data_size = 1;\n\
     new_event.float_data.data[0] = average;\n\
     for(i = 0; i < 32; ++i)\n\
     {\n\
       new_event.entity_id.id[i] = old_event->entity_id.id[i];\n\
     }\n\
     EVsubmit(0, new_event);\n\0"; 


  if (argc != 3)
  {
    fprintf(stderr, "Usage: client_modify pos_unique_number [123], where unique_number is a positive or zero unique number for the group\n"
                    "And the second number is a identifier for which group the fprintf is submitting too...\n");
    exit(1);
  }
  
  proc_id = atoi(argv[1]);
  group_id = atoi(argv[2]);
  if (group_id != 1 && group_id != 2 && group_id != 3)
  {
    fprintf(stderr, "Error: the group id should be one, two or three.  It is: %d\n", group_id);
    exit(1);
  }


  strcpy(send_group, "/experimental/pool");
  strcat(send_group, argv[2]);

  strcpy(log_name, "modify_");
  strcat(log_name, argv[1]);
  strcat(log_name, argv[2]);

  if(!initialize_log(log_name))
  {
    fprintf(stderr, "Error: log not initialized, quitting!\n");
    exit(1);
  }

  printf("Program starting\n");

  srand(time(NULL));

  /* Set up sigaction for handler call */
  struct sigaction sa;
  sa.sa_flags = SA_RESTART;
  sigemptyset(&sa.sa_mask);
  sa.sa_handler = alarm_handler;
  if(sigaction(SIGALRM, &sa, NULL) == -1)
  {
    fprintf(stderr, "SIGALRM sigaction failed!\n");
    exit(1);
  }

  float temp_float = get_rand_float(); 


  char **bindings;
  int i2;
  atom_t VAL1_ATOM, VAL2_ATOM;

  ft.data = &temp_float;
  ft.data_size = 1;

  pds_host = getenv ("PDS_SERVER_HOST");

  if (!pds_host && !access(PDS_CONNECT_FILE, F_OK))
  {
      char hostname[128];
      FILE * temp_ptr = fopen(PDS_CONNECT_FILE, "r");
      fscanf(temp_ptr, "%s", hostname);
      printf("Hostname is: %s\n", hostname);
      fclose(temp_ptr);
      pds_host = strdup(hostname);
  }

  if (pds_host == NULL) 
      pds_host = getenv ("HOSTNAME");

  if (pds_host == NULL) {
      char hostname[128];
      if (gethostname(&hostname[0], sizeof(hostname)) == 0) {
	  pds_host = strdup(hostname);
      }
  }

  contact_attrs = create_attr_list();
  set_attr (contact_attrs,
            attr_atom_from_string("IP_HOST"),
            Attr_String,
            strdup (pds_host));
  set_attr (contact_attrs,
            attr_atom_from_string("IP_PORT"),
            Attr_Int4,
            (attr_value*)8848);

  /* Get the CM from the client_manager 
  cm = CManager_create();
  CMlisten(cm);*/

  if ((wps = pds_service_open (contact_attrs)) == NULL)
    {
      fprintf (stderr, "Couldn't init PDS client-side (is pdsd running?)\n");
      exit (1);
    }

  cm = pds_get_CManager();
  new_domain_id = pds_open_domain (wps, 
                                  "newDomain",
                                  "newDomaintype",
                                  1,
                                  "wp-register");


  cid1 = pds_get_root_context (new_domain_id);
  if((atoi(argv[1])) == 0)
  {
    eid1 = pds_create_entity_float (new_domain_id, send_group, null_pds_context_id, &ft, NULL);
    if(!pds_aggregate_entity(new_domain_id, cod_func, eid1, ENTITY_DATA_CHANGE_FLOAT))
    {
      fprintf(stderr, "Error: pds_aggregate_entity failed\n");
      return 1;
    }
  }

  

  if((pds_set_entity_float_data (new_domain_id, send_group, cid1, &ft, 0)) > 0 )
    printf("Successfully set the new float data, should see something in the other program.\n");
  else
    printf ("Failed to set the new float data\n");

  fflush (0);

  alarm(2);

  CMrun_network(cm);

  pds_service_close (wps);
  
  return 0;
}