示例#1
0
文件: timer.c 项目: 9thsector/ccnet
CcnetTimer*
ccnet_timer_new (TimerCB         func,
                 void           *user_data,
                 uint64_t        interval_milliseconds)
{
    CcnetTimer *timer = g_new0 (CcnetTimer, 1);

    timer->tv = timeval_from_msec (interval_milliseconds);
    timer->func = func;
    timer->user_data = user_data;

    evtimer_set (&timer->event, timer_callback, timer);
    evtimer_add (&timer->event, &timer->tv);

    return timer;
}
int main(int argc, char** argv) {

  network = network_new(GCS_HOST, GCS_PORT, DATALINK_PORT, FALSE);

  /* Initalize the event library */
  event_init();

  /* Add a timeout event */
  evtimer_set(&timeout, timeout_cb, &timeout);

  ADD_TIMEOUT();

  event_dispatch();

  return 0;
}
示例#3
0
文件: timer.cpp 项目: jb55/libcage
        void
        timer::set_timer(callback *func, timeval *t)
        {
                typedef boost::shared_ptr<event> ev_ptr;
                ev_ptr ev = ev_ptr(new event);

                // delete old event
                unset_timer(func);

                func->m_timer = this;

                // add new event
                m_events[func] = ev;
                evtimer_set(ev.get(), timer_callback, func);
                evtimer_add(ev.get(), t);
        }
示例#4
0
static int update_conn_event_sleep(struct connection *c)
{
    struct timeval t = {.tv_sec = 0, .tv_usec = 0};
    struct timeval now;
    if (event_del(&c->ev) == -1) return 0;

    c->ev_flags = 0; // clear event flags in case we ping-pong to other modes
    evtimer_set(&c->ev, sleep_handler, (void *)c);
    event_base_set(c->t->base, &c->ev);

    gettimeofday(&now, NULL);

    // every time we come into this loop, we've run once. which means we
    // always have to advance the next_sleep timer.
    if (c->next_sleep.tv_sec == 0) {
        // initialize next_sleep as late as possible to avoid spamming.
        gettimeofday(&c->next_sleep, NULL);
    }
    memcpy(&t, &c->next_sleep, sizeof(struct timeval));
    timeradd(&t, &c->tosleep, &c->next_sleep);

    timersub(&c->next_sleep, &now, &t);
    // so far as I can tell, it treats times in the past as "Wake up
    // immediately".
    evtimer_add(&c->ev, &t);

    return 1;
}

/* TODO: Be more wary of IOV_MAX */
static void drain_iovecs(struct iovec *vecs, const int iov_count, const int written) {
    int i;
    int todrain = written;
    for (i = 0; i < iov_count; i++) {
        if (vecs[i].iov_len > 0) {
            if (todrain >= vecs[i].iov_len) {
                todrain -= vecs[i].iov_len;
                vecs[i].iov_base = NULL;
                vecs[i].iov_len  = 0;
            } else {
                vecs[i].iov_len -= todrain;
                vecs[i].iov_base += todrain;
                break;
            }
        }
    }
}
示例#5
0
文件: master.c 项目: cygsxak/memlink
void
mb_conn_destroy_delay(Conn *conn)
{
    if (conn->is_destroy)
        return;
    conn->is_destroy = TRUE;
    event_del(&conn->evt);

    struct timeval tm;
    evtimer_set(&conn->evt, mb_conn_destroy, conn);
    evutil_timerclear(&tm);
    tm.tv_sec = 0;
    event_base_set(conn->base, &conn->evt);
    event_add(&conn->evt, &tm);

    return;
}
示例#6
0
int InitTimers( void )
{
	timerlist = list_create( TIMER_TABLE_SIZE);
	if( !timerlist )
	{
		nlog( LOG_CRITICAL, "Unable to create timer hash" );
		return NS_FAILURE;
	}
	evtimers = ns_malloc( sizeof( struct event ) );
	evtimer_set( evtimers, CheckTimers_cb, NULL );
	AddTimer(TIMER_TYPE_INTERVAL, PingServers, "PingServers", nsconfig.pingtime, NULL);
	AddTimer(TIMER_TYPE_INTERVAL, FlushLogs, "FlushLogs", nsconfig.pingtime, NULL);
	if (nsconfig.setservertimes > 0) 
		AddTimer(TIMER_TYPE_INTERVAL, SetServersTime, "SetServersTime", nsconfig.setservertimes, NULL);
	AddTimer(TIMER_TYPE_DAILY, ResetLogs, "ResetLogs", 0, NULL);
	return NS_SUCCESS;
}
示例#7
0
文件: hub.c 项目: xiserge/Shakespeer
void hub_start_myinfo_updater(void)
{
    static struct event ev;
    evtimer_set(&ev, myinfo_updater, &ev);
    struct timeval tv = {.tv_sec = 4, .tv_usec = 0};
    evtimer_add(&ev, &tv);
}

static void hub_set_passive_GFunc(hub_t *hub, void *user_data)
{
    return_if_fail(hub);
    return_if_fail(hub->me);

    bool *on = user_data;
    return_if_fail(on);

    hub->me->passive = *on;
}
示例#8
0
int main(void)
{
	event_init();

	CClient *client = new CClient;
	if( client->open( CONNECT_ADDR, CONNECT_PORT ) < 0 ){
		return 0;
	}

    struct timeval t;
    timerclear( &t );
    struct event ev;
    evtimer_set( &ev, oneshot_send, client );
    evtimer_add( &ev, &t );

    event_dispatch();
    return 0;
}
示例#9
0
int
control_listen(struct control_sock *cs)
{
	if (cs->cs_name == NULL)
		return (0);

	if (listen(cs->cs_fd, CONTROL_BACKLOG) == -1) {
		log_warn("%s: listen", __func__);
		return (-1);
	}

	event_set(&cs->cs_ev, cs->cs_fd, EV_READ,
	    control_accept, cs);
	event_add(&cs->cs_ev, NULL);
	evtimer_set(&cs->cs_evt, control_accept, cs);

	return (0);
}
示例#10
0
static guint event_timeout_add (guint interval, GSourceFunc function, gpointer data) {
	struct timeval timeout;
	PurpleIOClosure *closure = g_new0(PurpleIOClosure, 1);
	closure->function2 = function;
	closure->data = data;
	
	timeout.tv_sec = interval/1000;
	timeout.tv_usec = (interval%1000)*1000;
	evtimer_set(&closure->evfifo, event_io_invoke, closure);
	evtimer_add(&closure->evfifo, &timeout);
	closure->timeout = timeout;
	
	guint *f = (guint *) g_malloc(sizeof(guint));
	*f = id;
	id++;
	g_hash_table_replace(events, f, closure);
	return *f;
}
示例#11
0
/****************************************************************************
  Message Handler: grab_job() -> no_job(), job_assign(handle, func, arg)
 ****************************************************************************/
int msg_grab_job(Client *cli, unsigned char *arg, int argsize)
{
    Job *job = NULL;
    Ability *ability = NULL;
    int nabilities = cli->abilities->len;
    int i;
    for(i = 0; i < nabilities && !job; i++) {
        cli->ability_iter = (cli->ability_iter + 1) % nabilities;
        ability = g_ptr_array_index(cli->abilities, cli->ability_iter);
        job = jobqueue_pop(g_jobqueue, ability->func);
    }

    if (!job) {
        #if DEBUG
        g_debug("[%s] grab_job - no job", cli->id);
        #endif
        MemBlock *block = simple_response(MSG_NO_JOB);
        client_send(cli, block);
    } else {
        #if DEBUG
        g_debug("[%s] grab_job - assigned %s %s", cli->id, ability->func, job->handle);
        #endif

        job->worker = cli;
        client_add_working(cli, job);

        int data_len = strlen(job->handle) + strlen(job->func) + job->arg_len + 2;
        MemBlock *block = new_response(MSG_JOB_ASSIGN, data_len, NULL);
        unsigned char *p = block->bytes + HEADER_SIZE;
        p += sprintf((char*)p, job->handle) + 1;
        p += sprintf((char*)p, job->func) + 1;
        memcpy(p, job->arg, job->arg_len);
        client_send(cli, block);

        if (ability->timeout > 0) {
            struct timeval tv = {ability->timeout, 0};
            job->timeout = ability->timeout;
            evtimer_set(&job->work_timer, (void*)_work_timeout, job);
            evtimer_add(&job->work_timer, &tv);
        }
    }

    return 0;
}
示例#12
0
文件: lua2.c 项目: kalloc/tester
void timerLuaTask(int fd, short action, void *arg) {
    struct timeval tv;
    lua_State *thread,*L=(lua_State *)arg;

    timerclear(&tv);
    tv.tv_sec=10;
    printf("timer,run main task\n");
    evtimer_set(&time_ev, timerLuaTask,0);
    evtimer_add(&time_ev, &tv);

    thread=lua_newthread(L);
    luaL_loadstring(thread,"function main() print(\".Start\") wait() wait() print(\".End\",quit) end");
    lua_pcall(thread,0,0,0);
    lua_getglobal(thread, "main");
    printf("timer do resume lua_status(L)=%d\n",lua_status(thread));

    luaResume(thread,0);
    printf("timer posle resume lua_status(L)=%d\n",lua_status(thread));
}
示例#13
0
//超时事件测试
void timer_test()
{
     // 初始化  
       base=event_init();  
       struct event evTime;  
      // 设置定时事件  
        evtimer_set(&evTime, onTime, &evTime);  
        struct timeval tv;  
        tv.tv_sec = 10;  
        tv.tv_usec = 0;  
      // 添加定时事件  
        event_add(&evTime, &tv); 
		//设置为base事件
		event_base_set(base, &evTime); 
        // 事件调度循环  
       event_dispatch(); 
	    
	    event_base_free(base);
}
示例#14
0
void reconnect(CONNECTOR *cr, int delay_connect)
{
	struct timeval tv = { delay_connect, 0};

	if ( cr->bev )
		bufferevent_free(cr->bev);

	cr->bev = bufferevent_socket_new(cr->cb->ebase, -1, BEV_OPT_CLOSE_ON_FREE|BEV_OPT_DEFER_CALLBACKS );
	
	assert(cr->bev);

	bufferevent_setcb(cr->bev, NULL, NULL, connecting_event_cb, cr);
	cr->state = STATE_NOT_CONNECTED;

	evtimer_set(&cr->timer, go_connecting, cr);
	event_base_set(cr->cb->ebase, &cr->timer);
	evtimer_add(&cr->timer, &tv);

}
示例#15
0
/* Initialize PPPoE session context */
int
pppoe_session_init(pppoe_session *_this, pppoed *_pppoed, int idx,
    int session_id, u_char *ether_addr)
{
	memset(_this, 0, sizeof(pppoe_session));

	_this->pppoed = _pppoed;
	_this->session_id = session_id;
	_this->listener_index = idx;
	memcpy(_this->ether_addr, ether_addr, ETHER_ADDR_LEN);

	memcpy(_this->ehdr.ether_dhost, ether_addr, ETHER_ADDR_LEN);
	memcpy(_this->ehdr.ether_shost, pppoe_session_sock_ether_addr(_this),
	    ETHER_ADDR_LEN);

	evtimer_set(&_this->ev_disposing, pppoe_session_dispose_event, _this);

	return 0;
}
示例#16
0
文件: sample4.cpp 项目: jb55/libcage
        void operator() (bool result)
        {
                // print state
                if (result)
                        std::cout << "join: succeeded, n = "
                                  << n
                                  << std::endl;
                else
                        std::cout << "join: failed, n = "
                                  << n
                                  << std::endl;

                cage[n].print_state();

                // put data
                cage[n].put(&n, sizeof(n), &n, sizeof(n), 300);

                n++;

                if (n < max_node) {
                        // start nodes recursively
                        if (! cage[n].open(PF_INET, port + n)) {
                                std::cerr << "cannot open port: Port = "
                                          << port + n
                                          << std::endl;
                                return;
                        }

                        cage[n].set_dgram_callback(recv_dgram);
                        cage[n].join("localhost", 10000, *this);
                } else {
                        // start timer
                        timeval tval;

                        ev = new event;

                        tval.tv_sec  = 1;
                        tval.tv_usec = 0;

                        evtimer_set(ev, timer_callback, NULL);
                        evtimer_add(ev, &tval);
                }
        }
int main()   
{   
    // ???   
    event_init();   
     
    struct event ev_time;   
    // ??????   
    evtimer_set(&ev_time, onTime, &ev_time);   
     
    struct timeval tv;   
    tv.tv_sec = 1;   
    tv.tv_usec = 0;   
    // ??????   
    event_add(&ev_time, &tv);   
     
    // ????   
    event_dispatch();   
     
    return 0;   
}  
示例#18
0
tr_shared *
tr_sharedInit( tr_session  * session )
{
    tr_shared * s = tr_new0( tr_shared, 1 );

    s->session      = session;
    s->isEnabled    = FALSE;
    s->upnpStatus   = TR_PORT_UNMAPPED;
    s->natpmpStatus = TR_PORT_UNMAPPED;

#if 0
    if( isEnabled )
    {
        s->timer = tr_new0( struct event, 1 );
        evtimer_set( s->timer, onTimer, s );
        tr_timerAdd( s->timer, 0, 333000 );
    }
#endif

    return s;
}
示例#19
0
文件: lua2.c 项目: kalloc/tester
int main(int argc, char *argv[])    
{   
    lua_State *L = lua_open();   
    luaopen_base(L);             /* opens the basic library */
    luaopen_table(L);            /* opens the table library */
    luaopen_string(L);           /* opens the string lib. */
    luaopen_math(L);             /* opens the math lib. */
    lua_pop(L,5);
    lua_pushcfunction(L, wait);    
    lua_setglobal(L, "wait"); 
    lua_pushcfunction(L, quit);    
    lua_setglobal(L, "quit"); 

    struct timeval tv;
    event_init();
    timerclear(&tv);
    evtimer_set(&time_ev, timerLuaTask,L);
    evtimer_add(&time_ev, &tv);
    event_dispatch();
    return 0;   
} 
示例#20
0
static void clock_handler(const int fd, const short which, void *arg) {
  struct timeval t;
  static bool initialized = false;
  
  if (initialized) {
    /* only delete the event if it's actually there. */
    evtimer_del(&clockevent);
  } else {
    initialized = true;
  }

  t.tv_sec = 1;
  t.tv_usec = 0;
  evtimer_set(&clockevent, clock_handler, 0);
  event_base_set(main_base, &clockevent);
  evtimer_add(&clockevent, &t);

  set_current_time();
  if (clock_callback)
    clock_callback(current_time);
}
示例#21
0
tr_timer*
tr_timerNew( tr_session  * session,
             timer_func    func,
             void        * user_data,
             uint64_t      interval_milliseconds )
{
    tr_timer * timer;

    assert( tr_amInEventThread( session ) );

    timer = tr_new0( tr_timer, 1 );
    timer->func = func;
    timer->user_data = user_data;
    timer->eh = session->events;

    tr_timevalMsec( interval_milliseconds, &timer->tv );
    evtimer_set( &timer->event, timerCallback, timer );
    evtimer_add( &timer->event,  &timer->tv );

    return timer;
}
示例#22
0
struct rrdtool_drv *
rrdtool_init(const char *path_rrdtool)
{
	struct rrdtool_drv *rrd;

	if ((rrd = calloc(1, sizeof(struct rrdtool_drv))) == NULL) {
		warn("%s: calloc", __func__);
		goto error;
	}

	/* Remember the path so that we can respawn */
	rrd->bin_path = path_rrdtool;

	if (rrdtool_fork(rrd) == -1) {
		goto error;
	}

	if ((rrd->evb = bufferevent_new(rrd->fd,
		 rrdtool_evb_readcb, rrdtool_evb_writecb, rrdtool_evb_errcb,
		 rrd)) == NULL)
		goto error;

	TAILQ_INIT(&rrd->commands);

	bufferevent_disable(rrd->evb, EV_READ);
	bufferevent_enable(rrd->evb, EV_WRITE);

	evtimer_set(&rrd->ev_timeout, rrdtool_restart, rrd);

	return rrd;

 error:
	if (rrd != NULL) {
		if (rrd->fd != -1)
			close(rrd->fd);
		free(rrd);
	}

	return (NULL);
}
示例#23
0
int main ()
{
    struct event ev_accept;

    g_print("server_proc_main init .\n");

    int listenfd = create_socket( NULL, 9009, SOCK_STREAM);
    if (listenfd < 0)
    {
        g_print("tcp_listen failed!\n");
        return -1;
    }	

    g_print("listenfd is %d\n", listenfd);
    // initialize libevent.
    event_init ();

    /* We now have a listeing socket, we create a read event to
     * be notified when a client connects. */
    time_slot_t *ts = (time_slot_t *)calloc(1,sizeof(time_slot_t));

    ts->tv.tv_sec=1;
    ts->tv.tv_usec=0;
    evtimer_set(&ts->ev_timeout, timeout_cb, ts /*&ev_timeout*/);
    evtimer_add(&ts->ev_timeout, &ts->tv);
    
    event_set (&ev_accept, listenfd, EV_READ | EV_PERSIST,
               conn_accept, NULL);

    struct timeval tvaccept;
    tvaccept.tv_sec=3;
    tvaccept.tv_usec=3;

    event_add (&ev_accept, &tvaccept);

    
    event_dispatch ();

    return 0;
}
示例#24
0
/*
 * Setup klogger thread
 */
static rstatus_t
thread_setup_klogger(void)
{
    int status;

    klogger.base = event_base_new();
    if (NULL == klogger.base) {
        log_error("event init failed: %s", strerror(errno));
        return MC_ERROR;
    }

    evtimer_set(&klogger.ev, thread_klogger_collect, NULL);
    event_base_set(klogger.base, &klogger.ev);

    status = evtimer_add(&klogger.ev, &settings.klog_intvl);
    if (status < 0) {
        log_error("evtimer add failed: %s", strerror(errno));
        return status;
    }

    return MC_OK;
}
示例#25
0
int
main (int argc, char **argv)
{
	struct event timeout;
	struct timeval tv;
 
	
	event_init();

	
	evtimer_set(&timeout, timeout_cb, &timeout);

	evutil_timerclear(&tv);
	tv.tv_sec = 2;
	event_add(&timeout, &tv);

	lasttime = time(NULL);
	
	event_dispatch();

	return (0);
}
int main(int argc, char** argv) {

  printf("hello world\n");

  network = network_new(GCS_HOST, GCS_PORT);

  /* Initalize the event library */
  event_init();

  /* Add a timeout event */
  evtimer_set(&timeout, timeout_cb, &timeout);

  struct timeval tv;
  evutil_timerclear(&tv);
  tv.tv_sec  = TIMEOUT_DT_SEC;
  tv.tv_usec = TIMEOUT_DT_USEC;
  event_add(&timeout, &tv);

  event_dispatch();

  return 0;
}
示例#27
0
/*
 * Setup aggregator thread
 */
static rstatus_t
thread_setup_aggregator(void)
{
    int status;

    aggregator.base = event_base_new();
    if (NULL == aggregator.base) {
        log_error("event init failed: %s", strerror(errno));
        return MC_ERROR;
    }

    /* +1 because we also aggregate from dispatcher */
    sem_init(&aggregator.stats_sem, 0, settings.num_workers + 1);

    aggregator.stats_thread = stats_thread_init();
    if (aggregator.stats_thread == NULL) {
        sem_destroy(&aggregator.stats_sem);
        return MC_ERROR;
    }

    aggregator.stats_slabs = stats_slabs_init();
    if (aggregator.stats_slabs == NULL) {
        sem_destroy(&aggregator.stats_sem);
        stats_thread_deinit(aggregator.stats_thread);
        return MC_ERROR;
    }

    evtimer_set(&aggregator.ev, thread_aggregate_stats, NULL);
    event_base_set(aggregator.base, &aggregator.ev);

    status = evtimer_add(&aggregator.ev, &settings.stats_agg_intvl);
    if (status < 0) {
        log_error("evtimer add failed: %s", strerror(errno));
        return status;
    }

    return MC_OK;
}
示例#28
0
static void _schedule_heartbeat( T _this_ ) {

    item_ *timer_item;
    timer_item = Mem_calloc( 1, sizeof *timer_item, __FILE__, __LINE__ );
    oe_id sid = _gen_id(_this_);
    timer_item->sid = sid;
    timer_item->dispatcher = _this_;
    timer_item->type = HEARTBEAT;

    struct timeval tv;
    struct event *timeout;

    timeout = Mem_calloc(1, sizeof *timeout, __FILE__, __LINE__);
    event_base_set(_this_->net_obj, timeout);
    timer_item->timer_event = timeout;

    evtimer_set(timeout, _heartbeat_cb, timer_item);

    evutil_timerclear(&tv);
    tv.tv_sec = 10;

    event_add(timeout, &tv);
}
示例#29
0
int
main(int argc, char **argv)
{
    struct timeval tv;
    int i;

    /* Initalize the event library */
    event_init();

    for (i = 0; i < NEVENT; i++) {
        ev[i] = malloc(sizeof(struct event));

        /* Initalize one event */
        evtimer_set(ev[i], time_cb, ev[i]);
        tv.tv_sec = 0;
        tv.tv_usec = rand_int(50000);
        evtimer_add(ev[i], &tv);
    }

    event_dispatch();

    return (called < NEVENT);
}
示例#30
0
void
pptpd_stop(pptpd *_this)
{
	int nctrl;
	pptp_ctrl *ctrl;
	struct timeval tv;

	if (event_initialized(&_this->ev_timer))
		evtimer_del(&_this->ev_timer);
	pptpd_close_1723(_this);

	/* XXX: use common procedure with l2tpd_stop */

	if (pptpd_is_stopped(_this))
		return;
	if (pptpd_is_shutting_down(_this)) {
		pptpd_stop_immediatly(_this);
		return;
	}
	_this->state = PPTPD_STATE_SHUTTING_DOWN;
	nctrl = 0;
	for (slist_itr_first(&_this->ctrl_list);
	    (ctrl = slist_itr_next(&_this->ctrl_list)) != NULL;) {
		pptp_ctrl_stop(ctrl, PPTP_CDN_RESULT_ADMIN_SHUTDOWN);
		nctrl++;
	}
	if (nctrl > 0) {
		tv.tv_sec = PPTPD_SHUTDOWN_TIMEOUT;
		tv.tv_usec = 0;

		evtimer_set(&_this->ev_timer, pptpd_stop_timeout, _this);
		evtimer_add(&_this->ev_timer, &tv);

		return;
	}
	pptpd_stop_immediatly(_this);
}