Exemple #1
0
static EpollWatch* _epollwatch_new(Epoll* epoll, Descriptor* descriptor, struct epoll_event* event) {
	EpollWatch* watch = g_new0(EpollWatch, 1);
	MAGIC_INIT(watch);
	g_assert(event);

	/* ref it for the EpollWatch, which also covers the listener reference
	 * (which is freed below in _epollwatch_free) */
	descriptor_ref(descriptor);

	watch->listener = listener_new((CallbackFunc)epoll_descriptorStatusChanged, epoll, descriptor);
	watch->descriptor = descriptor;
	watch->event = *event;

	descriptor_addStatusListener(watch->descriptor, watch->listener);

	return watch;
}
guint
mateconf_listeners_add     (MateConfListeners* listeners,
                         const gchar* listen_point,
                         gpointer listener_data,
                         GFreeFunc destroy_notify)
{
  LTable* lt = (LTable*)listeners;
  Listener* l;

  l = listener_new(ltable_next_cnxn(lt),
                   listener_data,
                   destroy_notify);
  
  ltable_insert(lt, listen_point, l);

  return l->cnxn;
}
Exemple #3
0
int __server_bind_port(int port)
{
    loop_t *loop = NULL;
    listener_t *listener = NULL;
#if 0    
    loop_t *loop = __get_main_loop(); //@{get_best_loop}
#else
    sched_t* sched = __get_main_sched();
    if (!sched)
    {
        goto _error;
    }

    loop = get_best_loop2(sched);
    if (!loop)
    {//@{not need free loop, because loop is own to sched}
        goto _error;
    }
    LOG_I("get best loop:%p", loop);
#endif
    
    listener = listener_new(port, NULL);
    if (!listener)
    {
        goto _error;
    }
    
    SET_OWNER(listener, (void*)loop);       // set listen's loop
    LOOP_ADD_WEIGHT(loop);

    listener->stat = STAT_ACCEPT;
    
    if(gettid() == loop->set_tid) 
    {
        fd_set_add_node3(loop->_set, (fd_node_t *)listener, FD_R_FLAGS, listener->timeout);
    }
    else
    {

#ifdef WIN32
        EnterCriticalSection(&loop->set_mutex);
#else
		pthread_mutex_lock(&loop->set_mutex);
#endif
        
        fd_set_add_node3(loop->_set, (fd_node_t *)listener, FD_R_FLAGS, listener->timeout);
#ifdef WIN32
		LeaveCriticalSection(&loop->set_mutex);	
#else
        pthread_mutex_unlock(&loop->set_mutex);
#endif
    }
    
    return 0;
    
_error:
    if (sched)
    {   printf("sched:%p g_sched: %p\n", sched, __get_main_sched());
        sched_del(sched);
        printf("g_sched: %p\n", __get_main_sched());
    }
    
    if(listener)
    {
        listener_del(listener);
    }
 
    return -1;
}
/**
 * Test program looping and reading LLDP/CDP packets and exercising most of the packet
 * send/receive mechanism and a good bit of nanoprobe and CMA basic infrastructure.
 *
 * It plays both sides of the game - the CMA and the nanoprobe.
 *
 * It leaves most of the work of starting up the nanoprobe code to nano_start_full()
 */
int
main(int argc, char **argv)
{
	const guint8	loopback[] = CONST_IPV6_LOOPBACK;
	//const guint8	mcastaddrstring[] = CONST_ASSIM_DEFAULT_V4_MCAST;
	//NetAddr*	mcastaddr;
	const guint8	otheradstring[] = {127,0,0,1};
	const guint8	otheradstring2[] = {10,10,10,4};
	const guint8	anyadstring[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
	guint16		testport = TESTPORT;
	SignFrame*	signature = signframe_glib_new(G_CHECKSUM_SHA256, 0);
	Listener*	otherlistener;
	ConfigContext*	config = configcontext_new(0);
	PacketDecoder*	decoder = nano_packet_decoder();
	AuthListener*	listentonanoprobes;
	ReliableUDP*	rtransport;

#if 0
#	ifdef HAVE_MCHECK_PEDANTIC
	g_assert(mcheck_pedantic(NULL) == 0);
#	else
#		ifdef HAVE_MCHECK
	g_assert(mcheck(NULL) == 0);
#		endif
#	endif
#endif
	g_setenv("G_MESSAGES_DEBUG", "all", TRUE);
#if 0
	proj_class_incr_debug(NULL);
	proj_class_incr_debug(NULL);
	proj_class_incr_debug(NULL);
	proj_class_incr_debug(NULL);
#endif
	g_log_set_fatal_mask(NULL, G_LOG_LEVEL_ERROR);

	if (argc > 1) {
		maxpkts = atol(argv[1]);
                g_debug("Max packet count is "FMT_64BIT"d", maxpkts);
	}

	if (netio_is_dual_ipv4v6_stack()) {
		g_message("Our OS supports dual ipv4/v6 sockets. Hurray!");
	}else{
		g_warning("Our OS DOES NOT support dual ipv4/v6 sockets - this may not work!!");
	}
	

	config->setframe(config, CONFIGNAME_OUTSIG, &signature->baseclass);

	// Create a network transport object for normal UDP packets
	rtransport = reliableudp_new(0, config, decoder, 0);
	rtransport->_protocol->window_size = 8;
	nettransport = &(rtransport->baseclass.baseclass);
	g_return_val_if_fail(NULL != nettransport, 2);

	// Set up the parameters the 'CMA' is going to send to our 'nanoprobe'
	// in response to their request for configuration data.
	nanoconfig = configcontext_new(0);
	nanoconfig->setint(nanoconfig, CONFIGNAME_INTERVAL, 1);
	nanoconfig->setint(nanoconfig, CONFIGNAME_TIMEOUT, 3);
	nanoconfig->setint(nanoconfig, CONFIGNAME_CMAPORT, testport);


	// Construct the NetAddr we'll talk to (i.e., ourselves) and listen from
	destaddr =  netaddr_ipv6_new(loopback, testport);
	g_return_val_if_fail(NULL != destaddr, 3);
	config->setaddr(config, CONFIGNAME_CMAINIT, destaddr);
	nanoconfig->setaddr(nanoconfig, CONFIGNAME_CMAADDR, destaddr);
	nanoconfig->setaddr(nanoconfig, CONFIGNAME_CMAFAIL, destaddr);
	nanoconfig->setaddr(nanoconfig, CONFIGNAME_CMADISCOVER, destaddr);

	// Construct another couple of NetAddrs to talk to and listen from
	// for good measure...
	otheraddr =  netaddr_ipv4_new(otheradstring, testport);
	g_return_val_if_fail(NULL != otheraddr, 4);
	otheraddr2 =  netaddr_ipv4_new(otheradstring2, testport);
	g_return_val_if_fail(NULL != otheraddr2, 4);

	// Construct another NetAddr to bind to (anything)
	anyaddr =  netaddr_ipv6_new(anyadstring, testport);
	g_return_val_if_fail(NULL != destaddr, 5);

	// Bind to ANY address (as noted above)
	g_return_val_if_fail(nettransport->bindaddr(nettransport, anyaddr, FALSE),16);
	//g_return_val_if_fail(nettransport->bindaddr(nettransport, destaddr),16);

	g_message("NOT Joining multicast address.");
#if 0
	// We can't do this because of encryption and we will likely screw up
	// others on our network even if that weren't a problem...
	mcastaddr =  netaddr_ipv4_new(mcastaddrstring, testport);
	g_return_val_if_fail(nettransport->mcastjoin(nettransport, mcastaddr, NULL), 17);
	UNREF(mcastaddr);
	g_message("multicast join succeeded.");
#endif

	// Connect up our network transport into the g_main_loop paradigm
	// so we get dispatched when packets arrive
	netpkt = netgsource_new(nettransport, NULL, G_PRIORITY_HIGH, FALSE, NULL, 0, NULL);

	// Set up so that we can observe all unclaimed packets
	otherlistener = listener_new(config, 0);
	otherlistener->got_frameset = gotnetpkt;
	netpkt->addListener(netpkt, 0, otherlistener);
	otherlistener->associate(otherlistener,netpkt);

	// Unref the "other" listener - we hold other references to it
	UNREF(otherlistener);

	// Pretend to be the CMA...
	// Listen for packets from our nanoprobes - scattered throughout space...
	listentonanoprobes = authlistener_new(0, cmalist, config, TRUE, NULL);
	listentonanoprobes->baseclass.associate(&listentonanoprobes->baseclass, netpkt);

	nano_start_full("netconfig", 900, netpkt, config, test_cma_authentication);

	g_timeout_add_seconds(1, timeout_agent, NULL);
	mainloop = g_main_loop_new(g_main_context_default(), TRUE);

	/********************************************************************
	 *	Start up the main loop - run our test program...
	 *	(the one pretending to be both the nanoprobe and the CMA)
	 ********************************************************************/
	g_main_loop_run(mainloop);

	/********************************************************************
	 *	We exited the main loop.  Shut things down.
	 ********************************************************************/

	nano_shutdown(TRUE);	// Tell it to shutdown and print stats
	g_message("Count of 'other' pkts received:\t%d", wirepktcount);

	UNREF(nettransport);

	// Main loop is over - shut everything down, free everything...
	g_main_loop_unref(mainloop); mainloop=NULL;


	// Unlink misc dispatcher - this should NOT be necessary...
	netpkt->addListener(netpkt, 0, NULL);

	// Dissociate packet actions from the packet source.
	listentonanoprobes->baseclass.dissociate(&listentonanoprobes->baseclass);

	// Unref the AuthListener object
	UNREF2(listentonanoprobes);

	g_source_destroy(&netpkt->baseclass);
	g_source_unref(&netpkt->baseclass);
	//g_main_context_unref(g_main_context_default());

	// Free signature frame
	UNREF2(signature);

	// Free misc addresses
	UNREF(destaddr);
	UNREF(otheraddr);
	UNREF(otheraddr2);
	UNREF(anyaddr);

	// Free config object
	UNREF(config);
	UNREF(nanoconfig);


	// At this point - nothing should show up - we should have freed everything
	if (proj_class_live_object_count() > 0) {
		g_warning("Too many objects (%d) alive at end of test.", 
			proj_class_live_object_count());
		proj_class_dump_live_objects();
		++errcount;
	}else{
		g_message("No objects left alive.  Awesome!");
	}
        proj_class_finalize_sys(); // Shut down object system to make valgrind happy :-D
	return(errcount <= 127 ? errcount : 127);
}
Exemple #5
0
int main(int argc, char **argv)
{
    /* open log */
    if (0 != LOG_OPEN("./center", LOG_LEVEL_DEBUG, -1)) {
        fprintf(stderr, "open center log failed!\n");
        return 1;
    }

    if (0 != check_cmd()) {
        return 1;
    }

    /* protobuf verify version */
    GOOGLE_PROTOBUF_VERIFY_VERSION;

    struct event_base *main_base = event_base_new();
    if (NULL == main_base) {
        mfatal("main_base = event_base_new() failed!");
        return 1;
    }

    conn_init();

    /* thread */
    pthread_t worker[WORKER_NUM];
    thread_init(main_base, WORKER_NUM, worker);

    /* signal */
    struct event *signal_event;
    signal_event = evsignal_new(main_base, SIGINT, signal_cb, (void *)main_base);
    if (NULL == signal_event || 0 != event_add(signal_event, NULL)) {
        mfatal("create/add a signal event failed!");
        return 1;
    }

    /* listener for gate */
    struct sockaddr_in sa;
    bzero(&sa, sizeof(sa));
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = htonl(INADDR_ANY);
    sa.sin_port = htons(44000);

    listener *lg = listener_new(main_base, (struct sockaddr *)&sa, sizeof(sa), gate_cb);
    if (NULL == lg) {
        mfatal("create client listener failed!");
        return 1;
    }

    /* connector to center */
    struct sockaddr_in csa;
    bzero(&csa, sizeof(csa));
    csa.sin_family = AF_INET;
    csa.sin_addr.s_addr = inet_addr("127.0.0.1");
    csa.sin_port = htons(43001);

    connector *ce = connector_new((struct sockaddr *)&csa, sizeof(csa), center_cb);
    if (NULL == ce) {
        mfatal("create center connector failed!");
        return 1;
    }

    event_base_dispatch(main_base);

    for (int i = 0; i < WORKER_NUM; i++)
        pthread_join(worker[i], NULL);

    connector_free(ce);
    listener_free(lg);
    event_free(signal_event);
    event_base_free(main_base);

    /* shutdown protobuf */
    google::protobuf::ShutdownProtobufLibrary();

    /* close log */
    LOG_CLOSE();

    return 0;
}
Exemple #6
0
static int run_exec_wait(shell_t *shell, shell_argv_t *cmd_argv, char *tag)
{
  int argc = cmd_argv->argc;
  char **argv = cmd_argv->argv;
  listener_t *listener;
  char *s;
  struct timeval tv0;
  long sec, usec;
  int unblock, timeout, error;

  /* Set and check wait timeout */
  run_wait_timeout = run_timeout;
  if ( (argc > 1) && (argv[1][0] == '-') ) {
    if ( timer_value(shell, &(argv[1][1]), NULL, &run_wait_timeout) )
      return -1;
    argv++;
    argc--;
  }

  if ( run_wait_timeout == -1 ) {
    shell_error(shell, "No timeout value initialized. Please use command 'timeout' before 'wait'\n");
    return -1;
  }

  /* Setup wait conditions */
  argv++;
  argc--;
  if ( (listener = listener_new(argc, argv)) == NULL )
    return -1;

  /* Report what we are waiting for */
  s = listener_str(listener);

  if ( debug_flag ) { /* For speedup in non-debug mode */
    debug("Wait: '%s'\n", s);
  }

  result_puts(result_header_engine(tag));
  result_puts(s);
  result_puts("\n");

  free(s);

  /* Init timeout counter */
  gettimeofday(&tv0, NULL);
  sec = run_wait_timeout / 1000;
  usec = (run_wait_timeout % 1000) * 1000;

  /* Wait until an unblocking condition is met */
  unblock = 0;
  timeout = 0;
  error = 0;
  while ( ! (unblock || timeout || error) ) {
    /* Check unblocking conditions */
    if ( listener_check(listener) ) {
      unblock = 1;
    }
    else {
      struct timeval tv1, tv;

      /* Update timeout counter */
      gettimeofday(&tv1, NULL);
      tv_sub(&tv1, &tv0);
      /*fprintf(stderr, "** %ld.%06ld\n", tv1.tv_sec, tv1.tv_usec);*/

      tv.tv_sec = sec;
      tv.tv_usec = usec;
      if ( tv_sub(&tv, &tv1) ) {
        timeout = 1;
      }
      else {
        /*fprintf(stderr, "   %ld.%06ld\n", tv.tv_sec, tv.tv_usec);*/
        /* Wait for events to arrive, and process incoming data */
        switch ( run_wait_event(shell, tag, &tv) ) {
        case -1:
          error = 1;
          break;
        case 0:
          timeout = 1;
          break;
        default :
          break;
        }
      }
    }
  }

  if ( unblock ) {
    run_wait_event_unblock(shell, tag, listener);
  }
  else if ( timeout ) {
    run_wait_event_timeout(shell, tag);
  }

  /* Clear wait conditions */
  listener_destroy(listener);

  return error ? -1 : 0;
}