Skip to content

Commit

Permalink
xxxxxxxxxxx
Browse files Browse the repository at this point in the history
  • Loading branch information
idning committed Sep 25, 2012
1 parent 6f7005e commit dd52671
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 23 deletions.
8 changes: 5 additions & 3 deletions common/log.h
@@ -1,3 +1,7 @@

#ifndef _LOG_H_
#define _LOG_H_

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
Expand All @@ -8,9 +12,6 @@
#include <sys/stat.h>
#include <sys/types.h>

#ifndef _LOG_H__
#define _LOG_H__

#define LOG_ALL 0
#define LOG_DEUBG 1
#define LOG_TRACE 2
Expand Down Expand Up @@ -52,3 +53,4 @@ int log_set_level(int level);
(log_print(LOG_DEUBG, "(%s:%d) [func:%s] " fmt, REMOVE_PATH(__FILE__), __LINE__, __func__, ## __VA_ARGS__), 0)

#endif

2 changes: 1 addition & 1 deletion src/mongo_server.c → src/mongo_backend.c
Expand Up @@ -5,6 +5,6 @@
*/

#include<stdio.h>
#include "mongo_server.h"
#include "mongo_backend.h"

/*mongo_replset_t * replset;*/
17 changes: 7 additions & 10 deletions src/mongo_server.h → src/mongo_backend.h
Expand Up @@ -3,11 +3,9 @@

#define MONGO_MAX_SERVERS 12

#include "mongoproxy.h"

typedef struct mongo_conn_s mongo_conn_t;

typedef struct mongo_server_s {
typedef struct mongo_backend_s {
char *host;
int port;
int is_master;
Expand All @@ -16,7 +14,7 @@ typedef struct mongo_server_s {
int connection_cnt; // used for load balance
mongo_conn_t * free_conn;

} mongo_server_t;
} mongo_backend_t;


typedef enum mongo_conn_state_s {
Expand All @@ -30,7 +28,7 @@ typedef enum mongo_conn_state_s {

struct mongo_conn_s {
struct mongo_conn_s *next; // in free_conn linked list
mongo_server_t *server;
mongo_backend_t *server;
int fd;
mongo_conn_state_t conn_state;
};
Expand All @@ -42,15 +40,14 @@ void mongo_conn_close(mongo_conn_t * conn);

typedef struct mongo_replset_s {
int replset_size;
mongo_server_t *slaves[MONGO_MAX_SERVERS];
mongo_server_t *master;
mongo_backend_t *slaves[MONGO_MAX_SERVERS];
mongo_backend_t *master;
} mongo_replset_t;


mongo_conn_t *mongo_server_new_conn(mongo_server_t * server);

mongo_conn_t *mongo_server_new_conn(mongo_backend_t * server);

void mongo_replset_init(mongo_replset_t * replica_set, mongoproxy_cfg_t * cfg);
//void mongo_replset_init(mongo_replset_t * replica_set, mongoproxy_cfg_t * cfg);
mongo_conn_t *mongo_replset_get_conn(mongo_replset_t* replica_set, int primary);

mongo_conn_t *mongo_replset_release_conn(mongo_conn_t * conn);
Expand Down
11 changes: 6 additions & 5 deletions src/mongoproxy.c
Expand Up @@ -13,11 +13,11 @@

#include "mongoproxy.h"
#include "mongoproxy_session.h"
#include "mongo_server.h"
#include "mongo_backend.h"

//globals
//
static mongoproxy_server_t g_server;
mongoproxy_server_t g_server;

static int _mongoproxy_load_config(){

Expand All @@ -29,6 +29,7 @@ static int _mongoproxy_load_config(){
static int _mongoproxy_read_client_request_done(mongoproxy_session_t * sess){
if ( sess->buf->used < MONGO_HEAD_LEN )
return 0;

int body_len = *(int*) sess->buf->ptr;
return sess->buf->used >= body_len;
}
Expand Down Expand Up @@ -90,10 +91,10 @@ void on_accept(int fd, short ev, void *arg)

static int _mongoproxy_init(){
struct event ev_accept;

mongoproxy_cfg_t * cfg = &(g_server.cfg);
int listen_fd ;
listen_fd = network_server_socket(g_mongoproxy_cfg->listen_host, g_mongoproxy_cfg->listen_port);

listen_fd = network_server_socket(cfg->listen_host, cfg->listen_port);

event_init(); //init libevent
event_set(&ev_accept, listen_fd, EV_READ | EV_PERSIST, on_accept, NULL);
Expand Down
4 changes: 3 additions & 1 deletion src/mongoproxy.h
Expand Up @@ -8,7 +8,7 @@
#define _MONGOPROXY_H_

#include "mongoproxy_session.h"
#include "mongo_server.h"
#include "mongo_backend.h"

typedef struct mongoproxy_cfg_s {
char *backend;
Expand All @@ -23,4 +23,6 @@ typedef struct mongoproxy_server_s{

int mongoproxy_mainloop();

extern mongoproxy_server_t g_server;

#endif
6 changes: 3 additions & 3 deletions src/mongoproxy_session.c
Expand Up @@ -11,7 +11,7 @@

#include "mongoproxy.h"
#include "mongoproxy_session.h"
#include "mongo_server.h"
#include "mongo_backend.h"

mongoproxy_session_t * mongoproxy_session_new(){
mongoproxy_session_t * sess;
Expand Down Expand Up @@ -50,7 +50,7 @@ int mongoproxy_session_select_backend(mongoproxy_session_t * sess, int primary){
mongo_replset_release_conn(sess->backend);
sess->backend = NULL;
}

sess->backend = mongo_replset_get_conn(primary);
mongo_replset_t * replset = &(g_server.replset);
sess->backend = mongo_replset_get_conn(replset, primary);
}

0 comments on commit dd52671

Please sign in to comment.