Skip to content

Commit

Permalink
Implemented call function.
Browse files Browse the repository at this point in the history
Improved Log.
Improved c++ api for msgunpack.
  • Loading branch information
alvistar committed Nov 30, 2015
1 parent a8cf8a1 commit 81c2dfa
Show file tree
Hide file tree
Showing 15 changed files with 243 additions and 56 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ if(YOTTA_CFG_MBED)
else(YOTTA_CFG_MBED)
cmake_minimum_required(VERSION 3.3)
project(wamp_mbed)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Werror -Wall -Wextra -pedantic")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -Wextra -pedantic")


endif(YOTTA_CFG_MBED)
Expand Down
15 changes: 12 additions & 3 deletions source/AppDesktop.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <iostream>
#include "WampMBED.h"
#include "Wamp.h"
#include "logger.h"
#include "MpackPrinter.h"
#include "WampTransportRaw.h"
Expand Down Expand Up @@ -34,10 +34,19 @@ int main() {

wamp->publish("test", MsgPackArr {"hello"}, MsgPackMap {});

wamp->subscribe("com.example.oncounter", [](mpack_node_t &args, mpack_node_t &kwargs) {
wamp->subscribe("com.example.oncounter", [](MPNode args, MPNode kwargs) {
(void) kwargs;
LOG("Received event: " << MpackPrinter(args).toJSON());
LOG("Received event: " << args.toJson());
});

wamp->call("com.example.add", MsgPackArr {20,3}, MsgPackMap{},
[](WampError *err, MPNode args, MPNode kwargs) {
if (!err) {
LOG("Received result:" << args.toJson());
}

});

});


Expand Down
2 changes: 1 addition & 1 deletion source/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
include_directories(../mpack/src)

LIST(APPEND SOURCES MsgPackCPP.cpp MsgUnpack.cpp MpackPrinter.cpp
WampMBED.cpp
Wamp.cpp
WampTransportWS.cpp
WampTransportRaw.cpp
SocketTypes.cpp)
Expand Down
8 changes: 8 additions & 0 deletions source/LogConfig.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef LOGCONFIG_H
#define LOGCONFIG_H

//#define DEBUG_WAMP_TRANSPORT //Rawsocket/Websocket Layer transport
//#define DEBUG_WAMP_SOCKET //Low level socket layer
#define DEBUG_WAMP //Wamp Protocol Layer

#endif //LOGCONFIG_H
2 changes: 1 addition & 1 deletion source/MsgPackCPP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ void MsgPack::pack(MsgPack mp) {

std::string MsgPack::getJson() {
MsgUnpack unp {getData(), getUsedBuffer()};
return unp.toJson();
return unp.getRoot().toJson();
}

52 changes: 48 additions & 4 deletions source/MsgUnpack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "MsgUnpack.h"
#include "MpackPrinter.h"
#include <sstream>
#include <string>
#include <memory>

MsgUnpack::MsgUnpack(char *buffer, size_t size) {
mpack_tree_init(&tree, buffer, size);
Expand All @@ -14,16 +16,58 @@ MsgUnpack::~MsgUnpack() {
mpack_tree_destroy(&tree);
}

mpack_node_t MsgUnpack::getRoot() {
return mpack_tree_root(&tree);
MPNode MsgUnpack::getRoot() {
return MPNode(mpack_tree_root(&tree));
}

mpack_error_t MsgUnpack::getError() {
return tree.error;
}

std::string MsgUnpack::toJson() {
MpackPrinter printer(getRoot());
std::string MPNode::toJson() {
MpackPrinter printer(node);
return printer.toJSON();
}

MPNode MPNode::operator[](const u_int16_t &index) {
return at(index);
}

MPNode MPNode::at(const u_int16_t &index, bool ignore_errors) {

if (mpack_node_error(node) != mpack_ok)
return MPNode(mpack_tree_nil_node(node.tree));

if (node.data->type != mpack_type_array) {
if (!ignore_errors)
mpack_node_flag_error(node, mpack_error_type);

return MPNode(mpack_tree_nil_node(node.tree));
}

if (index >= node.data->value.content.n) {
if (!ignore_errors)
mpack_node_flag_error(node, mpack_error_data);
return MPNode(mpack_tree_nil_node(node.tree));
}

return MPNode(mpack_node(node.tree, mpack_node_child(node, index)));
}


size_t MPNode::arrayLength() {
return mpack_node_array_length(node);
}

MPNode::operator std::string() {
if (mpack_node_error(node) != mpack_ok) {
return std::string();
}

if (node.data->type != mpack_type_str) {
mpack_node_flag_error(node, mpack_error_type);
return std::string();
}

return std::string(mpack_node_data(node), mpack_node_strlen(node));
}
40 changes: 37 additions & 3 deletions source/MsgUnpack.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,53 @@
#include "mpack/mpack-node.h"
#include <string>

class MPNode {
private:

public:
mpack_node_t node;
MPNode(mpack_node_t node):node(node){};
MPNode();

MPNode operator[](const u_int16_t &index);
MPNode at(const u_int16_t &index, bool ignore_errors=false);

size_t arrayLength ();
std::string toJson();

operator std::string();

operator int() {
return mpack_node_i16(node);
}

operator u_int16_t () {
return mpack_node_u16(node);
}

operator unsigned long long int() {
return mpack_node_u64(node);
}
};

class MsgUnpack {
private:
mpack_tree_t tree;
public:
MsgUnpack(char* buffer, size_t size);
~MsgUnpack();

mpack_node_t getRoot();
MPNode getRoot();
MPNode nil() {
return MPNode(mpack_node(&tree, &tree.nil_node));
};

mpack_error_t getError();
std::string toJson();

static void nodeToJson(mpack_node_t node, std::stringstream &s);

};




#endif //WAMP_MBED_MSGUNPACK_H
3 changes: 1 addition & 2 deletions source/SocketMBED.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
#include "SocketMBED.h"
#include "sal-stack-lwip/lwipv4_init.h"

#define DEBUG_WAMP_SOCKET

#include "LogConfig.h"
#ifdef DEBUG_WAMP_SOCKET
#include "logger.h"
#else
Expand Down
4 changes: 2 additions & 2 deletions source/SocketOSX.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "SocketOSX.h"
#include <netdb.h>
#include <stdlib.h>
#include <sys/errno.h>

#define DEBUG_WAMP_SOCKET

#include "LogConfig.h"
#ifdef DEBUG_WAMP_SOCKET
#include "logger.h"
#else
Expand Down
Loading

0 comments on commit 81c2dfa

Please sign in to comment.