messy client<->server checkpoint
This commit is contained in:
83
client/src/TcpClient.cpp
Normal file
83
client/src/TcpClient.cpp
Normal file
@@ -0,0 +1,83 @@
|
||||
|
||||
#include "TcpClient.hpp"
|
||||
|
||||
TcpClient::TcpClient(ConfigService* config, LoggerService* logger) : config_(config), logger_(logger) {
|
||||
|
||||
if(!(config->getConfig<ClientConfig>("TcpClient", "main", &configuration_))) {
|
||||
logger_->log("TcpClient", LogFlag::Error, "Failed to get configuration");
|
||||
return;
|
||||
}
|
||||
|
||||
if(init() == ErrorCode::Success) {
|
||||
logger_->log("TcpClient", LogFlag::Info, "TcpClient initialized.");
|
||||
} else {
|
||||
logger_->log("TcpClient", LogFlag::Error, "TcpClient failed to initialize.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TcpClient::~TcpClient() {
|
||||
// close port
|
||||
}
|
||||
|
||||
ErrorCode TcpClient::init() {
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Initializing TcpClient...");
|
||||
|
||||
// keeping everything in here for now
|
||||
int socketFd;
|
||||
int sendBytes;
|
||||
int receiveBytes;
|
||||
char buffer[1024];
|
||||
|
||||
struct hostent* host;
|
||||
struct sockaddr_in serverAddress;
|
||||
struct timeval timestamp;
|
||||
struct timeval timestampEnd;
|
||||
|
||||
host = gethostbyname(configuration_.hostname.c_str());
|
||||
if(host == NULL) {
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Unable to parse hostname.");
|
||||
return ErrorCode::Error;
|
||||
}
|
||||
|
||||
socketFd = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if(socketFd < 0) {
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Unable to open client socket.");
|
||||
return ErrorCode::Error;
|
||||
}
|
||||
|
||||
serverAddress.sin_family = AF_INET;
|
||||
serverAddress.sin_port = htons(configuration_.port);
|
||||
serverAddress.sin_addr = *((struct in_addr*)host->h_addr);
|
||||
memset(&serverAddress.sin_zero, 0, sizeof(serverAddress.sin_zero));
|
||||
|
||||
if(connect(socketFd, (struct sockaddr*)&serverAddress, sizeof(struct sockaddr)) < 0) {
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Unable to connect to server.");
|
||||
return ErrorCode::Error;
|
||||
}
|
||||
|
||||
memset(buffer, 0x67, sizeof(buffer));
|
||||
gettimeofday(×tamp, NULL);
|
||||
|
||||
sendBytes = send(socketFd, buffer, sizeof(buffer), 0);
|
||||
if(sendBytes < 0) {
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Unable to send to server.");
|
||||
return ErrorCode::Error;
|
||||
}
|
||||
|
||||
gettimeofday(×tampEnd, NULL);
|
||||
|
||||
memset(buffer, 0x00, sizeof(buffer));
|
||||
receiveBytes = recv(socketFd, buffer, sizeof(buffer), 0);
|
||||
if(receiveBytes < 0) {
|
||||
logger_->log("TcpClient", LogFlag::Debug, "Unable to receive from server.");
|
||||
return ErrorCode::Error;
|
||||
}
|
||||
|
||||
std::string msg = "Received " + std::to_string(receiveBytes) + " bytes back from server.";
|
||||
logger_->log("TcpClient", LogFlag::Debug, msg);
|
||||
|
||||
close(socketFd);
|
||||
|
||||
return ErrorCode::Success;
|
||||
}
|
||||
31
client/src/TcpClient.hpp
Normal file
31
client/src/TcpClient.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <netdb.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/time.h> // TODO replace with chrono
|
||||
|
||||
#include "common/config/ConfigService.hpp"
|
||||
#include "common/LoggerService.hpp"
|
||||
#include "common/ErrorCodes.hpp"
|
||||
#include "config/ClientConfig.hpp"
|
||||
|
||||
class TcpClient {
|
||||
|
||||
public:
|
||||
|
||||
TcpClient(ConfigService* config, LoggerService* logger);
|
||||
~TcpClient();
|
||||
|
||||
private:
|
||||
|
||||
ErrorCode init();
|
||||
|
||||
LoggerService* logger_;
|
||||
ConfigService* config_;
|
||||
ClientParams configuration_;
|
||||
|
||||
};
|
||||
31
client/src/config/ClientConfig.hpp
Normal file
31
client/src/config/ClientConfig.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "common/config/IConfig.hpp"
|
||||
|
||||
struct ClientParams {
|
||||
std::string id;
|
||||
|
||||
std::string hostname;
|
||||
uint32_t port;
|
||||
};
|
||||
|
||||
class ClientConfig : public IConfig<ClientParams> {
|
||||
|
||||
public:
|
||||
|
||||
using IConfig<ClientParams>::IConfig;
|
||||
|
||||
bool parseConfig(const libconfig::Setting& setting) override {
|
||||
|
||||
if (!setting.lookupValue("Id", params_->id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
setting.lookupValue("Hostname", params_->hostname);
|
||||
setting.lookupValue("Port", params_->port);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
};
|
||||
@@ -2,14 +2,24 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "common/config/ConfigService.hpp"
|
||||
#include "common/config/LoggerConfig.hpp"
|
||||
#include "common/LoggerService.hpp"
|
||||
|
||||
#include "TcpClient.hpp"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
||||
ConfigService config {"client/config/main.cfg"}; // TODO: main config file should be the foremost cli argument
|
||||
std::string centralConfigPath = "client/config/main.cfg"; // default path
|
||||
if(argc > 1) {
|
||||
centralConfigPath = std::string(argv[1]);
|
||||
}
|
||||
|
||||
ConfigService config { centralConfigPath };
|
||||
LoggerService logger {&config, "main"};
|
||||
|
||||
logger.log("main", LogFlag::Debug, "hello world from the client!");
|
||||
|
||||
TcpClient client {&config, &logger};
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user