码迷,mamicode.com
首页 > 其他好文 > 详细

一种简单的RPC框架

时间:2014-10-26 21:18:56      阅读:296      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   os   ar   for   strong   sp   

一 系统模型

bubuko.com,布布扣

二、数据库代码实现

1. mkdir database

    cd database

    vim dbInit.c

/* 
 * * Database Init tool 
 * */  
#include <stdio.h>  
#include <stdlib.h>  
#include <sqlite3.h>

sqlite3 *db;  
/* 
 * * return 0 if database can be created  successfully else return -1 
 * */  
int InitDatabase() {  
	int rc;   
	char *sql;  
	char *zErrMsg = NULL;    // error string   
	rc = sqlite3_open("example.db", &db);  // open or create a database  
    if(rc) {  
		perror("can not create or open da660.db...!\n");  
    }  
	else {  
		/* 
		 * * create baseinfo table 
		 * */  
		sql = "CREATE TABLE simpleTable(key INTEGER, value INTEGER, primary key(key));";    
        rc = sqlite3_exec(db, sql, 0, 0, &zErrMsg);   // create data table  
        if(rc != SQLITE_OK) {  
			perror("create baseinfo table failed... !\n");  
			puts(zErrMsg);
		}  
        if(!rc) {  
			printf("Database Init Successfully!\n");  
        }  
	}  
	/* 
	 * * never to forget to close the db 
	 * */  
	sqlite3_close(db);  

    return rc;  
}
int main() {  
	if(InitDatabase()) {  
		perror("Data Init failed!\n");  
        exit(EXIT_FAILURE);  
    }  

	return 0;  
}  

3. 在当前目录下 autoscan

   sudo mv configure.scan configure.in

   vim configure.in

#                                               -*- Autoconf -*-
# Process this file with autoconf to produce a configure script.

AC_PREREQ([2.68])
#AC_INIT([FULL-PACKAGE-NAME], [VERSION], [BUG-REPORT-ADDRESS])
AC_INIT(dbInit.c)
AM_INIT_AUTOMAKE(dbInit, 1.0)
#AC_CONFIG_SRCDIR([dbInit.c])
#AC_CONFIG_HEADERS([config.h])

# Checks for programs.
AC_PROG_CC

# Checks for libraries.

# Checks for header files.
#AC_CHECK_HEADERS([stdlib.h])

# Checks for typedefs, structures, and compiler characteristics.

# Checks for library functions.

AC_OUTPUT(Makefile)

aclocal

vim Makefile.am

AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=dbInit
dbInit_SOURCES=dbInit.c

autoconf

automake --add-missing

./configure

vim Makefile

# Makefile.in generated by automake 1.11.3 from Makefile.am.
# Makefile.  Generated from Makefile.in by configure.

# Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
# 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software
# Foundation, Inc.
# This Makefile.in is free software; the Free Software Foundation
# gives unlimited permission to copy and/or distribute it,
# with or without modifications, as long as this notice is preserved.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY, to the extent permitted by law; without
# even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE.




pkgdatadir = $(datadir)/dbInit
pkgincludedir = $(includedir)/dbInit
pkglibdir = $(libdir)/dbInit
pkglibexecdir = $(libexecdir)/dbInit
am__cd = CDPATH="$${ZSH_VERSION+.}$(PATH_SEPARATOR)" && cd
install_sh_DATA = $(install_sh) -c -m 644
install_sh_PROGRAM = $(install_sh) -c
install_sh_SCRIPT = $(install_sh) -c
INSTALL_HEADER = $(INSTALL_DATA)
transform = $(program_transform_name)
NORMAL_INSTALL = :
PRE_INSTALL = :
POST_INSTALL = :
NORMAL_UNINSTALL = :
PRE_UNINSTALL = :
POST_UNINSTALL = :
bin_PROGRAMS = dbInit$(EXEEXT)
subdir = .
DIST_COMMON = $(am__configure_deps) $(srcdir)/Makefile.am 	$(srcdir)/Makefile.in $(top_srcdir)/configure depcomp 	install-sh missing
ACLOCAL_M4 = $(top_srcdir)/aclocal.m4
am__aclocal_m4_deps = $(top_srcdir)/configure.in
am__configure_deps = $(am__aclocal_m4_deps) $(CONFIGURE_DEPENDENCIES) 	$(ACLOCAL_M4)
am__CONFIG_DISTCLEAN_FILES = config.status config.cache config.log  configure.lineno config.status.lineno
mkinstalldirs = $(install_sh) -d
CONFIG_CLEAN_FILES =
CONFIG_CLEAN_VPATH_FILES =
am__installdirs = "$(DESTDIR)$(bindir)"
PROGRAMS = $(bin_PROGRAMS)
am_dbInit_OBJECTS = dbInit.$(OBJEXT)
dbInit_OBJECTS = $(am_dbInit_OBJECTS)
dbInit_LDADD = $(LDADD)
DEFAULT_INCLUDES = -I.
depcomp = $(SHELL) $(top_srcdir)/depcomp
am__depfiles_maybe = depfiles
am__mv = mv -f
COMPILE = $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) 	$(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS)
CCLD = $(CC)
LINK = $(CCLD) $(AM_CFLAGS) $(CFLAGS) $(AM_LDFLAGS) $(LDFLAGS) -o $@ 
SOURCES = $(dbInit_SOURCES)
DIST_SOURCES = $(dbInit_SOURCES)
ETAGS = etags
CTAGS = ctags
DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST)
distdir = $(PACKAGE)-$(VERSION)
top_distdir = $(distdir)
am__remove_distdir =   if test -d "$(distdir)"; then     find "$(distdir)" -type d ! -perm -200 -exec chmod u+w {} ';'       && rm -rf "$(distdir)"       || { sleep 5 && rm -rf "$(distdir)"; };   else :; fi
DIST_ARCHIVES = $(distdir).tar.gz
GZIP_ENV = --best
distuninstallcheck_listfiles = find . -type f -print
am__distuninstallcheck_listfiles = $(distuninstallcheck_listfiles)   | sed 's|^\./|$(prefix)/|' | grep -v '$(infodir)/dir$$'
distcleancheck_listfiles = find . -type f -print
ACLOCAL = ${SHELL} /home/wangzhicheng/C/RPC/simpleRPC/database/missing --run aclocal-1.11
AMTAR = $${TAR-tar}
AUTOCONF = ${SHELL} /home/wangzhicheng/C/RPC/simpleRPC/database/missing --run autoconf
AUTOHEADER = ${SHELL} /home/wangzhicheng/C/RPC/simpleRPC/database/missing --run autoheader
AUTOMAKE = ${SHELL} /home/wangzhicheng/C/RPC/simpleRPC/database/missing --run automake-1.11
AWK = mawk
CC = gcc
CCDEPMODE = depmode=gcc3
CFLAGS = -g -O2 
CPPFLAGS = 
CYGPATH_W = echo
DEFS = -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DPACKAGE_URL=\"\" -DPACKAGE=\"dbInit\" -DVERSION=\"1.0\"
DEPDIR = .deps
ECHO_C = 
ECHO_N = -n
ECHO_T = 
EXEEXT = 
INSTALL = /usr/bin/install -c
INSTALL_DATA = ${INSTALL} -m 644
INSTALL_PROGRAM = ${INSTALL}
INSTALL_SCRIPT = ${INSTALL}
INSTALL_STRIP_PROGRAM = $(install_sh) -c -s
LDFLAGS = 
LIBOBJS = 
LIBS = -lpthread -lsqlite3
LTLIBOBJS = 
MAKEINFO = ${SHELL} /home/wangzhicheng/C/RPC/simpleRPC/database/missing --run makeinfo
MKDIR_P = /bin/mkdir -p
OBJEXT = o
PACKAGE = dbInit 
PACKAGE_BUGREPORT = 
PACKAGE_NAME = 
PACKAGE_STRING = 
PACKAGE_TARNAME = 
PACKAGE_URL = 
PACKAGE_VERSION = 
PATH_SEPARATOR = :
SET_MAKE = 
SHELL = /bin/bash
STRIP = 
VERSION = 1.0
abs_builddir = /home/wangzhicheng/C/RPC/simpleRPC/database
abs_srcdir = /home/wangzhicheng/C/RPC/simpleRPC/database
abs_top_builddir = /home/wangzhicheng/C/RPC/simpleRPC/database
abs_top_srcdir = /home/wangzhicheng/C/RPC/simpleRPC/database
ac_ct_CC = gcc
am__include = include
am__leading_dot = .
am__quote = 
am__tar = $${TAR-tar} chof - "$$tardir"
am__untar = $${TAR-tar} xf -
bindir = ${exec_prefix}/bin
build_alias = 
builddir = .
datadir = ${datarootdir}
datarootdir = ${prefix}/share
docdir = ${datarootdir}/doc/${PACKAGE}
dvidir = ${docdir}
exec_prefix = ${prefix}
host_alias = 
htmldir = ${docdir}
includedir = ${prefix}/include
infodir = ${datarootdir}/info
install_sh = ${SHELL} /home/wangzhicheng/C/RPC/simpleRPC/database/install-sh
libdir = ${exec_prefix}/lib 
libexecdir = ${exec_prefix}/libexec
localedir = ${datarootdir}/locale
localstatedir = ${prefix}/var
mandir = ${datarootdir}/man
mkdir_p = /bin/mkdir -p
oldincludedir = /usr/include
pdfdir = ${docdir}
prefix = /usr/local
program_transform_name = s,x,x,
psdir = ${docdir}
sbindir = ${exec_prefix}/sbin
sharedstatedir = ${prefix}/com
srcdir = .
sysconfdir = ${prefix}/etc
target_alias = 
top_build_prefix = 
top_builddir = .
top_srcdir = .
AUTOMAKE_OPTIONS = foreign
dbInit_SOURCES = dbInit.c -lpthread -lsqlite3
all: all-am

.SUFFIXES:
.SUFFIXES: .c .o .obj
am--refresh: Makefile
	@:
$(srcdir)/Makefile.in:  $(srcdir)/Makefile.am  $(am__configure_deps)
	@for dep in $?; do 	  case '$(am__configure_deps)' in 	    *$$dep*) 	      echo ' cd $(srcdir) && $(AUTOMAKE) --foreign'; 	      $(am__cd) $(srcdir) && $(AUTOMAKE) --foreign 		&& exit 0; 	      exit 1;; 	  esac; 	done; 	echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign Makefile'; 	$(am__cd) $(top_srcdir) && 	  $(AUTOMAKE) --foreign Makefile
.PRECIOUS: Makefile
Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status
	@case '$?' in 	  *config.status*) 	    echo ' $(SHELL) ./config.status'; 	    $(SHELL) ./config.status;; 	  *) 	    echo ' cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe)'; 	    cd $(top_builddir) && $(SHELL) ./config.status $@ $(am__depfiles_maybe);; 	esac;

$(top_builddir)/config.status: $(top_srcdir)/configure $(CONFIG_STATUS_DEPENDENCIES)
	$(SHELL) ./config.status --recheck

$(top_srcdir)/configure:  $(am__configure_deps)
	$(am__cd) $(srcdir) && $(AUTOCONF)
$(ACLOCAL_M4):  $(am__aclocal_m4_deps)
	$(am__cd) $(srcdir) && $(ACLOCAL) $(ACLOCAL_AMFLAGS)
$(am__aclocal_m4_deps):
install-binPROGRAMS: $(bin_PROGRAMS)
	@$(NORMAL_INSTALL)
	test -z "$(bindir)" || $(MKDIR_P) "$(DESTDIR)$(bindir)"
	@list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; 	for p in $$list; do echo "$$p $$p"; done | 	sed 's/$(EXEEXT)$$//' | 	while read p p1; do if test -f $$p; 	  then echo "$$p"; echo "$$p"; else :; fi; 	done | 	sed -e 'p;s,.*/,,;n;h' -e 's|.*|.|' 	    -e 'p;x;s,.*/,,;s/$(EXEEXT)$$//;$(transform);s/$$/$(EXEEXT)/' | 	sed 'N;N;N;s,\n, ,g' | 	$(AWK) 'BEGIN { files["."] = ""; dirs["."] = 1 } 	  { d=$$3; if (dirs[d] != 1) { print "d", d; dirs[d] = 1 } 	    if ($$2 == $$4) files[d] = files[d] " " $$1; 	    else { print "f", $$3 "/" $$4, $$1; } } 	  END { for (d in files) print "f", d, files[d] }' | 	while read type dir files; do 	    if test "$$dir" = .; then dir=; else dir=/$$dir; fi; 	    test -z "$$files" || { 	      echo " $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files '$(DESTDIR)$(bindir)$$dir'"; 	      $(INSTALL_PROGRAM_ENV) $(INSTALL_PROGRAM) $$files "$(DESTDIR)$(bindir)$$dir" || exit $$?; 	    } 	; done

uninstall-binPROGRAMS:
	@$(NORMAL_UNINSTALL)
	@list='$(bin_PROGRAMS)'; test -n "$(bindir)" || list=; 	files=`for p in $$list; do echo "$$p"; done | 	  sed -e 'h;s,^.*/,,;s/$(EXEEXT)$$//;$(transform)' 	      -e 's/$$/$(EXEEXT)/' `; 	test -n "$$list" || exit 0; 	echo " ( cd '$(DESTDIR)$(bindir)' && rm -f" $$files ")"; 	cd "$(DESTDIR)$(bindir)" && rm -f $$files

clean-binPROGRAMS:
	-test -z "$(bin_PROGRAMS)" || rm -f $(bin_PROGRAMS)
dbInit$(EXEEXT): $(dbInit_OBJECTS) $(dbInit_DEPENDENCIES) $(EXTRA_dbInit_DEPENDENCIES) 
	@rm -f dbInit$(EXEEXT)
	$(LINK) $(dbInit_OBJECTS) $(dbInit_LDADD) $(LIBS)

mostlyclean-compile:
	-rm -f *.$(OBJEXT)

distclean-compile:
	-rm -f *.tab.c

include ./$(DEPDIR)/dbInit.Po

.c.o:
	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ $<
	$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
#	source='$<' object='$@' libtool=no #	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) #	$(COMPILE) -c $<

.c.obj:
	$(COMPILE) -MT $@ -MD -MP -MF $(DEPDIR)/$*.Tpo -c -o $@ `$(CYGPATH_W) '$<'`
	$(am__mv) $(DEPDIR)/$*.Tpo $(DEPDIR)/$*.Po
#	source='$<' object='$@' libtool=no #	DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) #	$(COMPILE) -c `$(CYGPATH_W) '$<'`

ID: $(HEADERS) $(SOURCES) $(LISP) $(TAGS_FILES)
	list='$(SOURCES) $(HEADERS) $(LISP) $(TAGS_FILES)'; 	unique=`for i in $$list; do 	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; 	  done | 	  $(AWK) '{ files[$$0] = 1; nonempty = 1; } 	      END { if (nonempty) { for (i in files) print i; }; }'`; 	mkid -fID $$unique
tags: TAGS

TAGS:  $(HEADERS) $(SOURCES)  $(TAGS_DEPENDENCIES) 		$(TAGS_FILES) $(LISP)
	set x; 	here=`pwd`; 	list='$(SOURCES) $(HEADERS)  $(LISP) $(TAGS_FILES)'; 	unique=`for i in $$list; do 	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; 	  done | 	  $(AWK) '{ files[$$0] = 1; nonempty = 1; } 	      END { if (nonempty) { for (i in files) print i; }; }'`; 	shift; 	if test -z "$(ETAGS_ARGS)$$*$$unique"; then :; else 	  test -n "$$unique" || unique=$$empty_fix; 	  if test $$# -gt 0; then 	    $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) 	      "$$@" $$unique; 	  else 	    $(ETAGS) $(ETAGSFLAGS) $(AM_ETAGSFLAGS) $(ETAGS_ARGS) 	      $$unique; 	  fi; 	fi
ctags: CTAGS
CTAGS:  $(HEADERS) $(SOURCES)  $(TAGS_DEPENDENCIES) 		$(TAGS_FILES) $(LISP)
	list='$(SOURCES) $(HEADERS)  $(LISP) $(TAGS_FILES)'; 	unique=`for i in $$list; do 	    if test -f "$$i"; then echo $$i; else echo $(srcdir)/$$i; fi; 	  done | 	  $(AWK) '{ files[$$0] = 1; nonempty = 1; } 	      END { if (nonempty) { for (i in files) print i; }; }'`; 	test -z "$(CTAGS_ARGS)$$unique" 	  || $(CTAGS) $(CTAGSFLAGS) $(AM_CTAGSFLAGS) $(CTAGS_ARGS) 	     $$unique

GTAGS:
	here=`$(am__cd) $(top_builddir) && pwd` 	  && $(am__cd) $(top_srcdir) 	  && gtags -i $(GTAGS_ARGS) "$$here"

distclean-tags:
	-rm -f TAGS ID GTAGS GRTAGS GSYMS GPATH tags

distdir: $(DISTFILES)
	$(am__remove_distdir)
	test -d "$(distdir)" || mkdir "$(distdir)"
	@srcdirstrip=`echo "$(srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; 	topsrcdirstrip=`echo "$(top_srcdir)" | sed 's/[].[^$$\\*]/\\\\&/g'`; 	list='$(DISTFILES)'; 	  dist_files=`for file in $$list; do echo $$file; done | 	  sed -e "s|^$$srcdirstrip/||;t" 	      -e "s|^$$topsrcdirstrip/|$(top_builddir)/|;t"`; 	case $$dist_files in 	  */*) $(MKDIR_P) `echo "$$dist_files" | 			   sed '/\//!d;s|^|$(distdir)/|;s,/[^/]*$$,,' | 			   sort -u` ;; 	esac; 	for file in $$dist_files; do 	  if test -f $$file || test -d $$file; then d=.; else d=$(srcdir); fi; 	  if test -d $$d/$$file; then 	    dir=`echo "/$$file" | sed -e 's,/[^/]*$$,,'`; 	    if test -d "$(distdir)/$$file"; then 	      find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; 	    fi; 	    if test -d $(srcdir)/$$file && test $$d != $(srcdir); then 	      cp -fpR $(srcdir)/$$file "$(distdir)$$dir" || exit 1; 	      find "$(distdir)/$$file" -type d ! -perm -700 -exec chmod u+rwx {} \;; 	    fi; 	    cp -fpR $$d/$$file "$(distdir)$$dir" || exit 1; 	  else 	    test -f "$(distdir)/$$file" 	    || cp -p $$d/$$file "$(distdir)/$$file" 	    || exit 1; 	  fi; 	done
	-test -n "$(am__skip_mode_fix)" 	|| find "$(distdir)" -type d ! -perm -755 		-exec chmod u+rwx,go+rx {} \; -o 	  ! -type d ! -perm -444 -links 1 -exec chmod a+r {} \; -o 	  ! -type d ! -perm -400 -exec chmod a+r {} \; -o 	  ! -type d ! -perm -444 -exec $(install_sh) -c -m a+r {} {} \; 	|| chmod -R a+r "$(distdir)"
dist-gzip: distdir
	tardir=$(distdir) && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz
	$(am__remove_distdir)

dist-bzip2: distdir
	tardir=$(distdir) && $(am__tar) | BZIP2=$${BZIP2--9} bzip2 -c >$(distdir).tar.bz2
	$(am__remove_distdir)

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

dist-lzma: distdir
	tardir=$(distdir) && $(am__tar) | lzma -9 -c >$(distdir).tar.lzma
	$(am__remove_distdir)

dist-xz: distdir
	tardir=$(distdir) && $(am__tar) | XZ_OPT=$${XZ_OPT--e} xz -c >$(distdir).tar.xz
	$(am__remove_distdir)

dist-tarZ: distdir
	tardir=$(distdir) && $(am__tar) | compress -c >$(distdir).tar.Z
	$(am__remove_distdir)

dist-shar: distdir
	shar $(distdir) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).shar.gz
	$(am__remove_distdir)

dist-zip: distdir
	-rm -f $(distdir).zip
	zip -rq $(distdir).zip $(distdir)
	$(am__remove_distdir)

dist dist-all: distdir
	tardir=$(distdir) && $(am__tar) | GZIP=$(GZIP_ENV) gzip -c >$(distdir).tar.gz
	$(am__remove_distdir)

# This target untars the dist file and tries a VPATH configuration.  Then
# it guarantees that the distribution is self-contained by making another
# tarfile.
distcheck: dist
	case '$(DIST_ARCHIVES)' in 	*.tar.gz*) 	  GZIP=$(GZIP_ENV) gzip -dc $(distdir).tar.gz | $(am__untar) ;;	*.tar.bz2*) 	  bzip2 -dc $(distdir).tar.bz2 | $(am__untar) ;;	*.tar.lzma*) 	  lzma -dc $(distdir).tar.lzma | $(am__untar) ;;	*.tar.lz*) 	  lzip -dc $(distdir).tar.lz | $(am__untar) ;;	*.tar.xz*) 	  xz -dc $(distdir).tar.xz | $(am__untar) ;;	*.tar.Z*) 	  uncompress -c $(distdir).tar.Z | $(am__untar) ;;	*.shar.gz*) 	  GZIP=$(GZIP_ENV) gzip -dc $(distdir).shar.gz | unshar ;;	*.zip*) 	  unzip $(distdir).zip ;;	esac
	chmod -R a-w $(distdir); chmod a+w $(distdir)
	mkdir $(distdir)/_build
	mkdir $(distdir)/_inst
	chmod a-w $(distdir)
	test -d $(distdir)/_build || exit 0; 	dc_install_base=`$(am__cd) $(distdir)/_inst && pwd | sed -e 's,^[^:\\/]:[\\/],/,'` 	  && dc_destdir="$${TMPDIR-/tmp}/am-dc-$$$$/" 	  && am__cwd=`pwd` 	  && $(am__cd) $(distdir)/_build 	  && ../configure --srcdir=.. --prefix="$$dc_install_base" 	    $(AM_DISTCHECK_CONFIGURE_FLAGS) 	    $(DISTCHECK_CONFIGURE_FLAGS) 	  && $(MAKE) $(AM_MAKEFLAGS) 	  && $(MAKE) $(AM_MAKEFLAGS) dvi 	  && $(MAKE) $(AM_MAKEFLAGS) check 	  && $(MAKE) $(AM_MAKEFLAGS) install 	  && $(MAKE) $(AM_MAKEFLAGS) installcheck 	  && $(MAKE) $(AM_MAKEFLAGS) uninstall 	  && $(MAKE) $(AM_MAKEFLAGS) distuninstallcheck_dir="$$dc_install_base" 	        distuninstallcheck 	  && chmod -R a-w "$$dc_install_base" 	  && ({ 	       (cd ../.. && umask 077 && mkdir "$$dc_destdir") 	       && $(MAKE) $(AM_MAKEFLAGS) DESTDIR="$$dc_destdir" install 	       && $(MAKE) $(AM_MAKEFLAGS) DESTDIR="$$dc_destdir" uninstall 	       && $(MAKE) $(AM_MAKEFLAGS) DESTDIR="$$dc_destdir" 	            distuninstallcheck_dir="$$dc_destdir" distuninstallcheck; 	      } || { rm -rf "$$dc_destdir"; exit 1; }) 	  && rm -rf "$$dc_destdir" 	  && $(MAKE) $(AM_MAKEFLAGS) dist 	  && rm -rf $(DIST_ARCHIVES) 	  && $(MAKE) $(AM_MAKEFLAGS) distcleancheck 	  && cd "$$am__cwd" 	  || exit 1
	$(am__remove_distdir)
	@(echo "$(distdir) archives ready for distribution: "; 	  list='$(DIST_ARCHIVES)'; for i in $$list; do echo $$i; done) | 	  sed -e 1h -e 1s/./=/g -e 1p -e 1x -e '$$p' -e '$$x'
distuninstallcheck:
	@test -n '$(distuninstallcheck_dir)' || { 	  echo 'ERROR: trying to run $@ with an empty' 	       '$$(distuninstallcheck_dir)' >&2; 	  exit 1; 	}; 	$(am__cd) '$(distuninstallcheck_dir)' || { 	  echo 'ERROR: cannot chdir into $(distuninstallcheck_dir)' >&2; 	  exit 1; 	}; 	test `$(am__distuninstallcheck_listfiles) | wc -l` -eq 0 	   || { echo "ERROR: files left after uninstall:" ; 	        if test -n "$(DESTDIR)"; then 	          echo "  (check DESTDIR support)"; 	        fi ; 	        $(distuninstallcheck_listfiles) ; 	        exit 1; } >&2
distcleancheck: distclean
	@if test '$(srcdir)' = . ; then 	  echo "ERROR: distcleancheck can only run from a VPATH build" ; 	  exit 1 ; 	fi
	@test `$(distcleancheck_listfiles) | wc -l` -eq 0 	  || { echo "ERROR: files left in build directory after distclean:" ; 	       $(distcleancheck_listfiles) ; 	       exit 1; } >&2
check-am: all-am
check: check-am
all-am: Makefile $(PROGRAMS)
installdirs:
	for dir in "$(DESTDIR)$(bindir)"; do 	  test -z "$$dir" || $(MKDIR_P) "$$dir"; 	done
install: install-am
install-exec: install-exec-am
install-data: install-data-am
uninstall: uninstall-am

install-am: all-am
	@$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am

installcheck: installcheck-am
install-strip:
	if test -z '$(STRIP)'; then 	  $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" 	    install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s 	      install; 	else 	  $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" 	    install_sh_PROGRAM="$(INSTALL_STRIP_PROGRAM)" INSTALL_STRIP_FLAG=-s 	    "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'" install; 	fi
mostlyclean-generic:

clean-generic:

distclean-generic:
	-test -z "$(CONFIG_CLEAN_FILES)" || rm -f $(CONFIG_CLEAN_FILES)
	-test . = "$(srcdir)" || test -z "$(CONFIG_CLEAN_VPATH_FILES)" || rm -f $(CONFIG_CLEAN_VPATH_FILES)

maintainer-clean-generic:
	@echo "This command is intended for maintainers to use"
	@echo "it deletes files that may require special tools to rebuild."
clean: clean-am

clean-am: clean-binPROGRAMS clean-generic mostlyclean-am

distclean: distclean-am
	-rm -f $(am__CONFIG_DISTCLEAN_FILES)
	-rm -rf ./$(DEPDIR)
	-rm -f Makefile
distclean-am: clean-am distclean-compile distclean-generic 	distclean-tags

dvi: dvi-am

dvi-am:

html: html-am

html-am:

info: info-am

info-am:

install-data-am:

install-dvi: install-dvi-am

install-dvi-am:

install-exec-am: install-binPROGRAMS

install-html: install-html-am

install-html-am:

install-info: install-info-am

install-info-am:

install-man:

install-pdf: install-pdf-am

install-pdf-am:

install-ps: install-ps-am

install-ps-am:

installcheck-am:

maintainer-clean: maintainer-clean-am
	-rm -f $(am__CONFIG_DISTCLEAN_FILES)
	-rm -rf $(top_srcdir)/autom4te.cache
	-rm -rf ./$(DEPDIR)
	-rm -f Makefile
maintainer-clean-am: distclean-am maintainer-clean-generic

mostlyclean: mostlyclean-am

mostlyclean-am: mostlyclean-compile mostlyclean-generic

pdf: pdf-am

pdf-am:

ps: ps-am

ps-am:

uninstall-am: uninstall-binPROGRAMS

.MAKE: install-am install-strip

.PHONY: CTAGS GTAGS all all-am am--refresh check check-am clean 	clean-binPROGRAMS clean-generic ctags dist dist-all dist-bzip2 	dist-gzip dist-lzip dist-lzma dist-shar dist-tarZ dist-xz 	dist-zip distcheck distclean distclean-compile 	distclean-generic distclean-tags distcleancheck distdir 	distuninstallcheck dvi dvi-am html html-am info info-am 	install install-am install-binPROGRAMS install-data 	install-data-am install-dvi install-dvi-am install-exec 	install-exec-am install-html install-html-am install-info 	install-info-am install-man install-pdf install-pdf-am 	install-ps install-ps-am install-strip installcheck 	installcheck-am installdirs maintainer-clean 	maintainer-clean-generic mostlyclean mostlyclean-compile 	mostlyclean-generic pdf pdf-am ps ps-am tags uninstall 	uninstall-am uninstall-binPROGRAMS


# Tell versions [3.59,3.63) of GNU make to not export all variables.
# Otherwise a system limit (for SysV at least) may be exceeded.
.NOEXPORT:

在修改Makefile时,最重要的是在LIBS中增加 -lpthread -lsqlite3

make

生成可执行文件 dbInit,用于初始化数据库

三 远程调用实现

mkdir rpc

cd rpc

vim rpc.x

struct record {
	int key;
	int value;
};
program SEVERPROG {
   version VERSION {
     int QUERY(int) = 1;
	 int INSERT(record) = 2;
	 int DELETE(int) = 3;
   } = 1;
} = 87654321;

rpcgen -C rpc.x

rpcgen -Ss -o rpc_srv_func.c rpc.x

rpcgen -Sc -o rpc_cln_func.c rpc.x

rpcgen -Sm rpc.x  > Makefile

vim rpc_srv_func.c

/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include <sqlite3.h>
#include "rpc.h"
#define SIZE 128
sqlite3 *db = NULL;
int *
query_1_svc(int *argp, struct svc_req *rqstp)
{
	static int  result;
	/*
	 * insert server code here
	 */
	int rc;  
    int nRow, nColumn;  
    int row, col;  
    int index;  
    int key = *argp, value;   
    char *zErrMsg = NULL;  
    char **dbResult;			 // point to two dimension table  
    char select_sql[SIZE];  
    char *domain;				// the attribute name  of query table  
    /* 
     * to open database 
     * */  
    rc = sqlite3_open("example.db", &db);  
    if(rc != SQLITE_OK) {  
        perror("example.db open failed...!\n");  
        rc = -1;  
    }  
    else {  
        /* 
         * to select data record 
         * */  
        memset(select_sql, '\0', sizeof(select_sql));  
        sprintf(select_sql, "select * from simpleTable where key = %d", key);  
        /* 
         * to get the two dimension table, nRow means rows and nColumn means cols 
         * */  
        rc = sqlite3_get_table(db, select_sql, &dbResult, &nRow, &nColumn, &zErrMsg);  
        if(rc != SQLITE_OK) {  
            perror("database query failed!\n");  
            rc = -1;  
        }  
        else {  
            /* 
             * query statement 
             * */  
            index = nColumn;   // the first value should start from nColum  
            for(row = 0;row < nRow;row++) {  
                printf("-------the %d record----------\n", row + 1);  
                for(col = 0;col < nColumn;col++) {  
                    domain = dbResult[col];  
                    value = atoi(dbResult[index]);  
                    printf("\t%-5s:", domain);  
                    printf("%d\n", value);  
                    index++;  
                }  
            }  
        }  
    }  
    sqlite3_free_table(dbResult);  // never to forget to free the query table  
    sqlite3_close(db);  
	result = rc;

	return &result;
}

int *
insert_1_svc(record *argp, struct svc_req *rqstp)
{
	static int  result;
	/*
	 * insert server code here
	 */
    int rc;  
    char *zErrMsg = NULL;  
    char insert_sql[SIZE];  
	int key, value;
	key = argp->key;
	value = argp->value;
    /* 
     * to open da660.db  
     * */  
    rc = sqlite3_open("example.db", &db);  
    if(rc) {  
        perror("example.db open failed...!\n");  
        rc = -1;  
    }  
    else {  
        /* 
         * insert data record 
         * */  
        memset(insert_sql, '\0', sizeof(insert_sql));  
        sprintf(insert_sql, "INSERT INTO simpleTable(key, value) VALUES(%u, %u)", key, value);  
        rc = sqlite3_exec(db, insert_sql, 0, 0, &zErrMsg); // execute sql statement and insert data record  
        if(rc == SQLITE_OK) {  
            printf("insert one data record success...!\n");  
        }  
        else {  
            perror("insert one data record failure...!\n");  
            rc = -1;  
        }  
        sqlite3_close(db);  // never to forget to close the db  
    }  
    result = rc;  

	return &result;
}

int *
delete_1_svc(int *argp, struct svc_req *rqstp)
{
	static int  result;
	/*
	 * insert server code here
	 */
    int rc;  
	int key = *argp;
    char *zErrMsg = NULL;   
    char delete_sql[SIZE];  
    /* 
     * to open da660.db  
     * */  
    rc = sqlite3_open("example.db", &db);  
    if(rc) {  
        perror("example.db open failed...!\n");  
        rc = -1;  
    }  
    else {  
		sprintf(delete_sql, "delete from baseinfo where port = %d", key);  
		rc = sqlite3_exec(db, delete_sql, 0, 0, &zErrMsg); // execute sql statement and delete data record  
		if(rc == SQLITE_OK) {  
			printf("delete one data record success...!\n");  
        }  
		else {  
			perror("delete one data record failure...!\n");  
			rc = -1;  
        }  
    }  
    sqlite3_close(db);  // never to forget to close the db    
	result = rc;  

	return &result;
}

vim rpc_cln_func.c

/*
 * This is sample code generated by rpcgen.
 * These are only templates and you can use them
 * as a guideline for developing your own functions.
 */

#include "rpc.h"


void
severprog_1(char *host)
{
	CLIENT *clnt;
	int  *result_1;
	int  query_1_arg;
	int  *result_2;
	record  insert_1_arg;
	int  *result_3;
	int  delete_1_arg;

#ifndef	DEBUG
	clnt = clnt_create (host, SEVERPROG, VERSION, "udp");
	if (clnt == NULL) {
		clnt_pcreateerror (host);
		exit (1);
	}
#endif	/* DEBUG */

/*	result_1 = query_1(&query_1_arg, clnt);
	if (result_1 == (int *) NULL) {
		clnt_perror (clnt, "call failed");
	}
	result_2 = insert_1(&insert_1_arg, clnt);
	if (result_2 == (int *) NULL) {
		clnt_perror (clnt, "call failed");
	}
	result_3 = delete_1(&delete_1_arg, clnt);
	if (result_3 == (int *) NULL) {
		clnt_perror (clnt, "call failed");
	}
	*/
	int choice;
	int key, value;
	record M;
	printf("\t%10s", "Welcome to System...!\n");  
    printf("\t%10s", "0 -- query data record from simpleTable...\n");  
    printf("\t%10s", "1 -- insert data record to simpleTable...\n");  
    printf("\t%10s", "2 -- delete data record from simpleTable...\n");  
    printf("\t%10s", "3 -- quit the system...\n"); 
	printf("please make your choice:");  
    if(scanf("%d", &choice) != 1) {  
		perror("input data error, system crash...!\n");  
		exit(EXIT_FAILURE);  
    }  
	if(!(choice <= 3 && choice >= 0)) {  
		perror("Sorry, You have made wrong choice...!\n");  
		exit(EXIT_FAILURE);
    }  
	switch(choice) {
		case 0:
			printf("please input key:");  
			if(scanf("%d", &key) != 1) {  
				perror("input data error, system crash...!\n");  
				exit(EXIT_FAILURE);  
            }  
			query_1_arg = key;
			result_1 = query_1(&query_1_arg, clnt);
			if (result_1 == (int *) NULL) {
				clnt_perror (clnt, "data query failed...!\n");
			}
			break;
		case 1:
			printf("please input key:");  
			if(scanf("%d", &key) != 1) {  
				perror("input data error, system crash...!\n");  
				exit(EXIT_FAILURE);  
            }  
			printf("please input value:");  
			if(scanf("%d", &value) != 1) {  
				perror("input data error, system crash...!\n");  
				exit(EXIT_FAILURE);  
            }  
			M.key = key;
			M.value = value;
			insert_1_arg = M;
			result_2 = insert_1(&insert_1_arg, clnt);
			if (result_2 == (int *) NULL) {
				clnt_perror (clnt, "data insert failed...!\n");
			}
			else printf("data insert successfully...!\n");
			break;
		case 2:
			printf("please input key:");  
			if(scanf("%d", &key) != 1) {  
				perror("input data error, system crash...!\n");  
				exit(EXIT_FAILURE);  
			}
			delete_1_arg = key;
			result_3 = delete_1(&delete_1_arg, clnt);
			if (result_3 == (int *) NULL) {
				clnt_perror (clnt, "datat delete failed...!\n");
			}
			else printf("data delete successfully...!\n");
			break;
		case 3:
			goto exit;
			break;
	}
exit:
#ifndef	DEBUG
	clnt_destroy (clnt);
#endif	 /* DEBUG */
}


int
main (int argc, char *argv[])
{
	char *host;

	if (argc < 2) {
		printf ("usage: %s server_host\n", argv[0]);
		exit (1);
	}
	host = argv[1];
	severprog_1 (host);
exit (0);
}

vim Makefile

# This is a template Makefile generated by rpcgen

# Parameters

CLIENT = rpc_client
SERVER = rpc_server

SOURCES_CLNT.c = 
SOURCES_CLNT.h = 
SOURCES_SVC.c = 
SOURCES_SVC.h = 
SOURCES.x = rpc.x

TARGETS_SVC.c = rpc_svc.c   rpc_xdr.c 			rpc_srv_func.c
TARGETS_CLNT.c = rpc_clnt.c   rpc_xdr.c 		rpc_cln_func.c
TARGETS = rpc.h rpc_xdr.c rpc_clnt.c rpc_svc.c    

OBJECTS_CLNT = $(SOURCES_CLNT.c:%.c=%.o) $(TARGETS_CLNT.c:%.c=%.o)
OBJECTS_SVC = $(SOURCES_SVC.c:%.c=%.o) $(TARGETS_SVC.c:%.c=%.o)
# Compiler flags 

CFLAGS += -g 
LDLIBS += -lnsl -lpthread -lsqlite3
RPCGENFLAGS = 

# Targets 

all : $(CLIENT) $(SERVER)

$(TARGETS) : $(SOURCES.x) 
	rpcgen $(RPCGENFLAGS) $(SOURCES.x)

$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) $(TARGETS_CLNT.c) 

$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) $(TARGETS_SVC.c) 

$(CLIENT) : $(OBJECTS_CLNT) 
	$(LINK.c) -o $(CLIENT) $(OBJECTS_CLNT) $(LDLIBS) 

$(SERVER) : $(OBJECTS_SVC) 
	$(LINK.c) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)

 clean:
	 $(RM) core $(TARGETS) $(OBJECTS_CLNT) $(OBJECTS_SVC) $(CLIENT) $(SERVER)

make

生成rpc_server 和rpc_client两个可执行文件

四 测试

在server端输入 sudo ./rpc_server

在client端输入 ./rpc_client 127.0.0.1



bubuko.com,布布扣

一种简单的RPC框架

标签:des   blog   http   io   os   ar   for   strong   sp   

原文地址:http://blog.csdn.net/wangzhicheng1983/article/details/40406669

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!