Commit 899f9208 authored by Frederic Logier's avatar Frederic Logier
Browse files

added an echo api

parent 8f310d26
......@@ -44,6 +44,9 @@ ZerogwProxy::~ZerogwProxy()
thread_payload[0]->wait();
qDebug() << "!!!!!!! ApiPayload::wait 1 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
thread_payload[1]->wait();
delete(check_zerogw);
delete(check_reply);
}
void ZerogwProxy::init()
......@@ -180,8 +183,8 @@ Service::Service(params a_ncs_params, QObject *parent) : m_ncs_params(a_ncs_para
// m_http_api = NULL;
m_xmpp_server = NULL;
m_xmpp_client = NULL;
zerogwToPayload[0] = NULL;
zerogwToPayload[1] = NULL;
// zerogwToPayload[0] = NULL;
// zerogwToPayload[1] = NULL;
}
Service::~Service()
......@@ -225,7 +228,7 @@ Service::~Service()
worker_api->deleteLater();
worker_thread_api->wait();
/*
if (zerogwToPayload[0])
{
// delete(zerogwToPayload[0]);
......@@ -239,14 +242,15 @@ Service::~Service()
}
if (zerogwToPayload[1])
{
/*delete(zerogwToPayload[1]);
zerogwToPayload[1]->thread->quit();
while(!zerogwToPayload[1]->thread->isFinished ()){};
*/
// delete(zerogwToPayload[1]);
// zerogwToPayload[1]->thread->quit();
// while(!zerogwToPayload[1]->thread->isFinished ()){};
qDebug() << "zerogwToPayload[1]->deleteLater();";
zerogwToPayload[1]->deleteLater();
}
*/
qDebug() << "zerogw_payload->deleteLater();";
zerogw_payload->deleteLater();
......@@ -257,6 +261,10 @@ Service::~Service()
thread_ZerogwProxy2->wait();
qDebug() << "api_echo->deleteLater();";
api_echo->deleteLater();
thread_api_echo->wait();
qDebug() << "api_node->deleteLater();";
api_node->deleteLater();
thread_api_node->wait();
......@@ -316,6 +324,17 @@ void Service::Http_api_init()
//zerogwToPayload[0] = new ZerogwProxy(m_ncs_params, port);
// zerogwToPayload[1] = new ZerogwProxy(m_ncs_params, port+1);
thread_api_echo = new QThread(this);
api_echo = new Api_echo(m_ncs_params.base_directory, 2500);
connect(thread_api_echo, SIGNAL(started()), api_echo, SLOT(init()));
connect(api_echo, SIGNAL(destroyed()), thread_api_echo, SLOT(quit()), Qt::DirectConnection);
api_echo->moveToThread(thread_api_echo);
thread_api_echo->start();
thread_ZerogwProxy = new QThread(this);
zerogw_payload = new ZerogwProxy(m_ncs_params, port);
......@@ -399,7 +418,7 @@ void Service::Http_api_init()
connect(api_ftp, SIGNAL(destroyed()), thread_api_ftp, SLOT(quit()), Qt::DirectConnection);
api_ftp->moveToThread(thread_api_ftp);
thread_api_ftp->start();
thread_api_ftp->start();
}
......
......@@ -98,6 +98,7 @@ public:
Api_session *api_session;
Api_app *api_app;
Api_ftp *api_ftp;
Api_echo *api_echo;
private:
......@@ -110,6 +111,7 @@ private:
QThread *thread_api_session;
QThread *thread_api_app;
QThread *thread_api_ftp;
QThread *thread_api_echo;
QThread *thread_xmpp_server;
......
......@@ -50,11 +50,10 @@ Routing:
zmq-forward:
enabled: yes
socket:
- !zmq.Bind "tcp://127.0.0.1:7001"
- !zmq.Bind "tcp://127.0.0.1:2500"
contents:
- !Uri ~
- !Body ~
timeout: 2
timeout: 5
retry: !RetryFirst
count: 3
"/payload*":
......@@ -156,20 +155,3 @@ Routing:
timeout: 2
retry: !RetryFirst
count: 3
"/chat*":
websocket:
enabled: yes
forward:
- !zmq.Bind "tcp://127.0.0.1:7002"
subscribe:
- !zmq.Bind "tcp://127.0.0.1:7003"
"/crossdomain.xml":
responses:
default:
headers:
Content-Type: text/xml; charset="utf-8"
body: |
<?xml version="1.0"?>
<cross-domain-policy>
<allow-access-from domain="*" />
</cross-domain-policy>
......@@ -197,6 +197,7 @@ Zerogw::~Zerogw()
delete(m_socket_zerogw);
delete(z_push_api);
delete(check_http_data);
}
......@@ -1553,3 +1554,123 @@ void Api_ftp::receive_http_payload()
/******************************/
/*********************/
Api_echo::Api_echo(QString basedirectory, int port) : Zerogw(basedirectory, port)
{
std::cout << "Api_echo::Api_echo constructor" << std::endl;
enumToZerogwHeaderEcho.insert(0, HTTP_METHOD);
enumToZerogwHeaderEcho.insert(1, URI);
}
void Api_echo::receive_http_payload()
{
check_http_data->setEnabled(false);
std::cout << "Api_echo::receive_payload" << std::endl;
QHash <QString, QString> zerogw;
QString key;
int counter=0;
QString bodyMessage="";
while (true)
{
zmq::message_t request;
bool res = m_socket_zerogw->recv(&request, ZMQ_NOBLOCK);
if (!res && zmq_errno () == EAGAIN) break;
qint32 events = 0;
std::size_t eventsSize = sizeof(events);
m_socket_zerogw->getsockopt(ZMQ_RCVMORE, &events, &eventsSize);
std::cout << "Api_echo::receive_payload received request: [" << (char*) request.data() << "]" << std::endl;
QString data_from_zerogw;
switch(enumToZerogwHeaderEcho[counter])
{
case HTTP_METHOD:
// check METHOD
if (request.size() == 0) bodyMessage = buildResponse("error", "header", "METHOD is empty");
else
{
data_from_zerogw = QString::fromAscii((char*)request.data(), request.size());
zerogw["METHOD"] = data_from_zerogw;
if (zerogw["METHOD"] != "GET")
bodyMessage = "BAD REQUEST : " + zerogw["METHOD"];
}
break;
case URI:
if (!bodyMessage.isEmpty()) break;
// Check URI
if (request.size() == 0) bodyMessage = buildResponse("error", "header", "URI is empty");
else
{
data_from_zerogw = QString::fromAscii((char*)request.data(), request.size());
QList <QString> uri_params = data_from_zerogw.split("/");
qDebug() << "URI PARAMS " << uri_params[2];
zerogw["URI"] = uri_params[2];
}
break;
}
if (!(events & ZMQ_RCVMORE))
{
qDebug() << "ZMQ_RCVMORE";
if (bodyMessage.isEmpty())
{
if (zerogw["URI"] == "echo")
{
bodyMessage = buildResponse("status", "OK");
qDebug() << "Api_echo::receive_payload TOKEN : " << bodyMessage;
}
else bodyMessage = buildResponse("error", "URI", "unknown");
}
zmq::message_t m_message(bodyMessage.length());
memcpy(m_message.data(), bodyMessage.toAscii(), bodyMessage.length());
m_socket_zerogw->send(m_message, 0);
qDebug() << "returning : " << bodyMessage;
}
counter++;
}
qDebug() << "ZEROGW : " << zerogw;
check_http_data->setEnabled(true);
}
/******************************/
......@@ -68,6 +68,7 @@ typedef QMap<int, ZerogwHeader> IntToZerogwHeaderPayload;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderSession;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderApp;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderFtp;
typedef QMap<int, ZerogwHeader> IntToZerogwHeaderEcho;
......@@ -220,4 +221,18 @@ private slots:
};
class Api_echo : public Zerogw
{
Q_OBJECT
public:
Api_echo(QString basedirectory, int port);
private:
IntToZerogwHeaderEcho enumToZerogwHeaderEcho;
private slots:
void receive_http_payload();
};
#endif // ZEROGW_H
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 to comment