Commit 5e62cbed by amir

first time

parent 357d3fed
Pipeline #72 skipped in 0 seconds
/*
* MicroserviceApp.h
*
* Created on: Mar 25, 2015
* Author: amir
*/
#ifndef MICROSERVICEAPP_H_
#define MICROSERVICEAPP_H_
#include <map>
#include <string>
#include <pthread.h>
#include <Microservice_Iface.h>
#include "Microservice_Defines.h"
#include "handlers/Microservice_MonitorHandler.h"
#include <mutex> // std::mutex, std::unique_lock
#include <condition_variable> // std::condition_variable
#include "Microservice_Client.h"
class cMicroservice_RestServerParams;
class cMicroservice_RMQServerParams;
class cMicroservice_BaseHandler;
//class cMicroservice_RestServer;
//class cMicroservice_RMQServer;
class cMicroservice_MonitorHandler;
using namespace nsMicroservice_Iface;
class cMicroservice_App
{
private:
cMicroservice_RestServerParams* mpc_RestParams;
cMicroservice_RMQServerParams* mpc_RMQParams;
std::string mc_AppName;
std::string mc_AppInstance;
std::map<std::string,cMicroservice_BaseHandler*> mc_HandlersMap;
std::map<std::string, cMicroservice_Client*> mc_ClientMap;
std::vector<IRestServer*> mc_ServerList;
// pthread_t mt_RestServerThreadId;
// pthread_t mt_RMQServerThreadId;
IServiceDiscovery* mpc_ServiceDiscovery;
bool enableMetrics;
ILogger* mpc_Logger;
IPubSub* mpc_PubSubClient;
IConfiguration* mpc_Configuration;
cMicroservice_MonitorHandler* mpc_MonitorHandler;
// servers
// cMicroservice_RestServer* mpc_RestServer;
// cMicroservice_RMQServer* mpc_RMQServer;
// bool buildRMQServer();
// bool buildRestServer();
// bool Init();
public:
// cMicroservice_App(cMicroservice_RestServerParams* pc_RsiParams,
// cMicroservice_RMQServerParams* pc_MbiParams,
// const char* pba_AppName);
cMicroservice_App(const char* appName);
/**************************************************/
/* with section
**************************************************/
/**
* use service discovery for this service
* @param serviceDiscovery
* @return
*/
cMicroservice_App& withServiceDiscovery(IServiceDiscovery* pc_ServiceDiscovery) {
this->mpc_ServiceDiscovery = pc_ServiceDiscovery;
return *this;
}
/**
* enable service metrics
* @return
*/
cMicroservice_App& withMetrics()
{
// enableMetrics = true;
return *this;
}
/**
* direct the ms app to use this logger
* otherwise using the default logger
* @param logger
* @return
*/
cMicroservice_App& withLogger(ILogger* pc_Logger)
{
this->mpc_Logger = pc_Logger;
return *this;
}
cMicroservice_App& withPubSub(IPubSub* pc_PubSubClient)
{
this->mpc_PubSubClient = pc_PubSubClient;
return *this;
}
cMicroservice_App& withConfiguration(IConfiguration* pc_Configuration)
{
this->mpc_Configuration = pc_Configuration;
return *this;
}
cMicroservice_App& withMonitoring();
/*************************************************
* ADD SECTION
**************************************************/
cMicroservice_App& addRestServer(IRestServer* pc_Server);
cMicroservice_App& addMicroserviceClient(cMicroservice_Client* pc_client);
cMicroservice_App& addHandler(const char* pba_Prefix, cMicroservice_BaseHandler* pc_Handler);
/**************************************************************/
void AddHandler(const char* pba_Prefix, cMicroservice_BaseHandler* pc_Handler);
// bool StartApp();
// void StopApp();
cMicroservice_App& build();
void run();
void stop();
/************************************************
* Get section
***********************************************/
IConfiguration* GetConfiguration() const { return mpc_Configuration; }
ILogger* GetLogger() const { return mpc_Logger; }
cMicroservice_Client* GetMSClient(std::string& ms_name) {
return mc_ClientMap[ms_name];
}
std::map<std::string, cMicroservice_Client*>& GetClientMap() { return mc_ClientMap; }
};
#endif /* MICROSERVICEAPP_H_ */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Microservice_BaseResponse.h
* Author: amir
*
* Created on April 11, 2016, 12:16 PM
*/
#ifndef MICROSERVICE_BASERESPONSE_H
#define MICROSERVICE_BASERESPONSE_H
#include <string>
#include <document.h>
class cMicroservice_BaseRestResponse {
public:
cMicroservice_BaseRestResponse(): mb_Success(true){}
cMicroservice_BaseRestResponse(bool b_Success, std::string& c_Error)
:mb_Success(b_Success), mc_Error(c_Error) {}
cMicroservice_BaseRestResponse(bool b_Success, std::string& c_Error, rapidjson::Document& c_ObjectNode)
:mb_Success(b_Success), mc_Error(c_Error), mc_ObjectNode(c_ObjectNode) {}
virtual ~cMicroservice_BaseRestResponse() {}
void SetError(std::string& error) {
this->mc_Error = error;
}
bool IsSuccess() const {
return mb_Success;
}
std::string& GetError(){
return mc_Error;
}
rapidjson::Document& GetObjectNode() {
return mc_ObjectNode;
}
// void SetObjectNode(rapidjson::Document& c_ObjectNode) {
// this->mc_ObjectNode = c_ObjectNode;
// }
void Reset() {
mb_Success = true;
mc_Error.clear();
if(!mc_ObjectNode.IsNull())
mc_ObjectNode.Clear();
}
private:
bool mb_Success;
std::string mc_Error;
rapidjson::Document mc_ObjectNode;
};
#endif /* MICROSERVICE_BASERESPONSE_H */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: Microservice_Client.h
* Author: amir
*
* Created on April 13, 2016, 12:21 PM
*/
#ifndef MICROSERVICE_CLIENT_H
#define MICROSERVICE_CLIENT_H
#include <Microservice_Iface.h>
#include <Microservice_BaseRestResponse.h>
#include "common/MSTypes.h"
#include "params/MSCommandParams.h"
#include "params/Microservice_Params.h"
using namespace nsMicroservice_Iface;
class cMicroservice_Client {
private:
ICommandClient* mpc_CommandClient;
cMicroservice_BaseClientParams* mpc_Params;
ICacheClient* mpc_CacheClient;
ILogger* p_logger_;
public:
cMicroservice_Client();
cMicroservice_Client(const cMicroservice_Client& orig);
virtual ~cMicroservice_Client();
cMicroservice_Client(ICommandClient* mpc_CommandClient, cMicroservice_BaseClientParams* mpc_Params);
MSRetStat Init(ILogger* p_logger);
ICommandClient* GetCommandClient() const {
return mpc_CommandClient;
}
cMicroservice_BaseClientParams* GetParams() const {
return mpc_Params;
}
ICacheClient* GetCacheClient() const {
return mpc_CacheClient;
}
MSRetStat Create(MSCommandParams* p_command_params, cMicroservice_BaseRestResponse* p_resoonse);
MSRetStat Read(MSCommandParams* p_command_params, cMicroservice_BaseRestResponse* p_resoonse);
MSRetStat Update(MSCommandParams* p_command_params, cMicroservice_BaseRestResponse* p_resoonse);
MSRetStat Delete(MSCommandParams* p_command_params, cMicroservice_BaseRestResponse* p_resoonse);
void GetMetrics(std::map<std::string,long>& metrics_map);
};
#endif /* MICROSERVICE_CLIENT_H */
/*
* Microservice_Defines.h
*
* Created on: Mar 23, 2015
* Author: amir
*/
#ifndef MICROSERVICE_DEFINES_H_
#define MICROSERVICE_DEFINES_H_
#ifndef CNULL
#define CNULL '\0'
#endif
/*
* constants
*/
namespace nsMicroservice_Constants
{
const int MAX_METHOD_NAME = 16;
const int MAX_PARAMS = 8;
const int MAX_JSON_BUFFER = 4096; // 4K
const int MAX_URI_LENGTH = 2048; // 1K
const int MAX_ERROR_BUFF_URI_LENGTH = 1024; // 1K
const int MAX_LOGEER_BUFF_LENGTH = 2*1024; // 2K
static const char* SLASH_SEPERATOR = "/";
static const char* AND_SEPERATOR = "&";
static const char* HEADER_CONTENT_TYPE = "Content-Type";
static const char* CONTENT_TYPE_JSON = "application/json";
static const char* REQUEST_ERROR = "Request Error";
static const char* REQUEST_TIMEOUT = "Request Timeout";
static const char* METHOD_NOT_IMPLEMENTED = "method not implemented";
static const char* FAILED_TO_GET_PARAMS = "failed to get params";
static const char* JSON_NULL_VALUE = "null";
static const char* SUCCESS_REST_RESPONSE_TEMPLATE = "{ \"success\": true, \"error\": null, \"objectNode\": ";
static const char* ERROR_REST_RESPONSE_TEMPLATE = "{ \"success\": false, \"error\": \"%s\", \"objectNode\": null }";
static const char* COMMAND_ERROR = "Command Error: ";
static const char* MON_PREFIX = "/_mon";
static const char* LOG_FILE_PATH = "/var/log/mcx/msApp.log";
static const int LOG_FILE_SIZE = 50*1024*1024;
static const char* LOCALHOST = "localhost";
}
/*
* enums
*/
class cMicroservice_Enums
{
public:
typedef enum
{
eGet,
ePost,
ePut,
eDelete,
eMaxMethods
}eMethod;
typedef enum
{
eFatal,
eError,
eWarning,
eInfo,
eDebug,
eTrace
}eLogLevel;
};
static char gbaa_Microservice_MethodNames[cMicroservice_Enums::eMaxMethods][nsMicroservice_Constants::MAX_METHOD_NAME] =
{
"GET",
"POST",
"PUT",
"DELETE"
};
#endif /* MICROSERVICE_DEFINES_H_ */
/*
* Microservice_Iface.h
*
* Created on: Mar 23, 2015
* Author: amir
*/
#ifndef MICROSERVICE_IFACE_H_
#define MICROSERVICE_IFACE_H_
#include <string>
#include <map>
#include <vector>
#include <document.h>
#include "Microservice_Defines.h"
#include "common/MSTypes.h"
#include "params/MSCommandParams.h"
class cMicroservice_BaseRestResponse;
class cMicroservice_BaseHandler;
namespace nsMicroservice_Iface
{
struct INotifyCallback
{
virtual void onMessage(std::string& t_Topic, std::string& t_Message) = 0;
virtual void onError(std::string& t_Topic, std::string& t_Error) = 0;
};
struct IConfigurationProvider {
virtual std::map<std::string,std::string> getAllProperties() = 0;
virtual std::string getPropertyAsString(std::string key) = 0;
};
struct IConfiguration {
virtual void AddConfigurationProvider(IConfigurationProvider& iProvider) = 0;
virtual void Reload() = 0;
virtual long GetLong(std::string key, long defaultVal) = 0;
virtual std::string GetString(std::string key, std::string defaultVal) = 0;
virtual bool GetBoolean(std::string key, bool defaultVal) = 0;
};
struct ILogger
{
virtual void fatal(const std::string& msg) = 0;
virtual void error(const std::string& msg) = 0;
virtual void warning(const std::string& msg) = 0;
virtual void info(const std::string& msg) = 0;
virtual void debug(const std::string& msg) = 0;
virtual void trace(const std::string& msg) = 0;
virtual void fatal(const char* stringFormat, ...) = 0;
virtual void error(const char* stringFormat, ...) = 0;
virtual void warning(const char* stringFormat, ...) = 0;
virtual void info(const char* stringFormat, ...) = 0;
virtual void debug(const char* stringFormat, ...) = 0;
virtual void trace(const char* stringFormat, ...) = 0;
virtual void setLevel(cMicroservice_Enums::eLogLevel level) = 0;
};
struct ICommandClient
{
ILogger* p_logger_;
public:
/*
public abstract class Command extends HystrixCommand<BaseRestResponse> {
protected MSCommandParams* p_cmd_params = null;
public Command(MSCommandParams* p_cmd_params, String cmdName)
{
super(HystrixCommandGroupKey.Factory.asKey(cmdName));
this.reqCtx = reqCtx;
}
@Override
protected BaseRestResponse getFallback()
{
return new BaseRestResponse(false, COMMAND_ERROR + getFailedExecutionException().getMessage());
}
}
*/
virtual MSRetStat Create(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) = 0;
/**
* the read/get of CRUD
* @param reqCtx
*/
virtual MSRetStat Read(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) = 0;
/**
* the update/put of CRUD
* @param reqCtx
*/
virtual MSRetStat Update(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) = 0;
/**
* the delete of CRUD
* @param reqCtx
*/
virtual MSRetStat Delete(MSCommandParams* p_cmd_params, cMicroservice_BaseRestResponse* p_response) = 0;
/**
* getting the metrics as jsonnode - array
* @return
*/
virtual void GetMetrics(std::map<std::string, long>& metrics_map) = 0;
virtual void SetLogger(ILogger* logger) { p_logger_ = logger; }
};
struct IMetricsFactory
{
struct IMeter
{
virtual void mark() = 0;
virtual void mark(long n) = 0;
virtual long getCount() = 0;
};
struct ICounter
{
virtual void inc() = 0;
virtual void inc(long n) = 0;
virtual void dec() = 0;
virtual void dec(long n) = 0;
virtual long getCount() = 0;
};
struct ITimer
{
virtual void start() = 0;
virtual void stop() = 0;
};
/**
* must be at the end of init , after the netrics are defined
*/
virtual void startReporting() = 0;
virtual IMeter* createMeter(std::string& name) = 0;
virtual ICounter* createCounter(std::string& name) = 0;
virtual ITimer* createTimer(std::string& name) = 0;
};
struct IPubSub
{
/**
* you can subscribe multiple times but
* every subscription opens a thread
* @param topic - can be with wildcard: activity/*
* @param notifyHandler
*/
virtual void subscribe(std::string& topic, INotifyCallback& notifyHandler) = 0;
/**
*
* @param topic
*/
virtual void unsubscribe(std::string& topic) = 0;
virtual void publish(std::string& topic, std::string& message) = 0;
};
struct IServiceDiscovery
{
virtual bool registerService(std::string& name, std::string& id, std::string& host,int port) = 0;
virtual bool unregisterService() = 0;
virtual IConfigurationProvider& getConfigurationProvider() = 0;
};
struct IRestServer
{
public:
virtual bool build(std::string& appName,
std::map<std::string, cMicroservice_BaseHandler*>& msHandlersMap,
ILogger* pc_Logger,
IPubSub* pc_PubSub,
bool withMetrics) = 0;
virtual void run() = 0;
virtual void stop() = 0;
virtual void registerService(IServiceDiscovery* pc_ServiceDiscovery, std::string& id) = 0;
};
struct IRequest
{
public:
virtual ~IRequest() {};
//InputStream getInputStream() = 0;
virtual const char* GetQueryString() = 0;
virtual const char* GetRelativePath() = 0;
virtual const char* GetContent() = 0;
virtual void Reset() {};
};
struct IResponse
{
public:
virtual ~IResponse() {};
//public void send(ByteBuffer buffer);
virtual void Send(const char* response) = 0;
virtual void Reset() {};
};
/**
* this interface defines the basic operations
* required from the container implementor
* needed by the ms handler
* @see BaseHandler
* @author amir
*
*/
struct IContainer
{
public:
virtual ~IContainer() {};
//public static Pattern seperatorPattern = Pattern.compile("/");
virtual void SendErrorResp(nsMicroservice_Iface::IResponse* pti_Response,std::string error) = 0;
/**
* writing the value to resp as json
* @param res
* @param value
*/
virtual void WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,rapidjson::Document& t_ObjectDoc) = 0;
virtual void WriteObjectToResponse(nsMicroservice_Iface::IResponse* pti_Response,cMicroservice_BaseRestResponse& t_BaseRestResponse) = 0;
virtual void WriteStringToResponse(nsMicroservice_Iface::IResponse* pti_Response,const char* pba_Doc) = 0;
/**
* reading the object from the request body json
* @param req
* @param ObjClass
* @return
*/
virtual bool ReadObjectFromRequest(nsMicroservice_Iface::IRequest* pti_Request,rapidjson::Document& t_ObjectDoc) = 0;
/**
* subscribing to specific topic
* @param topic
* @param notifyHandler
*/
virtual void Subscribe(std::string& t_Topic, nsMicroservice_Iface::INotifyCallback& t_NotifyHandler) = 0;
/**
* un-subscribing from topic
* @param topic
*/
virtual void Unsubscribe(std::string& t_Topic) = 0;
/**
* publish msg on specific topic
* @param topic
* @param messageNode
*/
virtual void Publish(std::string& t_Topic, std::string& t_Message) = 0;
};
struct ICacheClient
{
/**
* set/update with the default expiration
* @param key
* @param value
*/
virtual void set(std::string& key, std::string& value) = 0;
virtual void set(std::string& key, std::string& value, int expiration) = 0;
virtual void setExpiration(std::string& key, int expiration) = 0;
virtual bool get(std::string& key, std::string& retval) = 0;
virtual void del(std::string& key) = 0;
virtual void delByPattern(std::string& pattern) = 0;
virtual bool getKeysByPattern(std::string& pattern,std::vector<std::string>& retKeys) = 0;
virtual bool getByPattern(std::string& pattern,std::vector<std::pair<std::string,std::string>>& retKeyValues) = 0;
virtual bool exists(std::string& key) = 0;
};
}
#endif /* MICROSERVICE_IFACE_H_ */
/*
* Microservice_RequestContext.h
*
* Created on: Mar 23, 2015
* Author: amir
*/
#ifndef MICROSERVICE_REQUESTCONTEXT_H_
#define MICROSERVICE_REQUESTCONTEXT_H_
#include <map>
#include <deque>
#include <vector>
#include <string>
#include <Microservice_Defines.h>
#include <stringbuffer.h> //rapidjson string
#include <writer.h> //rapidjson writer
#include <Microservice_Iface.h>
typedef rapidjson::Writer<rapidjson::StringBuffer> JsonStringWriter;
typedef std::map<std::string, std::deque<std::string> > DequeStringMap;
/**
*
*/
class cMicroservice_RequestContext
{
public:
std::vector<std::string> mc_Params; //(nsMicroservice_Constants::MAX_PARAMS);
DequeStringMap mc_QueryParameters;
// request-interface
// response interface
nsMicroservice_Iface::IContainer* mpti_Container;
nsMicroservice_Iface::IResponse* mpti_Response;
nsMicroservice_Iface::IRequest* mpti_Request;
JsonStringWriter* mpc_Writer;
cMicroservice_RequestContext( nsMicroservice_Iface::IContainer* pti_Container,
JsonStringWriter* pc_Writer,
nsMicroservice_Iface::IResponse* pti_Response,
nsMicroservice_Iface::IRequest* pti_Request):
mpti_Response(pti_Response),mpti_Request(pti_Request)
{
mpti_Container = pti_Container;
mpc_Writer = pc_Writer;
if (mc_Params.capacity() < nsMicroservice_Constants::MAX_PARAMS)
mc_Params.reserve(nsMicroservice_Constants::MAX_PARAMS);
}
void Reset()
{
mc_Params.clear();
mc_QueryParameters.clear();
mpti_Response->Reset();
mpti_Request->Reset();
}
};
#endif /* MICROSERVICE_REQUESTCONTEXT_H_ */
libMicroservice.so.0
\ No newline at end of file
libMicroservice.so.0.1.0
\ No newline at end of file
No preview for this file type
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment