Skip to content

Commit

Permalink
Tried to make sort of useful API.
Browse files Browse the repository at this point in the history
  • Loading branch information
GamePad64 committed Apr 18, 2014
1 parent 8231157 commit 92b1b70
Show file tree
Hide file tree
Showing 23 changed files with 233 additions and 530 deletions.
43 changes: 31 additions & 12 deletions src/common/api/APIMessage.proto
Expand Up @@ -19,37 +19,56 @@ package p2pnet.api;

message APIMessage {
enum MessageType {
REGISTER_SOCKET = 0;
REGISTER_SOCKET_CALLBACK = 1;
READ = 0;
READ_CALLBACK = 1;

GENERATE_PRIVATE_KEY = 2; // Well, we make it here, but I will create an ability to generate it on client.
GENERATE_PRIVATE_KEY_CALLBACK = 3;
WRITE = 2;
WRITE_CALLBACK = 3;

BIND_SOCKET = 4;
REGISTER_SOCKET = 4;
REGISTER_SOCKET_CALLBACK = 5;

CONNECT = 5;
CONNECT_CALLBACK = 6;
UNREGISTER_SOCKET = 6;
UNREGISTER_SOCKET_CALLBACK = 7;

GENERATE_PRIVATE_KEY = 8; // Well, we make it here, but I will create an ability to generate it on client.
GENERATE_PRIVATE_KEY_CALLBACK = 9;

BIND_SOCKET = 10;
BIND_SOCKET_CALLBACK = 11;

CONNECT = 12;
CONNECT_CALLBACK = 13;

ACCEPT = 14;
ACCEPT_CALLBACK = 15;
}

required MessageType type = 1;
optional uint32 error_code = 2;

/* READ_CALLBACK,
* WRITE
*/
optional bytes data = 3;

/* REGISTER_SOCKET_CALLBACK,
* UNREGISTER_SOCKET,
* BIND_SOCKET,
* CONNECT,
* CONNECT_CALLBACK
*/
optional uint64 socket_id = 2;
optional uint64 socket_id = 4;

/*
* GENERATE_PRIVATE_KEY_CALLBACK,
* BIND_SOCKET
*/
optional string privkey_b58 = 3;
optional bool published = 4;
optional string privkey_b58 = 5;

/* CONNECT */
optional string dest_sh = 5;
optional string dest_sh = 6;

/* CONNECT_CALLBACK */
optional string association
optional string association = 7;
}
42 changes: 0 additions & 42 deletions src/library/SocketManager.h

This file was deleted.

27 changes: 0 additions & 27 deletions src/library/impl/P2PAssociation.cpp

This file was deleted.

53 changes: 0 additions & 53 deletions src/library/impl/P2PAssociation.h

This file was deleted.

Expand Up @@ -11,11 +11,11 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "P2PContext.h"
#include "P2PContextImpl.h"

namespace p2pnet {

P2PContext::P2PContext(ContextType context_type, std::weak_ptr<P2PAssociation> parent_association) :
P2PContext::P2PContext(ContextType context_type, std::weak_ptr<P2PAssociationImpl> parent_association) :
m_context_type(context_type),
m_parent_association(parent_association) {
}
Expand Down
22 changes: 11 additions & 11 deletions src/library/impl/P2PContext.h → src/library/impl/P2PContextImpl.h
Expand Up @@ -14,30 +14,30 @@
#ifndef P2PCONTEXT_H_
#define P2PCONTEXT_H_

#include "daemon_comm/ClientDataSocket.h"

namespace p2pnet {
namespace impl {

class P2PContext {
class P2PContextImpl {
public:
enum ContextType {
SEQUENCE = 0,
STREAM = 1,
DATAGRAM = 2
TYPE_SEQPACKET = 0,
TYPE_STREAM = 1,
TYPE_DATAGRAM = 2,
TYPE_ANY = 255
};

P2PContext(ContextType context_type, std::weak_ptr<P2PAssociation> parent_association);
virtual ~P2PContext();
P2PContextImpl(ContextType context_type, P2PSocketImpl* parent_socket);
virtual ~P2PContextImpl();

ContextType getContextType() const;

private:
ContextType m_context_type;
std::weak_ptr<P2PAssociation> m_parent_association;

impl::ClientDataSocket* m_data_socket;
uint32_t m_id;
P2PSocketImpl* m_parent_socket;
};

} /* namespace impl */
} /* namespace p2pnet */

#endif /* P2PCONTEXT_H_ */
Expand Up @@ -11,16 +11,17 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DaemonConnectionImpl.h"
#include "P2PDaemonConnectionImpl.h"

#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
#include "daemon_comm/UnixAPIClient.h"
#endif
#include "daemon_comm/APIClient.h"

namespace p2pnet {
namespace impl {

DaemonConnectionImpl::DaemonConnectionImpl(bool autoconnect) :
P2PDaemonConnectionImpl::P2PDaemonConnectionImpl(bool autoconnect) :
m_external_io_service(false) {
m_io_service = new boost::asio::io_service();
socket_thread = std::thread([&](){m_io_service->run();});
Expand All @@ -29,15 +30,15 @@ DaemonConnectionImpl::DaemonConnectionImpl(bool autoconnect) :
}
}

DaemonConnectionImpl::DaemonConnectionImpl(boost::asio::io_service& io_service, bool autoconnect) :
P2PDaemonConnectionImpl::P2PDaemonConnectionImpl(boost::asio::io_service& io_service, bool autoconnect) :
m_external_io_service(true) {
m_io_service = &io_service;
if(autoconnect){
connect();
}
}

DaemonConnectionImpl::~DaemonConnectionImpl() {
P2PDaemonConnectionImpl::~P2PDaemonConnectionImpl() {
if(!m_external_io_service){
m_io_service->stop();
if(socket_thread.joinable())
Expand All @@ -46,7 +47,7 @@ DaemonConnectionImpl::~DaemonConnectionImpl() {
}
}

int DaemonConnectionImpl::connect() {
int P2PDaemonConnectionImpl::connect() {
int ec = 0;
#ifdef BOOST_ASIO_HAS_LOCAL_SOCKETS
try {
Expand All @@ -58,12 +59,13 @@ int DaemonConnectionImpl::connect() {
return ec;
}

bool DaemonConnectionImpl::is_connected() {
bool P2PDaemonConnectionImpl::is_connected() {
return bool(m_api_client);
}

std::shared_ptr<api::APIClient> DaemonConnectionImpl::getAPIClient() {
std::shared_ptr<api::APIClient> P2PDaemonConnectionImpl::getAPIClient() {
return m_api_client;
}

} /* namespace impl */
} /* namespace p2pnet */
Expand Up @@ -20,6 +20,7 @@
#include <memory>

namespace p2pnet {
namespace impl {

/**
* This class is used for connection to p2pnetd.
Expand All @@ -28,24 +29,25 @@ namespace p2pnet {
* So, for writing applications that use only single default p2pnetd instance (and this is likely)
* you don't need to bother with this class, just create P2PSocket.
*/
class DaemonConnectionImpl {
class P2PDaemonConnectionImpl {
std::thread socket_thread;
boost::asio::io_service* m_io_service;
bool m_external_io_service;

std::shared_ptr<api::APIClient> m_api_client;
std::map<int, std::shared_ptr<P2PSocket>> m_socket_ids;
std::map<int, std::shared_ptr<P2PSocketImpl>> m_socket_ids;
public:
DaemonConnectionImpl(bool autoconnect = true);
DaemonConnectionImpl(boost::asio::io_service& io_service, bool autoconnect = true);
virtual ~DaemonConnectionImpl();
P2PDaemonConnectionImpl(bool autoconnect = true);
P2PDaemonConnectionImpl(boost::asio::io_service& io_service, bool autoconnect = true);
virtual ~P2PDaemonConnectionImpl();

int connect();
bool is_connected();

std::shared_ptr<api::APIClient> getAPIClient();
};

} /* namespace impl */
} /* namespace p2pnet */

#endif /* SOCKETMANAGER_H_ */
27 changes: 0 additions & 27 deletions src/library/impl/P2PDatagramContext.cpp

This file was deleted.

0 comments on commit 92b1b70

Please sign in to comment.