Skip to content

Commit

Permalink
use shared_ptr for context
Browse files Browse the repository at this point in the history
  • Loading branch information
jcosentino11 committed Jun 3, 2024
1 parent 4485691 commit 371f4b8
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 20 deletions.
5 changes: 3 additions & 2 deletions include/Command.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include "Context.h"
#include <memory>

namespace MqttClient {

class Command {
public:
Command(Context context);
Command(std::shared_ptr<Context> context);
void execute();

private:
Context mContext;
std::shared_ptr<Context> mContext;
};
} // namespace MqttClient
15 changes: 15 additions & 0 deletions include/Network.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once
#include "Context.h"
#include <memory>

namespace MqttClient {

class Network {
public:
Network(std::shared_ptr<Context> context);

private:
std::shared_ptr<Context> mContext;
};

} // namespace MqttClient
5 changes: 3 additions & 2 deletions include/Packet.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Context.h"
#include <memory>
#include <vector>

namespace MqttClient {
Expand All @@ -10,10 +11,10 @@ using Payload = std::vector<char>;
class PacketBuilder {

public:
PacketBuilder(Context &context);
PacketBuilder(std::shared_ptr<Context> context);
bool connect(Payload &payload); // TODO more specific error handling?

private:
Context mContext;
std::shared_ptr<Context> mContext;
};
} // namespace MqttClient
18 changes: 10 additions & 8 deletions src/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Packet.h"
#include <cstring>
#include <iostream>
#include <memory>
#include <vector>

#include <arpa/inet.h>
Expand All @@ -12,17 +13,18 @@

namespace MqttClient {

Command::Command(Context context) : mContext(context) {}
Command::Command(std::shared_ptr<Context> context)
: mContext(std::move(context)) {}

void Command::execute() {
if (mContext.verbose) {
std::cout << "Executing " << mContext.command << " command\n";
if (mContext->verbose) {
std::cout << "Executing " << mContext->command << " command\n";
}

auto packetBuilder = PacketBuilder(mContext);

// TODO this needs major cleanup
if (mContext.command == "pub") {
if (mContext->command == "pub") {

Payload conn{};
if (packetBuilder.connect(conn)) {
Expand All @@ -38,13 +40,13 @@ void Command::execute() {
}

// TODO move this into context
std::string host = mContext.address;
std::string host = mContext->address;
std::string port = "1883";

size_t pos = mContext.address.find_last_of(':');
size_t pos = mContext->address.find_last_of(':');
if (pos != std::string::npos) {
host = mContext.address.substr(0, pos);
port = mContext.address.substr(pos + 1);
host = mContext->address.substr(0, pos);
port = mContext->address.substr(pos + 1);
}
std::cout << "host: " << host << "\n";
std::cout << "port: " << port << "\n";
Expand Down
17 changes: 17 additions & 0 deletions src/Network.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "Network.h"
#include "Command.h"
#include "Context.h"
#include "Packet.h"
#include <cstring>
#include <iostream>
#include <vector>

#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

namespace MqttClient {
Network::Network(std::shared_ptr<Context> context)
: mContext(std::move(context)) {};
}
16 changes: 9 additions & 7 deletions src/Packet.cpp
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#include "Packet.h"
#include "Context.h"
#include <memory>

namespace MqttClient {
PacketBuilder::PacketBuilder(Context &context) : mContext(context) {};
PacketBuilder::PacketBuilder(std::shared_ptr<Context> context)
: mContext(std::move(context)) {};

bool PacketBuilder::connect(Payload &payload) {
char variableHeaderLength = 10; // TODO const

// client id length needs to fit in the second byte of the connect
// packet (remainingLength)
if (mContext.clientId.size() >
if (mContext->clientId.size() >
255 - variableHeaderLength) { // TODO can this just be moved to
// earlier validation?
return false; // TODO error handling
}
char remainingLength =
variableHeaderLength +
(2 + mContext.clientId.size()); // 2 two encode length
(2 + mContext->clientId.size()); // 2 two encode length

size_t connPacketLen = 2 + remainingLength;
payload.reserve(connPacketLen);
Expand All @@ -41,10 +43,10 @@ bool PacketBuilder::connect(Payload &payload) {
payload.push_back(0); // keep alive LSB
// BEGIN PAYLOAD
// client id
payload.push_back(0); // TODO client id length MSB
payload.push_back(mContext.clientId.size()); // client id length LSB
for (size_t c = 0; c < mContext.clientId.size(); ++c) {
payload.push_back(mContext.clientId[c]);
payload.push_back(0); // TODO client id length MSB
payload.push_back(mContext->clientId.size()); // client id length LSB
for (size_t c = 0; c < mContext->clientId.size(); ++c) {
payload.push_back(mContext->clientId[c]);
}
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "Context.h"
#include <getopt.h>
#include <iostream>
#include <memory>
#include <string>

using namespace MqttClient;
Expand Down Expand Up @@ -31,7 +32,7 @@ int main(int argc, char *argv[]) {
<< ", message=" << context.message << "]\n";
}

auto command = new Command(context);
auto command = new Command(std::make_shared<Context>(context));
command->execute();

return 0;
Expand Down

0 comments on commit 371f4b8

Please sign in to comment.