MASA-Core
MasaNet.hpp
Go to the documentation of this file.
00001 /*******************************************************************************
00002  *
00003  * Copyright (c) 2010-2015   Edans Sandes
00004  *
00005  * This file is part of MASA-Core.
00006  * 
00007  * MASA-Core is free software: you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation, either version 3 of the License, or
00010  * (at your option) any later version.
00011  * 
00012  * MASA-Core is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  * 
00017  * You should have received a copy of the GNU General Public License
00018  * along with MASA-Core.  If not, see <http://www.gnu.org/licenses/>.
00019  *
00020  ******************************************************************************/
00021 
00022 class MasaNet;
00023 struct peer_t; // TODO remove
00024 
00025 #ifndef MASANET_HPP_
00026 #define MASANET_HPP_
00027 
00028 #include <pthread.h>
00029 #include <string>
00030 #include <map>
00031 #include <set>
00032 #include <vector>
00033 #include <sstream>
00034 using namespace std;
00035 
00036 #include "MasaNetStatus.hpp"
00037 #include "Peer.hpp"
00038 #include "PeerList.hpp"
00039 
00040 
00041 /* Node Types */
00042 #define TYPE_CLI                                (0)
00043 #define TYPE_PROCESSING_NODE    (1)
00044 #define TYPE_GATEWAY                    (2)
00045 #define TYPE_UNKNOWN                    (-1)
00046 
00047 
00048 #define CMD_CONNECTED_PEERS             (0)
00049 #define CMD_DISCOVERED_PEERS    (1)
00050 #define CMD_DATA_PEERS                  (2)
00051 
00052 typedef void (MasaNet::*cmd_handler_f)(Command* cmd, Peer* socket);
00053 
00054 
00055 class MasaNet : public MasaNetCallbacks {
00056 public:
00057         MasaNet(int nodeType, string description);
00058         virtual ~MasaNet();
00059 
00060         void startServer(int port);
00061         Peer* connectToPeer(string address, int connection_type);
00062         void createRing();
00063 
00064         bool onConnect(Peer* peer);
00065         bool onConnectData(Peer* peer);
00066         void onDisconnect(Peer* peer);
00067 
00068         void announce(vector<Peer*> announcedPeers, Peer* excludeSocket);
00069         void disanounce(Peer* peer, Peer* excludeSocket);
00070         MasaNetStatus* getPeerStatus();
00071         const set<string>& testRing(string peer);
00072 
00073         const PeerList& getConnectedPeers() const;
00074         const PeerList& getDiscoveredPeers() const;
00075         Peer* getLeftPeer() const;
00076         Peer* getRightPeer() const;
00077 
00078         const vector<Peer*>& getRemotePeers(string peer, int type);
00079 
00080 private:
00081         int nodeType;
00082         string nodeDescription;
00083 
00084         int serverSocket;
00085         pthread_t listeningThread;
00086         bool serverActive;
00087         pthread_mutex_t mutex;
00088 
00089         MasaNetStatus status;
00090 
00091         string myId;
00092         int serverPort;
00093 
00094         PeerList peers;
00095         PeerList discoveredPeers;
00096 
00097         string leftPeerId;
00098         Peer* leftPeerData;
00099         Peer* leftPeer;
00100         string rightPeerId;
00101         Peer* rightPeerData;
00102         Peer* rightPeer;
00103 
00104 
00105         static map<int, cmd_handler_f> cmdHandlers;
00106 
00107 
00108         static void registerCommand(cmd_creator_f creator, cmd_handler_f handler);
00109 
00110         void broadcastCommand(Command* command, Peer* excludeSocket = NULL);
00111 
00112         static void* staticListeningThread(void *arg);
00113         static void* staticPeerHandler(void *arg);
00114 
00115         static int hostname_to_ip(const char *hostname , char *ip);
00116 
00117         void cmd_discover(Command* _cmd, Peer* socket);
00118         void cmd_undiscover(Command* _cmd, Peer* socket);
00119         void cmd_status_request(Command* _cmd, Peer* socket);
00120         void cmd_peer_request(Command* _cmd, Peer* socket);
00121         void cmd_create_ring(Command* _cmd, Peer* socket);
00122         void cmd_test_ring(Command* _cmd, Peer* socket);
00123         Peer* getPeer(const string& peer);
00124         Peer* solveSimultaneousConnection(Peer* newPeer, Peer* oldPeer);
00125 };
00126 
00127 #endif /* MASANET_HPP_ */