Skip to content

Commit

Permalink
merged github
Browse files Browse the repository at this point in the history
  • Loading branch information
espeed committed Apr 4, 2012
2 parents 040313c + ecf5d45 commit bb115d7
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 21 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -6,10 +6,14 @@ old/
.tox/
.cache/
.coverage
*.o
*.d
*.egg
*.egg-info/
*.pyc
*.pyo
*.class
*.project
*.cproject
*~

15 changes: 14 additions & 1 deletion README
Expand Up @@ -35,7 +35,10 @@ on automating this real soon now):

See http://www.zeromq.org/docs:source-git

* jzmq, the Java ZeroMQ binding (install using maven):
* jzmq, (for detailed install instructions, see
http://lists.zeromq.org/pipermail/zeromq-dev/2011-October/013762.html). The Java
ZeroMQ binding (As jzmq is both, Java and C++ Code, you might have to use two
build tools):

$ git clone https://github.com/zeromq/jzmq.git
$ cd jzmq
Expand All @@ -44,6 +47,16 @@ on automating this real soon now):
$ ./make
$ sudo ./make install
$ mvn clean install
* Build using make:
$ ./autogen.sh
$ ./configure.sh
$ ./make
$ sudo ./make install
If you don't install the C++ libs in a regular system lib path,
you will have to introduce them to Java / Jython via
$ java -Djava.library.path=/path/to/lib -classpath /path/to/zeromq/jar/

* Jython >=2.5.1 (http://www.jython.org/downloads.html)

* Jackson JSON (http://jackson.codehaus.org/)

Expand Down
24 changes: 24 additions & 0 deletions cppclient/Makefile
@@ -0,0 +1,24 @@
LIBRARY_NAME = rexterCppClient
LIB_SRC_DIR = ./src/$(LIBRARY_NAME)/
TEST_DIR = ./src/test/
CXX = g++
CPPFLAGS =
LIBS := -lzmq -ljson
BUILD_DIR = ./build/
INCLUDES = -I/usr/local/include -I./$(LIB_SRC_DIR)
INSTALL_DIR = /usr/local/

main: $(LIBRARY_NAME).so $(TEST_DIR)main.cpp
$(CXX) $(INCLUDES) -o $(BUILD_DIR)main $(TEST_DIR)main.cpp -L$(BUILD_DIR) -l$(LIBRARY_NAME) $(LIBS)

$(LIBRARY_NAME).so: $(LIB_SRC_DIR)/$(LIBRARY_NAME).cpp
$(CXX) $(INCLUDES) -MMD -std=c++0x -pipe -fPIC -c $(LIB_SRC_DIR)/$(LIBRARY_NAME).cpp -o $(BUILD_DIR)$(LIBRARY_NAME).o $(LIBS)
$(CXX) -shared -Wl,-soname=$(LIBRARY_NAME).so -o $(BUILD_DIR)$(LIBRARY_NAME).so $(BUILD_DIR)$(LIBRARY_NAME).o
ln $(BUILD_DIR)$(LIBRARY_NAME).so $(BUILD_DIR)lib$(LIBRARY_NAME).so

clean:
rm $(BUILD_DIR)$(LIBRARY_NAME).o $(BUILD_DIR)$(LIBRARY_NAME).d $(BUILD_DIR)$(LIBRARY_NAME).so $(BUILD_DIR)lib$(LIBRARY_NAME).so $(BUILD_DIR)main

install:
sudo cp $(BUILD_DIR)$(LIBRARY_NAME).so $(BUILD_DIR)lib$(LIBRARY_NAME).so $(INSTALL_DIR)lib/
sudo cp $(LIB_SRC_DIR)/$(LIBRARY_NAME).h $(INSTALL_DIR)include/
104 changes: 104 additions & 0 deletions cppclient/src/rexterCppClient/rexterCppClient.cpp
@@ -0,0 +1,104 @@
//============================================================================
// Name : cppclient.cpp
// Author : Andreas Hermann
// Version : 0.1
// Copyright :
// Description : Hello Neo4J. From C++ to Neo4J and back
//============================================================================

// Bugs: send and receive always returns a EAGAIN error
// Todo: fix EAGAIN error

#include "rexterCppClient.h"

rexterCppClient::rexterCppClient(std::string zsock_addr)
: connected(false)
{
connect(zsock_addr);
}

rexterCppClient::~rexterCppClient()
{
if(connected)
{
disconnect();
}
}

int rexterCppClient::connect(std::string zsock_addr)
{
zcontext = zmq_init(1);
if (!zcontext) {
std::cout << "Error occurred during zmq_init(): " << zmq_strerror(zmq_errno()) << std::endl;
return -1;
}
zsock = zmq_socket(zcontext, ZMQ_REQ);
if (!zsock) {
std::cout << "Error occurred during zmq_socket(): " << zmq_strerror(zmq_errno()) << std::endl;
return -1;
}
if(zmq_connect(zsock, zsock_addr.c_str()) != 0)
{
std::cout << "Error occurred during zmq_connect(): " << zmq_strerror(zmq_errno()) << std::endl;
return -1;
}
connected = true;
return 0;
}

int rexterCppClient::disconnect()
{
if(connected)
{
std::cout << "Disconnecting from zsock" << std::endl;
zmq_close(zsock);
zmq_term(zcontext);
connected = false;
}
return 0;
}

int rexterCppClient::send_request(const Json::Value &path, const Json::Value &params, const Json::Value &data, Json::Value &answer)
{

// Code for sending:
Json::Value js_msg(Json::objectValue);
js_msg["data"] = data;
js_msg["params"] = params;
js_msg["path"] = path;
std::string msg_to_send;
msg_to_send = js_writer.write(js_msg);

//std::cout << "Trying to send " << msg_to_send << std::endl;

if(zmq_send(zsock, msg_to_send.c_str(), msg_to_send.length(), 0) != 0)
{
std::cout << "Error sending message to server: " << zmq_errno() << " "
<< zmq_strerror(zmq_errno()) << std::endl;
//return -1;
}

// Code for receiving:
zmq_msg_t message;
zmq_msg_init(&message);
if(zmq_recvmsg(zsock, &message, 0) != 0)
{
std::cout << "Error receiving the answer from server: "
<< zmq_strerror(zmq_errno()) << std::endl;
//return -1;
}
std::string str_message(static_cast<char*> (zmq_msg_data(&message)), zmq_msg_size(&message));
zmq_msg_close (&message);

// JSon parsing:
Json::Value js_rcv;
if(js_reader.parse(str_message, js_rcv, false))
{
std::cout << "Received: " << js_rcv << std::endl; // prints !!!Hello World!!!
return 0;
}else{
std::cout << "Error parsing the answer from server" << std::endl;
return -1;
}
}

48 changes: 48 additions & 0 deletions cppclient/src/rexterCppClient/rexterCppClient.h
@@ -0,0 +1,48 @@
/*
* cppclient.h
*
* Created on: 04.10.2011
* Author: hermann-local
*/

#ifndef CPPCLIENT_H_
#define CPPCLIENT_H_

#include <iostream>
#include "zmq.h"
#include "stdio.h"
#include "string.h"
#include "json/json.h"


class rexterCppClient
{
public:

rexterCppClient(std::string zsock_addr);
~rexterCppClient();

// Send request and receive answer from server
int send_request(const Json::Value &path, const Json::Value &params, const Json::Value &data, Json::Value &answer);

protected:

private:

// Open and close ZeroMQ socket connection
int connect(std::string zsock_addr);
int disconnect();

// Verbose
void display_progress();

bool connected;
void * zsock;
void * zcontext;

Json::FastWriter js_writer;
Json::Reader js_reader;

};

#endif /* CPPCLIENT_H_ */
34 changes: 34 additions & 0 deletions cppclient/src/test/main.cpp
@@ -0,0 +1,34 @@
/*
* main.cpp
*
* Created on: 04.10.2011
* Author: hermann-local
*/

#include "rexterCppClient.h"

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

rexterCppClient* client = new rexterCppClient("tcp://localhost:5555");

Json::Value js_data(Json::objectValue);
Json::Value js_params(Json::objectValue);
Json::Value js_answer;

Json::Value js_path("wordgraph/vertices/create");
js_data["city"] = "Dallas";
js_data["name"] = "Frank";


for( int i = 0; i < 1; i++)
{
js_data["counter"] = i;
client->send_request(js_path, js_params, js_data, js_answer);
}
delete client;

return 0;
}


6 changes: 3 additions & 3 deletions init.py
Expand Up @@ -10,12 +10,12 @@
example = Example()

# WordGraph Resource
wordgraph = Neo4jGraph("/home/james/data/wordgraph")
wordgraph.graph.setMaxBufferSize(1000)
#wordgraph = Neo4jGraph("/home/james/data/wordgraph")
#wordgraph.graph.setMaxBufferSize(1000)

# Initialize the server, add your resources, and start the server
server = Server()
server.add_resource("example",example)
server.add_resource("wordgraph",wordgraph)
#server.add_resource("wordgraph",wordgraph)
server.start()

12 changes: 1 addition & 11 deletions lightsocket/resources/blueprints.py
Expand Up @@ -6,19 +6,13 @@

from java.lang import Long

#from com.tinkerpop.rexster import AbstractSubResource
#from com.tinkerpop.rexster import RexsterApplicationProvider
from com.tinkerpop.rexster.util import ElementHelper
from com.tinkerpop.blueprints.pgm.impls.neo4j import Neo4jGraph
from com.tinkerpop.blueprints.pgm.impls.neo4jbatch import Neo4jBatchGraph
#from com.tinkerpop.pipes.util import FluentPipeline

from lightsocket.server import Resource, Response, Router

# These are needed to set typed values via Rexster's property type system
#class BatchResource(AbstractSubResource): pass
#class BatchRap(RexsterApplicationProvider): pass
#batch_resource = BatchResource(BatchRap())
from lightsocket.server import Resource, Response, Router

class VertexProxy(Resource):

Expand All @@ -36,10 +30,6 @@ def create(self,request):
for key, value in request.data.items():
if key == "element_type":
element_type = value
#value = batch_resource.getTypedPropertyValue(str(value))
#print "VALUE", value
# not using Rexster type system anymore
#value = ElementHelper.getTypedPropertyValue(str(value))
try:
vertex.setProperty(key,value)
except:
Expand Down
2 changes: 1 addition & 1 deletion lightsocket/server.py
Expand Up @@ -175,7 +175,7 @@ def receive_requests(self):
# max req/sec with one python client: 10174 req/sec
# python client json serialization drops 10174 to 8153
# jython server json read drops it from 8153 to 6302
request = Request(self.mapper.readValue(raw,HashMap))
request = Request(self.mapper.readValue(raw.tostring(),HashMap))
# router drops is from 6302 to 4600 (this is the only thing to optimize)
resp = self.router.get(request)
# re-serialization drops it from 4600 to 4282 (you don't have to serialize)
Expand Down
15 changes: 13 additions & 2 deletions client_example.py → pyclient/client_example.py
Expand Up @@ -38,7 +38,7 @@ def send_request(self,path,params,data):
self.socket.send(message)
raw = self.socket.recv()
resp = json.loads(raw)
return raw
return resp

def start_timer(self):
self.start_time = time.time()
Expand All @@ -63,6 +63,17 @@ def run_test(self,requests=30000):
self.display_progress()
self.stop_timer()

# Example how to add node to the wordgraph DB
def create_node(self):
path = "wordgraph/vertices/create"
data = dict(name="James",city="Dallas")
params = None
resp = self.send_request(path,params,data)
print resp
node_id = resp['results']['_id']
return node_id


client = ClientExample()
client.run_test()

#client.create_node()
16 changes: 13 additions & 3 deletions startup.sh
Expand Up @@ -4,13 +4,25 @@
# BSD License (see LICENSE for details)
#

export CLASSPATH=$CLASSPATH:/home/hermann-local/src/jzmq/src/zmq.jar
export CLASSPATH=$CLASSPATH:/home/hermann-local/src/google-gson-1.7.1/gson-1.7.1.jar
export CLASSPATH=$CLASSPATH:/home/hermann-local/src/jackson/trunk/regression/*
export CLASSPATH=$CLASSPATH:/home/hermann-local/src/rexster/target/rexster-0.7-SNAPSHOT-standalone/lib/*
export CLASSPATH=$CLASSPATH:/home/hermann-local/src/blueprints/blueprints-core/target/blueprints-core-1.1-SNAPSHOT.jar
export CLASSPATH=$CLASSPATH:/home/hermann-local/src/blueprints/blueprints-dex-graph/target/blueprints-dex-graph-1.1-SNAPSHOT.jar
export CLASSPATH=$CLASSPATH:/home/hermann-local/src/blueprints/blueprints-neo4jbatch-graph/target/blueprints-neo4jbatch-graph-1.1-SNAPSHOT.jar

# Modify this to point to your Rexster root dir and lib dir
REXSTER_DIR=/home/james/packages/tinkerpop/rexster
REXSTER_LIB="${REXSTER_DIR}/rexster-server/target/rexster-server-0.9-SNAPSHOT-standalone/lib/*"

# Modify this to point to your Jython dir
JYTHON_DIR=/home/james/packages/jython2.5.2

# Modify this to point to your lib dir with the jzmq libs
# (Dir not needed, when JZMQ libs are installed in regular sytem lib path)
JZMQ_LIB_DIR=/usr/local/lib

#
# You don't need to modify anyting past this point.
#
Expand All @@ -21,7 +33,5 @@ export CLASSPATH=$CLASSPATH:$DIR:$REXSTER_LIB;

# TODO: catch CTRL-C and do server shutdown()

$JYTHON_DIR/bin/jython $DIR/init.py


$JYTHON_DIR/bin/jython -Djava.library.path=$JZMQ_LIB_DIR $DIR/init.py

0 comments on commit bb115d7

Please sign in to comment.