2018-12-30 03:33:15 +01:00
|
|
|
/*
|
|
|
|
* IXTest.cpp
|
|
|
|
* Author: Benjamin Sergeant
|
|
|
|
* Copyright (c) 2018 Machine Zone. All rights reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "IXTest.h"
|
|
|
|
#include <ixwebsocket/IXWebSocket.h>
|
2019-01-29 00:14:49 +01:00
|
|
|
#include <ixwebsocket/IXNetSystem.h>
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
2019-01-08 03:04:28 +01:00
|
|
|
#include <mutex>
|
2018-12-30 03:33:15 +01:00
|
|
|
#include <string>
|
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
|
|
|
#include <stdlib.h>
|
2019-01-29 00:14:49 +01:00
|
|
|
#include <stack>
|
2019-03-22 23:33:04 +01:00
|
|
|
#include <iomanip>
|
2019-04-20 01:50:04 +02:00
|
|
|
#include <random>
|
|
|
|
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
namespace ix
|
|
|
|
{
|
|
|
|
std::atomic<size_t> incomingBytes(0);
|
|
|
|
std::atomic<size_t> outgoingBytes(0);
|
2019-01-08 03:04:28 +01:00
|
|
|
std::mutex Logger::_mutex;
|
2019-01-29 00:14:49 +01:00
|
|
|
std::stack<int> freePorts;
|
2018-12-30 03:33:15 +01:00
|
|
|
|
|
|
|
void setupWebSocketTrafficTrackerCallback()
|
|
|
|
{
|
|
|
|
ix::WebSocket::setTrafficTrackerCallback(
|
|
|
|
[](size_t size, bool incoming)
|
|
|
|
{
|
|
|
|
if (incoming)
|
|
|
|
{
|
|
|
|
incomingBytes += size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outgoingBytes += size;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void reportWebSocketTraffic()
|
|
|
|
{
|
2019-01-08 03:04:28 +01:00
|
|
|
Logger() << incomingBytes;
|
|
|
|
Logger() << "Incoming bytes: " << incomingBytes;
|
|
|
|
Logger() << "Outgoing bytes: " << outgoingBytes;
|
2018-12-30 03:33:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void msleep(int ms)
|
|
|
|
{
|
|
|
|
std::chrono::duration<double, std::milli> duration(ms);
|
|
|
|
std::this_thread::sleep_for(duration);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string generateSessionId()
|
|
|
|
{
|
|
|
|
auto now = std::chrono::system_clock::now();
|
2019-02-21 03:59:07 +01:00
|
|
|
auto seconds =
|
2018-12-30 03:33:15 +01:00
|
|
|
std::chrono::duration_cast<std::chrono::seconds>(
|
|
|
|
now.time_since_epoch()).count();
|
|
|
|
|
|
|
|
return std::to_string(seconds);
|
|
|
|
}
|
2019-01-08 03:04:28 +01:00
|
|
|
|
|
|
|
void log(const std::string& msg)
|
|
|
|
{
|
|
|
|
Logger() << msg;
|
|
|
|
}
|
|
|
|
|
2019-03-22 23:33:04 +01:00
|
|
|
void hexDump(const std::string& prefix,
|
|
|
|
const std::string& s)
|
|
|
|
{
|
|
|
|
std::ostringstream ss;
|
|
|
|
bool upper_case = false;
|
|
|
|
|
|
|
|
for (std::string::size_type i = 0; i < s.length(); ++i)
|
|
|
|
{
|
|
|
|
ss << std::hex
|
|
|
|
<< std::setfill('0')
|
|
|
|
<< std::setw(2)
|
|
|
|
<< (upper_case ? std::uppercase : std::nouppercase) << (int)s[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << prefix << ": " << s << " => " << ss.str() << std::endl;
|
|
|
|
}
|
2019-05-16 23:25:31 +02:00
|
|
|
|
|
|
|
bool startWebSocketEchoServer(ix::WebSocketServer& server)
|
|
|
|
{
|
|
|
|
server.setOnConnectionCallback(
|
|
|
|
[&server](std::shared_ptr<ix::WebSocket> webSocket,
|
|
|
|
std::shared_ptr<ConnectionState> connectionState)
|
|
|
|
{
|
|
|
|
webSocket->setOnMessageCallback(
|
2019-06-09 20:33:17 +02:00
|
|
|
[webSocket, connectionState, &server](const ix::WebSocketMessagePtr& msg)
|
2019-05-16 23:25:31 +02:00
|
|
|
{
|
2019-06-09 20:33:17 +02:00
|
|
|
if (msg->type == ix::WebSocketMessageType::Open)
|
2019-05-16 23:25:31 +02:00
|
|
|
{
|
|
|
|
Logger() << "New connection";
|
2019-06-09 20:33:17 +02:00
|
|
|
Logger() << "Uri: " << msg->openInfo.uri;
|
2019-05-16 23:25:31 +02:00
|
|
|
Logger() << "Headers:";
|
2019-06-09 20:33:17 +02:00
|
|
|
for (auto it : msg->openInfo.headers)
|
2019-05-16 23:25:31 +02:00
|
|
|
{
|
|
|
|
Logger() << it.first << ": " << it.second;
|
|
|
|
}
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Close)
|
2019-05-16 23:25:31 +02:00
|
|
|
{
|
|
|
|
Logger() << "Closed connection";
|
|
|
|
}
|
2019-06-09 20:33:17 +02:00
|
|
|
else if (msg->type == ix::WebSocketMessageType::Message)
|
2019-05-16 23:25:31 +02:00
|
|
|
{
|
|
|
|
for (auto&& client : server.getClients())
|
|
|
|
{
|
|
|
|
if (client != webSocket)
|
|
|
|
{
|
2019-06-09 20:55:34 +02:00
|
|
|
client->send(msg->str, msg->binary);
|
2019-05-16 23:25:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
auto res = server.listen();
|
|
|
|
if (!res.first)
|
|
|
|
{
|
|
|
|
Logger() << res.second;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
server.start();
|
|
|
|
return true;
|
|
|
|
}
|
2019-09-06 05:48:38 +02:00
|
|
|
|
|
|
|
std::vector<uint8_t> load(const std::string& path)
|
|
|
|
{
|
|
|
|
std::vector<uint8_t> memblock;
|
|
|
|
|
|
|
|
std::ifstream file(path);
|
|
|
|
if (!file.is_open()) return memblock;
|
|
|
|
|
|
|
|
file.seekg(0, file.end);
|
|
|
|
std::streamoff size = file.tellg();
|
|
|
|
file.seekg(0, file.beg);
|
|
|
|
|
|
|
|
memblock.resize((size_t) size);
|
|
|
|
file.read((char*)&memblock.front(), static_cast<std::streamsize>(size));
|
|
|
|
|
|
|
|
return memblock;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string readAsString(const std::string& path)
|
|
|
|
{
|
|
|
|
auto vec = load(path);
|
|
|
|
return std::string(vec.begin(), vec.end());
|
|
|
|
}
|
|
|
|
|
|
|
|
snake::AppConfig makeSnakeServerConfig()
|
|
|
|
{
|
|
|
|
snake::AppConfig appConfig;
|
|
|
|
appConfig.port = 8008;
|
|
|
|
appConfig.hostname = "127.0.0.1";
|
|
|
|
appConfig.verbose = true;
|
|
|
|
appConfig.redisPort = 6379;
|
|
|
|
appConfig.redisPassword = "";
|
|
|
|
appConfig.redisHosts.push_back("localhost"); // only one host supported now
|
|
|
|
|
|
|
|
std::string appsConfigPath("appsConfig.json");
|
|
|
|
|
|
|
|
// Parse config file
|
|
|
|
auto str = readAsString(appsConfigPath);
|
|
|
|
if (str.empty())
|
|
|
|
{
|
|
|
|
std::cout << "Cannot read content of " << appsConfigPath << std::endl;
|
|
|
|
return appConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cout << str << std::endl;
|
|
|
|
auto apps = nlohmann::json::parse(str);
|
|
|
|
appConfig.apps = apps["apps"];
|
|
|
|
|
|
|
|
// Display config on the terminal for debugging
|
|
|
|
dumpConfig(appConfig);
|
|
|
|
|
|
|
|
return appConfig;
|
|
|
|
}
|
2018-12-30 03:33:15 +01:00
|
|
|
}
|