Example #1
0
void on_event(int sock, short event, void *arg) 
{
  Peer * peer = 0;
  if (sock == server.getHandle())
  {
    int cfd = -1;
    if (-1 == (cfd = server.accept()))
    {
      COMMON_LOGGER_ERROR_S("accept failed\n");

	  return;
    }

    if (0 != (peer = server.findPeer(cfd)))
    {
      (void)event_set(peer->event() , cfd, EV_READ | EV_PERSIST , on_event , 0);
      (void)event_add(peer->event() , 0);
    }

    COMMON_LOGGER_DEBUG("accept peer %d OK.\n" , cfd);

    return;
  }
  else if ((peer = server.findPeer(sock)) != 0)
  {
    int ret = -1;
    if ((ret = server.read(sock)) == -1)
    {
      event_del(peer->event());
      server.deletePeer(sock);

      COMMON_LOGGER_DEBUG("close socket %d\n" , sock);
    }
    
    return;
  }
  else
  {
    COMMON_LOGGER_INFO("total:%d get:%d set:%d del:%d rep:%d\n" , 
		pscache_get_counter + pscache_set_counter + pscache_del_counter + pscache_rep_counter , 
		pscache_get_counter , 
		pscache_set_counter ,
		pscache_del_counter ,
		pscache_rep_counter);

	pscache_get_counter = pscache_set_counter = pscache_del_counter = pscache_rep_counter = 0;
    
    struct timeval tv; 
    tv.tv_sec = DEFAULT_PSC_TIMEOUT; 
    tv.tv_usec = 0; 
  
    (void)event_add((struct event*)arg, &tv); 

	return;
  }
}
Example #2
0
int main(int argc , char ** args)
{
  if (argc < 3)
  {
    printf("Usage: pscache isDeamon port\n");

	return -1;
  }
  
  if (atoi(args[1]) != 0)
  {
    init_daemon();
  }

  (void)signal(SIGPIPE , SIG_IGN);

  if (-1 == PsCacheHandlerMgr::instance()->initialize())
  {
    COMMON_LOGGER_ERROR_S("initialize handler mgr failed.");
	
    return -1;
  }
  
  if (0 != Logger::instance()->initialize("pscache"))
  {
    COMMON_LOGGER_ERROR_S("initialize failed.\n");

	return -1;
  }

#ifndef __STORAGE_FASTDB__
  int pre_malloc_size = 5000000;
  if (argc == 4)
  {
    pre_malloc_size = atoi(args[3]);
	if (pre_malloc_size > 5000000)
	{
	  pre_malloc_size = 5000000;
	}
  }

  COMMON_LOGGER_INFO("pre-malloc-size is %d\n" , pre_malloc_size);
  
  if (!PscMemMgr::instance()->initialize(pre_malloc_size))
  {
    COMMON_LOGGER_ERROR_S("malloc memory failed.\n");
    
    return -1;
  }
#endif

  if (!PsDataMgr::instance()->initialize())
  {
    COMMON_LOGGER_ERROR_S("init failed.\n");
    
    return -1;
  }

  (void)event_init();

  struct event evTime; 
  (void)evtimer_set(&evTime, on_event, &evTime); 
   
  struct timeval tv; 
  tv.tv_sec = DEFAULT_PSC_TIMEOUT; 
  tv.tv_usec = 0; 

  (void)event_add(&evTime, &tv);

  int pcPort = atoi(args[2]);
  PsrHandlerMgr::instance()->initialize(pcPort);

  union ipu iptest;
  
  iptest.ip = getlocalhostip();
  char localIp[32] = {0};
  (void)sprintf(localIp , "%u.%u.%u.%u" , iptest.ipchar[0] , 
  	iptest.ipchar[1] , iptest.ipchar[2] , iptest.ipchar[3]);

  COMMON_LOGGER_DEBUG("local ip is %s, local port is %d\n" , localIp , pcPort);

  if (!PsrManager::instance()->initialize(localIp , pcPort))
  {
    COMMON_LOGGER_ERROR_S("initialize psr mgr failed.\n");
    
    return -1;
  }

  if (-1 == server.intialize(pcPort , socketCallback))
  {
    COMMON_LOGGER_ERROR_S("create server failed.\n");
  }
  
  (void)event_set(&svrEvt , server.getHandle(), EV_READ | EV_PERSIST, on_event , 0);
  (void)event_add(&svrEvt, 0);
    
  (void)event_dispatch();
	  
  return 0;
}