| 1 | // [License]
|
---|
| 2 | // The Ariba-Underlay Copyright
|
---|
| 3 | //
|
---|
| 4 | // Copyright (c) 2008-2009, Institute of Telematics, UniversitÀt Karlsruhe (TH)
|
---|
| 5 | //
|
---|
| 6 | // Institute of Telematics
|
---|
| 7 | // UniversitÀt Karlsruhe (TH)
|
---|
| 8 | // Zirkel 2, 76128 Karlsruhe
|
---|
| 9 | // Germany
|
---|
| 10 | //
|
---|
| 11 | // Redistribution and use in source and binary forms, with or without
|
---|
| 12 | // modification, are permitted provided that the following conditions are
|
---|
| 13 | // met:
|
---|
| 14 | //
|
---|
| 15 | // 1. Redistributions of source code must retain the above copyright
|
---|
| 16 | // notice, this list of conditions and the following disclaimer.
|
---|
| 17 | // 2. Redistributions in binary form must reproduce the above copyright
|
---|
| 18 | // notice, this list of conditions and the following disclaimer in the
|
---|
| 19 | // documentation and/or other materials provided with the distribution.
|
---|
| 20 | //
|
---|
| 21 | // THIS SOFTWARE IS PROVIDED BY THE INSTITUTE OF TELEMATICS ``AS IS'' AND
|
---|
| 22 | // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
| 23 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
---|
| 24 | // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OF TELEMATICS OR
|
---|
| 25 | // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
| 26 | // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
---|
| 27 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
---|
| 28 | // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
---|
| 29 | // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
---|
| 30 | // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
---|
| 31 | // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 32 | //
|
---|
| 33 | // The views and conclusions contained in the software and documentation
|
---|
| 34 | // are those of the authors and should not be interpreted as representing
|
---|
| 35 | // official policies, either expressed or implied, of the Institute of
|
---|
| 36 | // Telematics.
|
---|
| 37 | // [License]
|
---|
| 38 |
|
---|
| 39 | #include "Node.h"
|
---|
| 40 |
|
---|
| 41 | #include "ariba/overlay/BaseOverlay.h"
|
---|
| 42 | #include "ariba/utility/types/OverlayParameterSet.h"
|
---|
| 43 | #include "ariba/interface/ServiceInterface.h"
|
---|
| 44 | #include "ariba/communication/EndpointDescriptor.h"
|
---|
| 45 |
|
---|
| 46 | using ariba::communication::EndpointDescriptor;
|
---|
| 47 |
|
---|
| 48 | namespace ariba {
|
---|
| 49 |
|
---|
| 50 | class ServiceInterfaceWrapper: public interface::ServiceInterface {
|
---|
| 51 | private:
|
---|
| 52 | NodeListener* nodeListener;
|
---|
| 53 | CommunicationListener* commListener;
|
---|
| 54 | public:
|
---|
| 55 | ServiceInterfaceWrapper(NodeListener* listener) :
|
---|
| 56 | nodeListener(listener), commListener(NULL) {
|
---|
| 57 |
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | ServiceInterfaceWrapper(CommunicationListener* listener) :
|
---|
| 61 | nodeListener(NULL), commListener(listener) {
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | ~ServiceInterfaceWrapper() {
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | protected:
|
---|
| 68 |
|
---|
| 69 | bool isJoinAllowed(const NodeID& nodeid, const SpoVNetID& spovnetid) {
|
---|
| 70 | return true;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | void onNodeJoin(const NodeID& nodeid, const SpoVNetID& spovnetid) {
|
---|
| 74 | // not handled
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | void onNodeLeave(const NodeID& id, const SpoVNetID& spovnetid) {
|
---|
| 78 | // not handled
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void onJoinSuccess(const SpoVNetID& spovnetid) {
|
---|
| 82 | if (nodeListener != NULL) nodeListener->onJoinCompleted(spovnetid);
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void onJoinFail(const SpoVNetID& spovnetid) {
|
---|
| 86 | if (nodeListener != NULL) nodeListener->onJoinFailed(spovnetid);
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | void onLeaveSuccess( const SpoVNetID& spovnetid ){
|
---|
| 90 | if (nodeListener != NULL) nodeListener->onLeaveCompleted(spovnetid);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | void onLeaveFail( const SpoVNetID& spovnetid ){
|
---|
| 94 | if (nodeListener != NULL) nodeListener->onLeaveFailed(spovnetid);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | void onLinkUp(const LinkID& link, const NodeID& local, const NodeID& remote) {
|
---|
| 98 | if (commListener != NULL) commListener->onLinkUp(link, remote);
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | void onLinkDown(const LinkID& link, const NodeID& local,
|
---|
| 102 | const NodeID& remote) {
|
---|
| 103 | if (commListener != NULL) commListener->onLinkDown(link, remote);
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | void onLinkChanged(const LinkID& link, const NodeID& local,
|
---|
| 107 | const NodeID& remote) {
|
---|
| 108 | if (commListener != NULL) commListener->onLinkChanged(link, remote);
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void onLinkFail(const LinkID& id, const NodeID& local, const NodeID& remote) {
|
---|
| 112 | if (commListener != NULL) commListener->onLinkFail(id, remote);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | void onLinkQoSChanged(const LinkID& id, const NodeID& local,
|
---|
| 116 | const NodeID& remote, const QoSParameterSet& qos) {
|
---|
| 117 | if (commListener != NULL) commListener->onLinkQoSChanged(id, remote,
|
---|
| 118 | LinkProperties::DEFAULT);
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | bool receiveMessage(const Message* message, const LinkID& link,
|
---|
| 122 | const NodeID& node) {
|
---|
| 123 | if (commListener != NULL) commListener->onMessage(
|
---|
| 124 | const_cast<Message*>(message), node, link);
|
---|
| 125 | }
|
---|
| 126 | };
|
---|
| 127 |
|
---|
| 128 | ServiceID Node::anonymousService = ServiceID(0xFF00);
|
---|
| 129 |
|
---|
| 130 | Node::Node(AribaModule& ariba_mod, const Name& node_name) :
|
---|
| 131 | ariba_mod(ariba_mod), name(node_name) {
|
---|
| 132 | base_overlay = new BaseOverlay();
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | Node::~Node() {
|
---|
| 136 | delete base_overlay;
|
---|
| 137 | base_overlay = NULL;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | //TODO: Implement error handling: no bootstrap node available
|
---|
| 141 | void Node::join(const Name& vnetname) {
|
---|
| 142 | spovnetId = vnetname.toSpoVNetId();
|
---|
| 143 | nodeId = generateNodeId(name);
|
---|
| 144 |
|
---|
| 145 | ariba_mod.base_comm->start(ariba_mod.ip_addr, ariba_mod.tcp_port);
|
---|
| 146 | base_overlay->start( *ariba_mod.base_comm, nodeId );
|
---|
| 147 |
|
---|
| 148 | const communication::EndpointDescriptor* ep =
|
---|
| 149 | ariba_mod.getBootstrapNode(vnetname);
|
---|
| 150 | if( ep == NULL ) return;
|
---|
| 151 |
|
---|
| 152 | base_overlay->joinSpoVNet( spovnetId, *ep);
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | void Node::initiate(const Name& vnetname, const SpoVNetProperties& parm) {
|
---|
| 156 | utility::OverlayParameterSet ovrpset =
|
---|
| 157 | (utility::OverlayParameterSet::_OverlayStructure)
|
---|
| 158 | parm.getBaseOverlayType();
|
---|
| 159 |
|
---|
| 160 | spovnetId = vnetname.toSpoVNetId();
|
---|
| 161 | nodeId = generateNodeId(name);
|
---|
| 162 |
|
---|
| 163 | ariba_mod.base_comm->start(ariba_mod.ip_addr, ariba_mod.tcp_port);
|
---|
| 164 |
|
---|
| 165 | base_overlay->start( *ariba_mod.base_comm, nodeId );
|
---|
| 166 | base_overlay->createSpoVNet( spovnetId );
|
---|
| 167 |
|
---|
| 168 | ariba_mod.addBootstrapNode(vnetname,
|
---|
| 169 | new EndpointDescriptor(ariba_mod.base_comm->getEndpointDescriptor()));
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void Node::leave() {
|
---|
| 173 | base_overlay->leaveSpoVNet();
|
---|
| 174 | ariba_mod.base_comm->stop();
|
---|
| 175 |
|
---|
| 176 | base_overlay->stop();
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | const SpoVNetProperties& Node::getSpoVNetProperties() const {
|
---|
| 180 | return SpoVNetProperties::DEFAULT;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | const SpoVNetID& Node::getSpoVNetId() const {
|
---|
| 184 | return spovnetId;
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | const NodeID& Node::getNodeId(const LinkID& lid) const {
|
---|
| 188 | if( lid == LinkID::UNSPECIFIED ) return nodeId;
|
---|
| 189 | else return base_overlay->getNodeID( lid );
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | NodeID Node::generateNodeId(const Name& name) const {
|
---|
| 193 | if (name == Name::UNSPECIFIED) return Name::random().toNodeId();
|
---|
| 194 | else return name.toNodeId();
|
---|
| 195 | }
|
---|
| 196 |
|
---|
| 197 | vector<NodeID> Node::getNeighborNodes() const {
|
---|
| 198 | return base_overlay->getOverlayNeighbors();
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 | LinkID Node::establishLink(const NodeID& nid, const ServiceID& sid,
|
---|
| 202 | const LinkProperties& req, const DataMessage& msg) {
|
---|
| 203 | return base_overlay->establishLink(nid, sid);
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | void Node::dropLink(const LinkID& lnk) {
|
---|
| 207 | base_overlay->dropLink(lnk);
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | seqnum_t Node::sendMessage(const DataMessage& msg, const NodeID& nid,
|
---|
| 211 | const ServiceID& sid, const LinkProperties& req) {
|
---|
| 212 | return base_overlay->sendMessage((Message*) msg, nid, sid);
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | seqnum_t Node::sendMessage(const DataMessage& msg, const LinkID& lnk) {
|
---|
| 216 | return base_overlay->sendMessage((Message*) msg, lnk);
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void Node::sendBroadcastMessage(const DataMessage& msg, const ServiceID& sid) {
|
---|
| 220 | return base_overlay->broadcastMessage((Message*)msg, sid);
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | void Node::bind(NodeListener* listener) {
|
---|
| 224 | base_overlay->bind(new ServiceInterfaceWrapper(listener),
|
---|
| 225 | Node::anonymousService);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | void Node::unbind(NodeListener* listener) {
|
---|
| 229 | delete base_overlay->unbind(Node::anonymousService);
|
---|
| 230 | }
|
---|
| 231 |
|
---|
| 232 | void Node::bind(CommunicationListener* listener, const ServiceID& sid) {
|
---|
| 233 | base_overlay->bind(new ServiceInterfaceWrapper(listener), sid);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | void Node::unbind(CommunicationListener* listener, const ServiceID& sid) {
|
---|
| 237 | delete base_overlay->unbind(sid);
|
---|
| 238 | }
|
---|
| 239 |
|
---|
| 240 | // service directory
|
---|
| 241 | /*
|
---|
| 242 | void Node::put(const Identifier<>& key, Message* value) {
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | void Node::get(const Identifier<>& key) {
|
---|
| 246 |
|
---|
| 247 | }
|
---|
| 248 | */
|
---|
| 249 |
|
---|
| 250 | // @see Module.h
|
---|
| 251 | void Node::initialize() {
|
---|
| 252 |
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | // @see Module.h
|
---|
| 256 | void Node::start() {
|
---|
| 257 |
|
---|
| 258 | }
|
---|
| 259 |
|
---|
| 260 | // @see Module.h
|
---|
| 261 | void Node::stop() {
|
---|
| 262 |
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | // @see Module.h
|
---|
| 266 | string Node::getName() const {
|
---|
| 267 |
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | // @see Module.h
|
---|
| 271 | void Node::setProperty(string key, string value) {
|
---|
| 272 |
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | // @see Module.h
|
---|
| 276 | const string Node::getProperty(string key) const {
|
---|
| 277 |
|
---|
| 278 | }
|
---|
| 279 |
|
---|
| 280 | // @see Module.h
|
---|
| 281 | const vector<string> Node::getProperties() const {
|
---|
| 282 |
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | } // namespace ariba
|
---|