[Assorted-commits] SF.net SVN: assorted: [810] sandbox/trunk/src/cc
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-05-14 07:26:03
|
Revision: 810 http://assorted.svn.sourceforge.net/assorted/?rev=810&view=rev Author: yangzhang Date: 2008-05-14 00:26:07 -0700 (Wed, 14 May 2008) Log Message: ----------- added xmap.lzz demo Added Paths: ----------- sandbox/trunk/src/cc/lzz/ sandbox/trunk/src/cc/lzz/xmap.lzz sandbox/trunk/src/cc/lzz/xmap.mk Added: sandbox/trunk/src/cc/lzz/xmap.lzz =================================================================== --- sandbox/trunk/src/cc/lzz/xmap.lzz (rev 0) +++ sandbox/trunk/src/cc/lzz/xmap.lzz 2008-05-14 07:26:07 UTC (rev 810) @@ -0,0 +1,104 @@ +#hdr +#include <iostream> +#include <map> + +#include <pthread.h> + +#include <boost/bind.hpp> + +#include <commons/st/st.h> +#include <commons/boost/threads.h> + +using namespace boost; +using namespace commons; +using namespace std; +#end + +/** + * A thin wrapper around the map class. + */ +class xmap +{ + public: + // Note: map::operator[] is not a const method, so we use map::find(). + void * const & operator[](int k) const { return m.find(k)->second; } + void * & operator[](int k) { return m[k]; } + private: + map<int, void*> m; +}; + +/** + * A server that handles requests to read or update an xmap. + */ +class xmap_server(int port = 17000) +{ + public: + /** + * The main accept loop. + */ + void + serve() + { + st_netfd_t listener = st_tcp_listen(port); + while (true) { + st_netfd_t client = st_accept(listener, NULL, 0, ST_UTIME_NO_TIMEOUT); + st_spawn(boost::bind(&xmap_server::handle, this, client)); + } + } + + private: + /** + * Handle a client. + */ + void + handle(st_netfd_t client) + { + cout << "handling a client " << errno << endl; + string msg("hello\n"); + checkeq(st_write(client, msg.c_str(), msg.size(), ST_UTIME_NO_TIMEOUT), + static_cast<ssize_t>(msg.size())); + check0x(st_netfd_close(client)); + cout << errno << endl; + } + + xmap m; +}; + +void +st_bg() +{ + cout << errno << endl; + while (true) { + cout << "hello st_bg " << errno << endl; + st_sleep(1); + } +} + +void +worker(int i, cqueue<int> queue) +{ + string worker = "worker " + lexical_cast<string>(i) + ": "; + while (true) { + queue.pop(); + cout << worker << "got work" << endl; + cout << worker << "calculating for conn " << errno << endl; + sleep(1); + } + st_call(bind(st_write, fd)); +} + +int +main() +{ + const int nworkers = 5; + vector<pthread_t> workers(nworkers); + for (int i = 0; i < nworkers; i++) { + workers[i] = checknneg(spawn(bind(worker, i))); + } + check0x(pthread_join(workers[i], NULL)); + check0x(st_init()); + check(st_spawn(bind(st_bg))); + xmap_server s; + s.serve(); + return 0; +} Added: sandbox/trunk/src/cc/lzz/xmap.mk =================================================================== --- sandbox/trunk/src/cc/lzz/xmap.mk (rev 0) +++ sandbox/trunk/src/cc/lzz/xmap.mk 2008-05-14 07:26:07 UTC (rev 810) @@ -0,0 +1,32 @@ +TARGET := xmap +LZZS := $(wildcard *.lzz) +SRCS := $(foreach lzz,$(LZZS),$(patsubst %.lzz,%.o,$(lzz))) +HDRS := $(foreach lzz,$(LZZS),$(patsubst %.lzz,%.hh,$(lzz))) +OBJS := $(foreach lzz,$(LZZS),$(patsubst %.lzz,%.cc,$(lzz))) +LDFLAGS := -lstx -lst -lresolv -lpthread +CXXFLAGS := -g3 -Wall -Werror + +all: $(TARGET) + +$(TARGET): $(OBJS) + $(CXX) -o $@ $< $(LDFLAGS) + +%.o: %.cc + wtf $(CXX) $(CXXFLAGS) -c -o $@ $< + +%.cc: %.lzz + lzz -hx hh -sx cc -hl -sl -hd -sd $< + +%.hh: %.lzz + lzz -hx hh -sx cc -hl -sl -hd -sd $< + +clean: + rm -f *.cc *.hh *.o xmap + +doc: $(SRCS) $(HDRS) + doxygen + +.PHONY: clean + +# %.cc doesn't work +.SECONDARY: main.cc This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |