Skip to content

Commit

Permalink
move to company svn
Browse files Browse the repository at this point in the history
  • Loading branch information
auxten committed May 9, 2012
1 parent a30c0ac commit a3165ad
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 182 deletions.
14 changes: 14 additions & 0 deletions example/client.cpp
Expand Up @@ -6,6 +6,7 @@
*/ */


#include "gko.h" #include "gko.h"
#include "async_clnt.h"


/// gingko global stuff /// gingko global stuff
s_gingko_global_t gko; s_gingko_global_t gko;
Expand Down Expand Up @@ -36,7 +37,10 @@ int main(int argc, char** argv)
pthread_attr_t g_attr; pthread_attr_t g_attr;
pthread_t vnode_pthread[T_NUM]; pthread_t vnode_pthread[T_NUM];
void *status; void *status;
unsigned short proto_ver;
unsigned int msg_len;


/*
if (pthread_attr_init(&g_attr) != 0) if (pthread_attr_init(&g_attr) != 0)
{ {
gko_log(FATAL, FLF("pthread_mutex_destroy error")); gko_log(FATAL, FLF("pthread_mutex_destroy error"));
Expand Down Expand Up @@ -70,6 +74,16 @@ int main(int argc, char** argv)
return -1; return -1;
} }
} }
*/
char buf[20] = {'\0'};
fill_cmd_head(buf, INT32_MAX);
parse_cmd_head(buf, &proto_ver, &msg_len);
gko_log(DEBUG, "%s %d", buf, msg_len);
std::vector<struct conn_client> conn_vec;
std::vector<s_host_t> srv_vec(1000, server);
connect_hosts(srv_vec, &conn_vec);
sleep(10);
disconnect_hosts(conn_vec);
return 0; return 0;
} }


2 changes: 1 addition & 1 deletion src/Makefile.am
Expand Up @@ -11,6 +11,6 @@ export INCLUDES
noinst_LIBRARIES=libgko.a noinst_LIBRARIES=libgko.a


DEFS+=-O0 -D_GNU_SOURCE -ggdb -funsigned-char DEFS+=-O0 -D_GNU_SOURCE -ggdb -funsigned-char
libgko_a_SOURCES=async_threads.cpp async_conn.cpp \ libgko_a_SOURCES=async_threads.cpp async_conn.cpp async_clnt.cpp \
gingko_base.cpp log.cpp socket.cpp limit.cpp gingko_base.cpp log.cpp socket.cpp limit.cpp
libgko_a_LDFLAGS=-O0 -D_GNU_SOURCE -ggdb -funsigned-char libgko_a_LDFLAGS=-O0 -D_GNU_SOURCE -ggdb -funsigned-char
31 changes: 20 additions & 11 deletions src/Makefile.in
@@ -1,4 +1,4 @@
# Makefile.in generated by automake 1.11.2 from Makefile.am. # Makefile.in generated by automake 1.11.3 from Makefile.am.
# @configure_input@ # @configure_input@


# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, # Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
Expand Down Expand Up @@ -36,7 +36,7 @@ subdir = .
DIST_COMMON = README $(am__configure_deps) $(srcdir)/Makefile.am \ DIST_COMMON = README $(am__configure_deps) $(srcdir)/Makefile.am \
$(srcdir)/Makefile.in $(srcdir)/config.h.in \ $(srcdir)/Makefile.in $(srcdir)/config.h.in \
$(top_srcdir)/configure AUTHORS COPYING ChangeLog INSTALL NEWS \ $(top_srcdir)/configure AUTHORS COPYING ChangeLog INSTALL NEWS \
depcomp install-sh missing TODO depcomp install-sh missing
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/configure.in am__aclocal_m4_deps = $(top_srcdir)/configure.in
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \ am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) \
Expand All @@ -53,8 +53,8 @@ ARFLAGS = cru
libgko_a_AR = $(AR) $(ARFLAGS) libgko_a_AR = $(AR) $(ARFLAGS)
libgko_a_LIBADD = libgko_a_LIBADD =
am_libgko_a_OBJECTS = async_threads.$(OBJEXT) async_conn.$(OBJEXT) \ am_libgko_a_OBJECTS = async_threads.$(OBJEXT) async_conn.$(OBJEXT) \
gingko_base.$(OBJEXT) log.$(OBJEXT) socket.$(OBJEXT) \ async_clnt.$(OBJEXT) gingko_base.$(OBJEXT) log.$(OBJEXT) \
limit.$(OBJEXT) socket.$(OBJEXT) limit.$(OBJEXT)
libgko_a_OBJECTS = $(am_libgko_a_OBJECTS) libgko_a_OBJECTS = $(am_libgko_a_OBJECTS)
DEFAULT_INCLUDES = -I.@am__isrc@ DEFAULT_INCLUDES = -I.@am__isrc@
depcomp = $(SHELL) $(top_srcdir)/depcomp depcomp = $(SHELL) $(top_srcdir)/depcomp
Expand Down Expand Up @@ -86,9 +86,11 @@ DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
distdir = $(PACKAGE)-$(VERSION) distdir = $(PACKAGE)-$(VERSION)
top_distdir = $(distdir) top_distdir = $(distdir)
am__remove_distdir = \ am__remove_distdir = \
{ test ! -d "$(distdir)" \ if test -d "$(distdir)"; then \
|| { find "$(distdir)" -type d ! -perm -200 -exec chmod u+w {} ';' \ find "$(distdir)" -type d ! -perm -200 -exec chmod u+w {} ';' \
&& rm -fr "$(distdir)"; }; } && rm -rf "$(distdir)" \
|| { sleep 5 && rm -rf "$(distdir)"; }; \
else :; fi
am__relativize = \ am__relativize = \
dir0=`pwd`; \ dir0=`pwd`; \
sed_first='s,^\([^/]*\)/.*$$,\1,'; \ sed_first='s,^\([^/]*\)/.*$$,\1,'; \
Expand Down Expand Up @@ -222,7 +224,7 @@ INCLUDES = -I$(srcdir)/src\


#noinst_PROGRAMS=gingko_serv gingko_clnt serv_unittest clnt_unittest #noinst_PROGRAMS=gingko_serv gingko_clnt serv_unittest clnt_unittest
noinst_LIBRARIES = libgko.a noinst_LIBRARIES = libgko.a
libgko_a_SOURCES = async_threads.cpp async_conn.cpp \ libgko_a_SOURCES = async_threads.cpp async_conn.cpp async_clnt.cpp \
gingko_base.cpp log.cpp socket.cpp limit.cpp gingko_base.cpp log.cpp socket.cpp limit.cpp


libgko_a_LDFLAGS = -O0 -D_GNU_SOURCE -ggdb -funsigned-char libgko_a_LDFLAGS = -O0 -D_GNU_SOURCE -ggdb -funsigned-char
Expand Down Expand Up @@ -283,7 +285,7 @@ distclean-hdr:


clean-noinstLIBRARIES: clean-noinstLIBRARIES:
-test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES) -test -z "$(noinst_LIBRARIES)" || rm -f $(noinst_LIBRARIES)
libgko.a: $(libgko_a_OBJECTS) $(libgko_a_DEPENDENCIES) libgko.a: $(libgko_a_OBJECTS) $(libgko_a_DEPENDENCIES) $(EXTRA_libgko_a_DEPENDENCIES)
-rm -f libgko.a -rm -f libgko.a
$(libgko_a_AR) libgko.a $(libgko_a_OBJECTS) $(libgko_a_LIBADD) $(libgko_a_AR) libgko.a $(libgko_a_OBJECTS) $(libgko_a_LIBADD)
$(RANLIB) libgko.a $(RANLIB) libgko.a
Expand All @@ -294,6 +296,7 @@ mostlyclean-compile:
distclean-compile: distclean-compile:
-rm -f *.tab.c -rm -f *.tab.c


@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/async_clnt.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/async_conn.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/async_conn.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/async_threads.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/async_threads.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gingko_base.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/gingko_base.Po@am__quote@
Expand Down Expand Up @@ -525,6 +528,10 @@ dist-bzip2: distdir
tardir=$(distdir) && $(am__tar) | BZIP2=$${BZIP2--9} bzip2 -c >$(distdir).tar.bz2 tardir=$(distdir) && $(am__tar) | BZIP2=$${BZIP2--9} bzip2 -c >$(distdir).tar.bz2
$(am__remove_distdir) $(am__remove_distdir)


dist-lzip: distdir
tardir=$(distdir) && $(am__tar) | lzip -c $${LZIP_OPT--9} >$(distdir).tar.lz
$(am__remove_distdir)

dist-lzma: distdir dist-lzma: distdir
tardir=$(distdir) && $(am__tar) | lzma -9 -c >$(distdir).tar.lzma tardir=$(distdir) && $(am__tar) | lzma -9 -c >$(distdir).tar.lzma
$(am__remove_distdir) $(am__remove_distdir)
Expand Down Expand Up @@ -561,6 +568,8 @@ distcheck: dist
bzip2 -dc $(distdir).tar.bz2 | $(am__untar) ;;\ bzip2 -dc $(distdir).tar.bz2 | $(am__untar) ;;\
*.tar.lzma*) \ *.tar.lzma*) \
lzma -dc $(distdir).tar.lzma | $(am__untar) ;;\ lzma -dc $(distdir).tar.lzma | $(am__untar) ;;\
*.tar.lz*) \
lzip -dc $(distdir).tar.lz | $(am__untar) ;;\
*.tar.xz*) \ *.tar.xz*) \
xz -dc $(distdir).tar.xz | $(am__untar) ;;\ xz -dc $(distdir).tar.xz | $(am__untar) ;;\
*.tar.Z*) \ *.tar.Z*) \
Expand Down Expand Up @@ -747,8 +756,8 @@ uninstall-am:
.PHONY: $(RECURSIVE_CLEAN_TARGETS) $(RECURSIVE_TARGETS) CTAGS GTAGS \ .PHONY: $(RECURSIVE_CLEAN_TARGETS) $(RECURSIVE_TARGETS) CTAGS GTAGS \
all all-am am--refresh check check-am clean clean-generic \ all all-am am--refresh check check-am clean clean-generic \
clean-noinstLIBRARIES ctags ctags-recursive dist dist-all \ clean-noinstLIBRARIES ctags ctags-recursive dist dist-all \
dist-bzip2 dist-gzip dist-lzma dist-shar dist-tarZ dist-xz \ dist-bzip2 dist-gzip dist-lzip dist-lzma dist-shar dist-tarZ \
dist-zip distcheck distclean distclean-compile \ dist-xz dist-zip distcheck distclean distclean-compile \
distclean-generic distclean-hdr distclean-tags distcleancheck \ distclean-generic distclean-hdr distclean-tags distcleancheck \
distdir distuninstallcheck dvi dvi-am html html-am info \ distdir distuninstallcheck dvi dvi-am html html-am info \
info-am install install-am install-data install-data-am \ info-am install install-am install-data install-data-am \
Expand Down
15 changes: 8 additions & 7 deletions src/aclocal.m4
@@ -1,4 +1,4 @@
# generated automatically by aclocal 1.11.2 -*- Autoconf -*- # generated automatically by aclocal 1.11.3 -*- Autoconf -*-


# Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
# 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, # 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation,
Expand Down Expand Up @@ -38,7 +38,7 @@ AC_DEFUN([AM_AUTOMAKE_VERSION],
[am__api_version='1.11' [am__api_version='1.11'
dnl Some users find AM_AUTOMAKE_VERSION and mistake it for a way to dnl Some users find AM_AUTOMAKE_VERSION and mistake it for a way to
dnl require some minimum version. Point them to the right macro. dnl require some minimum version. Point them to the right macro.
m4_if([$1], [1.11.2], [], m4_if([$1], [1.11.3], [],
[AC_FATAL([Do not call $0, use AM_INIT_AUTOMAKE([$1]).])])dnl [AC_FATAL([Do not call $0, use AM_INIT_AUTOMAKE([$1]).])])dnl
]) ])


Expand All @@ -54,7 +54,7 @@ m4_define([_AM_AUTOCONF_VERSION], [])
# Call AM_AUTOMAKE_VERSION and AM_AUTOMAKE_VERSION so they can be traced. # Call AM_AUTOMAKE_VERSION and AM_AUTOMAKE_VERSION so they can be traced.
# This function is AC_REQUIREd by AM_INIT_AUTOMAKE. # This function is AC_REQUIREd by AM_INIT_AUTOMAKE.
AC_DEFUN([AM_SET_CURRENT_AUTOMAKE_VERSION], AC_DEFUN([AM_SET_CURRENT_AUTOMAKE_VERSION],
[AM_AUTOMAKE_VERSION([1.11.2])dnl [AM_AUTOMAKE_VERSION([1.11.3])dnl
m4_ifndef([AC_AUTOCONF_VERSION], m4_ifndef([AC_AUTOCONF_VERSION],
[m4_copy([m4_PACKAGE_VERSION], [AC_AUTOCONF_VERSION])])dnl [m4_copy([m4_PACKAGE_VERSION], [AC_AUTOCONF_VERSION])])dnl
_AM_AUTOCONF_VERSION(m4_defn([AC_AUTOCONF_VERSION]))]) _AM_AUTOCONF_VERSION(m4_defn([AC_AUTOCONF_VERSION]))])
Expand Down Expand Up @@ -886,7 +886,7 @@ AC_DEFUN([AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE($@)])


# Check how to create a tarball. -*- Autoconf -*- # Check how to create a tarball. -*- Autoconf -*-


# Copyright (C) 2004, 2005 Free Software Foundation, Inc. # Copyright (C) 2004, 2005, 2012 Free Software Foundation, Inc.
# #
# This file is free software; the Free Software Foundation # This file is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it, # gives unlimited permission to copy and/or distribute it,
Expand All @@ -908,10 +908,11 @@ AC_DEFUN([AM_SUBST_NOTMAKE], [_AM_SUBST_NOTMAKE($@)])
# a tarball read from stdin. # a tarball read from stdin.
# $(am__untar) < result.tar # $(am__untar) < result.tar
AC_DEFUN([_AM_PROG_TAR], AC_DEFUN([_AM_PROG_TAR],
[# Always define AMTAR for backward compatibility. [# Always define AMTAR for backward compatibility. Yes, it's still used
AM_MISSING_PROG([AMTAR], [tar]) # in the wild :-( We should find a proper way to deprecate it ...
AC_SUBST([AMTAR], ['$${TAR-tar}'])
m4_if([$1], [v7], m4_if([$1], [v7],
[am__tar='${AMTAR} chof - "$$tardir"'; am__untar='${AMTAR} xf -'], [am__tar='$${TAR-tar} chof - "$$tardir"' am__untar='$${TAR-tar} xf -'],
[m4_case([$1], [ustar],, [pax],, [m4_case([$1], [ustar],, [pax],,
[m4_fatal([Unknown tar format])]) [m4_fatal([Unknown tar format])])
AC_MSG_CHECKING([how to create a $1 tar archive]) AC_MSG_CHECKING([how to create a $1 tar archive])
Expand Down
110 changes: 103 additions & 7 deletions src/async_clnt.cpp
Expand Up @@ -5,36 +5,120 @@
* Author: auxten * Author: auxten
*/ */


#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netdb.h>

#include "config.h"
#include "gingko.h"
#include "log.h"
#include "socket.h"
#ifdef __APPLE__
#include <poll.h>
#else
#include <sys/poll.h>
#endif /** __APPLE__ **/

#include "async_pool.h" #include "async_pool.h"
#include "socket.h" #include "socket.h"


/**
* @brief non-blocking version connect
*
* @see
* @note
* @author auxten <auxtenwpc@gmail.com>
* @date Apr 22, 2012
**/
int nb_connect(const s_host_t * h, struct conn_client* conn)
{
int sock = -1;
int ret = -1;
struct sockaddr_in channel;
in_addr_t host;
int addr_len;

addr_len = getaddr_my(h->addr, &host);
if (FAIL_CHECK(!addr_len))
{
gko_log(WARNING, "gethostbyname %s error", h->addr);
ret = -1;
goto NB_CONNECT_END;
}
conn->client_addr = host;
sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if (FAIL_CHECK(sock < 0))
{
gko_log(WARNING, "get socket error");
ret = -1;
goto NB_CONNECT_END;
}

memset(&channel, 0, sizeof(channel));
channel.sin_family = AF_INET;
memcpy(&channel.sin_addr.s_addr, &host, addr_len);
channel.sin_port = htons(h->port);

/** set the connect non-blocking then blocking for add timeout on connect **/
if (FAIL_CHECK(setnonblock(sock) < 0))
{
gko_log(WARNING, "set socket non-blocking error");
ret = -1;
goto NB_CONNECT_END;
}

/** connect and send the msg **/
if (FAIL_CHECK(connect(sock, (struct sockaddr *) &channel, sizeof(channel)) &&
errno != EINPROGRESS))
{
gko_log(WARNING, "connect error");
ret = HOST_DOWN_FAIL;
goto NB_CONNECT_END;
}

ret = sock;

NB_CONNECT_END:
///
if (ret < 0 && sock >= 0)
{
close_socket(sock);
}
return ret;
}

int connect_hosts(const std::vector<s_host_t> & host_vec, int connect_hosts(const std::vector<s_host_t> & host_vec,
std::vector<struct conn_client> * conn_vec) std::vector<struct conn_client> * conn_vec)
{ {
struct conn_client conn;
memset(&conn, 0, sizeof(conn));
int conn_ok = 0; int conn_ok = 0;
int conn_err = 0; int conn_err = 0;


if (host_vec.size() <= 0 || conn_vec == NULL) if (host_vec.size() <= 0 || conn_vec == NULL)
{ {
return -1; return -1;
} }

conn_vec->clear();

/// perform non-blocking connect /// perform non-blocking connect
for (std::vector<s_host_t>::iterator it = host_vec.begin(); for (std::vector<s_host_t>::const_iterator it = host_vec.begin();
it != host_vec.end(); it != host_vec.end();
it++) it++)
{ {
int nb_conn_sock; int nb_conn_sock;
struct conn_client clnt; struct conn_client clnt;


clnt.client_addr = it-> /// connect and set in_addr_t to clnt->client_addr
nb_conn_sock = nb_connect(&(*it)); nb_conn_sock = nb_connect(&(*it), &clnt);
if (nb_conn_sock < 0) if (nb_conn_sock < 0)
{ {
char ip[18] = {'\0'};
clnt.client_fd = -1; clnt.client_fd = -1;
clnt.state = conn_connect_fail; clnt.state = conn_connect_fail;
conn_err++; conn_err++;
gko_log(DEBUG, "connect fail for %s:%d failed",
inet_neta(clnt.client_addr, ip, 17), clnt.client_port);
} }
else else
{ {
Expand All @@ -51,12 +135,24 @@ int connect_hosts(const std::vector<s_host_t> & host_vec,


int disconnect_hosts(std::vector<struct conn_client> & conn_vec) int disconnect_hosts(std::vector<struct conn_client> & conn_vec)
{ {
for(std::vector<struct conn_client>::iterator it = conn_vec.begin(); for (std::vector<struct conn_client>::iterator it = conn_vec.begin();
it != conn_vec.end(); it != conn_vec.end();
it++) it++)
{ {
close(it->client_fd) if (it->state != conn_closed)
{
char ip[18] = {'\0'};
if (close(it->client_fd) != 0)
gko_log(DEBUG, "close fd failed for %s:%d",
inet_neta(it->client_addr, ip, 17), it->client_port);
it->state = conn_closed;
}
} }
return 0;
}

int fill_request(const char * request, const int req_len, std::vector<struct conn_client> * conn_vec)
{


return 0; return 0;
} }
26 changes: 26 additions & 0 deletions src/async_clnt.h
@@ -0,0 +1,26 @@
/*
* aysnc_clnt.h
*
* Created on: May 4, 2012
* Author: auxten
*/

#ifndef AYSNC_CLNT_H_
#define AYSNC_CLNT_H_

/**
* @brief non-blocking version connect
*
* @see
* @note
* @author auxten <auxtenwpc@gmail.com>
* @date Apr 22, 2012
**/
int nb_connect(const s_host_t * h, struct conn_client* conn);

int connect_hosts(const std::vector<s_host_t> & host_vec,
std::vector<struct conn_client> * conn_vec);

int disconnect_hosts(std::vector<struct conn_client> & conn_vec);

#endif /* AYSNC_CLNT_H_ */
3 changes: 2 additions & 1 deletion src/async_pool.h
Expand Up @@ -59,6 +59,7 @@ enum conn_states {
conn_timeout, conn_timeout,
conn_reseted, conn_reseted,
conn_connect_fail, conn_connect_fail,
conn_closed,


conn_max_state /**< Max state value (used for assertion) */ conn_max_state /**< Max state value (used for assertion) */
}; };
Expand All @@ -68,7 +69,7 @@ struct conn_client
{ {
int id; int id;
int client_fd; int client_fd;
unsigned long client_addr; in_addr_t client_addr;
int client_port; int client_port;
unsigned int conn_time; unsigned int conn_time;
func_t handle_client; func_t handle_client;
Expand Down

0 comments on commit a3165ad

Please sign in to comment.