|
@@ -1,4 +1,4 @@
|
|
|
-#include <client.hpp>
|
|
|
+#include <client.hpp>
|
|
|
#include <arpa/inet.h>
|
|
|
#include <netdb.h>
|
|
|
#include <string.h>
|
|
@@ -15,6 +15,7 @@
|
|
|
#include <future>
|
|
|
#include <headers/kmessage_codec.hpp>
|
|
|
#include <headers/json.hpp>
|
|
|
+#include <headers/util.hpp>
|
|
|
|
|
|
using namespace KData;
|
|
|
|
|
@@ -75,38 +76,72 @@ void Client::handleMessages() {
|
|
|
* @return A meaningless integer
|
|
|
*/
|
|
|
void Client::start() {
|
|
|
- m_client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
- if (m_client_socket_fd != -1) {
|
|
|
- sockaddr_in server_socket;
|
|
|
- char* end;
|
|
|
- server_socket.sin_family = AF_INET;
|
|
|
- auto port_value = strtol(argv[2], &end, 10);
|
|
|
- if (port_value < 0 || end == argv[2]) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- int socket_option = 1;
|
|
|
- // Free up the port to begin listening again
|
|
|
- setsockopt(m_client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
|
|
|
- sizeof(socket_option));
|
|
|
-
|
|
|
- server_socket.sin_port = htons(port_value);
|
|
|
- inet_pton(AF_INET, argv[1], &server_socket.sin_addr.s_addr);
|
|
|
-
|
|
|
- if (::connect(m_client_socket_fd, reinterpret_cast<sockaddr*>(&server_socket),
|
|
|
- sizeof(server_socket)) != -1) {
|
|
|
-
|
|
|
- std::function<void()> message_send_fn = [this]() {
|
|
|
- this->handleMessages();
|
|
|
- };
|
|
|
- MessageHandler message_handler = createMessageHandler(message_send_fn);
|
|
|
- // Handle received messages on separate thread
|
|
|
- std::thread (message_handler).detach();
|
|
|
-
|
|
|
+ if (m_client_socket_fd == -1) {
|
|
|
+ m_client_socket_fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
+ if (m_client_socket_fd != -1) {
|
|
|
+ sockaddr_in server_socket;
|
|
|
+ char* end;
|
|
|
+ server_socket.sin_family = AF_INET;
|
|
|
+ auto port_value = strtol(argv[2], &end, 10);
|
|
|
+ if (port_value < 0 || end == argv[2]) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int socket_option = 1;
|
|
|
+ // Free up the port to begin listening again
|
|
|
+ setsockopt(m_client_socket_fd, SOL_SOCKET, SO_REUSEADDR, &socket_option,
|
|
|
+ sizeof(socket_option));
|
|
|
+
|
|
|
+ server_socket.sin_port = htons(port_value);
|
|
|
+ inet_pton(AF_INET, argv[1], &server_socket.sin_addr.s_addr);
|
|
|
+
|
|
|
+ if (::connect(m_client_socket_fd, reinterpret_cast<sockaddr*>(&server_socket),
|
|
|
+ sizeof(server_socket)) != -1) {
|
|
|
+ std::string start_operation_string = createOperation("start", {});
|
|
|
+ std::vector<uint8_t> fb_byte_vector{start_operation_string.begin(), start_operation_string.end()};
|
|
|
+ auto byte_vector = builder.CreateVector(fb_byte_vector);
|
|
|
+ auto message = CreateMessage(builder, 69, byte_vector);
|
|
|
+
|
|
|
+ builder.Finish(message);
|
|
|
+
|
|
|
+ uint8_t* encoded_message_buffer = builder.GetBufferPointer();
|
|
|
+ uint32_t size = builder.GetSize();
|
|
|
+
|
|
|
+ qDebug() << "Size is " << size;
|
|
|
+
|
|
|
+ uint8_t send_buffer[MAX_BUFFER_SIZE];
|
|
|
+ memset(send_buffer, 0, MAX_BUFFER_SIZE);
|
|
|
+ send_buffer[0] = (size & 0xFF) >> 24;
|
|
|
+ send_buffer[1] = (size & 0xFF) >> 16;
|
|
|
+ send_buffer[2] = (size & 0xFF) >> 8;
|
|
|
+ send_buffer[3] = (size & 0xFF);
|
|
|
+ std::memcpy(send_buffer + 4, encoded_message_buffer, size);
|
|
|
+ qDebug() << "Ready to send:";
|
|
|
+ std::string message_to_send{};
|
|
|
+ for (unsigned int i = 0; i < (size + 4); i++) {
|
|
|
+ message_to_send += (char)*(send_buffer + i);
|
|
|
+ }
|
|
|
+ qDebug() << message_to_send.c_str();
|
|
|
+ // Send start operation
|
|
|
+ ::send(m_client_socket_fd, send_buffer, size + 4, 0);
|
|
|
+ builder.Clear();
|
|
|
+ // Delegate message handling to its own thread
|
|
|
+ std::function<void()> message_send_fn = [this]() {
|
|
|
+ this->handleMessages();
|
|
|
+ };
|
|
|
+ MessageHandler message_handler = createMessageHandler(message_send_fn);
|
|
|
+ // Handle received messages on separate thread
|
|
|
+ std::thread (message_handler).detach();
|
|
|
+
|
|
|
+ } else {
|
|
|
+ qDebug() << errno;
|
|
|
+ ::close(m_client_socket_fd);
|
|
|
+ }
|
|
|
} else {
|
|
|
- qDebug() << errno;
|
|
|
- ::close(m_client_socket_fd);
|
|
|
+ qDebug() << "Failed to create new connection";
|
|
|
}
|
|
|
+ } else {
|
|
|
+ qDebug() << "Connection already in progress";
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -116,21 +151,11 @@ void Client::start() {
|
|
|
*/
|
|
|
void Client::sendMessage(const QString& s) {
|
|
|
if (m_client_socket_fd != -1) {
|
|
|
- // Convert QString to std byte vector
|
|
|
- // TODO: switch to using one or the other completely
|
|
|
- json data_json{};
|
|
|
- data_json["type"] = "custom";
|
|
|
- data_json["program"] = "placeholder";
|
|
|
- data_json["message"] = s.toUtf8().data();
|
|
|
-
|
|
|
- std::string json_string = data_json.dump();
|
|
|
-
|
|
|
+ std::string json_string = createMessage(s.toUtf8().data());
|
|
|
std::vector<uint8_t> fb_byte_vector{json_string.begin(), json_string.end()};
|
|
|
-// for (int i = 0; i < q_byte_array.size(); i++) {
|
|
|
-// fb_byte_vector.push_back(static_cast<uint8_t>(q_byte_array.at(i)));
|
|
|
-// }
|
|
|
auto byte_vector = builder.CreateVector(fb_byte_vector);
|
|
|
auto message = CreateMessage(builder, 69, byte_vector);
|
|
|
+
|
|
|
builder.Finish(message);
|
|
|
|
|
|
uint8_t* encoded_message_buffer = builder.GetBufferPointer();
|
|
@@ -152,7 +177,6 @@ void Client::sendMessage(const QString& s) {
|
|
|
}
|
|
|
qDebug() << message_to_send.c_str();
|
|
|
::send(m_client_socket_fd, send_buffer, size + 4, 0);
|
|
|
- memset(send_buffer, 0, MAX_BUFFER_SIZE);
|
|
|
builder.Clear();
|
|
|
} else {
|
|
|
qDebug() << "You must first open a connection";
|
|
@@ -161,6 +185,37 @@ void Client::sendMessage(const QString& s) {
|
|
|
|
|
|
void Client::closeConnection() {
|
|
|
if (m_client_socket_fd != -1) {
|
|
|
+ std::string stop_operation_string = createOperation("stop", {});
|
|
|
+
|
|
|
+ std::vector<uint8_t> fb_byte_vector{stop_operation_string.begin(), stop_operation_string.end()};
|
|
|
+ auto byte_vector = builder.CreateVector(fb_byte_vector);
|
|
|
+ auto message = CreateMessage(builder, 69, byte_vector);
|
|
|
+
|
|
|
+ builder.Finish(message);
|
|
|
+
|
|
|
+ uint8_t* encoded_message_buffer = builder.GetBufferPointer();
|
|
|
+ uint32_t size = builder.GetSize();
|
|
|
+
|
|
|
+ qDebug() << "Size is " << size;
|
|
|
+
|
|
|
+ uint8_t send_buffer[MAX_BUFFER_SIZE];
|
|
|
+ memset(send_buffer, 0, MAX_BUFFER_SIZE);
|
|
|
+ send_buffer[0] = (size & 0xFF) >> 24;
|
|
|
+ send_buffer[1] = (size & 0xFF) >> 16;
|
|
|
+ send_buffer[2] = (size & 0xFF) >> 8;
|
|
|
+ send_buffer[3] = (size & 0xFF);
|
|
|
+ std::memcpy(send_buffer + 4, encoded_message_buffer, size);
|
|
|
+ qDebug() << "Ready to send:";
|
|
|
+ std::string message_to_send{};
|
|
|
+ for (unsigned int i = 0; i < (size + 4); i++) {
|
|
|
+ message_to_send += (char)*(send_buffer + i);
|
|
|
+ }
|
|
|
+ qDebug() << message_to_send.c_str();
|
|
|
+ // Send stop operation
|
|
|
+ ::send(m_client_socket_fd, send_buffer, size + 4, 0);
|
|
|
+ builder.Clear();
|
|
|
+ // Clean up socket file descriptor
|
|
|
+ ::shutdown(m_client_socket_fd, SHUT_RDWR);
|
|
|
::close(m_client_socket_fd);
|
|
|
m_client_socket_fd = -1;
|
|
|
}
|