Commit b6af8bd2 authored by Yoann Pigné's avatar Yoann Pigné

first C++ implementation of the sender

parent 8a7a77f6
File deleted
BINDIR=bin
OBJDIR=obj
SRCDIR=src
INCDIR=include
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) $(CDBG)
DFLAGS = -I$(INCDIR) -g $(CWARN) -fno-inline -DDEBUG=1
CTAG = ctags
CTAGFILE = filelist
# src, object and bin files
MAIN := netstream-main
HEADERS = $(INCDIR)/global.h
OBJS = \
$(OBJDIR)/tcpip/storage.o \
$(OBJDIR)/tcpip/socket.o \
$(OBJDIR)/netstream-sender.o \
$(OBJDIR)/netstream-main.o
.SECONDARY:
#-- Rules
all : $(MAIN)
$(MAIN) : $(OBJS)
$(CC) $(CFLAGS) $(OBJS) -o $(MAIN)
$(OBJDIR)/%.o: $(SRCDIR)/%.cpp
$(CC) $(CFLAGS) -c $? -o $@
.PHONY : clean depend fresh
tag :
find src/*.cpp include/*.h > filelist
$(CTAG) -L $(CTAGFILE)
clean :
-rm -f $(OBJDIR)/*.o $(PARSE_C) $(PARSE_H)
-rm -f $(OBJDIR)/*/*.o $(PARSE_C) $(PARSE_H)
-rm -f $(SRCDIR)/*.output $(LEX_C)
-rm -f */*~ *~ core
-rm -f $(BINDIR)/*
-rm -f $(MAIN)
fresh : clean all
\ No newline at end of file
/**
*
*
* Copyright (c) 2010-2011 University of Luxembourg
*
*
* @file client_socket.h
* @date 2011-08-21
*
* @author Yoann Pigné
*/
#ifndef CLIENT_SOCKET_H
#define CLIENT_SOCKET_H
class ClientSocket{
public:
ClientSocket();
Initialize();
void SetNonBloking();
void Open(const string & host, int port);
int Send((uint8 *) data, int size);
int Receive(int limit);
}
#endif // CLIENT_SOCKET_H
\ No newline at end of file
/**
*
*
* Copyright (c) 2010-2011 University of Luxembourg
*
*
* @file netstream_constants.h
* @date 2011-08-21
*
* @author Yoann Pigné
*/
#ifndef NETSTREAM_CONSTANTS_H
#define NETSTREAM_CONSTANTS_H
/**
* Followed by an 32-bit signed integer for this protocol version. Certainly useless.
*/
#define EVENT_GETVERSION 0x00
/**
* Not used.
*/
#define EVENT_START 0x01
/**
* Not used.
*/
#define EVENT_END 0x02
//
// ----------------------------------
// GraphStream's graph events
// ----------------------------------
//
/**
* Followed by a node id (TYPE_STRING format)
*/
#define EVENT_ADD_NODE 0x10
/**
* Followed by a node id (TYPE_STRING format)
*/
#define EVENT_DEL_NODE 0x11
/**
* Followed by
* - an edge id (TYPE_STRING format),
* - an source node id (TYPE_STRING format),
* - a target node id (TYPE_STRING format
* - a boolean indicating if directed (TYPE_BOOLEAN format)
*/
#define EVENT_ADD_EDGE 0x12
/**
* Followed by an edge id (TYPE_STRING format)
*/
#define EVENT_DEL_EDGE 0x13
/**
* Followed by double (TYPE_DOUBLE format)
*/
#define EVENT_STEP 0x14
/**
*
*/
#define EVENT_CLEARED 0x15
/**
* Followed by
* - an attribute id (TYPE_STRING format)
* - the attribute TYPE
* - the attribute value
*/
#define EVENT_ADD_GRAPH_ATTR 0x16
/**
* Followed by
* - an attribute id (TYPE_STRING format)
* - the attribute TYPE
* - the attribute old value
* - the attribute new value
*/
#define EVENT_CHG_GRAPH_ATTR 0x17
/**
* Followed by
* - the attribute id (TYPE_STRING format)
*/
#define EVENT_DEL_GRAPH_ATTR 0x18
/**
* Followed by
* - an attribute id (TYPE_STRING format)
* - the attribute TYPE
* - the attribute value
*/
#define EVENT_ADD_NODE_ATTR 0x19
/**
* Followed by
* - an attribute id (TYPE_STRING format)
* - the attribute TYPE
* - the attribute old value
* - the attribute new value
*/
#define EVENT_CHG_NODE_ATTR 0x1a
/**
* Followed by
* - the node id (TYPE_STRING format)
* - the attribute id (TYPE_STRING format)
*/
#define EVENT_DEL_NODE_ATTR 0x1b
/**
* Followed by
* - an attribute id (TYPE_STRING format)
* - the attribute TYPE
* - the attribute value
*/
#define EVENT_ADD_EDGE_ATTR 0x1c
/**
* Followed by
* - an attribute id (TYPE_STRING format)
* - the attribute TYPE
* - the attribute old value
* - the attribute new value
*/
#define EVENT_CHG_EDGE_ATTR 0x1d
/**
* Followed by
* - the edge id (TYPE_STRING format)
* - the attribute id (TYPE_STRING format)
*/
#define EVENT_DEL_EDGE_ATTR 0x1e
// Values types
/**
* Followed by a byte who's value is 0 or 1
*/
#define TYPE_BOOLEAN 0x50
/**
* An array of booleans. Followed by first, a 16-bits integer for the number
* of booleans and then, a list of bytes who's value is 0 or 1
*/
#define TYPE_BOOLEAN_ARRAY 0x51
/**
* Followed by a signed byte [-127,127]
*/
#define TYPE_BYTE 0x52
/**
* An array of bytes. Followed by first, a 16-bits integer for the number
* of integers and then, a list of signed bytes.
*/
#define TYPE_BYTE_ARRAY 0x53
/**
* Followed by an 16-bit signed integer (a short)
*/
#define 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
*/
#define TYPE_SHORT_ARRAY 0x55
/**
* Followed by an 32-bit signed integer
*/
#define TYPE_INT 0x56
/**
* An array of integers. Followed by first, a 16-bits integer for the number
* of integers and then, a list of 32-bit signed integers
*/
#define TYPE_INT_ARRAY 0x57
/**
* Followed by an 64-bit signed integer
*/
#define TYPE_LONG 0x58
/**
* 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
*/
#define TYPE_LONG_ARRAY 0x59
/**
* Followed by a single precision 32-bits floating point number
*/
#define TYPE_FLOAT 0x5a
/**
* Array of double. Followed by first, a 16-bits integer for the number of
* floats and then, a list of 32-bit floats
*/
#define TYPE_FLOAT_ARRAY 0x5b
/**
* Followed by a double precision 64-bits floating point number
*/
#define TYPE_DOUBLE 0x5c
/**
* Array of double. Followed by first, a 16-bits integer for the number of
* doubles and then, a list of 64-bit doubles
*/
#define TYPE_DOUBLE_ARRAY 0x5d
/**
* Array of characters. Followed by first, a 16-bits integer for the size in
* bytes (not in number of characters) of the string, then by the unicode
* string
*/
#define TYPE_STRING 0x5e
/**
* Raw data, good for serialization. Followed by first, a 16-bits integer
* indicating the length in bytes of the dataset, and then the data itself.
*/
#define TYPE_RAW 0x5f
/**
* An type-unspecified array. Followed by first, a
* 16-bits integer indicating the number of elements, and then, the elements
* themselves. The elements themselves have to give their type.
*/
#define TYPE_ARRAY 0x60
#endif
/**
*
*
* Copyright (c) 2010-2011 University of Luxembourg
*
*
* @file netstream_main.cc
* @date 2011-08-22
*
* @author Yoann Pigné
*/
#include <iostream>
#include <sstream>
#include "tcpip/storage.h"
#include "tcpip/socket.h"
#include "netstream-constants.h"
#include "netstream-sender.h"
using namespace tcpip;
using namespace std;
void events_test();
void types_test();
void example();
int main (int argc, char const *argv[])
{
//events_test();
//types_test();
example();
return 0;
}
void example(){
NetStreamSender stream("default","localhost",2001,false);
string style("node{fill-mode:plain;fill-color:#567;size:6px;}");
stream.addGraphAttribute("stylesheet", style);
stream.addGraphAttribute("ui.antialias", true);
stream.addGraphAttribute("layout.stabilization-limit", 0);
for (int i = 0; i < 500; i++) {
stringstream n1;
n1<<i;
stream.addNode(n1.str());
if (i > 0) {
stringstream n2;
n2<<(i-1);
stringstream n3;
n3<<(i/2);
stringstream e1;
e1<<n1.str()<<"-"<<n2.str();
stringstream e2;
e2<<n1.str()<<"-"<<n3.str();
//cout<<"edge :"<<e1.str()<<endl;
stream.addEdge(e1.str(), n1.str(), n2.str(), false);
stream.addEdge(e2.str(), n1.str(), n3.str(), false);
}
}
}
void types_test(){
NetStreamSender stream("default","localhost",2001,true);
stream.addGraphAttribute("int", 1);
stream.addGraphAttribute("float", (float)1);
stream.addGraphAttribute("double", 1.0);
stream.addGraphAttribute("long", 1L);
stream.addGraphAttribute("byte", (char) 0);
stream.addGraphAttribute("boolean", true);
int v[] = {1776,7,4};
vector<int> value(v,v+3);
stream.addGraphAttribute("intArray", value);
float v2[] = {(float)1776.3,(float)7.3};
vector<float> value2(v2,v2+2);
stream.addGraphAttribute("floatArray", value2);
double v3[] = {776.3,.3};
vector<double> value3(v3,v3+2);
stream.addGraphAttribute("doubleArray", value3);
long int v4[] = {1776,7,4};
vector<long int> value4(v4,v4+3);
stream.addGraphAttribute("longArray", value4);
char v5[] = {'0',(char)0,'z'};
vector<char> value5(v5,v5+3);
stream.addGraphAttribute("byteArray",value5 );
bool v6[] = {true,false};
vector<bool> value6(v6,v6+2);
stream.addGraphAttribute("booleanArray", value6);
stream.addGraphAttribute("string", string("true"));
}
void events_test(){
NetStreamSender stream("localhost", 2001);
stream.addNode("node0");
stream.addEdge("edge", "node0", "node1", true);
stream.addNodeAttribute("node0","nodeAttribute", 0);
stream.changeNodeAttribute("node0","nodeAttribute",0, 1);
stream.removeNodeAttribute("node0","nodeAttribute");
stream.addEdgeAttribute("edge","edgeAttribute", 0);
stream.changeEdgeAttribute("edge","edgeAttribute", 0,1);
stream.removeEdgeAttribute("edge","edgeAttribute");
stream.addGraphAttribute("graphAttribute", 0);
stream.changeGraphAttribute("graphAttribute", 0, 1);
stream.removeGraphAttribute("graphAttribute");
stream.stepBegins(1.1);
stream.removeEdge("edge");
stream.removeNode("node0");
stream.graphClear();
}
void crapy_test(){
//NetStreamSender stream = NetStreamSender("default","localhost",2001);
//stream.nodeAdded("node");
string stream = "default";
Socket socket = Socket("localhost",2001);
Storage _stream = Storage();
_stream.writeString(stream);
try
{
socket.connect();
}
catch (tcpip::SocketException &e)
{
cout << "#Error while connecting: " << e.what();
return ;
}
Storage event = Storage();
event.writeByte(EVENT_ADD_NODE);
event.writeString("node0");
//Storage all = Storage();
//all.writeStorage(_stream);
//all.writeStorage(event);
//cout.setf ( ios::hex, ios::basefield ); // set hex as the basefield
//cout.setf ( ios::showbase ); // activate showbase
for(Storage::StorageType::const_iterator it = event.begin(); it != event.end();){
cout<<(int)(*it)<<",";
it++;
}
cout<<endl;
try
{
socket.sendExact(_stream+event);
}
catch (SocketException &e)
{
cout << "Error while sending command: " << e.what();
return ;
}
socket.close();
}
\ No newline at end of file
/**
*
*
* Copyright (c) 2010-2011 University of Luxembourg
*
*
* @file netstream_sender.cc
* @date 2011-08-22
*
* @author Yoann Pigné
*/
#include "netstream-sender.h"
NetStreamSender::NetStreamSender(const string & host, int port):
_stream_name("default"),_host(host),_port(port),_stream(),_socket(host,port),debug(false) {
init();
}
NetStreamSender::NetStreamSender(int port):
_stream_name("default"),_host("localhost"),_port(port),_stream(),_socket("localhost",port),debug(false) {
init();
}
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 string & stream, const string & host, int port, bool debug):
_stream_name(stream),_host(host),_port(port),_stream(),_socket(host,port),debug(debug)
{
init();
}
void NetStreamSender::init()
{
_stream.writeString(_stream_name);
_socket.connect();
}
// ===========================================
// = Values type guess (templates) =
// ===========================================
int NetStreamSender::_getType(char object){
if(debug){
cerr<<"NetStreamSernder: _getType : char"<<endl;
}
return TYPE_BYTE;
}
int NetStreamSender::_getType(bool object){
if(debug){
cerr<<"NetStreamSernder: _getType : bool"<<endl;
}
return TYPE_BOOLEAN;}
int NetStreamSender::_getType(int object){
if(debug){
cerr<<"NetStreamSernder: _getType : int"<<endl;
}
return TYPE_INT;}
int NetStreamSender::_getType(long object){
if(debug){
cerr<<"NetStreamSernder: _getType : long"<<endl;
}
return TYPE_LONG;}
int NetStreamSender::_getType(float object){
if(debug){
cerr<<"NetStreamSernder: _getType : float"<<endl;
}
return TYPE_FLOAT;}
int NetStreamSender::_getType(double object){
if(debug){
cerr<<"NetStreamSernder: _getType : double"<<endl;
}
return TYPE_DOUBLE;}
int NetStreamSender::_getType(const string & object){
if(debug){
cerr<<"NetStreamSernder: _getType : string"<<endl;
}
return TYPE_STRING;}
int NetStreamSender::_getType(const vector<char> & object){
if(debug){
cerr<<"NetStreamSernder: _getType : char* "<<endl;
}
return TYPE_BYTE_ARRAY;}
int NetStreamSender::_getType(const vector <bool> & object){
if(debug){
cerr<<"NetStreamSernder: _getType : bool*"<<endl;
}
return TYPE_BOOLEAN_ARRAY;}
int NetStreamSender::_getType(const vector<int> & object){
if(debug){
cerr<<"NetStreamSernder: _getType : int*"<<endl;
}
return TYPE_INT_ARRAY;}
int NetStreamSender::_getType(const vector<long> & object){
if(debug){
cerr<<"NetStreamSernder: _getType : long*"<<endl;
}
return TYPE_LONG_ARRAY;}
int NetStreamSender::_getType(const vector<float> & object){
if(debug){
cerr<<"NetStreamSernder: _getType : float*"<<endl;
}
return TYPE_FLOAT_ARRAY;}
int NetStreamSender::_getType(const vector <double> & object){
if(debug){
cerr<<"NetStreamSernder: _getType : double*"<<endl;
}
return TYPE_DOUBLE_ARRAY;}
// =================
// = data encoding =
// =================
void NetStreamSender::_encode(Storage & event, char value){
event.writeByte((int)value);
}
void NetStreamSender::_encode(Storage & event, bool value){
event.writeByte(value?1:0);
}
void NetStreamSender::_encode(Storage & event, int value){
event.writeInt(value);
}
void NetStreamSender::_encode(Storage & event, long value){
event.writeLong(value);
}
void NetStreamSender::_encode(Storage & event, float value){
event.writeFloat(value);
}
void NetStreamSender::_encode(Storage & event, double value){
event.writeDouble(value);
}
void NetStreamSender::_encode(Storage & event,const string & value){
event.writeString(value);
}
void NetStreamSender::_encode(Storage & event, const vector<char> & value){
event.writeInt(value.size());
for(vector<char>::const_iterator i = value.begin(); i != value.end(); i++){
event.writeByte((*i));
}
}
void NetStreamSender::_encode(Storage & event, const vector<bool> & value){
event.writeInt(value.size());
for(vector<bool>::const_iterator i = value.begin(); i != value.end(); i++){
event.writeByte((*i));
}
}
void NetStreamSender::_encode(Storage & event, const vector<int> & value){
event.writeInt(value.size());
for(vector<int>::const_iterator i = value.begin(); i != value.end(); i++){
event.writeInt((*i));
}
}
void NetStreamSender::_encode(Storage & event, const vector<long> & value){
event.writeInt(value.size());
for(vector<long>::const_iterator i = value.begin(); i != value.end(); i++){
event.writeLong((*i));
}
}
void NetStreamSender::_encode(Storage & event, const vector<float> & value){