IBR-DTNSuite  0.8
daemon/src/api/ManagementConnection.cpp
Go to the documentation of this file.
00001 /*
00002  * ManagementConnection.cpp
00003  *
00004  *  Created on: 13.10.2011
00005  *      Author: morgenro
00006  */
00007 
00008 #include "config.h"
00009 #include "Configuration.h"
00010 #include "ManagementConnection.h"
00011 #include "core/BundleCore.h"
00012 #include "core/GlobalEvent.h"
00013 #include "routing/prophet/ProphetRoutingExtension.h"
00014 
00015 #include <ibrdtn/utils/Utils.h>
00016 
00017 #include <ibrcommon/Logger.h>
00018 #include <ibrcommon/net/LinkManager.h>
00019 
00020 namespace dtn
00021 {
00022         namespace api
00023         {
00024                 ManagementConnection::ManagementConnection(ClientHandler &client, ibrcommon::tcpstream &stream)
00025                  : ProtocolHandler(client, stream)
00026                 {
00027                 }
00028 
00029                 ManagementConnection::~ManagementConnection()
00030                 {
00031                 }
00032 
00033                 void ManagementConnection::run()
00034                 {
00035                         std::string buffer;
00036                         _stream << ClientHandler::API_STATUS_OK << " SWITCHED TO MANAGEMENT" << std::endl;
00037 
00038                         // run as long the stream is ok
00039                         while (_stream.good())
00040                         {
00041                                 getline(_stream, buffer);
00042 
00043                                 // search for '\r\n' and remove the '\r'
00044                                 std::string::reverse_iterator iter = buffer.rbegin();
00045                                 if ( (*iter) == '\r' ) buffer = buffer.substr(0, buffer.length() - 1);
00046 
00047                                 std::vector<std::string> cmd = dtn::utils::Utils::tokenize(" ", buffer);
00048                                 if (cmd.size() == 0) continue;
00049 
00050                                 if (cmd[0] == "exit")
00051                                 {
00052                                         // return to previous level
00053                                         break;
00054                                 }
00055                                 else
00056                                 {
00057                                         // forward to standard command set
00058                                         processCommand(cmd);
00059                                 }
00060                         }
00061                 }
00062 
00063                 void ManagementConnection::finally()
00064                 {
00065                 }
00066 
00067                 void ManagementConnection::setup()
00068                 {
00069                 }
00070 
00071                 void ManagementConnection::__cancellation()
00072                 {
00073                 }
00074 
00075                 void ManagementConnection::processCommand(const std::vector<std::string> &cmd)
00076                 {
00077                         class BundleFilter : public dtn::storage::BundleStorage::BundleFilterCallback
00078                         {
00079                         public:
00080                                 BundleFilter()
00081                                 {};
00082 
00083                                 virtual ~BundleFilter() {};
00084 
00085                                 virtual size_t limit() const { return 0; };
00086 
00087                                 virtual bool shouldAdd(const dtn::data::MetaBundle &meta) const
00088                                 {
00089                                         return true;
00090                                 }
00091                         };
00092 
00093                         try {
00094                                 if (cmd[0] == "neighbor")
00095                                 {
00096                                         if (cmd.size() < 2) throw ibrcommon::Exception("not enough parameters");
00097 
00098                                         if (cmd[1] == "list")
00099                                         {
00100                                                 const std::set<dtn::core::Node> nlist = dtn::core::BundleCore::getInstance().getNeighbors();
00101 
00102                                                 _stream << ClientHandler::API_STATUS_OK << " NEIGHBOR LIST" << std::endl;
00103                                                 for (std::set<dtn::core::Node>::const_iterator iter = nlist.begin(); iter != nlist.end(); iter++)
00104                                                 {
00105                                                         _stream << (*iter).getEID().getString() << std::endl;
00106                                                 }
00107                                                 _stream << std::endl;
00108                                         }
00109                                         else
00110                                         {
00111                                                 _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN COMMAND" << std::endl;
00112                                         }
00113                                 }
00114                                 else if (cmd[0] == "interface")
00115                                 {
00116                                         if (cmd[1] == "address")
00117                                         {
00118                                                 try {
00119                                                         ibrcommon::LinkManager &lm = ibrcommon::LinkManager::getInstance();
00120 
00121                                                         ibrcommon::vaddress::Family f = ibrcommon::vaddress::VADDRESS_UNSPEC;
00122 
00123                                                         if (cmd[4] == "ipv4")   f = ibrcommon::vaddress::VADDRESS_INET;
00124                                                         if (cmd[4] == "ipv6")   f = ibrcommon::vaddress::VADDRESS_INET6;
00125 
00126                                                         ibrcommon::vinterface iface(cmd[3]);
00127                                                         ibrcommon::vaddress addr(f, cmd[5]);
00128 
00129                                                         if (cmd[2] == "add")
00130                                                         {
00131                                                                 lm.addressAdded(iface, addr);
00132                                                                 _stream << ClientHandler::API_STATUS_OK << " ADDRESS ADDED" << std::endl;
00133 
00134                                                         }
00135                                                         else if (cmd[2] == "del")
00136                                                         {
00137                                                                 lm.addressRemoved(iface, addr);
00138                                                                 _stream << ClientHandler::API_STATUS_OK << " ADDRESS REMOVED" << std::endl;
00139                                                         }
00140                                                         else
00141                                                         {
00142                                                                 _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN COMMAND" << std::endl;
00143                                                         }
00144                                                 } catch (const std::bad_cast&) {
00145                                                         _stream << ClientHandler::API_STATUS_NOT_ALLOWED << " FEATURE NOT AVAILABLE" << std::endl;
00146                                                 };
00147                                         }
00148                                         else
00149                                         {
00150                                                 _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN COMMAND" << std::endl;
00151                                         }
00152                                 }
00153                                 else if (cmd[0] == "connection")
00154                                 {
00155                                         // need to process the connection arguments
00156                                         // the arguments look like:
00157                                         // <eid> [tcp] [add|del] <ip> <port>
00158                                         dtn::core::Node n(cmd[1]);
00159 
00160                                         if (cmd[2] == "tcp")
00161                                         {
00162                                                 if (cmd[3] == "add")
00163                                                 {
00164                                                         std::string uri = "ip=" + cmd[4] + ";port=" + cmd[5] + ";";
00165                                                         n.add(dtn::core::Node::URI(dtn::core::Node::NODE_STATIC, dtn::core::Node::CONN_TCPIP, uri, 0, 10));
00166                                                         dtn::core::BundleCore::getInstance().addConnection(n);
00167 
00168                                                         _stream << ClientHandler::API_STATUS_OK << " CONNECTION ADDED" << std::endl;
00169                                                 }
00170                                                 else if (cmd[3] == "del")
00171                                                 {
00172                                                         std::string uri = "ip=" + cmd[4] + ";port=" + cmd[5] + ";";
00173                                                         n.add(dtn::core::Node::URI(dtn::core::Node::NODE_STATIC, dtn::core::Node::CONN_TCPIP, uri, 0, 10));
00174                                                         dtn::core::BundleCore::getInstance().removeConnection(n);
00175 
00176                                                         _stream << ClientHandler::API_STATUS_OK << " CONNECTION REMOVED" << std::endl;
00177                                                 }
00178                                         }
00179                                         else if (cmd[2] == "udp")
00180                                         {
00181                                                 if (cmd[3] == "add")
00182                                                 {
00183                                                         std::string uri = "ip=" + cmd[4] + ";port=" + cmd[5] + ";";
00184                                                         n.add(dtn::core::Node::URI(dtn::core::Node::NODE_STATIC, dtn::core::Node::CONN_UDPIP, uri, 0, 10));
00185                                                         dtn::core::BundleCore::getInstance().addConnection(n);
00186 
00187                                                         _stream << ClientHandler::API_STATUS_OK << " CONNECTION ADDED" << std::endl;
00188                                                 }
00189                                                 else if (cmd[3] == "del")
00190                                                 {
00191                                                         std::string uri = "ip=" + cmd[4] + ";port=" + cmd[5] + ";";
00192                                                         n.add(dtn::core::Node::URI(dtn::core::Node::NODE_STATIC, dtn::core::Node::CONN_UDPIP, uri, 0, 10));
00193                                                         dtn::core::BundleCore::getInstance().removeConnection(n);
00194 
00195                                                         _stream << ClientHandler::API_STATUS_OK << " CONNECTION REMOVED" << std::endl;
00196                                                 }
00197                                         }
00198                                         else if (cmd[2] == "file")
00199                                         {
00200                                                 if (cmd[3] == "add")
00201                                                 {
00202                                                         n.add(dtn::core::Node::URI(dtn::core::Node::NODE_STATIC, dtn::core::Node::CONN_FILE, cmd[4], 0, 10));
00203                                                         dtn::core::BundleCore::getInstance().addConnection(n);
00204 
00205                                                         _stream << ClientHandler::API_STATUS_OK << " CONNECTION ADDED" << std::endl;
00206                                                 }
00207                                                 else if (cmd[3] == "del")
00208                                                 {
00209                                                         n.add(dtn::core::Node::URI(dtn::core::Node::NODE_STATIC, dtn::core::Node::CONN_FILE, cmd[4], 0, 10));
00210                                                         dtn::core::BundleCore::getInstance().removeConnection(n);
00211 
00212                                                         _stream << ClientHandler::API_STATUS_OK << " CONNECTION REMOVED" << std::endl;
00213                                                 }
00214                                         }
00215                                 }
00216                                 else if (cmd[0] == "logcat")
00217                                 {
00218                                         ibrcommon::Logger::writeBuffer(_stream);
00219                                         _stream << std::endl;
00220                                 }
00221                                 else if (cmd[0] == "core")
00222                                 {
00223                                         if (cmd[1] == "shutdown")
00224                                         {
00225                                                 // send shutdown signal
00226                                                 dtn::core::GlobalEvent::raise(dtn::core::GlobalEvent::GLOBAL_SHUTDOWN);
00227                                                 _stream << ClientHandler::API_STATUS_OK << " SHUTDOWN" << std::endl;
00228                                         }
00229                                         else if (cmd[1] == "reload")
00230                                         {
00231                                                 // send reload signal
00232                                                 dtn::core::GlobalEvent::raise(dtn::core::GlobalEvent::GLOBAL_RELOAD);
00233                                                 _stream << ClientHandler::API_STATUS_OK << " RELOAD" << std::endl;
00234                                         }
00235                                         else if (cmd[1] == "powersave")
00236                                         {
00237                                                 // send powersave signal
00238                                                 dtn::core::GlobalEvent::raise(dtn::core::GlobalEvent::GLOBAL_POWERSAVE);
00239                                                 _stream << ClientHandler::API_STATUS_OK << " POWERSAVE" << std::endl;
00240                                         }
00241                                         else if (cmd[1] == "suspend")
00242                                         {
00243                                                 // send suspend signal
00244                                                 dtn::core::GlobalEvent::raise(dtn::core::GlobalEvent::GLOBAL_SUSPEND);
00245                                                 _stream << ClientHandler::API_STATUS_OK << " SUSPEND" << std::endl;
00246                                         }
00247                                         else if (cmd[1] == "wakeup")
00248                                         {
00249                                                 // send wakeup signal
00250                                                 dtn::core::GlobalEvent::raise(dtn::core::GlobalEvent::GLOBAL_WAKEUP);
00251                                                 _stream << ClientHandler::API_STATUS_OK << " WAKEUP" << std::endl;
00252                                         }
00253                                 }
00254                                 else if (cmd[0] == "bundle")
00255                                 {
00256                                         if (cmd[1] == "list")
00257                                         {
00258                                                 // get storage object
00259                                                 dtn::core::BundleCore &bcore = dtn::core::BundleCore::getInstance();
00260                                                 BundleFilter filter;
00261 
00262                                                 _stream << ClientHandler::API_STATUS_OK << " BUNDLE LIST" << std::endl;
00263                                                 std::list<dtn::data::MetaBundle> blist = bcore.getStorage().get(filter);
00264 
00265                                                 for (std::list<dtn::data::MetaBundle>::const_iterator iter = blist.begin(); iter != blist.end(); iter++)
00266                                                 {
00267                                                         const dtn::data::MetaBundle &b = *iter;
00268                                                         _stream << b.toString() << ";" << b.destination.getString() << ";" << std::endl;
00269                                                 }
00270 
00271                                                 // last line empty
00272                                                 _stream << std::endl;
00273                                         }
00274                                 }
00275                                 else if (cmd[0] == "routing")
00276                                 {
00277                                         if (cmd.size() < 3) throw ibrcommon::Exception("not enough parameters");
00278 
00279                                         if ( cmd[1] == "prophet" )
00280                                         {
00281                                                 typedef dtn::routing::BaseRouter::Extension RoutingExtension;
00282                                                 const std::list<RoutingExtension*>& routingExtensions = dtn::core::BundleCore::getInstance().getRouter().getExtensions();
00283                                                 std::list<RoutingExtension*>::const_iterator it;
00284 
00285                                                 /* find the prophet extension in the BaseRouter */
00286 
00287                                                 for(it = routingExtensions.begin(); it != routingExtensions.end(); ++it)
00288                                                 {
00289                                                         try
00290                                                         {
00291                                                                 dtn::routing::ProphetRoutingExtension& prophet_extension = dynamic_cast<dtn::routing::ProphetRoutingExtension&>(**it);
00292 
00293                                                                 if ( cmd[2] == "info" ){
00294                                                                         ibrcommon::ThreadsafeReference<dtn::routing::ProphetRoutingExtension::DeliveryPredictabilityMap> dp_map = prophet_extension.getDeliveryPredictabilityMap();
00295 
00296                                                                         _stream << ClientHandler::API_STATUS_OK << " ROUTING PROPHET INFO" << std::endl;
00297                                                                         _stream << *dp_map << std::endl;
00298                                                                 } else if ( cmd[2] == "acknowledgements" ) {
00299                                                                         ibrcommon::ThreadsafeReference<const dtn::routing::ProphetRoutingExtension::AcknowledgementSet> ack_set = prophet_extension.getAcknowledgementSet();
00300 
00301                                                                         _stream << ClientHandler::API_STATUS_OK << " ROUTING PROPHET ACKNOWLEDGEMENTS" << std::endl;
00302                                                                         _stream << *ack_set << std::endl;
00303                                                                 } else {
00304                                                                         throw ibrcommon::Exception("malformed command");
00305                                                                 }
00306 
00307                                                                 break;
00308                                                         } catch (const std::bad_cast&) { }
00309                                                 }
00310                                                 if(it == routingExtensions.end())
00311                                                 {
00312                                                         /* no prophet routing extension found */
00313                                                         _stream << ClientHandler::API_STATUS_NOT_ACCEPTABLE << " ROUTING PROPHET EXTENSION NOT FOUND" << std::endl;
00314                                                 }
00315                                         } else {
00316                                                 throw ibrcommon::Exception("malformed command");
00317                                         }
00318                                 }
00319                                 else
00320                                 {
00321                                         _stream << ClientHandler::API_STATUS_BAD_REQUEST << " UNKNOWN COMMAND" << std::endl;
00322                                 }
00323                         } catch (const std::exception&) {
00324                                 _stream << ClientHandler::API_STATUS_BAD_REQUEST << " ERROR" << std::endl;
00325                         }
00326                 }
00327         } /* namespace api */
00328 } /* namespace dtn */