| 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), base_overlay(NULL) {
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | Node::~Node() {
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | //TODO: Implement error handling: no bootstrap node available
|
---|
| 138 | void Node::join(const Name& vnetname) {
|
---|
| 139 | spovnetId = vnetname.toSpoVNetId();
|
---|
| 140 | nodeId = generateNodeId(name);
|
---|
| 141 |
|
---|
| 142 | //logging("joining spovnet on"
|
---|
| 143 | // << " [spovnetid=]" << spovnetId.toString()
|
---|
| 144 | // << " [nodeid=]" << nodeId.toString() );
|
---|
| 145 |
|
---|
| 146 | //logging_info("starting base communication...");
|
---|
| 147 | ariba_mod.base_comm->start(ariba_mod.ip_addr, ariba_mod.tcp_port);
|
---|
| 148 |
|
---|
| 149 | //logging_info("starting base overlay...");
|
---|
| 150 | base_overlay = new BaseOverlay( *ariba_mod.base_comm, nodeId );
|
---|
| 151 |
|
---|
| 152 | const communication::EndpointDescriptor* ep =
|
---|
| 153 | ariba_mod.getBootstrapNode(vnetname);
|
---|
| 154 | if( ep == NULL ) return;
|
---|
| 155 |
|
---|
| 156 | base_overlay->joinSpoVNet( spovnetId, *ep);
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | void Node::initiate(const Name& vnetname, const SpoVNetProperties& parm) {
|
---|
| 160 | utility::OverlayParameterSet ovrpset =
|
---|
| 161 | (utility::OverlayParameterSet::_OverlayStructure)
|
---|
| 162 | parm.getBaseOverlayType();
|
---|
| 163 |
|
---|
| 164 | spovnetId = vnetname.toSpoVNetId();
|
---|
| 165 | nodeId = generateNodeId(name);
|
---|
| 166 |
|
---|
| 167 | //logging("joining spovnet on"
|
---|
| 168 | // << " [spovnetid=]" << spovnetId.toString()
|
---|
| 169 | // << " [nodeid=]" << nodeId.toString() );
|
---|
| 170 |
|
---|
| 171 | //logging_info("starting base communication...");
|
---|
| 172 | ariba_mod.base_comm->start(ariba_mod.ip_addr, ariba_mod.tcp_port);
|
---|
| 173 |
|
---|
| 174 | //logging_info("starting base overlay...");
|
---|
| 175 | base_overlay = new BaseOverlay( *ariba_mod.base_comm, nodeId );
|
---|
| 176 | base_overlay->createSpoVNet( spovnetId );
|
---|
| 177 |
|
---|
| 178 | ariba_mod.addBootstrapNode(vnetname,
|
---|
| 179 | new EndpointDescriptor(ariba_mod.base_comm->getEndpointDescriptor()));
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | void Node::leave() {
|
---|
| 183 | base_overlay->leaveSpoVNet();
|
---|
| 184 | ariba_mod.base_comm->stop();
|
---|
| 185 |
|
---|
| 186 | delete base_overlay;
|
---|
| 187 | base_overlay = NULL;
|
---|
| 188 | }
|
---|
| 189 |
|
---|
| 190 | const SpoVNetProperties& Node::getSpoVNetProperties() const {
|
---|
| 191 | return SpoVNetProperties::DEFAULT;
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | const SpoVNetID& Node::getSpoVNetId() const {
|
---|
| 195 | return spovnetId;
|
---|
| 196 | }
|
---|
| 197 |
|
---|
| 198 | const NodeID& Node::getNodeId(const LinkID& lid) const {
|
---|
| 199 | if( lid == LinkID::UNSPECIFIED ) return nodeId;
|
---|
| 200 | else return base_overlay->getNodeID( lid );
|
---|
| 201 | }
|
---|
| 202 |
|
---|
| 203 | NodeID Node::generateNodeId(const Name& name) const {
|
---|
| 204 | if (name == Name::UNSPECIFIED) return Name::random().toNodeId();
|
---|
| 205 | else return name.toNodeId();
|
---|
| 206 | }
|
---|
| 207 |
|
---|
| 208 | vector<NodeID> Node::getNeighborNodes() const {
|
---|
| 209 | return base_overlay->getOverlayNeighbors();
|
---|
| 210 | }
|
---|
| 211 |
|
---|
| 212 | LinkID Node::establishLink(const NodeID& nid, const ServiceID& sid,
|
---|
| 213 | const LinkProperties& req, const DataMessage& msg) {
|
---|
| 214 | return base_overlay->establishLink(nid, sid);
|
---|
| 215 | }
|
---|
| 216 |
|
---|
| 217 | void Node::dropLink(const LinkID& lnk) {
|
---|
| 218 | base_overlay->dropLink(lnk);
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | seqnum_t Node::sendMessage(const DataMessage& msg, const NodeID& nid,
|
---|
| 222 | const ServiceID& sid, const LinkProperties& req) {
|
---|
| 223 | return base_overlay->sendMessage((Message*) msg, nid, sid);
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | seqnum_t Node::sendMessage(const DataMessage& msg, const LinkID& lnk) {
|
---|
| 227 | return base_overlay->sendMessage((Message*) msg, lnk);
|
---|
| 228 | }
|
---|
| 229 |
|
---|
| 230 | void Node::sendBroadcastMessage(const DataMessage& msg, const ServiceID& sid) {
|
---|
| 231 | return base_overlay->broadcastMessage((Message*)msg, sid);
|
---|
| 232 | }
|
---|
| 233 |
|
---|
| 234 | void Node::bind(NodeListener* listener) {
|
---|
| 235 | base_overlay->bind(new ServiceInterfaceWrapper(listener),
|
---|
| 236 | Node::anonymousService);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | void Node::unbind(NodeListener* listener) {
|
---|
| 240 | delete base_overlay->unbind(Node::anonymousService);
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | void Node::bind(CommunicationListener* listener, const ServiceID& sid) {
|
---|
| 244 | base_overlay->bind(new ServiceInterfaceWrapper(listener), sid);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | void Node::unbind(CommunicationListener* listener, const ServiceID& sid) {
|
---|
| 248 | delete base_overlay->unbind(sid);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | // service directory
|
---|
| 252 | /*
|
---|
| 253 | void Node::put(const Identifier<>& key, Message* value) {
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | void Node::get(const Identifier<>& key) {
|
---|
| 257 |
|
---|
| 258 | }
|
---|
| 259 | */
|
---|
| 260 |
|
---|
| 261 | // @see Module.h
|
---|
| 262 | void Node::initialize() {
|
---|
| 263 |
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | // @see Module.h
|
---|
| 267 | void Node::start() {
|
---|
| 268 |
|
---|
| 269 | }
|
---|
| 270 |
|
---|
| 271 | // @see Module.h
|
---|
| 272 | void Node::stop() {
|
---|
| 273 |
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | // @see Module.h
|
---|
| 277 | string Node::getName() const {
|
---|
| 278 |
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | // @see Module.h
|
---|
| 282 | void Node::setProperty(string key, string value) {
|
---|
| 283 |
|
---|
| 284 | }
|
---|
| 285 |
|
---|
| 286 | // @see Module.h
|
---|
| 287 | const string Node::getProperty(string key) const {
|
---|
| 288 |
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | // @see Module.h
|
---|
| 292 | const vector<string> Node::getProperties() const {
|
---|
| 293 |
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | } // namespace ariba
|
---|