Skip to content

Commit

Permalink
Tried to create SocketManager and P2PSocket to be used in API.
Browse files Browse the repository at this point in the history
  • Loading branch information
GamePad64 committed Jan 24, 2014
1 parent 1d2abcd commit 2d7a94e
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 9 deletions.
26 changes: 26 additions & 0 deletions src/library/P2PSocket.cpp
@@ -0,0 +1,26 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 "P2PSocket.h"
#include "SocketManager.h"

namespace p2pnet {

P2PSocket::P2PSocket() {
m_parent_manager = Singleton<SocketManager>::getInstance();
}
P2PSocket::P2PSocket(SocketManager& parent_manager) : m_parent_manager(&parent_manager) {}

P2PSocket::~P2PSocket() {}

} /* namespace p2pnet */
31 changes: 31 additions & 0 deletions src/library/P2PSocket.h
@@ -0,0 +1,31 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef P2PSOCKET_H_
#define P2PSOCKET_H_

namespace p2pnet {

class SocketManager;

class P2PSocket {
SocketManager* m_parent_manager;
public:
P2PSocket();
P2PSocket(SocketManager& parent_manager);
virtual ~P2PSocket();
};

} /* namespace p2pnet */

#endif /* P2PSOCKET_H_ */
36 changes: 36 additions & 0 deletions src/library/SocketManager.cpp
@@ -0,0 +1,36 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* 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 "SocketManager.h"

namespace p2pnet {

SocketManager::SocketManager() : m_external_io_service(false) {
socket_thread = std::thread([&](){m_io_service->run();});
}

SocketManager::SocketManager(boost::asio::io_service& io_service) :
m_io_service(&io_service),
m_external_io_service(true) {
}

SocketManager::~SocketManager() {
if(!m_external_io_service){
m_io_service->stop();
if(socket_thread.joinable())
socket_thread.join();
delete m_io_service;
}
}

} /* namespace p2pnet */
42 changes: 42 additions & 0 deletions src/library/SocketManager.h
@@ -0,0 +1,42 @@
/*
* You may redistribute this program and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SOCKETMANAGER_H_
#define SOCKETMANAGER_H_

#include <boost/asio.hpp>
#include <thread>
#include "daemon_comm/APIClient.h"

namespace p2pnet {

/**
* This class is used for connection to p2pnetd.
*/
class SocketManager {
std::thread socket_thread;
boost::asio::io_service* m_io_service;
bool m_external_io_service;

api::APIClient* m_primary_api_client;
public:
SocketManager();
SocketManager(boost::asio::io_service& io_service);
virtual ~SocketManager();


};

} /* namespace p2pnet */

#endif /* SOCKETMANAGER_H_ */
File renamed without changes.
13 changes: 7 additions & 6 deletions src/library/APIClient.h → src/library/daemon_comm/APIClient.h
Expand Up @@ -14,22 +14,23 @@
#ifndef APICLIENT_H_
#define APICLIENT_H_

#include "../common/Loggable.h"
#include "../../common/Loggable.h"
#include "../../common/api/APIMessage.pb.h"

namespace p2pnet {
namespace api {

class APIClient : protected Loggable {
public:
APIClient();
virtual ~APIClient();
APIClient(){};
virtual ~APIClient(){};

void process(APIMessage message);
void send(APIMessage message) = 0;
virtual void send(APIMessage message) = 0;

void shutdown() = 0;
virtual void shutdown() = 0;

void connect() = 0;
virtual void connect() = 0;
};

} /* namespace api */
Expand Down
Expand Up @@ -12,8 +12,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "UnixAPIClient.h"
#include "../common/api/APIMessage.pb.h"
#include "export.h"
#include <list>

namespace p2pnet {
Expand Down
Expand Up @@ -14,7 +14,8 @@
#ifndef UNIXAPI_H_
#define UNIXAPICLIENT_H_

#include "../common/api/UnixAPISocket.h"
#include "../../common/api/UnixAPISocket.h"
#include "APIClient.h"

namespace p2pnet {
namespace api {
Expand Down

0 comments on commit 2d7a94e

Please sign in to comment.