diff options
author | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-07-18 16:35:52 -0400 |
---|---|---|
committer | Adam Ierymenko <adam.ierymenko@gmail.com> | 2013-07-18 16:35:52 -0400 |
commit | 5f4eb1ebc60abdd762bc77ef5b1120fe528ccc8f (patch) | |
tree | 6c42c048c0671c1e697bf677066de3b4a53ad945 /node | |
parent | c345c699fd90cbad1ceda6226475d9a27ae47072 (diff) | |
download | infinitytier-5f4eb1ebc60abdd762bc77ef5b1120fe528ccc8f.tar.gz infinitytier-5f4eb1ebc60abdd762bc77ef5b1120fe528ccc8f.zip |
Command line interface.
Diffstat (limited to 'node')
-rw-r--r-- | node/Node.cpp | 38 | ||||
-rw-r--r-- | node/Node.hpp | 11 | ||||
-rw-r--r-- | node/NodeConfig.cpp | 14 |
3 files changed, 55 insertions, 8 deletions
diff --git a/node/Node.cpp b/node/Node.cpp index 57bb8abc..f8de7908 100644 --- a/node/Node.cpp +++ b/node/Node.cpp @@ -79,13 +79,27 @@ struct _LocalClientImpl UdpSocket *sock; void (*resultHandler)(void *,unsigned long,const char *); void *arg; + InetAddress localDestAddr; Mutex inUseLock; }; static void _CBlocalClientHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len) { _LocalClientImpl *impl = (_LocalClientImpl *)arg; + if (!impl) + return; + if (!impl->resultHandler) + return; // sanity check Mutex::Lock _l(impl->inUseLock); + + try { + unsigned long convId = 0; + std::vector<std::string> results; + if (!NodeConfig::decodeControlMessagePacket(impl->key,data,len,convId,results)) + return; + for(std::vector<std::string>::iterator r(results.begin());r!=results.end();++r) + impl->resultHandler(impl->arg,convId,r->c_str()); + } catch ( ... ) {} } Node::LocalClient::LocalClient(const char *authToken,void (*resultHandler)(void *,unsigned long,const char *),void *arg) @@ -114,6 +128,8 @@ Node::LocalClient::LocalClient(const char *authToken,void (*resultHandler)(void impl->sock = sock; impl->resultHandler = resultHandler; impl->arg = arg; + impl->localDestAddr = InetAddress::LO4; + impl->localDestAddr.setPort(ZT_CONTROL_UDP_PORT); _impl = impl; } else delete impl; } @@ -131,9 +147,27 @@ Node::LocalClient::~LocalClient() unsigned long Node::LocalClient::send(const char *command) throw() { - uint32_t convId = (uint32_t)rand(); + if (!_impl) + return 0; + _LocalClientImpl *impl = (_LocalClientImpl *)_impl; + Mutex::Lock _l(impl->inUseLock); + + try { + uint32_t convId = (uint32_t)rand(); + if (!convId) + convId = 1; - return convId; + std::vector<std::string> tmp; + tmp.push_back(std::string(command)); + std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > packets(NodeConfig::encodeControlMessage(impl->key,convId,tmp)); + + for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator p(packets.begin());p!=packets.end();++p) + impl->sock->send(impl->localDestAddr,p->data(),p->size(),-1); + + return convId; + } catch ( ... ) { + return 0; + } } struct _NodeImpl diff --git a/node/Node.hpp b/node/Node.hpp index bddced58..b716b556 100644 --- a/node/Node.hpp +++ b/node/Node.hpp @@ -49,11 +49,6 @@ public: /** * Create a new node config client * - * The result handler will be called from a different thread. Its - * arguments are the request ID generated by send() and each line - * of output. It may be called more than once per request result - * if the command generates more than one line of output. - * * @param authToken Authentication token * @param resultHandler Function to call when commands provide results */ @@ -65,8 +60,12 @@ public: /** * Send a command to the local node * + * Note that the returned conversation ID will never be 0. A return value + * of 0 indicates a fatal error such as failure to bind to any local UDP + * port. + * * @param command - * @return Request ID that will be provided to result handler when/if results are sent back + * @return Conversation ID that will be provided to result handler when/if results are sent back */ unsigned long send(const char *command) throw(); diff --git a/node/NodeConfig.cpp b/node/NodeConfig.cpp index 381bbd62..21ed5188 100644 --- a/node/NodeConfig.cpp +++ b/node/NodeConfig.cpp @@ -218,6 +218,20 @@ bool NodeConfig::decodeControlMessagePacket(const void *key,const void *data,uns void NodeConfig::_CBcontrolPacketHandler(UdpSocket *sock,void *arg,const InetAddress &remoteAddr,const void *data,unsigned int len) { + NodeConfig *nc = (NodeConfig *)arg; + try { + unsigned long convId = 0; + std::vector<std::string> commands; + + if (!decodeControlMessagePacket(nc->_controlSocketKey,data,len,convId,commands)) + return; + + for(std::vector<std::string>::iterator c(commands.begin());c!=commands.end();++c) { + std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> > resultPackets(encodeControlMessage(nc->_controlSocketKey,convId,nc->execute(c->c_str()))); + for(std::vector< Buffer<ZT_NODECONFIG_MAX_PACKET_SIZE> >::iterator p(resultPackets.begin());p!=resultPackets.end();++p) + sock->send(remoteAddr,p->data(),p->size(),-1); + } + } catch ( ... ) {} } } // namespace ZeroTier |