Remove old launcher code, fix build error in idtool, add terminate command to control bus.

This commit is contained in:
Adam Ierymenko 2013-08-30 15:02:12 -04:00
commit 4875eb49f8
9 changed files with 37 additions and 529 deletions

View file

@ -183,20 +183,20 @@ struct _NodeImpl
{
RuntimeEnvironment renv;
std::string reasonForTerminationStr;
Node::ReasonForTermination reasonForTermination;
volatile Node::ReasonForTermination reasonForTermination;
volatile bool started;
volatile bool running;
volatile bool terminateNow;
// run() calls this on all return paths
inline Node::ReasonForTermination terminateBecause(Node::ReasonForTermination r,const char *rstr)
inline Node::ReasonForTermination terminate()
{
RuntimeEnvironment *_r = &renv;
LOG("terminating: %s",rstr);
LOG("terminating: %s",reasonForTerminationStr.c_str());
renv.shutdownInProgress = true;
Thread::sleep(500);
running = false;
#ifndef __WINDOWS__
delete renv.netconfService;
#endif
@ -209,11 +209,14 @@ struct _NodeImpl
delete renv.prng;
delete renv.log;
return reasonForTermination;
}
inline Node::ReasonForTermination terminateBecause(Node::ReasonForTermination r,const char *rstr)
{
reasonForTerminationStr = rstr;
reasonForTermination = r;
running = false;
return r;
return terminate();
}
};
@ -279,7 +282,6 @@ Node::Node(const char *hp)
impl->reasonForTermination = Node::NODE_RUNNING;
impl->started = false;
impl->running = false;
impl->terminateNow = false;
}
Node::~Node()
@ -377,6 +379,7 @@ Node::ReasonForTermination Node::run()
// One is running.
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,(std::string("another instance of ZeroTier One appears to be running, or local control UDP port cannot be bound: ") + exc.what()).c_str());
}
_r->node = this;
// TODO: make configurable
bool boundPort = false;
@ -424,7 +427,7 @@ Node::ReasonForTermination Node::run()
LOG("%s starting version %s",_r->identity.address().toString().c_str(),versionString());
while (!impl->terminateNow) {
while (impl->reasonForTermination == NODE_RUNNING) {
uint64_t now = Utils::now();
bool resynchronize = false;
@ -562,7 +565,7 @@ Node::ReasonForTermination Node::run()
return impl->terminateBecause(Node::NODE_UNRECOVERABLE_ERROR,"unexpected exception during outer main I/O loop");
}
return impl->terminateBecause(Node::NODE_NORMAL_TERMINATION,"normal termination");
return impl->terminate();
}
const char *Node::reasonForTermination() const
@ -573,10 +576,11 @@ const char *Node::reasonForTermination() const
return ((_NodeImpl *)_impl)->reasonForTerminationStr.c_str();
}
void Node::terminate()
void Node::terminate(ReasonForTermination reason,const char *reasonText)
throw()
{
((_NodeImpl *)_impl)->terminateNow = true;
((_NodeImpl *)_impl)->reasonForTermination = reason;
((_NodeImpl *)_impl)->reasonForTerminationStr = ((reasonText) ? reasonText : "");
((_NodeImpl *)_impl)->renv.mainLoopWaitCondition.signal();
}

View file

@ -86,8 +86,7 @@ public:
NODE_RUNNING = 0,
NODE_NORMAL_TERMINATION = 1,
NODE_RESTART_FOR_RECONFIGURATION = 2,
NODE_UNRECOVERABLE_ERROR = 3,
NODE_NEW_VERSION_AVAILABLE = 4
NODE_UNRECOVERABLE_ERROR = 3
};
/**
@ -124,13 +123,16 @@ public:
throw();
/**
* Cause run() to return with NODE_NORMAL_TERMINATION
* Cause run() to return
*
* This can be called from a signal handler or another thread to signal a
* running node to shut down. Shutdown may take a few seconds, so run()
* may not return instantly. Multiple calls are ignored.
*
* @param reason Reason for termination
* @param reasonText Text to be returned by reasonForTermination()
*/
void terminate()
void terminate(ReasonForTermination reason,const char *reasonText)
throw();
/**

View file

@ -55,6 +55,7 @@
#include "Peer.hpp"
#include "Salsa20.hpp"
#include "HMAC.hpp"
#include "Node.hpp"
#ifdef __WINDOWS__
#define strtoull _strtoui64
@ -170,6 +171,7 @@ std::vector<std::string> NodeConfig::execute(const char *command)
_P("200 help listnetworks");
_P("200 help join <network ID>");
_P("200 help leave <network ID>");
_P("200 help terminate [<reason>]");
} else if (cmd[0] == "listpeers") {
_P("200 listpeers <ztaddr> <ipv4> <ipv6> <latency> <version>");
_r->topology->eachPeer(_DumpPeerStatistics(r));
@ -231,6 +233,10 @@ std::vector<std::string> NodeConfig::execute(const char *command)
} else {
_P("400 leave requires a network ID (>0) in hexadecimal format");
}
} else if (cmd[0] == "terminate") {
if (cmd.size() > 1)
_r->node->terminate(Node::NODE_NORMAL_TERMINATION,cmd[1].c_str());
else _r->node->terminate(Node::NODE_NORMAL_TERMINATION,(const char *)0);
} else {
_P("404 %s No such command. Use 'help' for help.",cmd[0].c_str());
}

View file

@ -44,6 +44,7 @@ class SysEnv;
class Multicaster;
class CMWC4096;
class Service;
class Node;
/**
* Holds global state for an instance of ZeroTier::Node
@ -96,7 +97,7 @@ public:
Topology *topology;
SysEnv *sysEnv;
NodeConfig *nc;
Node *node;
#ifndef __WINDOWS__
Service *netconfService; // may be null
#endif