|
| 1 | +#include "BusCommandProxy.h" |
| 2 | + |
| 3 | +#include "Utils.h" |
| 4 | + |
| 5 | +using namespace service_bus; |
| 6 | + |
| 7 | +string ProxyNode::PROXY_COMMAND = "bus_command_proxy"; |
| 8 | + |
| 9 | +// ------------------------------------------------------------------------------------------------- |
| 10 | +// Constructors and destructors |
| 11 | + |
| 12 | +BusCommandProxy::BusCommandProxy() { |
| 13 | + this->proxy_port = 0; |
| 14 | + this->port_pool = NULL; |
| 15 | +} |
| 16 | + |
| 17 | +BusCommandProxy::BusCommandProxy(const string& command, const vector<string>& args) |
| 18 | + : command(command), args(args) {} |
| 19 | + |
| 20 | +BusCommandProxy::~BusCommandProxy() { |
| 21 | + // port_pool is initialized by ServiceBus. It's an static member of a singleton so there's no |
| 22 | + // need to manage its deletion |
| 23 | + if ((this->port_pool != NULL) && (this->proxy_port != 0)) { |
| 24 | + // Return the port to the pool of available ports |
| 25 | + this->port_pool->enqueue((void*) this->proxy_port); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +ProxyNode::ProxyNode(BusCommandProxy* proxy, const string& node_id) : StarNode(node_id) { |
| 30 | + // SERVER running in BUS command requestor |
| 31 | + this->proxy = proxy; |
| 32 | +} |
| 33 | + |
| 34 | +ProxyNode::ProxyNode(BusCommandProxy* proxy, const string& node_id, const string& server_id) |
| 35 | + : StarNode(node_id, server_id) { |
| 36 | + // CLIENT running in BUS command processor |
| 37 | + this->proxy = proxy; |
| 38 | +} |
| 39 | + |
| 40 | +ProxyNode::~ProxyNode() {} |
| 41 | + |
| 42 | +// ------------------------------------------------------------------------------------------------- |
| 43 | +// Proxy API |
| 44 | + |
| 45 | +void BusCommandProxy::setup_proxy_node(const string& client_id, const string& server_id) { |
| 46 | + if ((this->port_pool == NULL) || (this->proxy_port == 0)) { |
| 47 | + Utils::error("Proxy node can't be set up"); |
| 48 | + } else { |
| 49 | + if (client_id == "") { |
| 50 | + // This proxy is running in the requestor |
| 51 | + string id = this->requestor_id; |
| 52 | + string requestor_host = id.substr(0, id.find(":")); |
| 53 | + string requestor_id = requestor_host + ":" + to_string(this->proxy_port); |
| 54 | + this->proxy_node = new ProxyNode(this, requestor_id); |
| 55 | + } else { |
| 56 | + // This proxy is running in the processor |
| 57 | + this->proxy_node = new ProxyNode(this, client_id, server_id); |
| 58 | + this->proxy_node->peer_id = server_id; |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +void BusCommandProxy::to_remote_peer(const string& command, const vector<string>& args) { |
| 64 | + this->proxy_node->to_remote_peer(command, args); |
| 65 | +} |
| 66 | + |
| 67 | +const string& BusCommandProxy::get_command() { return this->command; } |
| 68 | + |
| 69 | +const vector<string>& BusCommandProxy::get_args() { return this->args; } |
| 70 | + |
| 71 | +// ------------------------------------------------------------------------------------------------- |
| 72 | +// ProxyNode API |
| 73 | + |
| 74 | +void ProxyNode::remote_call(const string& command, const vector<string>& args) { |
| 75 | + this->proxy->from_remote_peer(command, args); |
| 76 | +} |
| 77 | + |
| 78 | +void ProxyNode::to_remote_peer(const string& command, const vector<string>& args) { |
| 79 | + if (this->peer_id == "") { |
| 80 | + Utils::error("Unknown peer"); |
| 81 | + } |
| 82 | + vector<string> new_args(args); |
| 83 | + new_args.push_back(command); |
| 84 | + send(PROXY_COMMAND, new_args, this->peer_id); |
| 85 | +} |
| 86 | + |
| 87 | +void ProxyNode::node_joined_network(const string& node_id) { |
| 88 | + StarNode::node_joined_network(node_id); |
| 89 | + this->peer_id = node_id; |
| 90 | +} |
| 91 | + |
| 92 | +// ------------------------------------------------------------------------------------------------- |
| 93 | +// Messages |
| 94 | + |
| 95 | +shared_ptr<Message> ProxyNode::message_factory(string& command, vector<string>& args) { |
| 96 | + std::shared_ptr<Message> message = DistributedAlgorithmNode::message_factory(command, args); |
| 97 | + if (message) { |
| 98 | + return message; |
| 99 | + } |
| 100 | + if (command == ProxyNode::PROXY_COMMAND) { |
| 101 | + vector<string> new_args(args); |
| 102 | + new_args.pop_back(); |
| 103 | + return std::shared_ptr<Message>(new ProxyMessage(args.back(), new_args)); |
| 104 | + } |
| 105 | + return std::shared_ptr<Message>{}; |
| 106 | +} |
| 107 | + |
| 108 | +ProxyMessage::ProxyMessage(const string& command, const vector<string>& args) { |
| 109 | + this->command = command; |
| 110 | + this->args = args; |
| 111 | +} |
| 112 | + |
| 113 | +void ProxyMessage::act(shared_ptr<MessageFactory> node) { |
| 114 | + auto proxy_node = dynamic_pointer_cast<ProxyNode>(node); |
| 115 | + proxy_node->remote_call(this->command, this->args); |
| 116 | +} |
0 commit comments