Refactor and simplify controller integration with JSON API and OneService.

This commit is contained in:
Adam Ierymenko 2015-05-17 15:51:07 -07:00
parent 7649d6746b
commit 086f21ed70
5 changed files with 50 additions and 59 deletions

21
one.cpp
View File

@ -72,12 +72,7 @@
#include "service/OneService.hpp"
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
#include "controller/SqliteNetworkController.hpp"
#endif
#define ZT1_PID_PATH "zerotier-one.pid"
#define ZT1_CONTROLLER_DB_PATH "controller.db"
using namespace ZeroTier;
@ -1129,19 +1124,6 @@ int main(int argc,char **argv)
}
#endif // __WINDOWS__
NetworkController *controller = (NetworkController *)0;
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
try {
controller = new SqliteNetworkController((homeDir + ZT_PATH_SEPARATOR_S + ZT1_CONTROLLER_DB_PATH).c_str());
} catch (std::exception &exc) {
fprintf(stderr,"%s: failure initializing SqliteNetworkController: %s"ZT_EOL_S,argv[0],exc.what());
return 1;
} catch ( ... ) {
fprintf(stderr,"%s: failure initializing SqliteNetworkController: unknown exception"ZT_EOL_S,argv[0]);
return 1;
}
#endif // ZT_ENABLE_NETWORK_CONTROLLER
#ifdef __UNIX_LIKE__
std::string pidPath(homeDir + ZT_PATH_SEPARATOR_S + ZT1_PID_PATH);
{
@ -1158,7 +1140,7 @@ int main(int argc,char **argv)
try {
for(;;) {
zt1Service = OneService::newInstance(homeDir.c_str(),port,controller,(overrideRootTopology.length() > 0) ? overrideRootTopology.c_str() : (const char *)0);
zt1Service = OneService::newInstance(homeDir.c_str(),port,(overrideRootTopology.length() > 0) ? overrideRootTopology.c_str() : (const char *)0);
switch(zt1Service->run()) {
case OneService::ONE_STILL_RUNNING: // shouldn't happen, run() won't return until done
case OneService::ONE_NORMAL_TERMINATION:
@ -1191,7 +1173,6 @@ int main(int argc,char **argv)
delete zt1Service;
zt1Service = (OneService *)0;
delete controller;
#ifdef __UNIX_LIKE__
OSUtils::rm(pidPath.c_str());

View File

@ -245,6 +245,9 @@ static void _jsonAppend(unsigned int depth,std::string &buf,const ZT1_Peer *peer
ControlPlane::ControlPlane(OneService *svc,Node *n,const char *uiStaticPath) :
_svc(svc),
_node(n),
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
_controller((SqliteNetworkController *)0),
#endif
_uiStaticPath((uiStaticPath) ? uiStaticPath : "")
{
}
@ -446,9 +449,8 @@ unsigned int ControlPlane::handleRequest(
scode = 200;
} else {
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
std::map<std::string,SqliteNetworkController *>::const_iterator ss(_subsystems.find(ps[0]));
if (ss != _subsystems.end())
scode = ss->second->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
if (_controller)
_controller->handleControlPlaneHttpGET(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
else scode = 404;
#else
scode = 404;
@ -483,9 +485,8 @@ unsigned int ControlPlane::handleRequest(
}
} else {
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
std::map<std::string,SqliteNetworkController *>::const_iterator ss(_subsystems.find(ps[0]));
if (ss != _subsystems.end())
scode = ss->second->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
if (_controller)
_controller->handleControlPlaneHttpPOST(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
else scode = 404;
#else
scode = 404;
@ -519,9 +520,8 @@ unsigned int ControlPlane::handleRequest(
} else scode = 500;
} else {
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
std::map<std::string,SqliteNetworkController *>::const_iterator ss(_subsystems.find(ps[0]));
if (ss != _subsystems.end())
scode = ss->second->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
if (_controller)
_controller->handleControlPlaneHttpDELETE(std::vector<std::string>(ps.begin()+1,ps.end()),urlArgs,headers,body,responseBody,responseContentType);
else scode = 404;
#else
scode = 404;

View File

@ -52,6 +52,19 @@ public:
ControlPlane(OneService *svc,Node *n,const char *uiStaticPath);
~ControlPlane();
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
/**
* Set controller, which will be available under /controller
*
* @param c Network controller instance
*/
inline void setController(SqliteNetworkController *c)
{
Mutex::Lock _l(_lock);
_controller = c;
}
#endif
/**
* Add an authentication token for API access
*/
@ -61,23 +74,6 @@ public:
_authTokens.insert(std::string(tok));
}
/**
* Mount a subsystem under a prefix
*
* Note that the prefix must not contain a dot -- this is reserved for
* static pages -- and must not be a reserved prefix such as /peer
* or /network. Do not include path / characters in the prefix. Example
* would be 'controller' for SqliteNetworkController.
*
* @param prefix First element in URI path
* @param subsys Object to call for results of GET and POST/PUT operations
*/
inline void mount(const char *prefix,SqliteNetworkController *subsys)
{
Mutex::Lock _l(_lock);
_subsystems[std::string(prefix)] = subsys;
}
/**
* Handle HTTP request
*
@ -102,9 +98,11 @@ public:
private:
OneService *const _svc;
Node *const _node;
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
SqliteNetworkController *_controller;
#endif
std::string _uiStaticPath;
std::set<std::string> _authTokens;
std::map<std::string,SqliteNetworkController *> _subsystems;
Mutex _lock;
};

View File

@ -53,6 +53,12 @@
#include "OneService.hpp"
#include "ControlPlane.hpp"
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
#include "../controller/SqliteNetworkController.hpp"
#else
class SqliteNetworkController;
#endif
#ifdef __WINDOWS__
#include <ShlObj.h>
#endif
@ -85,6 +91,9 @@ namespace ZeroTier { typedef BSDEthernetTap EthernetTap; }
// How often to check for new multicast subscriptions on a tap device
#define ZT_TAP_CHECK_MULTICAST_INTERVAL 30000
// Path under ZT1 home for controller database if controller is enabled
#define ZT1_CONTROLLER_DB_PATH "controller.db"
namespace ZeroTier {
class OneServiceImpl;
@ -147,10 +156,12 @@ struct TcpConnection
class OneServiceImpl : public OneService
{
public:
OneServiceImpl(const char *hp,unsigned int port,NetworkController *master,const char *overrideRootTopology) :
OneServiceImpl(const char *hp,unsigned int port,const char *overrideRootTopology) :
_homePath((hp) ? hp : "."),
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
_controller((_homePath + ZT_PATH_SEPARATOR_S + ZT1_CONTROLLER_DB_PATH).c_str()),
#endif
_phy(this,true),
_master(master),
_overrideRootTopology((overrideRootTopology) ? overrideRootTopology : ""),
_node((Node *)0),
_controlPlane((ControlPlane *)0),
@ -227,13 +238,16 @@ public:
SnodeEventCallback,
((_overrideRootTopology.length() > 0) ? _overrideRootTopology.c_str() : (const char *)0));
if (_master)
_node->setNetconfMaster((void *)_master);
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
_node->setNetconfMaster((void *)&_controller);
#endif
_controlPlane = new ControlPlane(this,_node,(_homePath + ZT_PATH_SEPARATOR_S + "ui").c_str());
_controlPlane->addAuthToken(authToken.c_str());
if (_master)
_controlPlane->mount("controller",reinterpret_cast<SqliteNetworkController *>(_master));
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
_controlPlane->setController(&_controller);
#endif
{ // Remember networks from previous session
std::vector<std::string> networksDotD(OSUtils::listDirectory((_homePath + ZT_PATH_SEPARATOR_S + "networks.d").c_str()));
@ -763,8 +777,10 @@ private:
}
const std::string _homePath;
#ifdef ZT_ENABLE_NETWORK_CONTROLLER
SqliteNetworkController _controller;
#endif
Phy<OneServiceImpl *> _phy;
NetworkController *_master;
std::string _overrideRootTopology;
Node *_node;
PhySocket *_v4UdpSocket;
@ -923,7 +939,7 @@ std::string OneService::platformDefaultHomePath()
#endif // __UNIX_LIKE__ or not...
}
OneService *OneService::newInstance(const char *hp,unsigned int port,NetworkController *master,const char *overrideRootTopology) { return new OneServiceImpl(hp,port,master,overrideRootTopology); }
OneService *OneService::newInstance(const char *hp,unsigned int port,const char *overrideRootTopology) { return new OneServiceImpl(hp,port,overrideRootTopology); }
OneService::~OneService() {}
} // namespace ZeroTier

View File

@ -32,8 +32,6 @@
namespace ZeroTier {
class NetworkController;
/**
* Local service for ZeroTier One as system VPN/NFV provider
*/
@ -79,13 +77,11 @@ public:
*
* @param hp Home path
* @param port TCP and UDP port for packets and HTTP control
* @param master Instance of network config master if this instance is to act as one (default: NULL)
* @param overrideRootTopology String-serialized root topology (for testing, default: NULL)
*/
static OneService *newInstance(
const char *hp,
unsigned int port,
NetworkController *master = (NetworkController *)0,
const char *overrideRootTopology = (const char *)0);
virtual ~OneService();