Skip to content

Commit

Permalink
Stabilized APIClient. Now using new interface of UnixAPISocket.
Browse files Browse the repository at this point in the history
  • Loading branch information
GamePad64 committed Feb 10, 2014
1 parent 91a881c commit 9204092
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
Expand Up @@ -16,9 +16,5 @@
namespace p2pnet {
namespace api {

void APIClient::process(APIMessage message) {
log() << message.privkey_cert();
}

} /* namespace api */
} /* namespace p2pnet */
Expand Up @@ -21,16 +21,22 @@ namespace p2pnet {
namespace api {

class APIClient : protected Loggable {
protected:
typedef std::function<void(int)> SendHandler;
typedef std::function<void(api::APIMessage, int)> ReceiveHandler;
public:
APIClient(){};
virtual ~APIClient(){};

void process(APIMessage message);
virtual void send(APIMessage message) = 0;
virtual void send(api::APIMessage data, int& error_code) = 0;
virtual api::APIMessage receive(int& error_code) = 0;

virtual void shutdown() = 0;
virtual void asyncSend(api::APIMessage data, SendHandler send_handler) = 0;
virtual void asyncReceive(ReceiveHandler receive_handler) = 0;

virtual void connect() = 0;

virtual std::shared_ptr<impl::ClientDataSocket> createDataSocket(std::string socket_addr) = 0;
};

} /* namespace api */
Expand Down
35 changes: 23 additions & 12 deletions src/library/impl/daemon_comm/UnixAPIClient.cpp
Expand Up @@ -18,18 +18,27 @@ namespace p2pnet {
namespace api {
namespace unix {

UnixAPIClient::UnixAPIClient(boost::asio::io_service& io_service) : socket(io_service) {
socket.assignReceiveHandler(std::bind(&APIClient::process, this, std::placeholders::_1));
socket.assignShutdownHandler([](){});

UnixAPIClient::UnixAPIClient(boost::asio::io_service& io_service) : m_socket(io_service) {
connect();

socket.startReceive();
}

UnixAPIClient::~UnixAPIClient() {}

void UnixAPIClient::send(APIMessage message) {socket.send(message);}
void UnixAPIClient::send(api::APIMessage data, int& error_code) {
m_socket.send(data, error_code);
}

api::APIMessage UnixAPIClient::receive(int& error_code) {
return m_socket.receive(error_code);
}

void UnixAPIClient::asyncSend(api::APIMessage data, SendHandler send_handler){
m_socket.asyncSend(data, send_handler);
}

void UnixAPIClient::asyncReceive(ReceiveHandler receive_handler){
m_socket.asyncReceive(receive_handler);
}

void UnixAPIClient::connect() {
bool connected = false;
Expand All @@ -38,20 +47,22 @@ void UnixAPIClient::connect() {
auto it = path_list.begin();
while((!connected) && it != path_list.end()){
try {
socket_path = *it;
socket.getSocket().connect(stream_protocol::endpoint(socket_path));
m_socket_path = *it;
m_socket.getSocket().connect(stream_protocol::endpoint(m_socket_path));
connected = true;
} catch (boost::system::system_error& e) {
if(++it == path_list.end()) // sic! increment is here.
throw; // We can do nothing. No socket paths are available. Maybe, p2pnetd is down?
} // TODO: Yep, may throw.
}
log() << "Connected to daemon on: " << socket_path << std::endl;
log() << "Connected to daemon on: " << m_socket_path << std::endl;
}

void UnixAPIClient::shutdown(){
};
std::shared_ptr<impl::ClientDataSocket> UnixAPIClient::createDataSocket(std::string socket_addr) {
return std::make_shared<impl::UnixClientDataSocket>(socket_addr);
}

}

} /* namespace api */
} /* namespace p2pnet */
13 changes: 9 additions & 4 deletions src/library/impl/daemon_comm/UnixAPIClient.h
Expand Up @@ -22,18 +22,23 @@ namespace api {
namespace unix {

class UnixAPIClient : public APIClient {
std::string socket_path;
std::string m_socket_path;
UnixAPISocket m_socket;

UnixAPISocket socket;
boost::asio::io_service& m_io_service;
public:
UnixAPIClient(boost::asio::io_service& io_service);
virtual ~UnixAPIClient();

void send(APIMessage message);
void send(api::APIMessage data, int& error_code);
api::APIMessage receive(int& error_code);

void shutdown();
void asyncSend(api::APIMessage data, SendHandler send_handler);
void asyncReceive(ReceiveHandler receive_handler);

void connect();

std::shared_ptr<impl::ClientDataSocket> createDataSocket(std::string socket_addr);
};

} /* namespace unix */
Expand Down

0 comments on commit 9204092

Please sign in to comment.