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 ARIBA PROJECT 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 "OverlayBootstrap.h" |
---|
40 | #include "BaseOverlay.h" |
---|
41 | |
---|
42 | namespace ariba { |
---|
43 | namespace overlay { |
---|
44 | |
---|
45 | use_logging_cpp(OverlayBootstrap); |
---|
46 | SystemEventType OverlayBootstrapMethodType("OverlayBootstrapMethodType"); |
---|
47 | |
---|
48 | OverlayBootstrap::OverlayBootstrap() |
---|
49 | : manager( BootstrapManager::instance() ), |
---|
50 | spovnetid( SpoVNetID::UNSPECIFIED ), |
---|
51 | nodeid( NodeID::UNSPECIFIED ), |
---|
52 | overlay( NULL ){ |
---|
53 | } |
---|
54 | |
---|
55 | OverlayBootstrap::~OverlayBootstrap(){ |
---|
56 | } |
---|
57 | |
---|
58 | void OverlayBootstrap::start(BaseOverlay* _overlay, const SpoVNetID& _spovnetid, const NodeID& _nodeid){ |
---|
59 | overlay = _overlay; |
---|
60 | spovnetid = _spovnetid; |
---|
61 | nodeid = _nodeid; |
---|
62 | |
---|
63 | logging_info("starting overlay bootstrap"); |
---|
64 | |
---|
65 | manager.registerCallback( this ); |
---|
66 | manager.registerModule( BootstrapManager::BootstrapTypePeriodicBroadcast ); |
---|
67 | //manager.registerModule( BootstrapManager::BootstrapTypeBluetoothSdp ); |
---|
68 | } |
---|
69 | |
---|
70 | void OverlayBootstrap::stop(){ |
---|
71 | overlay = NULL; |
---|
72 | spovnetid = SpoVNetID::UNSPECIFIED; |
---|
73 | nodeid = NodeID::UNSPECIFIED; |
---|
74 | |
---|
75 | logging_info("stopping overlay bootstrap"); |
---|
76 | |
---|
77 | manager.unregisterCallback( this ); |
---|
78 | manager.unregisterModule( BootstrapManager::BootstrapTypePeriodicBroadcast ); |
---|
79 | //manager.unregisterModule( BootstrapManager::BootstrapTypeBluetoothSdp ); |
---|
80 | } |
---|
81 | |
---|
82 | void OverlayBootstrap::handleSystemEvent(const SystemEvent& event){ |
---|
83 | EventData* data = event.getData<EventData>(); |
---|
84 | |
---|
85 | // announcement for our spovnet |
---|
86 | logging_info( "found bootstrap node for our SpoVNetID " << data->spovnetid.toString() |
---|
87 | << " on NodeID " << data->nodeid.toString() << " with endpoint " << data->endpoint.toString() ); |
---|
88 | |
---|
89 | // tell the base overlay to join using this endpoint |
---|
90 | assert( overlay != NULL ); |
---|
91 | overlay->joinSpoVNet( spovnetid, data->endpoint ); |
---|
92 | |
---|
93 | delete data; |
---|
94 | } |
---|
95 | |
---|
96 | void OverlayBootstrap::onBootstrapServiceFound(string name, string info1, string info2, string info3){ |
---|
97 | if( overlay == NULL ) return; |
---|
98 | if(name.length() <= 0 || info1.length() <= 0 || info2.length() <= 0 || info3.length() <= 0) return; |
---|
99 | |
---|
100 | // |
---|
101 | // generate the types |
---|
102 | // |
---|
103 | |
---|
104 | SpoVNetID sid( info1 ); |
---|
105 | NodeID nid( info2 ); |
---|
106 | EndpointDescriptor ep( info3 ); |
---|
107 | |
---|
108 | // |
---|
109 | // is this announcement of interest for us? |
---|
110 | // |
---|
111 | |
---|
112 | // announcement for another spovnet |
---|
113 | if( sid != this->spovnetid ) return; |
---|
114 | |
---|
115 | // announcement with our nodeid (either our announcement |
---|
116 | // or a node with the same id, any way -> ignore) |
---|
117 | if( nid == this->nodeid ) return; |
---|
118 | |
---|
119 | // |
---|
120 | // send out the bootstrap information as |
---|
121 | // event to synchronize into the system queue |
---|
122 | // |
---|
123 | |
---|
124 | EventData* data = new EventData(); |
---|
125 | data->spovnetid = sid; |
---|
126 | data->nodeid = nid; |
---|
127 | data->endpoint = ep; |
---|
128 | |
---|
129 | SystemQueue::instance().scheduleEvent( |
---|
130 | SystemEvent( this, OverlayBootstrapMethodType, data), 0 ); |
---|
131 | } |
---|
132 | |
---|
133 | void OverlayBootstrap::publish(const EndpointDescriptor& _ep){ |
---|
134 | |
---|
135 | ostringstream r; |
---|
136 | r << std::hex << rand(); |
---|
137 | |
---|
138 | randname = r.str(); |
---|
139 | manager.publish( randname, spovnetid.toString(), nodeid.toString(), _ep.toString() ); |
---|
140 | } |
---|
141 | |
---|
142 | void OverlayBootstrap::revoke(){ |
---|
143 | manager.revoke( randname ); |
---|
144 | } |
---|
145 | |
---|
146 | }} // namespace ariba, overlay |
---|