mirror of
https://github.com/haiwen/ccnet-server.git
synced 2025-09-09 08:39:12 +00:00
Initial commit of Ccnet server.
This commit is contained in:
63
lib/client-pool.c
Normal file
63
lib/client-pool.c
Normal file
@@ -0,0 +1,63 @@
|
||||
#include "include.h"
|
||||
|
||||
#include <ccnet.h>
|
||||
#include <ccnet/ccnetrpc-transport.h>
|
||||
|
||||
#include <glib.h>
|
||||
#include <pthread.h>
|
||||
|
||||
struct CcnetClientPool {
|
||||
GQueue *clients;
|
||||
pthread_mutex_t lock;
|
||||
const char *central_config_dir;
|
||||
const char *conf_dir;
|
||||
};
|
||||
|
||||
struct CcnetClientPool *
|
||||
ccnet_client_pool_new (const char *central_config_dir, const char *conf_dir)
|
||||
{
|
||||
CcnetClientPool *pool = g_new0 (CcnetClientPool, 1);
|
||||
|
||||
pool->clients = g_queue_new ();
|
||||
pthread_mutex_init (&pool->lock, NULL);
|
||||
pool->conf_dir = g_strdup(conf_dir);
|
||||
pool->central_config_dir = g_strdup(central_config_dir);
|
||||
|
||||
return pool;
|
||||
}
|
||||
|
||||
CcnetClient *
|
||||
ccnet_client_pool_get_client (struct CcnetClientPool *cpool)
|
||||
{
|
||||
CcnetClient *client;
|
||||
|
||||
pthread_mutex_lock (&cpool->lock);
|
||||
client = g_queue_pop_head (cpool->clients);
|
||||
pthread_mutex_unlock (&cpool->lock);
|
||||
|
||||
if (!client) {
|
||||
client = ccnet_client_new ();
|
||||
if (ccnet_client_load_confdir (client, cpool->central_config_dir, cpool->conf_dir) < 0) {
|
||||
g_warning ("[client pool] Failed to load conf dir.\n");
|
||||
g_object_unref (client);
|
||||
return NULL;
|
||||
}
|
||||
if (ccnet_client_connect_daemon (client, CCNET_CLIENT_SYNC) < 0) {
|
||||
g_warning ("[client pool] Failed to connect.\n");
|
||||
g_object_unref (client);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
void
|
||||
ccnet_client_pool_return_client (struct CcnetClientPool *cpool,
|
||||
CcnetClient *client)
|
||||
{
|
||||
pthread_mutex_lock (&cpool->lock);
|
||||
g_queue_push_tail (cpool->clients, client);
|
||||
pthread_mutex_unlock (&cpool->lock);
|
||||
}
|
||||
|
Reference in New Issue
Block a user