diff --git a/.gitignore b/.gitignore index e35ce5038..32a940ba3 100644 --- a/.gitignore +++ b/.gitignore @@ -82,6 +82,7 @@ Ipopt-3.13.4/build FMIL/build/ FMIL/install/ +FMIL/include/ SuiteSparse/build/ @@ -149,4 +150,4 @@ sundials-5.4.0/build_msvc/ /libffi/install/ /libffi/x86_64-*-gnu/ -/libffi/x86_64-w64-mingw32/ \ No newline at end of file +/libffi/x86_64-w64-mingw32/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 1d2bf05bc..0441775d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -69,21 +69,25 @@ set(GC_BUILD_SHARED_LIBS OFF CACHE BOOL "Build shared libraries") set(enable_java_finalization OFF CACHE BOOL "Support for java finalization") set(enable_gcj_support OFF CACHE BOOL "Support for gcj") set(enable_large_config ON CACHE BOOL "Optimize for large heap or root set") + omc_add_subdirectory(gc) -target_include_directories(omcgc PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/gc/include) +set(LIBOMCGC omcgc) +# note that native GC will have a different library name + +target_include_directories(${LIBOMCGC} PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/gc/include) # make sure every target that links to gc-lib has its sources # compiled with -DGC_WIN32_PTHREADS (for pthreads on Windows, i.e., our MingW) # Or -DGC_THREADS (for auto detection on other systems.) # On Windows with MinGW OM uses pthreads-win32. GC_WIN32_PTHREADS is required # to be set explicitly for use of pthreads API on Windows. if(MINGW) - target_compile_definitions(omcgc PUBLIC GC_WIN32_PTHREADS) + target_compile_definitions(${LIBOMCGC} PUBLIC GC_WIN32_PTHREADS) else() - target_compile_definitions(omcgc PUBLIC GC_THREADS) + target_compile_definitions(${LIBOMCGC} PUBLIC GC_THREADS) endif(MINGW) # Finally add an alias for clarity purposes. -add_library(omc::3rd::omcgc ALIAS omcgc) +add_library(omc::3rd::omcgc ALIAS ${LIBOMCGC}) diff --git a/gc/tests/CMakeLists.txt b/gc/tests/CMakeLists.txt index 3c84220b9..a044372d7 100644 --- a/gc/tests/CMakeLists.txt +++ b/gc/tests/CMakeLists.txt @@ -45,3 +45,12 @@ ADD_TEST(NAME realloc_test COMMAND realloc_test) ADD_EXECUTABLE(smashtest smash_test.c) TARGET_LINK_LIBRARIES(smashtest gc-lib) ADD_TEST(NAME smashtest COMMAND smashtest) + +if(CMAKE_USE_PTHREADS_INIT) + target_link_libraries(gctest pthread) + target_link_libraries(hugetest pthread) + target_link_libraries(leaktest pthread) + target_link_libraries(middletest pthread) + target_link_libraries(realloc_test pthread) + target_link_libraries(smashtest pthread) +endif() diff --git a/graphstream/gs-netstream/c++/.gitignore b/graphstream/gs-netstream/c++/.gitignore new file mode 100644 index 000000000..291ff7f5e --- /dev/null +++ b/graphstream/gs-netstream/c++/.gitignore @@ -0,0 +1,6 @@ +netstream-main +*.exe +*.dll +*.so +*.o +*.a diff --git a/graphstream/gs-netstream/c++/Makefile b/graphstream/gs-netstream/c++/Makefile index 64ae89768..973442b4e 100644 --- a/graphstream/gs-netstream/c++/Makefile +++ b/graphstream/gs-netstream/c++/Makefile @@ -3,7 +3,7 @@ OBJDIR=obj SRCDIR=src INCDIR=include -ifeq (darwin,$(findstring darwin,$(shell $(CC) -v 2>&1 | grep ^Target))) +ifeq ($(shell uname),Darwin) AR=libtool ARFLAGS=-static -o else @@ -11,22 +11,25 @@ else ARFLAGS=rcs endif +CC = g++ CWARN = -W -Wall -Wshadow -Wimplicit -Wreturn-type -Wcomment -Wtrigraphs -Wformat -Wparentheses -Wpointer-arith -Wuninitialized -O CDBG = -g $(CWARN) -fno-inline +CFLAGS = -I$(INCDIR) -O3 DFLAGS = -I$(INCDIR) -g $(CWARN) -fno-inline -DDEBUG=1 -ifeq (MINGW,$(findstring MINGW,$(shell uname))) +# https://stackoverflow.com/questions/714100/os-detecting-makefile +ifeq ($(OS),Windows_NT) SOCKET_LIB = -lwsock32 else - SOCKET_LIB = + SOCKET_LIB = endif CTAG = ctags CTAGFILE = filelist # src, object and bin files -OUT_BINARY := netstream-main +OUT_BINARY := netstream-main OUT_LIBRARY := libnetstream.a HEADERS = $(INCDIR)/global.h @@ -35,22 +38,22 @@ OBJS = \ $(OBJDIR)/netstream-storage.o \ $(OBJDIR)/netstream-socket.o \ $(OBJDIR)/netstream-sender.o \ - $(OBJDIR)/netstream-main.o - + $(OBJDIR)/netstream-main.o + .SECONDARY: #-- Rules all: $(OUT_BINARY) $(OUT_LIBRARY) $(OUT_BINARY): $(OBJS) - $(CXX) $(CFLAGS) -I$(INCDIR) $(OBJS) -o $(OUT_BINARY) $(SOCKET_LIB) + $(CC) $(CFLAGS) $(OBJS) -o $(OUT_BINARY) $(SOCKET_LIB) $(OUT_LIBRARY): $(OBJS) - $(AR) $(ARFLAGS) $(OUT_LIBRARY) $(OBJS) + $(AR) $(ARFLAGS) $(OUT_LIBRARY) $(OBJS) $(OBJDIR)/%.o: $(SRCDIR)/%.cpp mkdir -p $(OBJDIR) - $(CXX) $(CFLAGS) -c $? -o $@ + $(CC) $(CFLAGS) -c $? -o $@ .PHONY: clean depend fresh @@ -64,8 +67,7 @@ clean: -rm -f $(SRCDIR)/*.output $(LEX_C) -rm -f */*~ *~ core -rm -f $(BINDIR)/* - -rm -f $(OUT_BINARY) $(OUT_BINARY).exe + -rm -f $(OUT_BINARY) -rm -f $(OUT_LIBRARY) - -rm -rf $(OBJDIR) fresh: clean all diff --git a/graphstream/gs-netstream/c++/src/netstream-constants.h b/graphstream/gs-netstream/c++/src/netstream-constants.h index 620d6bf41..5faa18dac 100644 --- a/graphstream/gs-netstream/c++/src/netstream-constants.h +++ b/graphstream/gs-netstream/c++/src/netstream-constants.h @@ -154,16 +154,19 @@ namespace netstream{ * An array of bytes. Followed by first, a 16-bits integer for the number * of integers and then, a list of signed bytes. */ + const unsigned char TYPE_BYTE_ARRAY=0x53; /** * Followed by an 16-bit signed integer (a short) */ const unsigned char TYPE_SHORT=0x54; + /** * An array of shorts. Followed by first, a 16-bits integer for the number * of integers and then, a list of 16-bit signed shorts */ const unsigned char TYPE_SHORT_ARRAY=0x55; + /** * Followed by an 32-bit signed integer */ @@ -173,6 +176,7 @@ namespace netstream{ * of integers and then, a list of 32-bit signed integers */ const unsigned char TYPE_INT_ARRAY=0x57; + /** * Followed by an 64-bit signed integer */ @@ -182,6 +186,9 @@ namespace netstream{ * longs and then, a list of 62-bit signed integers */ const unsigned char TYPE_LONG_ARRAY=0x59; + +/** NOTE: see also LONGLONG below, out of order */ + /** * Followed by a single precision 32-bits floating point number */ @@ -219,6 +226,19 @@ namespace netstream{ */ const unsigned char TYPE_ARRAY=0x60; +/** + * Followed by an 64-bit signed integer + */ + const unsigned char TYPE_LONGLONG=0x61; +/** + * An array of longs. Followed by first, a 16-bits integer for the number of + * longs and then, a list of 62-bit signed integers + */ + const unsigned char TYPE_LONGLONG_ARRAY=0x62; + + + + }// end netstream namespace #endif diff --git a/graphstream/gs-netstream/c++/src/netstream-main.cpp b/graphstream/gs-netstream/c++/src/netstream-main.cpp index b99ba7939..303a45352 100644 --- a/graphstream/gs-netstream/c++/src/netstream-main.cpp +++ b/graphstream/gs-netstream/c++/src/netstream-main.cpp @@ -13,8 +13,6 @@ #include #include - -#include "netstream-sizes.h" #include "netstream-storage.h" #include "netstream-socket.h" #include "netstream-constants.h" @@ -38,7 +36,7 @@ int main (int argc, char const *argv[]) void e(){ string source_id="C++_netstream_test"; - GS_LONG time_id=0L; + long time_id=0L; NetStreamSender stream("default","localhost",2001,false); string n1("node"); while(1) { @@ -48,9 +46,12 @@ void e(){ } + + + void example(){ - string source_id("C"); - GS_LONG time_id = 0L; + string source_id("C++_netstream_test"); + long time_id=0L; NetStreamSender stream("default","localhost",2001,false); string style("node{fill-mode:plain;fill-color:#567;size:6px;}"); stream.addGraphAttribute(source_id, time_id++, "stylesheet", style); @@ -58,39 +59,42 @@ void example(){ stream.changeGraphAttribute(source_id, time_id++, "test", "test",false); stream.addGraphAttribute(source_id, time_id++, "ui.antialias", true); stream.addGraphAttribute(source_id, time_id++, "layout.stabilization-limit", 0); - for (int i = 0; i < 10; i++) { + for (int i = 0; i < 500; i++) { stringstream n1; n1< 0) { - stringstream n2; - n2<<(i-1); - stringstream n3; - n3<<(i/2); - stringstream e1; - e1< 0) { + + + stringstream n2; + n2<<(i-1); + + stringstream n3; + n3<<(i/2); + + stringstream e1; + e1< value(v,v+3); @@ -104,12 +108,12 @@ void types_test(){ vector value3(v3,v3+2); stream.addGraphAttribute(source_id, time_id++, "doubleArray", value3); - GS_LONG v4[] = {1776,7,4}; - vector value4(v4,v4+3); + long int v4[] = {1776,7,4}; + vector value4(v4,v4+3); stream.addGraphAttribute(source_id, time_id++, "longArray", value4); - GS_CHAR v5[] = {'0',(GS_CHAR)0,'z'}; - vector value5(v5,v5+3); + char v5[] = {'0',(char)0,'z'}; + vector value5(v5,v5+3); stream.addGraphAttribute(source_id, time_id++, "byteArray",value5 ); bool v6[] = {true,false}; @@ -121,10 +125,9 @@ void types_test(){ void events_test(){ string source_id="C++_netstream_test"; - GS_LONG time_id = 0L; + long time_id=0L; NetStreamSender stream("localhost", 2001); stream.addNode(source_id, time_id++, "node0"); - stream.addNode(source_id, time_id++, "node1"); stream.addEdge(source_id, time_id++, "edge", "node0", "node1", true); stream.addNodeAttribute(source_id, time_id++, "node0","nodeAttribute", 0); stream.changeNodeAttribute(source_id, time_id++, "node0","nodeAttribute",0, 1); diff --git a/graphstream/gs-netstream/c++/src/netstream-sender.cpp b/graphstream/gs-netstream/c++/src/netstream-sender.cpp index 06bc4727b..e769a7892 100644 --- a/graphstream/gs-netstream/c++/src/netstream-sender.cpp +++ b/graphstream/gs-netstream/c++/src/netstream-sender.cpp @@ -10,31 +10,27 @@ * @author Yoann Pigné */ -/* include unistd on *nix systems for sleep */ -#if !defined(__MINGW32__) && !defined(_MSC_VER) -#include -#endif #include "netstream-sender.h" namespace netstream{ -NetStreamSender::NetStreamSender(const GS_STRING & host, GS_INT port): +NetStreamSender::NetStreamSender(const string & host, int port): _stream_name("default"),_host(host),_port(port),_stream(),_socket(host,port),debug(false) { init(); } -NetStreamSender::NetStreamSender(GS_INT port): +NetStreamSender::NetStreamSender(int port): _stream_name("default"),_host("localhost"),_port(port),_stream(),_socket("localhost",port),debug(false) { init(); } -NetStreamSender::NetStreamSender(const GS_STRING & stream, const GS_STRING & host, GS_INT port): +NetStreamSender::NetStreamSender(const string & stream, const string & host, int port): _stream_name(stream),_host(host),_port(port),_stream(),_socket(host,port),debug(false) { init(); } -NetStreamSender::NetStreamSender(const GS_STRING & stream, const GS_STRING & host, GS_INT port, GS_BOOL debug): +NetStreamSender::NetStreamSender(const string & stream, const string & host, int port, bool debug): _stream_name(stream),_host(host),_port(port),_stream(),_socket(host,port),debug(debug) { init(); @@ -42,7 +38,7 @@ _stream_name(stream),_host(host),_port(port),_stream(),_socket(host,port),debug( void NetStreamSender::init() -{ +{ _stream.writeString(_stream_name); int wait_for_server = 1; @@ -58,7 +54,11 @@ void NetStreamSender::init() else std::cout<<"."< & object){ +int NetStreamSender::_getType(const vector & object){ if(debug){ - cerr<<"NetStreamSender: _getType : char* "< & object){ +int NetStreamSender::_getType(const vector & object){ if(debug){ - cerr<<"NetStreamSender: _getType : bool*"< & object){ -if(debug){ - cerr<<"NetStreamSender: _getType : int*"< & object){ + if(debug){ + cerr<<" NetStreamSender: _getType : short*"< & object){ -if(debug){ - cerr<<"NetStreamSender: _getType : long*"< & object){ + if(debug){ + cerr<<" NetStreamSender: _getType : int*"< & object){ + if(debug){ + cerr<<" NetStreamSender: _getType : long*" < & object){ +int NetStreamSender::_getType(const vector & object){ + if(debug){ + cerr<<" NetStreamSender: _getType : long long*" < & object){ if(debug){ - cerr<<"NetStreamSender: _getType : float*"< & object){ +int NetStreamSender::_getType(const vector & object){ if(debug){ - cerr<<"NetStreamSender: _getType : double*"< & value){ - event.writeInt(value.size()); +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); for(vector::const_iterator i = value.begin(); i != value.end(); i++){ event.writeByte((*i)); } } - -void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ - event.writeInt(value.size()); +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); for(vector::const_iterator i = value.begin(); i != value.end(); i++){ event.writeByte((*i)); } } -void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ - event.writeInt(value.size()); - for(vector::const_iterator i = value.begin(); i != value.end(); i++){ - event.writeInt((*i)); +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ + event.writeVarint((*i)); } } - -void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ - event.writeInt(value.size()); - for(vector::const_iterator i = value.begin(); i != value.end(); i++){ - event.writeLong((*i)); +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ + event.writeVarint((*i)); + } +} +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ + event.writeVarint((*i)); + } +} +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ + event.writeVarint((*i)); } } -void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ - event.writeInt(value.size()); - for(vector::const_iterator i = value.begin(); i != value.end(); i++){ +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ event.writeFloat((*i)); } } - -void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ - event.writeInt(value.size()); - for(vector::const_iterator i = value.begin(); i != value.end(); i++){ +void NetStreamSender::_encode(NetStreamStorage & event, const vector & value){ + event.writeUnsignedVarint(value.size()); + for(vector::const_iterator i = value.begin(); i != value.end(); i++){ event.writeDouble((*i)); } } @@ -224,6 +264,13 @@ void NetStreamSender::_sendEvent(NetStreamStorage & event){ if(debug){ cout< +#else + #include +#endif + +#include +#define STATIC_ASSERT(condition) typedef char __static_assert##__LINE__[(condition)?1:-1] + #include -#if !defined(__MINGW32__) -#include +#ifdef _WIN32 +STATIC_ASSERT(sizeof(short)==sizeof(int16_t)); +STATIC_ASSERT(sizeof(int)==sizeof(int32_t)); +STATIC_ASSERT(sizeof(long)==sizeof(int32_t)); +STATIC_ASSERT(sizeof(long long)==sizeof(int64_t)); +STATIC_ASSERT(sizeof(void*)==sizeof(int64_t)); +#define GS_LONG long long +#else +STATIC_ASSERT(sizeof(short)==sizeof(int16_t)); +STATIC_ASSERT(sizeof(int)==sizeof(int32_t)); +STATIC_ASSERT(sizeof(long)==sizeof(int64_t)); +STATIC_ASSERT(sizeof(long long)==sizeof(int64_t)); +STATIC_ASSERT(sizeof(void*)==sizeof(int64_t)); +#define GS_LONG long #endif -#include -#include "netstream-sizes.h" +#define GS_STRINGIFY(VAR) #VAR + #include "netstream-storage.h" #include "netstream-socket.h" #include "netstream-constants.h" @@ -33,39 +55,43 @@ namespace netstream{ class NetStreamSender{ protected: - GS_STRING _stream_name; - GS_STRING _host; - GS_INT _port; + string _stream_name; + string _host; + int _port; NetStreamSocket _socket; NetStreamStorage _stream; - GS_BOOL debug; + bool debug; void init(); template - GS_INT getType(T t) + int getType(T t) { return _getType(t); } template - GS_INT getType(const vector & t) + int getType(const vector & t) { return _getType(t); } - GS_INT _getType(GS_CHAR object); - GS_INT _getType(GS_BOOL object); - GS_INT _getType(GS_INT object); - GS_INT _getType(GS_LONG object); - GS_INT _getType(GS_FLOAT object); - GS_INT _getType(GS_DOUBLE object); - GS_INT _getType(const GS_STRING & object); - GS_INT _getType( const vector & object); - GS_INT _getType( const vector & object); - GS_INT _getType( const vector & object); - GS_INT _getType( const vector & object); - GS_INT _getType( const vector & object); - GS_INT _getType( const vector & object); + int _getType(char object); + int _getType(bool object); + int _getType(short object); + int _getType(int object); + int _getType(long object); + int _getType(long long object); + int _getType(float object); + int _getType(double object); + int _getType(const string & object); + int _getType( const vector & object); + int _getType( const vector & object); + int _getType( const vector & object); + int _getType( const vector & object); + int _getType( const vector & object); + int _getType( const vector & object); + int _getType( const vector & object); + int _getType( const vector & object); template void encode(NetStreamStorage & event, const T & value){ @@ -75,20 +101,24 @@ class NetStreamSender{ void encode(NetStreamStorage & event, const vector & value){ _encode(event, value); } - void _encode(NetStreamStorage & event, GS_CHAR value); - void _encode(NetStreamStorage & event, GS_BOOL value); - void _encode(NetStreamStorage & event, GS_INT value); - void _encode(NetStreamStorage & event, GS_LONG value); - void _encode(NetStreamStorage & event, GS_FLOAT value); - void _encode(NetStreamStorage & event, GS_DOUBLE value); - void _encode(NetStreamStorage & event, const GS_STRING & value); + void _encode(NetStreamStorage & event, char value); + void _encode(NetStreamStorage & event, bool value); + void _encode(NetStreamStorage & event, short value); + void _encode(NetStreamStorage & event, int value); + void _encode(NetStreamStorage & event, long value); + void _encode(NetStreamStorage & event, long long value); + void _encode(NetStreamStorage & event, float value); + void _encode(NetStreamStorage & event, double value); + void _encode(NetStreamStorage & event, const string & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); - void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); + void _encode(NetStreamStorage & event, const vector & value); void _sendEvent(NetStreamStorage &); @@ -98,30 +128,29 @@ class NetStreamSender{ // ================ // = Constructors = // ================ - NetStreamSender(const GS_STRING & host, GS_INT port); - NetStreamSender(GS_INT port); - NetStreamSender(const GS_STRING & stream, const GS_STRING & host, GS_INT port); - NetStreamSender(const GS_STRING & stream, const GS_STRING & host, GS_INT port, GS_BOOL debug); - + NetStreamSender(const string & host, int port); + NetStreamSender(int port); + NetStreamSender(const string & stream, const string & host, int port); + NetStreamSender(const string & stream, const string & host, int port, bool debug); // ================== // = Element events = // ================== - void addNode(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & node_id); - void removeNode(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & node_id); - void addEdge(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & edge_id, const GS_STRING & from_node, const GS_STRING & to_node, GS_BOOL directed); - void removeEdge(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & edge_id); - void stepBegins(const GS_STRING & source_id, GS_LONG time_id, GS_DOUBLE timestamp); - void graphClear(const GS_STRING & source_id, GS_LONG time_id); + void addNode(const string & source_id, long time_id, const string & node_id); + void removeNode(const string & source_id, long time_id, const string & node_id); + void addEdge(const string & source_id, long time_id, const string & edge_id, const string & from_node, const string & to_node, bool directed); + void removeEdge(const string & source_id, long time_id, const string & edge_id); + void stepBegins(const string & source_id, long time_id, double timestamp); + void graphClear(const string & source_id, long time_id); // ==================== // = Attribute events = // ==================== template - void addGraphAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & attribute, T value){ + void addGraphAttribute(const string & source_id, long time_id, const string & attribute, T value){ NetStreamStorage event = NetStreamStorage(); event.writeByte(netstream::EVENT_ADD_GRAPH_ATTR); event.writeString(source_id); - event.writeUnsignedVarInt(time_id); + event.writeUnsignedVarint(time_id); event.writeString(attribute); event.writeByte(getType(value)); encode(event, value); @@ -129,11 +158,11 @@ class NetStreamSender{ } template - void changeGraphAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & attribute, const T1 & oldValue, const T2 & newValue){ + void changeGraphAttribute(const string & source_id, long time_id, const string & attribute, const T1 & oldValue, const T2 & newValue){ NetStreamStorage event = NetStreamStorage(); event.writeByte(EVENT_CHG_GRAPH_ATTR); event.writeString(source_id); - event.writeUnsignedVarInt(time_id); + event.writeUnsignedVarint(time_id); event.writeString(attribute); event.writeByte(getType(oldValue)); encode(event, oldValue); @@ -142,14 +171,14 @@ class NetStreamSender{ _sendEvent(event); } - void removeGraphAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & attribute); + void removeGraphAttribute(const string & source_id, long time_id, const string & attribute); template - void addNodeAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & node_id, const GS_STRING & attribute, const T & value){ + void addNodeAttribute(const string & source_id, long time_id, const string & node_id, const string & attribute, const T & value){ NetStreamStorage event = NetStreamStorage(); event.writeByte(EVENT_ADD_NODE_ATTR); event.writeString(source_id); - event.writeUnsignedVarInt(time_id); + event.writeUnsignedVarint(time_id); event.writeString(node_id); event.writeString(attribute); event.writeByte(getType(value)); @@ -158,11 +187,11 @@ class NetStreamSender{ } template - void changeNodeAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & node_id, const GS_STRING & attribute, const T1 & oldValue, const T2 & newValue){ + void changeNodeAttribute(const string & source_id, long time_id, const string & node_id, const string & attribute, const T1 & oldValue, const T2 & newValue){ NetStreamStorage event = NetStreamStorage(); event.writeByte(EVENT_CHG_NODE_ATTR); event.writeString(source_id); - event.writeUnsignedVarInt(time_id); + event.writeUnsignedVarint(time_id); event.writeString(node_id); event.writeString(attribute); event.writeByte(getType(oldValue)); @@ -172,14 +201,14 @@ class NetStreamSender{ _sendEvent(event); } - void removeNodeAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & node_id, const GS_STRING & attribute); + void removeNodeAttribute(const string & source_id, long time_id, const string & node_id, const string & attribute); template - void addEdgeAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & edge_id, const GS_STRING & attribute, const T & value){ + void addEdgeAttribute(const string & source_id, long time_id, const string & edge_id, const string & attribute, const T & value){ NetStreamStorage event = NetStreamStorage(); event.writeByte(EVENT_ADD_EDGE_ATTR); event.writeString(source_id); - event.writeUnsignedVarInt(time_id); + event.writeUnsignedVarint(time_id); event.writeString(edge_id); event.writeString(attribute); event.writeByte(getType(value)); @@ -189,11 +218,11 @@ class NetStreamSender{ } template - void changeEdgeAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & edge_id, const GS_STRING & attribute, const T1 & oldValue, const T2 & newValue){ + void changeEdgeAttribute(const string & source_id, long time_id, const string & edge_id, const string & attribute, const T1 & oldValue, const T2 & newValue){ NetStreamStorage event = NetStreamStorage(); event.writeByte(EVENT_CHG_EDGE_ATTR); event.writeString(source_id); - event.writeUnsignedVarInt(time_id); + event.writeUnsignedVarint(time_id); event.writeString(edge_id); event.writeString(attribute); event.writeByte(getType(oldValue)); @@ -203,7 +232,7 @@ class NetStreamSender{ _sendEvent(event); } - void removeEdgeAttribute(const GS_STRING & source_id, GS_LONG time_id, const GS_STRING & edge_id, const GS_STRING & attribute); + void removeEdgeAttribute(const string & source_id, long time_id, const string & edge_id, const string & attribute); }; diff --git a/graphstream/gs-netstream/c++/src/netstream-socket.cpp b/graphstream/gs-netstream/c++/src/netstream-socket.cpp index aa459e587..45bd8d500 100644 --- a/graphstream/gs-netstream/c++/src/netstream-socket.cpp +++ b/graphstream/gs-netstream/c++/src/netstream-socket.cpp @@ -6,31 +6,6 @@ ** file in the root of the Shawn source tree for further details. ** ************************************************************************/ -#if !defined(_WIN32) && !defined(_WIN64) - #include - #include - #include - #include - #include - #include - #include - #include - #include /* include unistd on *nix systems for close */ -#else - #ifdef ERROR - #undef ERROR - #endif - - #include - -#if !defined(__MINGW32__) - #ifndef vsnprintf - #define vsnprintf _vsnprintf - #endif -#endif - -#endif - #ifdef SHAWN #include #include @@ -38,6 +13,26 @@ #include "netstream-socket.h" #endif +#ifndef _WIN32 +# include +# include +# include +# include +# include +# include +# include +# include +# include +#else +# ifdef ERROR +# undef ERROR +# endif +# include +# ifndef vsnprintf +# define vsnprintf _vsnprintf +# endif +#endif + #include #include #include @@ -45,7 +40,6 @@ #include #include #include -#include using namespace std; @@ -59,7 +53,7 @@ using namespace std; namespace netstream { -#ifdef WIN32 +#ifdef _WIN32 bool NetStreamSocket::init_windows_sockets_ = true; bool NetStreamSocket::windows_sockets_initialized_ = false; int NetStreamSocket::instance_count_ = 0; @@ -67,7 +61,7 @@ namespace netstream // ---------------------------------------------------------------------- NetStreamSocket:: - NetStreamSocket(std::string host, int port) + NetStreamSocket(std::string host, int port) : host_( host ), port_( port ), socket_(-1), @@ -80,7 +74,7 @@ namespace netstream // ---------------------------------------------------------------------- NetStreamSocket:: - NetStreamSocket(int port) + NetStreamSocket(int port) : host_(""), port_( port ), socket_(-1), @@ -96,7 +90,7 @@ namespace netstream NetStreamSocket:: init() { -#ifdef WIN32 +#ifdef _WIN32 instance_count_++; if( init_windows_sockets_ && !windows_sockets_initialized_ ) @@ -115,14 +109,14 @@ namespace netstream { // Close first an existing client connection ... close(); -#ifdef WIN32 +#ifdef _WIN32 instance_count_--; #endif // ... then the server socket if( server_socket_ >= 0 ) { -#ifdef WIN32 +#ifdef _WIN32 ::closesocket( server_socket_ ); #else ::close( server_socket_ ); @@ -130,8 +124,8 @@ namespace netstream server_socket_ = -1; } -#ifdef WIN32 - if( server_socket_ == -1 && socket_ == -1 +#ifdef _WIN32 + if( server_socket_ == -1 && socket_ == -1 && init_windows_sockets_ && instance_count_ == 0 ) WSACleanup(); windows_sockets_initialized_ = false; @@ -139,12 +133,12 @@ namespace netstream } // ---------------------------------------------------------------------- - void + void NetStreamSocket:: - BailOnNetStreamSocketError( std::string context) - const throw( NetStreamSocketException ) + BailOnNetStreamSocketError( std::string context) + const { -#ifdef WIN32 +#ifdef _WIN32 int e = WSAGetLastError(); std::string msg = GetWinsockErrorString( e ); #else @@ -154,7 +148,7 @@ namespace netstream } // ---------------------------------------------------------------------- - int + int NetStreamSocket:: port() { @@ -163,10 +157,10 @@ namespace netstream // ---------------------------------------------------------------------- - bool + bool NetStreamSocket:: - datawaiting(int sock) - const throw() + datawaiting(int sock) + const { fd_set fds; FD_ZERO( &fds ); @@ -197,7 +191,7 @@ namespace netstream // First try nnn.nnn.nnn.nnn form saddr.s_addr = inet_addr(address.c_str()); - if (saddr.s_addr != static_cast(-1)) + if (saddr.s_addr != static_cast(-1)) { addr = saddr; return true; @@ -214,16 +208,15 @@ namespace netstream // ---------------------------------------------------------------------- - void + void NetStreamSocket:: accept() - throw( NetStreamSocketException ) { if( socket_ >= 0 ) return; struct sockaddr_in client_addr; -#ifdef WIN32 +#ifdef _WIN32 int addrlen = sizeof(client_addr); #else socklen_t addrlen = sizeof(client_addr); @@ -237,12 +230,12 @@ namespace netstream server_socket_ = static_cast(socket( AF_INET, SOCK_STREAM, 0 )); if( server_socket_ < 0 ) BailOnNetStreamSocketError("netstream::NetStreamSocket::accept() @ socket"); - + //"Address already in use" error protection { int reuseaddr = 1; - #ifdef WIN32 + #ifdef _WIN32 //setsockopt(server_socket_, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuseaddr, sizeof(reuseaddr)); // No address reuse in Windows!!! #else @@ -279,16 +272,15 @@ namespace netstream } // ---------------------------------------------------------------------- - void + void NetStreamSocket:: - set_blocking(bool blocking) - throw(NetStreamSocketException ) + set_blocking(bool blocking) { blocking_ = blocking; if( server_socket_ > 0 ) { -#ifdef WIN32 +#ifdef _WIN32 ULONG NonBlock = blocking_ ? 0 : 1; if (ioctlsocket(server_socket_, FIONBIO, &NonBlock) == SOCKET_ERROR) BailOnNetStreamSocketError("netstream::NetStreamSocket::set_blocking() Unable to initialize non blocking I/O"); @@ -303,14 +295,13 @@ namespace netstream fcntl(server_socket_, F_SETFL, arg); #endif } - + } // ---------------------------------------------------------------------- - void + void NetStreamSocket:: connect() - throw( NetStreamSocketException ) { in_addr addr; if( !atoaddr( host_.c_str(), addr) ) @@ -338,14 +329,14 @@ namespace netstream } // ---------------------------------------------------------------------- - void + void NetStreamSocket:: close() { - // Close client-connection + // Close client-connection if( socket_ >= 0 ) { -#ifdef WIN32 +#ifdef _WIN32 ::closesocket( socket_ ); #else ::close( socket_ ); @@ -356,10 +347,9 @@ namespace netstream } // ---------------------------------------------------------------------- - void + void NetStreamSocket:: - send( std::vector b) - throw( NetStreamSocketException ) + send( std::vector b) { if( socket_ < 0 ) return; @@ -371,7 +361,7 @@ namespace netstream buf[i] = b[i]; } - if (verbose_) + if (verbose_) { cerr << "Send " << numbytes << " bytes via netstream::NetStreamSocket: ["; for(size_t i = 0; i < numbytes; ++i) @@ -380,13 +370,12 @@ namespace netstream cerr << " " << (int)b[i] << " "; } cerr << "]" << endl; - cerr.flush(); } unsigned char const *buf_ptr = buf; while( numbytes > 0 ) { -#ifdef WIN32 +#ifdef _WIN32 int n = ::send( socket_, (const char*)buf_ptr, static_cast(numbytes), 0 ); #else int n = ::send( socket_, buf_ptr, numbytes, 0 ); @@ -404,15 +393,14 @@ namespace netstream delete[] buf; } + - - + // ---------------------------------------------------------------------- void NetStreamSocket:: sendExact( const NetStreamStorage &b) - throw( NetStreamSocketException ) { int length = static_cast(b.size()); NetStreamStorage length_storage; @@ -424,12 +412,11 @@ namespace netstream } - + // ---------------------------------------------------------------------- - vector + vector NetStreamSocket:: receive(int bufSize) - throw( NetStreamSocketException ) { vector b; @@ -455,7 +442,7 @@ namespace netstream b[i] = buf[i]; } - if (verbose_) + if (verbose_) { cerr << "Rcvd " << a << " bytes via netstream::NetStreamSocket: ["; for(int i = 0; i < a; ++i) @@ -470,18 +457,17 @@ namespace netstream } // ---------------------------------------------------------------------- - + bool NetStreamSocket:: receiveExact( NetStreamStorage &msg ) - throw( NetStreamSocketException ) { /* receive length of vector */ unsigned char * const bufLength = new unsigned char[4]; int bytesRead = 0; int readThisTime = 0; - + while (bytesRead<4) { readThisTime = recv( socket_, (char*)(bufLength + bytesRead), 4-bytesRead, 0 ); @@ -502,7 +488,7 @@ namespace netstream unsigned char * const buf = new unsigned char[NN]; bytesRead = 0; readThisTime = 0; - + while (bytesRead= 0; } // ---------------------------------------------------------------------- - bool + bool NetStreamSocket:: - is_blocking() - throw() + is_blocking() { return blocking_; } -#ifdef WIN32 +#ifdef _WIN32 // ---------------------------------------------------------------------- - std::string + std::string NetStreamSocket:: - GetWinsockErrorString(int err) + GetWinsockErrorString(int err) const { @@ -624,15 +609,8 @@ namespace netstream return "unknown"; } -#endif // WIN32 +#endif // _WIN32 } // namespace tcpip - -/*----------------------------------------------------------------------- -* Source $Source: $ -* Version $Revision: 385 $ -* Date $Date: 2010-01-13 16:10:20 +0100 (Wed, 13 Jan 2010) $ -*----------------------------------------------------------------------- -* $Log: $ -*-----------------------------------------------------------------------*/ +// vim: ts=4:sw=4:noet diff --git a/graphstream/gs-netstream/c++/src/netstream-socket.h b/graphstream/gs-netstream/c++/src/netstream-socket.h index b1fcc49fa..ea78ead7a 100644 --- a/graphstream/gs-netstream/c++/src/netstream-socket.h +++ b/graphstream/gs-netstream/c++/src/netstream-socket.h @@ -26,8 +26,8 @@ #endif // Disable exception handling warnings -#if defined(WIN32) && !defined(__MINGW32__) - #pragma warning( disable : 4290 ) +#ifdef _WIN32 +# pragma warning( disable : 4290 ) #endif #include @@ -48,18 +48,18 @@ namespace netstream private: std::string what_; public: - NetStreamSocketException( std::string what ) throw() + NetStreamSocketException( std::string what ) { what_ = what; //std::cerr << "netstream::NetStreamSocketException: " << what << std::endl << std::flush; } - virtual const char* what() const throw() + virtual const char* what() const noexcept { return what_.c_str(); } - ~NetStreamSocketException() throw() {} + ~NetStreamSocketException() {} }; class NetStreamSocket @@ -76,18 +76,18 @@ namespace netstream ~NetStreamSocket(); /// Connects to host_:port_ - void connect() throw( NetStreamSocketException ); + void connect(); /// Wait for a incoming connection to port_ - void accept() throw( NetStreamSocketException ); - void send( const std::vector ) throw( NetStreamSocketException ); - void sendExact( const NetStreamStorage & ) throw( NetStreamSocketException ); - std::vector receive( int bufSize = 2048 ) throw( NetStreamSocketException ); - bool receiveExact( NetStreamStorage &) throw( NetStreamSocketException ); + void accept(); + void send( const std::vector ) ; + void sendExact( const NetStreamStorage & ) ; + std::vector receive( int bufSize = 2048 ) ; + bool receiveExact( NetStreamStorage &) ; void close(); int port(); - void set_blocking(bool) throw( NetStreamSocketException ); - bool is_blocking() throw(); + void set_blocking(bool) ; + bool is_blocking() ; bool has_client_connection() const; // If verbose, each send and received data is written to stderr @@ -96,12 +96,12 @@ namespace netstream private: void init(); - void BailOnNetStreamSocketError( std::string ) const throw( NetStreamSocketException ); -#ifdef WIN32 + void BailOnNetStreamSocketError( std::string ) const ; +#ifdef _WIN32 std::string GetWinsockErrorString(int err) const; #endif bool atoaddr(std::string, struct in_addr& addr); - bool datawaiting(int sock) const throw(); + bool datawaiting(int sock) const ; std::string host_; int port_; @@ -110,7 +110,7 @@ namespace netstream bool blocking_; bool verbose_; -#ifdef WIN32 +#ifdef _WIN32 static bool init_windows_sockets_; static bool windows_sockets_initialized_; static int instance_count_; @@ -121,11 +121,4 @@ namespace netstream #endif // BUILD_TCPIP - -/*----------------------------------------------------------------------- -* Source $Source: $ -* Version $Revision: 197 $ -* Date $Date: 2008-04-29 17:40:51 +0200 (Tue, 29 Apr 2008) $ -*----------------------------------------------------------------------- -* $Log:$ -*-----------------------------------------------------------------------*/ +// vim: ts=4:sw=4:noet diff --git a/graphstream/gs-netstream/c++/src/netstream-storage.cpp b/graphstream/gs-netstream/c++/src/netstream-storage.cpp index b684b9159..adb538f41 100644 --- a/graphstream/gs-netstream/c++/src/netstream-storage.cpp +++ b/graphstream/gs-netstream/c++/src/netstream-storage.cpp @@ -18,7 +18,6 @@ #include #include #include -#include using namespace std; @@ -35,11 +34,11 @@ namespace netstream // ---------------------------------------------------------------------- - NetStreamStorage::NetStreamStorage(unsigned char packet[], size_t length) + NetStreamStorage::NetStreamStorage(unsigned char packet[], int length) { store.reserve(length); // Get the content - for(size_t i = 0; i < length; ++i) store.push_back(packet[i]); + for(int i = 0; i < length; ++i) store.push_back(packet[i]); init(); } @@ -91,7 +90,7 @@ namespace netstream * Reads a char form the array * @return The read char (between 0 and 255) */ - unsigned char NetStreamStorage::readChar() throw(std::invalid_argument) + unsigned char NetStreamStorage::readChar() { if ( !valid_pos() ) { @@ -105,19 +104,88 @@ namespace netstream /** * */ - void NetStreamStorage::writeChar(unsigned char value) throw() + void NetStreamStorage::writeChar(unsigned char value) { store.push_back(value); iter_ = store.begin(); } + + size_t NetStreamStorage::varintSize(uint_fast64_t data){ + // 7 bits -> 127 + if(data < (1LL << 7)){return 1;} + // 14 bits -> 16383 + if(data < (1LL << 14)){return 2;} + // 21 bits -> 2097151 + if(data < (1LL << 21)){return 3;} + // 28 bits -> 268435455 + if(data < (1LL << 28)){return 4;} + // 35 bits -> 34359738367 + if(data < (1LL << 35)){return 5;} + // 42 bits -> 4398046511103 + if(data < (1LL << 42)){return 6;} + // 49 bits -> 562949953421311 + if(data < (1LL << 49)){return 7;} + // 56 bits -> 72057594037927935 + if(data < (1LL << 56)){return 8;} + return 9; + } + + + // ---------------------------------------------------------------------- + /** + * Reads a varint form the array + * @return The read varint + */ + int_fast64_t NetStreamStorage::readVarint() + { + uint_fast64_t number = readUnsignedVarint(); + return (int_fast64_t)((number & 1) == 0) ? number >> 1 : -(number >> 1); + } + // ---------------------------------------------------------------------- + /** + * + */ + void NetStreamStorage::writeVarint(int_fast64_t value) + { + writeUnsignedVarint((value << 1) ^ (value >> 63)); + } + // ---------------------------------------------------------------------- + /** + * Reads a unsigned varint form the array + * @return The read u_varint + */ + uint_fast64_t NetStreamStorage::readUnsignedVarint() + { + // TODO + return 0; + } + // ---------------------------------------------------------------------- + /** + * + */ + void NetStreamStorage::writeUnsignedVarint(uint_fast64_t value) + { + size_t size = varintSize(value); + + unsigned char buffer[size]; + for(int i = 0; i < size; i++){ + int head=128; + if(i==size-1) head = 0; + long b = ((value >> (7*i)) & 127) ^ head; + buffer[size-1-i] = ((unsigned char)(b & 255 )); + } + writeByEndianess(buffer, size); + } + + // ---------------------------------------------------------------------- /** * Reads a byte form the array * @return The read byte (between -128 and 127) */ - int NetStreamStorage::readByte() throw(std::invalid_argument) + int NetStreamStorage::readByte() { int i = static_cast(readChar()); if (i < 128) return i; @@ -129,7 +197,7 @@ namespace netstream /** * */ - void NetStreamStorage::writeByte(int value) throw(std::invalid_argument) + void NetStreamStorage::writeByte(int value) { if (value < -128 || value > 127) { @@ -144,16 +212,17 @@ namespace netstream * Reads an unsigned byte form the array * @return The read byte (between 0 and 255) */ - int NetStreamStorage::readUnsignedByte() throw(std::invalid_argument) + int NetStreamStorage::readUnsignedByte() { return static_cast(readChar()); } + // ---------------------------------------------------------------------- /** * */ - void NetStreamStorage::writeUnsignedByte(int value) throw(std::invalid_argument) + void NetStreamStorage::writeUnsignedByte(int value) { if (value < 0 || value > 255) { @@ -168,14 +237,14 @@ namespace netstream * Reads a string form the array * @return The read string */ - std::string NetStreamStorage::readString() throw(std::invalid_argument) + std::string NetStreamStorage::readString() { int len = readInt(); checkReadSafe(len); - StorageType::const_iterator e = iter_; - std::advance(e, len); - const string tmp(iter_, e); - iter_ = e; + StorageType::const_iterator end = iter_; + std::advance(end, len); + const string tmp(iter_, end); + iter_ = end; return tmp; } @@ -185,10 +254,9 @@ namespace netstream * Writes a string into the array; * @param s The string to be written */ - void NetStreamStorage::writeString(const std::string &s) throw() + void NetStreamStorage::writeString(const std::string &s) { - writeUnsignedVarInt(static_cast(s.length())); - + writeUnsignedVarint(static_cast(s.length())); store.insert(store.end(), s.begin(), s.end()); iter_ = store.begin(); } @@ -199,7 +267,7 @@ namespace netstream * Reads a string list form the array * @return The read string */ - std::vector NetStreamStorage::readStringList() throw(std::invalid_argument) + std::vector NetStreamStorage::readStringList() { std::vector tmp; const int len = readInt(); @@ -217,9 +285,9 @@ namespace netstream * Writes a string into the array; * @param s The string to be written */ - void NetStreamStorage::writeStringList(const std::vector &s) throw() + void NetStreamStorage::writeStringList(const std::vector &s) { - writeInt(static_cast(s.size())); + writeUnsignedVarint(s.size()); for (std::vector::const_iterator it = s.begin(); it!=s.end() ; it++) { writeString(*it); @@ -235,17 +303,17 @@ namespace netstream * * @return the unspoiled integer value (between -32768 and 32767) */ - int NetStreamStorage::readShort() throw(std::invalid_argument) + int NetStreamStorage::readShort() { short value = 0; unsigned char *p_value = reinterpret_cast(&value); - readByEndianess(p_value, 2); + readByEndianess(p_value, sizeof(short)); return value; } // ---------------------------------------------------------------------- - void NetStreamStorage::writeShort( int value ) throw(std::invalid_argument) + void NetStreamStorage::writeShort( int value ) { if (value < -32768 || value > 32767) { @@ -254,7 +322,8 @@ namespace netstream short svalue = static_cast(value); unsigned char *p_svalue = reinterpret_cast(&svalue); - writeByEndianess(p_svalue, 2); + writeByEndianess(p_svalue, sizeof(short)); + } @@ -266,20 +335,20 @@ namespace netstream * * @return the unspoiled integer value (between -2.147.483.648 and 2.147.483.647) */ - GS_INT NetStreamStorage::readInt() throw(std::invalid_argument) + int NetStreamStorage::readInt() { - GS_INT value = 0; + int value = 0; unsigned char *p_value = reinterpret_cast(&value); - readByEndianess(p_value, sizeof(GS_INT)); + readByEndianess(p_value, sizeof(int)); return value; } // ---------------------------------------------------------------------- - void NetStreamStorage::writeInt( int value ) throw() + void NetStreamStorage::writeInt( int value ) { unsigned char *p_value = reinterpret_cast(&value); - writeByEndianess(p_value, sizeof(GS_INT)); + writeByEndianess(p_value, sizeof(int)); } // ---------------------------------------------------------------------- @@ -290,21 +359,38 @@ namespace netstream * * @return the unspoiled integer value (between -??? and ???) */ - GS_LONG NetStreamStorage::readLong() throw(std::invalid_argument) + long NetStreamStorage::readLong() { - GS_LONG value = (GS_LONG)0L; + long value = 0L; unsigned char *p_value = reinterpret_cast(&value); - readByEndianess(p_value, sizeof(GS_LONG)); + readByEndianess(p_value, sizeof(long)); return value; } // ---------------------------------------------------------------------- - void NetStreamStorage::writeLong( GS_LONG value ) throw() + void NetStreamStorage::writeLong( long value ) + { + unsigned char *p_value = reinterpret_cast(&value); + writeByEndianess(p_value, sizeof(long)); + } + + + + long long NetStreamStorage::readLongLong() { + long long value = 0L; unsigned char *p_value = reinterpret_cast(&value); - writeByEndianess(p_value, sizeof(GS_LONG)); + readByEndianess(p_value, sizeof(long long)); + return value; } + + void NetStreamStorage::writeLongLong( long long value ) + { + unsigned char *p_value = reinterpret_cast(&value); + writeByEndianess(p_value, sizeof(long long)); + } + // ---------------------------------------------------------------------- @@ -315,37 +401,37 @@ namespace netstream * * @return the unspoiled float value */ - GS_FLOAT NetStreamStorage::readFloat() throw(std::invalid_argument) + float NetStreamStorage::readFloat() { - GS_FLOAT value = 0; + float value = 0; unsigned char *p_value = reinterpret_cast(&value); - readByEndianess(p_value, sizeof(GS_FLOAT)); + readByEndianess(p_value, 4); return value; } // ---------------------------------------------------------------------- - void NetStreamStorage::writeFloat( GS_FLOAT value ) throw() + void NetStreamStorage::writeFloat( float value ) { unsigned char *p_value = reinterpret_cast(&value); - writeByEndianess(p_value, sizeof(GS_FLOAT)); + writeByEndianess(p_value, 4); } // ---------------------------------------------------------------------- - void NetStreamStorage::writeDouble( GS_DOUBLE value ) throw () + void NetStreamStorage::writeDouble( double value ) { unsigned char *p_value = reinterpret_cast(&value); - writeByEndianess(p_value, sizeof(GS_DOUBLE)); + writeByEndianess(p_value, 8); } // ---------------------------------------------------------------------- - GS_DOUBLE NetStreamStorage::readDouble( ) throw (std::invalid_argument) + double NetStreamStorage::readDouble( ) { - GS_DOUBLE value = 0; + double value = 0; unsigned char *p_value = reinterpret_cast(&value); - readByEndianess(p_value, sizeof(GS_DOUBLE)); + readByEndianess(p_value, 8); return value; } @@ -368,7 +454,7 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::checkReadSafe(unsigned int num) const throw(std::invalid_argument) + void NetStreamStorage::checkReadSafe(unsigned int num) const { if (std::distance(iter_, store.end()) < static_cast(num)) { @@ -390,29 +476,29 @@ namespace netstream // ---------------------------------------------------------------------- - void NetStreamStorage::writeByEndianess(const unsigned char * b, unsigned int sz) + void NetStreamStorage::writeByEndianess(const unsigned char * begin, unsigned int size) { - const unsigned char * e = &(b[sz]); + const unsigned char * end = &(begin[size]); if (bigEndian_) - store.insert(store.end(), b, e); + store.insert(store.end(), begin, end); else - store.insert(store.end(), std::reverse_iterator(e), std::reverse_iterator(b)); + store.insert(store.end(), std::reverse_iterator(end), std::reverse_iterator(begin)); iter_ = store.begin(); } // ---------------------------------------------------------------------- - void NetStreamStorage::readByEndianess(unsigned char * array, int sz) + void NetStreamStorage::readByEndianess(unsigned char * array, int size) { - checkReadSafe(sz); + checkReadSafe(size); if (bigEndian_) { - for (int i = 0; i < sz; ++i) + for (int i = 0; i < size; ++i) array[i] = readCharUnsafe(); } else { - for (int i = sz - 1; i >= 0; --i) + for (int i = size - 1; i >= 0; --i) array[i] = readCharUnsafe(); } } @@ -430,7 +516,7 @@ namespace netstream ostream &operator<<( ostream &out, const NetStreamStorage & s) { - out< 127 - if(data < ((GS_LONG)1L << 7)){ - return 1; - } - - // 14 bits -> 16383 - if(data < ((GS_LONG)1L << 14)){ - return 2; - } - - // 21 bits -> 2097151 - if(data < ((GS_LONG)1L << 21)){ - return 3; - } - - // 28 bits -> 268435455 - if(data < ((GS_LONG)1L << 28)){ - return 4; - } - - // 35 bits -> 34359738367 - if(data < ((GS_LONG)1L << 35)){ - return 5; - } - - // 42 bits -> 4398046511103 - if(data < ((GS_LONG)1L << 42)){ - return 6; - } - - // 49 bits -> 562949953421311 - if(data < ((GS_LONG)1L << 49)){ - return 7; - } - - // 56 bits -> 72057594037927935 - if(data < ((GS_LONG)1L << 56)){ - return 8; - } - - return 9; -} - - -// ---------------------------------------------------------------------- -void NetStreamStorage::writeUnsignedVarInt( GS_LONG data ) throw() -{ - int sz = varintSize(data); - unsigned char *p_value = (unsigned char*)malloc(sz); - for(int i = 0; i < sz; i++){ - int head=128; - if(i==sz-1) head = 0; - GS_LONG b = ((data >> (7*i)) & 127) ^ head; - p_value[sz-1-i] = ((unsigned char)(b & 255)); - } - writeByEndianess(p_value, sz); - // free(p_value); -} - - -/* - protected ByteBuffer encodeUnsignedVarint(Object in) { - long data = ((Number)in).longValue(); - - int size = varintSize(data); - - ByteBuffer buff = ByteBuffer.allocate(size); - for(int i = 0; i < size; i++){ - int head=128; - if(i==size-1) head = 0; - long b = ((data >> (7*i)) & 127) ^ head; - buff.put((byte)(b & 255 )); - } - buff.rewind(); - return buff; - } - - - private void putVarint(ByteBuffer buffer, long number, int byteSize) { - for(int i = 0; i < byteSize; i++){ - int head=128; - if(i==byteSize-1) head = 0; - long b = ((number >> (7*i)) & 127) ^ head; - buffer.put((byte)(b & 255 )); - } - } -*/ } diff --git a/graphstream/gs-netstream/c++/src/netstream-storage.h b/graphstream/gs-netstream/c++/src/netstream-storage.h index ed4912a6e..4a49d80b7 100644 --- a/graphstream/gs-netstream/c++/src/netstream-storage.h +++ b/graphstream/gs-netstream/c++/src/netstream-storage.h @@ -17,7 +17,7 @@ #include #include #include -#include "netstream-sizes.h" +#include namespace netstream { @@ -41,7 +41,7 @@ class NetStreamStorage void init(); /// Check if the next \p num bytes can be read safely - void checkReadSafe(unsigned int num) const throw(std::invalid_argument); + void checkReadSafe(unsigned int num) const; /// Read a byte \em without validity check unsigned char readCharUnsafe(); /// Write \p size elements of array \p begin according to endianess @@ -56,8 +56,8 @@ class NetStreamStorage /// Standard Constructor NetStreamStorage(); - /// Constructor, that fills the storage with an char array. - NetStreamStorage(unsigned char[], size_t length); + /// Constructor, that fills the storage with an char array. If length is -1, the whole array is handed over + NetStreamStorage(unsigned char[], int length=-1); // Destructor virtual ~NetStreamStorage(); @@ -67,45 +67,52 @@ class NetStreamStorage void reset(); - virtual unsigned char readChar() throw(std::invalid_argument); - virtual void writeChar(unsigned char) throw(); + virtual size_t varintSize(uint_fast64_t); + virtual uint_fast64_t readUnsignedVarint() ; + virtual void writeUnsignedVarint(uint_fast64_t) ; - virtual GS_INT readByte() throw(std::invalid_argument); - virtual void writeByte(GS_INT) throw(std::invalid_argument); -// virtual void writeByte(unsigned char) throw(); + virtual int_fast64_t readVarint() ; + virtual void writeVarint(int_fast64_t) ; - virtual int readUnsignedByte() throw(std::invalid_argument); - virtual void writeUnsignedByte(int) throw(std::invalid_argument); + virtual unsigned char readChar() ; + virtual void writeChar(unsigned char) ; - virtual std::string readString() throw(std::invalid_argument); - virtual void writeString(const std::string& s) throw(); + virtual int readByte() ; + virtual void writeByte(int) ; +// virtual void writeByte(unsigned char) ; - virtual std::vector readStringList() throw(std::invalid_argument); - virtual void writeStringList(const std::vector &s) throw(); + virtual int readUnsignedByte() ; + virtual void writeUnsignedByte(int) ; - virtual GS_INT readShort() throw(std::invalid_argument); - virtual void writeShort(GS_INT) throw(std::invalid_argument); + virtual std::string readString() ; + virtual void writeString(const std::string& s) ; - virtual GS_INT readInt() throw(std::invalid_argument); - virtual void writeInt(GS_INT) throw(); + virtual std::vector readStringList() ; + virtual void writeStringList(const std::vector &s) ; - GS_INT varintSize(GS_LONG data); - virtual void writeUnsignedVarInt(GS_LONG data) throw(); + virtual int readShort() ; + virtual void writeShort(int) ; - virtual GS_LONG readLong() throw(std::invalid_argument); - virtual void writeLong(GS_LONG) throw(); + virtual int readInt() ; + virtual void writeInt(int) ; - virtual GS_FLOAT readFloat() throw(std::invalid_argument); - virtual void writeFloat( GS_FLOAT ) throw(); + virtual long readLong() ; + virtual void writeLong(long) ; - virtual GS_DOUBLE readDouble() throw(std::invalid_argument); - virtual void writeDouble( GS_DOUBLE ) throw(); + virtual long long readLongLong() ; + virtual void writeLongLong(long long) ; + + virtual float readFloat() ; + virtual void writeFloat( float ) ; + + virtual double readDouble() ; + virtual void writeDouble( double ) ; virtual void writePacket(unsigned char* packet, int length); virtual void writeStorage(netstream::NetStreamStorage& store); - virtual NetStreamStorage operator+(const NetStreamStorage & storage); + virtual NetStreamStorage operator+(const NetStreamStorage & storage); // Some enabled functions of the underlying std::list