Permalink
Browse files

Added WebSocketClient library.

  • Loading branch information...
CodeShark committed Jul 30, 2014
1 parent 3db2da5 commit 0f58a494c609284627a9195f86d53b5f6351138f
@@ -142,6 +142,10 @@ then
exit
fi
cd ../WebSocketClient
make libs OS=$OS $OPTIONS
SYSROOT=../../sysroot make install
cd $CURRENT_DIR
${QMAKE_PATH}qmake $SPEC CONFIG+=$BUILD_TYPE && make $OPTIONS
@@ -16,6 +16,9 @@ make clean
cd ../CoinDB
make clean
cd ../WebSocketClient
make clean
cd ../../sysroot
rm -rf include
rm -rf lib
@@ -16,6 +16,9 @@ make clean
cd ../CoinDB
make clean
cd ../WebSocketClient
make clean
cd ../../sysroot
rm -rf include
rm -rf lib
@@ -0,0 +1,149 @@
CXXFLAGS += -Wall -O3
ifndef OS
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S), Linux)
OS = linux
else ifeq ($(UNAME_S), Darwin)
OS = osx
endif
endif
LOCAL_SYSROOT = ../../sysroot
WEBSOCKETPP = ../websocketpp
INCLUDE_PATH += \
-Isrc \
-I$(WEBSOCKETPP)
ifneq ($(wildcard $(LOCAL_SYSROOT)/include),)
INCLUDE_PATH += -I$(LOCAL_SYSROOT)/include
endif
LIB_PATH += \
-Llib
ifneq ($(wildcard $(LOCAL_SYSROOT)/lib),)
LIB_PATH += -L$(LOCAL_SYSROOT)/lib
endif
ifeq ($(OS), linux)
ifndef SYSROOT
SYSROOT = /usr/local
endif
CXX = g++
CC = gcc
CXXFLAGS += -Wno-unknown-pragmas -std=c++0x -DBOOST_SYSTEM_NOEXCEPT=""
ARCHIVER = ar
PLATFORM_LIBS += \
-lpthread
else ifeq ($(OS), mingw64)
ifndef SYSROOT
SYSROOT = /usr/x86_64-w64-mingw32
endif
CXX = x86_64-w64-mingw32-g++
CC = x86_64-w64-mingw32-gcc
CXXFLAGS += -Wno-unknown-pragmas -Wno-strict-aliasing -std=c++0x -DBOOST_SYSTEM_NOEXCEPT=""
INCLUDE_PATH += -I$(SYSROOT)/include
ARCHIVER = x86_64-w64-mingw32-ar
BOOST_THREAD_SUFFIX = _win32
BOOST_SUFFIX = -mt-s
PLATFORM_LIBS += \
-static-libgcc -static-libstdc++ \
-lws2_32 \
-lmswsock
EXE_EXT = .exe
else ifeq ($(OS), osx)
ifndef SYSROOT
SYSROOT = /usr/local
endif
CXX = clang++
CC = clang
CXXFLAGS += -Wno-unknown-pragmas -Wno-unneeded-internal-declaration -std=c++11 -stdlib=libc++ -DBOOST_THREAD_DONT_USE_CHRONO -DMAC_OS_X_VERSION_MIN_REQUIRED=MAC_OS_X_VERSION_10_6 -mmacosx-version-min=10.7
INCLUDE_PATH += -I$(SYSROOT)/include
ARCHIVER = ar
BOOST_SUFFIX = -mt
else ifneq ($(MAKECMDGOALS), clean)
$(error OS must be set to linux, mingw64, or osx)
endif
# libboost_random is needed by client
LIBS += \
-lWebSocketClient \
-lJsonRpc \
-lboost_system$(BOOST_SUFFIX) \
-lboost_random$(BOOST_SUFFIX) \
-lboost_thread$(BOOST_THREAD_SUFFIX)$(BOOST_SUFFIX)
all: libs tests
libs: jsonrpc client
# JSON-RPC
jsonrpc: lib/libJsonRpc.a
lib/libJsonRpc.a: obj/JsonRpc.o
$(ARCHIVER) rcs $@ $^
obj/JsonRpc.o: src/JsonRpc.cpp src/JsonRpc.h
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -c $< -o $@
# Client
client: jsonrpc lib/libWebSocketClient.a
lib/libWebSocketClient.a: obj/WebSocketClient.o
$(ARCHIVER) rcs $@ $^
obj/WebSocketClient.o: src/WebSocketClient.cpp src/WebSocketClient.h
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) -c $< -o $@
tests: client_tests
# Client Tests
client_tests: tests/build/CoinSocketClientTest$(EXE_EXT) tests/build/RippleClientTest$(EXE_EXT)
tests/build/CoinSocketClientTest$(EXE_EXT): tests/src/CoinSocketClientTest.cpp lib/libWebSocketClient.a
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) $(LIB_PATH) $< -o $@ $(LIBS) $(PLATFORM_LIBS)
tests/build/RippleClientTest$(EXE_EXT): tests/src/RippleClientTest.cpp lib/libWebSocketClient.a
$(CXX) $(CXXFLAGS) $(INCLUDE_PATH) $(LIB_PATH) $< -o $@ $(LIBS) $(PLATFORM_LIBS)
install: install_jsonrpc install_client
install_jsonrpc:
-mkdir -p $(SYSROOT)/include/WebSocketClient
-rsync -u src/JsonRpc.h $(SYSROOT)/include/WebSocketClient/
-mkdir -p $(SYSROOT)/lib
-rsync -u lib/libJsonRpc.a $(SYSROOT)/lib/
install_client: install_jsonrpc
-mkdir -p $(SYSROOT)/include/WebSocketClient
-rsync -u src/WebSocketClient.h $(SYSROOT)/include/WebSocketClient/
-mkdir -p $(SYSROOT)/lib
-rsync -u lib/libWebSocketClient.a $(SYSROOT)/lib/
remove:
-rm -rf $(SYSROOT)/include/WebSocketClient
-rm $(SYSROOT)/lib/libJsonRpc.a
-rm $(SYSROOT)/lib/libWebSocketClient.a
clean:
-rm -f obj/*.o lib/*.a tests/build/*Test
@@ -0,0 +1 @@
*.a
@@ -0,0 +1 @@
*.o
@@ -0,0 +1,56 @@
///////////////////////////////////////////////////////////////////////////////
//
// JsonExceptions.h
//
// Copyright (c) 2014 Eric Lombrozo
//
// All Rights Reserved.
//
#pragma once
#include <stdutils/customerror.h>
namespace JsonRpc
{
enum ErrorCodes
{
// JSON errors
JSON_INVALID = 10001, // start numbering high so as not to clobber application errors
JSON_MISSING_METHOD,
JSON_INVALID_PARAMETER_FORMAT
};
// JSON EXCEPTIONS
class JsonException : public stdutils::custom_error
{
public:
virtual ~JsonException() throw() { }
const std::string& json() const { return json_; }
protected:
explicit JsonException(const std::string& what, int code, const std::string& json) : stdutils::custom_error(what, code), json_(json) { }
std::string json_;
};
class JsonInvalidException : public JsonException
{
public:
explicit JsonInvalidException(const std::string& json) : JsonException("Invalid JSON.", JSON_INVALID, json) { }
};
class JsonMissingMethodException : public JsonException
{
public:
explicit JsonMissingMethodException(const std::string& json) : JsonException("Missing method.", JSON_MISSING_METHOD, json) { }
};
class JsonInvalidParameterFormatException: public JsonException
{
public:
explicit JsonInvalidParameterFormatException(const std::string& json) : JsonException("Invalid parameter format.", JSON_INVALID_PARAMETER_FORMAT, json) { }
};
}
@@ -0,0 +1,120 @@
///////////////////////////////////////////////////////////////////////////////
//
// JsonRpc.cpp
//
// Copyright (c) 2013-2014 Eric Lombrozo
//
// All Rights Reserved.
#include "JsonRpc.h"
#include "JsonExceptions.h"
using namespace JsonRpc;
using namespace json_spirit;
void Request::setJson(const std::string& json)
{
Value value;
read_string(json, value);
if (value.type() != obj_type)
{
throw JsonInvalidException(json);
}
const Object& obj = value.get_obj();
const Value& method = find_value(obj, "method");
if (method.type() != str_type)
{
throw JsonMissingMethodException(json);
}
const Value& params = find_value(obj, "params");
if (params.is_null())
{
m_params = Array();
}
else if (params.type() != array_type)
{
throw JsonInvalidParameterFormatException(json);
}
else
{
m_params = params.get_array();
}
m_method = method.get_str();
m_id = find_value(obj, "id");
}
std::string Request::getJson() const
{
Object req;
req.push_back(Pair("method", m_method));
req.push_back(Pair("params", m_params));
req.push_back(Pair("id", m_id));
return write_string<Value>(req);
}
void Response::setJson(const std::string& json)
{
Value value;
read_string(json, value);
if (value.type() != obj_type) {
throw JsonInvalidException(json);
}
const Object& obj = value.get_obj();
m_result = find_value(obj, "result");
m_error = find_value(obj, "error");
m_id = find_value(obj, "id");
}
std::string Response::getJson() const
{
Object res;
res.push_back(Pair("result", m_result));
res.push_back(Pair("error", m_error));
res.push_back(Pair("id", m_id));
return write_string<Value>(res);
}
void Response::setResult(const Value& result, const Value& id)
{
m_result = result;
m_error = Value();
m_id = id;
}
void Response::setError(const Value& error, const Value& id)
{
m_result = Value();
m_error = error;
m_id = id;
}
void Response::setError(const std::exception& e, const Value& id)
{
m_result = Value();
Object error;
error.push_back(Pair("message", e.what()));
error.push_back(Pair("code", Value()));
m_error = error;
m_id = id;
}
void Response::setError(const stdutils::custom_error& e, const Value& id)
{
m_result = Value();
Object error;
error.push_back(Pair("message", e.what()));
if (e.has_code()) { error.push_back(Pair("code", e.code())); }
else { error.push_back(Pair("code", Value())); }
m_error = error;
m_id = id;
}
Oops, something went wrong.

0 comments on commit 0f58a49

Please sign in to comment.