Skip to content

Commit

Permalink
implemented sync/async message handling, transport abstraction, deplo…
Browse files Browse the repository at this point in the history
…yment
  • Loading branch information
loentar committed Dec 18, 2015
1 parent 1036696 commit 92c3726
Show file tree
Hide file tree
Showing 58 changed files with 1,538 additions and 513 deletions.
14 changes: 11 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
cmake_minimum_required (VERSION 2.6)
project (ngrest)

#set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build)
include(CheckCXXCompilerFlag)

check_cxx_compiler_flag(-Wl,--no-undefined HAS_NO_UNDEFINED)
if (HAS_NO_UNDEFINED)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-undefined")
endif()

check_cxx_compiler_flag(-pedantic HAS_PEDANTIC)
if (HAS_PEDANTIC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,--no-undefined")
endif()

#set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
#set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})

set(PROJECT_DEPLOY_DIR ${PROJECT_BINARY_DIR}/deploy)
set(PROJECT_INCLUDE_DIR ${PROJECT_DEPLOY_DIR}/include)
Expand Down
3 changes: 2 additions & 1 deletion core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
cmake_minimum_required(VERSION 2.6)
project (common)
project (core)

add_subdirectory(utils)
add_subdirectory(common)
add_subdirectory(json)
add_subdirectory(engine)
add_subdirectory(server)
Expand Down
15 changes: 15 additions & 0 deletions core/common/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 2.6)
project (ngrestcommon CXX)

set (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)

FILE(GLOB NGRESTCOMMON_SOURCES ${PROJECT_SOURCE_DIR}/*.cpp)
FILE(GLOB NGRESTCOMMON_HEADERS ${PROJECT_SOURCE_DIR}/*.h)

file(COPY ${NGRESTCOMMON_HEADERS} DESTINATION ${PROJECT_INCLUDE_DIR}/ngrest/common/)

add_library(ngrestcommon SHARED ${NGRESTCOMMON_SOURCES})

target_link_libraries(ngrestcommon ngrestutils)

target_compile_features(ngrestcommon PRIVATE cxx_range_for)
8 changes: 5 additions & 3 deletions core/utils/src/Callback.h → core/common/src/Callback.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@

namespace ngrest {

template <typename R, typename E>
class Exception;

template <typename R>
class Callback
{
public:
virtual ~Callback() {}
virtual void onSuccess(R result) = 0;
virtual void onError(E error) = 0;
virtual void success(R result) = 0;
virtual void error(const Exception& error) = 0;
};

} // namespace ngrest
Expand Down
31 changes: 31 additions & 0 deletions core/common/src/HttpMessage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#ifndef NGREST_HTTPMESSAGE_H
#define NGREST_HTTPMESSAGE_H

#include "HttpMethod.h"
#include "HttpStatus.h"
#include "Message.h"

namespace ngrest {

struct HttpRequest: public Request
{
HttpMethod method = HttpMethod::UNKNOWN;
const char* methodStr = nullptr;

const char* clientHost = nullptr;
const char* clientPort = nullptr;
};


struct HttpResponse: public Response
{
int statusCode = HTTP_STATUS_200_OK;

// // name must be in lower case
// const Header* getHeader(const char* name) const;
};

}

#endif // NGREST_HTTPMESSAGE_H

18 changes: 18 additions & 0 deletions core/common/src/HttpMethod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef NGREST_HTTPMETHOD_H
#define NGREST_HTTPMETHOD_H

namespace ngrest {

enum class HttpMethod
{
UNKNOWN,
POST,
GET,
PUT,
DELETE
};

} // namespace ngrest

#endif // NGREST_HTTPMETHOD_H

File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions core/common/src/Message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <string.h>

#include "Message.h"

namespace ngrest {

const Header* Request::getHeader(const char* name) const
{
for (const Header* header = headers; header; header = header->next)
if (!strcmp(name, header->name))
return header;
return nullptr;
}

}
70 changes: 70 additions & 0 deletions core/common/src/Message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#ifndef NGREST_MESSAGECONTEXT_H
#define NGREST_MESSAGECONTEXT_H

#include <stdint.h>

#include <ngrest/common/Callback.h>
#include <ngrest/utils/MemPool.h>

namespace ngrest {

class Exception;
class Transport;
struct Node;
struct MessageContext;

struct Header
{
const char* name;
const char* value;
const Header* next;

inline Header(const char* name_ = nullptr, const char* value_ = nullptr, const Header* next_ = nullptr):
name(name_), value(value_), next(next_)
{
}
};


struct Request
{
const char* path = nullptr;
const Header* headers = nullptr;

char* body = nullptr;
uint64_t bodySize = 0;

Node* node;

// name must be in lower case
const Header* getHeader(const char* name) const;
};

struct Response
{
const Header* headers = nullptr;

Node* node;

// char* body = nullptr;
// uint64_t bodySize = 0;
MemPool poolBody;
};

class MessageCallback: public Callback<MessageContext*>
{
};

struct MessageContext
{
Transport* transport;
Request* request = nullptr;
Response* response = nullptr;
MessageCallback* callback = nullptr;
MemPool pool;
};

} // namespace ngrest

#endif // NGREST_MESSAGECONTEXT_H

17 changes: 17 additions & 0 deletions core/common/src/ObjectModel.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <string.h>

#include "ObjectModel.h"

namespace ngrest {

NamedNode* Object::findChildByName(const char* name) const
{
for (NamedNode* child = firstChild; child; child = child->nextSibling)
if (!strcmp(child->name, name))
return child;

return nullptr;
}


}
96 changes: 96 additions & 0 deletions core/common/src/ObjectModel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#ifndef NGREST_OBJECTMODEL_H
#define NGREST_OBJECTMODEL_H

namespace ngrest {

enum class NodeType
{
NamedNode,
LinkedNode,
Object,
Array,
Value
};

enum class ValueType
{
Undefined,
Null,
NaN,
String,
Number,
Boolean
};

struct Node
{
const NodeType type;

inline Node(NodeType type_):
type(type_)
{
}
};

struct LinkedNode: public Node {
Node* node;
LinkedNode* nextSibling = nullptr;

inline LinkedNode(Node* node_ = nullptr):
Node(NodeType::NamedNode),
node(node_)
{
}
};

struct NamedNode: public Node {
const char* name;
Node* node;
NamedNode* nextSibling = nullptr;

inline NamedNode(const char* name_ = nullptr, Node* node_ = nullptr):
Node(NodeType::NamedNode),
name(name_),
node(node_)
{
}
};

struct Object: public Node
{
NamedNode* firstChild = nullptr;

inline Object():
Node(NodeType::Object)
{
}

NamedNode* findChildByName(const char* name) const;
};

struct Array: public Node
{
LinkedNode* firstChild = nullptr;

inline Array():
Node(NodeType::Array)
{
}
};

struct Value: public Node
{
ValueType valueType;
const char* value;

inline Value(ValueType valueType_, const char* value_ = nullptr):
Node(NodeType::Value),
valueType(valueType_),
value(value_)
{
}
};

}

#endif // NGREST_OBJECTMODEL_H
16 changes: 16 additions & 0 deletions core/common/src/Service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "Service.h"

namespace ngrest {

Service::Service()
{

}

Service::~Service()
{

}

} // namespace ngrest

17 changes: 17 additions & 0 deletions core/common/src/Service.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef NGREST_SERVICE_H
#define NGREST_SERVICE_H

#include <string>

namespace ngrest {

class Service
{
public:
Service();
virtual ~Service();
};

} // namespace ngrest

#endif // NGREST_SERVICE_H
2 changes: 1 addition & 1 deletion core/engine/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ file(COPY ${NGRESTENGINE_HEADERS} DESTINATION ${PROJECT_INCLUDE_DIR}/ngrest/engi

add_library(ngrestengine SHARED ${NGRESTENGINE_SOURCES})

#target_link_libraries(ngrestengine ngrestutils ngrestjson)
target_link_libraries(ngrestengine ngrestutils ngrestcommon ngrestjson)

target_compile_features(ngrestengine PRIVATE cxx_range_for)
Loading

0 comments on commit 92c3726

Please sign in to comment.