Skip to content

Commit

Permalink
Compiles and works well!
Browse files Browse the repository at this point in the history
  • Loading branch information
GamePad64 committed Apr 18, 2014
1 parent 8400e67 commit 7c5cdee
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .cproject
Expand Up @@ -67,7 +67,7 @@
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="test|src/daemon/messaging|contrib" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry excluding="libtest|test|src/daemon/messaging|contrib" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
</sourceEntries>
</configuration>
</storageModule>
Expand Down Expand Up @@ -151,7 +151,7 @@
</toolChain>
</folderInfo>
<sourceEntries>
<entry excluding="test|src/daemon/messaging|src/daemon|contrib" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
<entry excluding="libtest|test|src/daemon/messaging|src/daemon|contrib" flags="VALUE_WORKSPACE_PATH" kind="sourcePath" name=""/>
</sourceEntries>
</configuration>
</storageModule>
Expand Down
3 changes: 2 additions & 1 deletion src/common/api/APIMessage.proto
Expand Up @@ -54,11 +54,12 @@ message APIMessage {

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

/*
* GENERATE_PRIVATE_KEY_CALLBACK,
Expand Down
1 change: 1 addition & 0 deletions src/common/api/UnixAPISocket.cpp
Expand Up @@ -134,6 +134,7 @@ void UnixAPISocket::handleReceive(const boost::system::error_code& error,
delete[] message;

session_socket.get_io_service().dispatch(std::bind(receive_handler, message_proto, 0));
asyncReceive(receive_handler);
}

} /* namespace unix */
Expand Down
2 changes: 0 additions & 2 deletions src/daemon/api/APIManager.h
Expand Up @@ -28,8 +28,6 @@ class APIManager : public Loggable {
public:
APIManager();
virtual ~APIManager();


};

} /* namespace api */
Expand Down
9 changes: 7 additions & 2 deletions src/daemon/api/APIServer.cpp
Expand Up @@ -19,10 +19,15 @@ namespace p2pnet {
namespace api {

APIServer::APIServer() {}
APIServer::~APIServer() {}
APIServer::~APIServer() {
for(auto session_ptr : api_sessions){
delete session_ptr;
}
}

void APIServer::dropSession(APISession* session_ptr) {
api_sessions.erase(std::unique_ptr<APISession>(session_ptr));
api_sessions.erase(session_ptr);
delete session_ptr;
}

} /* namespace api */
Expand Down
2 changes: 1 addition & 1 deletion src/daemon/api/APIServer.h
Expand Up @@ -25,7 +25,7 @@ class APISession;

class APIServer : public boost::noncopyable, std::enable_shared_from_this<APIServer> {
protected:
std::set<std::unique_ptr<APISession>> api_sessions;
std::set<APISession*> api_sessions;
public:
APIServer();
virtual ~APIServer();
Expand Down
60 changes: 55 additions & 5 deletions src/daemon/api/APISession.cpp
Expand Up @@ -21,6 +21,8 @@ namespace api {

APISession::APISession(APIServer* parent) {
parent_api_server = parent;
socket_count = 0;
next_id = 1;
log() << "New API session started" << std::endl;
}
APISession::~APISession() {
Expand All @@ -29,12 +31,60 @@ APISession::~APISession() {

void APISession::process(APIMessage message) {
switch(message.type()){
case APIMessage::GENERATE_PRIVATE_KEY:
auto privkey = crypto::PrivateKeyDSA::generateNewKey();
case APIMessage::REGISTER_SOCKET: {
auto sock_id = registerNewSocket();

APIMessage message_reply;
message_reply.set_type(APIMessage::GENERATE_PRIVATE_KEY_CALLBACK);
send(message_reply);
APIMessage message_reply;
message_reply.set_type(APIMessage::REGISTER_SOCKET_CALLBACK);
message_reply.set_socket_id(sock_id);
send(message_reply);
}
break;
case APIMessage::UNREGISTER_SOCKET: {
unregisterSocket(message.socket_id());

APIMessage message_reply;
message_reply.set_type(APIMessage::UNREGISTER_SOCKET_CALLBACK);
message_reply.set_socket_id(message.socket_id());
send(message_reply);
}
break;
}
}

uint32_t APISession::registerNewSocket() {
if(next_id == 0)
return 0;
uint32_t socket_id = next_id;
auto it_pair = endpoints.insert(std::make_pair(socket_id, std::shared_ptr<endpoint::LocalEndpoint>()));

log() << "Registered socket #" << socket_id << std::endl;

socket_count++;

next_id++; // If it turns into zero, than this was the last socket we can create;

auto new_iterator = it_pair.first;
new_iterator++;

while(next_id != 0 && new_iterator != endpoints.end()){
if(new_iterator->first <= next_id){
next_id++;
new_iterator++;
}else{
break;
}
}

return socket_id;
}

void APISession::unregisterSocket(uint32_t sock_id) {
auto it = endpoints.find(sock_id);
endpoints.erase(it);
log() << "Unegistered socket #" << sock_id << std::endl;
if(next_id > sock_id || next_id == 0){
next_id = sock_id;
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/daemon/api/APISession.h
Expand Up @@ -25,7 +25,11 @@ class APIServer;

class APISession : public Loggable, public boost::noncopyable {
std::map<uint32_t, std::shared_ptr<endpoint::LocalEndpoint>> endpoints;
uint32_t next_id;
uint32_t socket_count;

uint32_t registerNewSocket();
void unregisterSocket(uint32_t sock_id);
protected:
APISession(APIServer* parent);

Expand Down
2 changes: 1 addition & 1 deletion src/daemon/api/UnixAPIServer.cpp
Expand Up @@ -68,7 +68,7 @@ void UnixAPIServer::accept() {
void UnixAPIServer::handleAccept(UnixAPISocket* new_socket) {
auto new_session = new UnixAPISession(new_socket, this);

api_sessions.insert(std::unique_ptr<APISession>(new_session));
api_sessions.insert(new_session);
new_socket->asyncReceive(std::bind(&UnixAPIServer::handleReceive, this, new_session, std::placeholders::_1, std::placeholders::_2));

accept();
Expand Down
34 changes: 24 additions & 10 deletions src/library/impl/P2PSocket.cpp
Expand Up @@ -21,7 +21,7 @@ namespace p2pnet {

class P2PSocket::Impl {
public:
P2PDaemon* m_parent_daemon;
std::shared_ptr<P2PDaemon> m_parent_daemon;

uint32_t socket_id;
std::map<uint32_t, P2PContext*> m_contexts;
Expand All @@ -32,11 +32,14 @@ class P2PSocket::Impl {
};

// Constructors
P2PSocket::P2PSocket() : P2PSocket(Singleton<P2PLocalDaemon>::getInstance()) {}
P2PSocket::P2PSocket() : P2PSocket(SharedSingleton<P2PLocalDaemon>::getInstance()) {}

P2PSocket::P2PSocket(P2PDaemon* parent_daemon) {
P2PSocket::P2PSocket(P2PDaemon* parent_daemon) : P2PSocket(std::shared_ptr<P2PDaemon>(parent_daemon)) {}

P2PSocket::P2PSocket(std::shared_ptr<P2PDaemon> parent_shared_daemon) {
impl = new Impl();
impl->m_parent_daemon = parent_daemon;
Loggable::log("P2PSocket") << "Registering new socket." << std::endl;
impl->m_parent_daemon = parent_shared_daemon;

api::APIMessage send_message;
send_message.set_type(send_message.REGISTER_SOCKET);
Expand All @@ -46,10 +49,18 @@ P2PSocket::P2PSocket(P2PDaemon* parent_daemon) {
if(!ec){
api::APIMessage recv_message;
recv_message = impl->m_parent_daemon->receive(ec);
if(recv_message.type() == recv_message.REGISTER_SOCKET_CALLBACK)
if(recv_message.type() == recv_message.REGISTER_SOCKET_CALLBACK){
impl->socket_id = recv_message.socket_id();
Loggable::log("P2PSocket") << "Registered socket: #" << impl->socket_id << std::endl;
if(impl->socket_id == 0){
// Ouch, bad sign. It means, that p2pnetd didn't create the socket, as there are too much of them;
// TODO: new exception model.
throw std::out_of_range("Socket count exceeded maximum");
}
}
}else{
//EXCEPTION
// TODO: new exception model.
throw std::runtime_error("p2pnetd connection problem");
}
}

Expand All @@ -71,10 +82,12 @@ P2PSocket::~P2PSocket() {
int ec;
impl->m_parent_daemon->send(send_message, ec);
if(!ec){
api::APIMessage recv_message;
recv_message = impl->m_parent_daemon->receive(ec);
Loggable::log("P2PSocket") << "Unregistering socket: #" << impl->socket_id << std::endl;
auto recv_message = impl->m_parent_daemon->receive(ec);
delete impl;
}else{
//EXCEPTION
// TODO: new exception model.
throw std::runtime_error("p2pnetd connection problem");
}
}

Expand All @@ -91,7 +104,8 @@ void P2PSocket::bind(std::string base58_private_key) {
if(recv_message.type() == recv_message.BIND_SOCKET_CALLBACK)
impl->m_bound_private_key.setAsBase58(base58_private_key);
}else{
//EXCEPTION
// TODO: new exception model.
throw std::runtime_error("p2pnetd connection problem");
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/library/p2pnet.h
Expand Up @@ -101,11 +101,12 @@ LIBP2PNET_DLL_EXPORTED P2PSocket* p2p_getParentSocket(P2PContext* context);

class P2PSocket {
class Impl; Impl* impl;
P2PSocket(std::shared_ptr<P2PDaemon> parent_shared_daemon);
public:
P2PSocket();
P2PSocket(P2PDaemon* parent_socket_manager);
P2PSocket(P2PDaemon* parent_daemon);
P2PSocket(std::string base58_private_key);
P2PSocket(std::string base58_private_key, P2PDaemon* parent_socket_manager);
P2PSocket(std::string base58_private_key, P2PDaemon* parent_daemon);

virtual ~P2PSocket();

Expand Down

0 comments on commit 7c5cdee

Please sign in to comment.