[DEV] first http get work

This commit is contained in:
2014-05-04 22:27:56 +02:00
commit 77dd712c56
18 changed files with 1149 additions and 0 deletions

14
test/debug.cpp Normal file
View File

@@ -0,0 +1,14 @@
/**
* @author Edouard DUPIN
*
* @copyright 2014, Edouard DUPIN, all right reserved
*
* @license BSD 3 clauses (see license file)
*/
#include <test/debug.h>
int32_t appl::getLogId(void) {
static int32_t g_val = etk::log::registerInstance("enettest");
return g_val;
}

53
test/debug.h Normal file
View File

@@ -0,0 +1,53 @@
/**
* @author Edouard DUPIN
*
* @copyright 2014, Edouard DUPIN, all right reserved
*
* @license BSD 3 clauses (see license file)
*/
#ifndef __APPL_DEBUG_H__
#define __APPL_DEBUG_H__
#include <etk/log.h>
namespace appl {
int32_t getLogId(void);
};
// TODO : Review this problem of multiple intanciation of "std::stringbuf sb"
#define APPL_BASE(info,data) \
do { \
if (info <= etk::log::getLevel(appl::getLogId())) { \
std::stringbuf sb; \
std::ostream tmpStream(&sb); \
tmpStream << data; \
etk::log::logStream(appl::getLogId(), info, __LINE__, __class__, __func__, tmpStream); \
} \
} while(0)
#define APPL_CRITICAL(data) APPL_BASE(1, data)
#define APPL_ERROR(data) APPL_BASE(2, data)
#define APPL_WARNING(data) APPL_BASE(3, data)
#ifdef DEBUG
#define APPL_INFO(data) APPL_BASE(4, data)
#define APPL_DEBUG(data) APPL_BASE(5, data)
#define APPL_VERBOSE(data) APPL_BASE(6, data)
#define APPL_TODO(data) APPL_BASE(4, "TODO : " << data)
#else
#define APPL_INFO(data) do { } while(false)
#define APPL_DEBUG(data) do { } while(false)
#define APPL_VERBOSE(data) do { } while(false)
#define APPL_TODO(data) do { } while(false)
#endif
#define APPL_ASSERT(cond,data) \
do { \
if (!(cond)) { \
APPL_CRITICAL(data); \
assert(!#cond); \
} \
} while (0)
#endif

92
test/main.cpp Normal file
View File

@@ -0,0 +1,92 @@
/**
* @author Edouard DUPIN
*
* @copyright 2014, Edouard DUPIN, all right reserved
*
* @license BSD 3 clauses (see license file)
*/
#include <test/debug.h>
#include <enet/Tcp.h>
#include <enet/Http.h>
#undef __class__
#define __class__ "test"
int main(int argc, const char *argv[]) {
etk::log::setLevel(etk::log::logLevelVerbose);
APPL_VERBOSE("plop");
if (argc > 2) {
// client mode ...
enet::Http connection;
connection.setServer("example.com");
APPL_INFO("Get data : ");
if (connection.get("") == false) {
APPL_ERROR("can not Get data...");
return -1;
}
APPL_INFO("data : " << connection.dataString());
} else if (argc > 1) {
// client mode ...
enet::Tcp connection;
connection.setHostNane("127.0.0.1");
connection.setPort(31234);
connection.setServer(false);
APPL_INFO("CLIENT connect ...");
if (connection.link() == false) {
APPL_ERROR("can not link to the socket...");
return -1;
}
int32_t iii = 0;
while ( connection.getConnectionStatus() == enet::Tcp::statusLink
&& iii<10000) {
char data[1024];
int32_t len = connection.read(data, 1024);
APPL_INFO("read len=" << len << " data='" << data << "'");
iii++;
}
if (iii>=10000) {
APPL_INFO("auto disconnected");
} else if (connection.getConnectionStatus() != enet::Tcp::statusLink) {
APPL_INFO("server disconnected");
} else {
APPL_INFO("ERROR disconnected");
}
if (connection.unlink() == false) {
APPL_ERROR("can not unlink to the socket...");
return -1;
}
} else {
// server mode ...
enet::Tcp connection;
connection.setHostNane("127.0.0.1");
connection.setPort(31234);
connection.setServer(true);
APPL_INFO("SERVER connect ...");
if (connection.link() == false) {
APPL_ERROR("can not link to the socket...");
return -1;
}
int32_t iii = 0;
while (connection.getConnectionStatus() == enet::Tcp::statusLink) {
int32_t len = connection.write("plop" + std::to_string(iii));
APPL_INFO("write len=" << len);
iii++;
}
if (iii>=1000000) {
APPL_INFO("auto disconnected");
} else if (connection.getConnectionStatus() != enet::Tcp::statusLink) {
APPL_INFO("server disconnected");
} else {
APPL_INFO("ERROR disconnected");
}
if (connection.unlink() == false) {
APPL_ERROR("can not unlink to the socket...");
return -1;
}
}
return 0;
}