Permalink
Browse files

++

  • Loading branch information...
1 parent f511209 commit 7e4dd5ef3ba8daebdbc311b0ef3a1c5bc15fd43e @ValeriyBushenev ValeriyBushenev committed Aug 19, 2011
Showing with 131 additions and 127 deletions.
  1. +9 −0 misc/c_to_d.sh
  2. +0 −106 misc/mongo.c
  3. +122 −21 misc/net_i
View
@@ -7,6 +7,9 @@ cat tmp_o > tmp_i
sed "s/const char \*cur;/char \*cur;/g" tmp_i > tmp_o
cat tmp_o > tmp_i
+sed "s/( const char \* )\&node->host/node->host/g" tmp_i > tmp_o
+
+cat tmp_o > tmp_i
sed "s/static int ( \*oid_fuzz_func )( void ) = NULL;/static int function () oid_fuzz_func = null;/g" tmp_i > tmp_o
cat tmp_o > tmp_i
@@ -442,6 +445,12 @@ sed 's/connect( conn.sock/connect( cast(socket_t)conn.sock/g' tmp_i > tmp_o
cat tmp_o > tmp_i
sed 's/conn.sock = 0;/conn.sock = null;/g' tmp_i > tmp_o
+cat tmp_o > tmp_i
+sed 's/char \*host;/string host;/g' tmp_i > tmp_o
+
+cat tmp_o > tmp_i
+sed 's/char host\[255\];/string host;/g' tmp_i > tmp_o
+
cat tmp_o > $2
rm tmp_i
rm tmp_o
View
@@ -101,22 +101,6 @@ void mongo_init( mongo *conn ) {
conn->op_timeout_ms = 0;
}
-int mongo_connect( mongo *conn , const char *host, int port ) {
- conn->primary = bson_malloc( sizeof( mongo_host_port ) );
- strncpy( conn->primary->host, host, strlen( host ) + 1 );
- conn->primary->port = port;
- conn->primary->next = NULL;
-
- mongo_init( conn );
- if( mongo_socket_connect( conn, host, port ) != MONGO_OK )
- return MONGO_ERROR;
-
- if( mongo_check_is_master( conn ) != MONGO_OK )
- return MONGO_ERROR;
- else
- return MONGO_OK;
-}
-
void mongo_replset_init( mongo *conn, const char *name ) {
mongo_init( conn );
@@ -130,22 +114,6 @@ void mongo_replset_init( mongo *conn, const char *name ) {
conn->primary = bson_malloc( sizeof( mongo_host_port ) );
}
-static void mongo_replset_add_node( mongo_host_port **list, const char *host, int port ) {
- mongo_host_port *host_port = bson_malloc( sizeof( mongo_host_port ) );
- host_port->port = port;
- host_port->next = NULL;
- strncpy( host_port->host, host, strlen( host ) + 1 );
-
- if( *list == NULL )
- *list = host_port;
- else {
- mongo_host_port *p = *list;
- while( p->next != NULL )
- p = p->next;
- p->next = host_port;
- }
-}
-
static void mongo_replset_free_list( mongo_host_port **list ) {
mongo_host_port *node = *list;
mongo_host_port *prev;
@@ -159,80 +127,6 @@ static void mongo_replset_free_list( mongo_host_port **list ) {
*list = NULL;
}
-void mongo_replset_add_seed( mongo *conn, const char *host, int port ) {
- mongo_replset_add_node( &conn->replset->seeds, host, port );
-}
-
-void mongo_parse_host( const char *host_string, mongo_host_port *host_port ) {
- int len, idx, split;
- len = split = idx = 0;
-
- /* Split the host_port string at the ':' */
- while( 1 ) {
- if( *( host_string + len ) == '\0' )
- break;
- if( *( host_string + len ) == ':' )
- split = len;
-
- len++;
- }
-
- /* If 'split' is set, we know the that port exists;
- * Otherwise, we set the default port. */
- idx = split ? split : len;
- memcpy( host_port->host, host_string, idx );
- memcpy( host_port->host + idx, "\0", 1 );
- if( split )
- host_port->port = atoi( host_string + idx + 1 );
- else
- host_port->port = MONGO_DEFAULT_PORT;
-}
-
-static void mongo_replset_check_seed( mongo *conn ) {
- bson out;
- bson hosts;
- const char *data;
- bson_iterator it;
- bson_iterator it_sub;
- const char *host_string;
- mongo_host_port *host_port = NULL;
-
- out.data = NULL;
-
- hosts.data = NULL;
-
- if( mongo_simple_int_command( conn, "admin", "ismaster", 1, &out ) == MONGO_OK ) {
-
- if( bson_find( &it, &out, "hosts" ) ) {
- data = bson_iterator_value( &it );
- bson_iterator_from_buffer( &it_sub, data );
-
- /* Iterate over host list, adding each host to the
- * connection's host list. */
- while( bson_iterator_next( &it_sub ) ) {
- host_string = bson_iterator_string( &it_sub );
-
- host_port = bson_malloc( sizeof( mongo_host_port ) );
- mongo_parse_host( host_string, host_port );
-
- if( host_port ) {
- mongo_replset_add_node( &conn->replset->hosts,
- host_port->host, host_port->port );
-
- bson_free( host_port );
- host_port = NULL;
- }
- }
- }
- }
-
- bson_destroy( &out );
- bson_destroy( &hosts );
- mongo_close_socket( conn->sock );
- conn->sock = 0;
- conn->connected = 0;
-
-}
/* Find out whether the current connected node is master, and
* verify that the node's replica set name matched the provided name
View
@@ -77,8 +77,8 @@ int mongo_read_response(mongo* conn, mongo_reply** reply, bool retry = false)
{
if(res != MONGO_OK)
{
- bson_free(_out);
- }
+ bson_free(_out);
+ }
}
}
}
@@ -157,25 +157,6 @@ void mongo_close_socket(Socket sock)
sock.close();
}
-int mongo_socket_connect(mongo* conn, char* host, int port)
-{
- conn.sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
- InternetAddress addr = new InternetAddress(cast(string) host[0 .. strlen(host)], cast(ushort) port);
- conn.sock.connect(addr);
-
- if(conn.sock.isAlive())
- {
- conn.sock.setOption(SocketOptionLevel.TCP, SocketOption.TCP_NODELAY, true);
- return MONGO_OK;
- } else
- {
- conn.sock = null;
- conn.err = mongo_error_t.MONGO_CONN_FAIL;
- printf("mongo_socket_connect is fail\n");
- return MONGO_ERROR;
- }
-}
-
int mongo_write_socket(mongo* conn, void* buf, int len)
{
char* cbuf = cast(char*) buf;
@@ -239,6 +220,42 @@ int mongo_set_socket_op_timeout(mongo* conn, int millis)
return MONGO_OK;
}
+int mongo_socket_connect(mongo* conn, string host, int port)
+{
+ conn.sock = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
+ InternetAddress addr = new InternetAddress(host, cast(ushort) port);
+ conn.sock.connect(addr);
+
+ if(conn.sock.isAlive())
+ {
+ conn.sock.setOption(SocketOptionLevel.TCP, SocketOption.TCP_NODELAY, true);
+ return MONGO_OK;
+ } else
+ {
+ conn.sock = null;
+ conn.err = mongo_error_t.MONGO_CONN_FAIL;
+ printf("mongo_socket_connect is fail\n");
+ return MONGO_ERROR;
+ }
+}
+
+int mongo_connect(mongo* conn, string host, int port)
+{
+ conn.primary = cast(mongo_host_port*) bson_malloc(mongo_host_port.sizeof);
+ conn.primary.host = host;
+ conn.primary.port = port;
+ conn.primary.next = null;
+
+ mongo_init(conn);
+ if(mongo_socket_connect(conn, host, port) != MONGO_OK)
+ return MONGO_ERROR;
+
+ if(mongo_check_is_master(conn) != MONGO_OK)
+ return MONGO_ERROR;
+ else
+ return MONGO_OK;
+}
+
int mongo_reconnect(mongo* conn)
{
int res;
@@ -273,3 +290,87 @@ void mongo_disconnect(mongo* conn)
conn.connected = 0;
}
+static void mongo_replset_add_node( mongo_host_port **list, string host, int port ) {
+ mongo_host_port *host_port = cast(mongo_host_port*)bson_malloc( mongo_host_port.sizeof );
+ host_port.port = port;
+ host_port.host = host;
+ host_port.next = null;
+
+ if( *list == null )
+ *list = host_port;
+ else {
+ mongo_host_port *p = *list;
+ while( p.next != null )
+ p = p.next;
+ p.next = host_port;
+ }
+}
+
+void mongo_parse_host( string host_string, mongo_host_port *host_port ) {
+
+ string[] host_port_s = std.string.split(host_string,":");
+
+ if (host_port_s.length == 2)
+ {
+ host_port.host = host_port_s[0];
+ host_port.port = atoi( cast(char*)host_port_s[1]);
+ }
+ else if (host_port_s.length == 1)
+ {
+ host_port.host = host_string;
+ host_port.port = MONGO_DEFAULT_PORT;
+ }
+
+}
+
+void mongo_replset_add_seed( mongo *conn, string host, int port )
+{
+ mongo_replset_add_node( &conn.replset.seeds, host, port );
+}
+
+static void mongo_replset_check_seed( mongo *conn ) {
+ bson _out;
+ bson hosts;
+ char *data;
+ bson_iterator it;
+ bson_iterator it_sub;
+ char *host_string;
+ mongo_host_port *host_port = null;
+
+ _out.data = null;
+
+ hosts.data = null;
+
+ if( mongo_simple_int_command( conn, cast(char*) "admin", cast(char*) "ismaster", 1, &_out ) == MONGO_OK ) {
+
+ if( bson_find( &it, &_out, cast(char*) "hosts" ) ) {
+ data = bson_iterator_value( &it );
+ bson_iterator_from_buffer( &it_sub, data );
+
+ /* Iterate over host list, adding each host to the
+ * connection's host list. */
+ while( bson_iterator_next( &it_sub ) ) {
+ host_string = bson_iterator_string( &it_sub );
+
+ host_port = cast(mongo_host_port*)bson_malloc( mongo_host_port.sizeof );
+ mongo_parse_host( cast(string)host_string[0..strlen (host_string)], host_port );
+
+ if( host_port ) {
+ mongo_replset_add_node( &conn.replset.hosts,
+ host_port.host, host_port.port );
+
+ bson_free( host_port );
+ host_port = null;
+ }
+ }
+ }
+ }
+
+ bson_destroy( &_out );
+ bson_destroy( &hosts );
+ mongo_close_socket( conn.sock );
+ conn.sock = null;
+ conn.connected = 0;
+
+}
+

0 comments on commit 7e4dd5e

Please sign in to comment.