EMANE  1.2.1
transportadapterimpl.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2017 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2011-2012 - DRS CenGen, LLC, Columbia, Maryland
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of DRS CenGen, LLC nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include "transportadapterimpl.h"
35 #include "logservice.h"
37 
39 #include "emane/startexception.h"
40 
42  TransportAdapter{nemId},
44  id_{nemId}{}
45 
47 
48 void EMANE::Application::TransportAdapterImpl::setTransport(std::unique_ptr<NEMLayer> & pTransport)
49 {
50  pTransport_ = std::move(pTransport);
51  setUpstreamTransport(pTransport_.get());
52  pTransport_->setDownstreamTransport(this);
53 }
54 
56 {
57  auto & configRegistrar = registrar.configurationRegistrar();
58 
59  configRegistrar.registerNonNumeric<INETAddr>("platformendpoint",
61  {},
62  "IPv4 or IPv6 NEM Platform Service endpoint.");
63 
64  configRegistrar.registerNonNumeric<INETAddr>("transportendpoint",
66  {},
67  "IPv4 or IPv6 Transport endpoint.");
68 
69  configRegistrar.registerNonNumeric<std::string>("protocol",
71  {"udp"},
72  "Defines the protocl used for communictation:"
73  " udp or tcp.",
74  1,
75  1,
76  "^(udp|tcp)$");
77 }
78 
80 {
81  for(const auto & item : update)
82  {
83  if(item.first == "platformendpoint")
84  {
85  platformEndpointAddr_ = item.second[0].asINETAddr();
86 
88  INFO_LEVEL,
89  "TRANS %03hu TransportAdapterImpl::configure %s: %s",
90  id_,
91  item.first.c_str(),
92  platformEndpointAddr_.str().c_str());
93 
94  }
95  else if(item.first == "transportendpoint")
96  {
97  transportEndpointAddr_ = item.second[0].asINETAddr();
98 
100  INFO_LEVEL,
101  "TRANS %03hu TransportAdapterImpl::configure %s: %s",
102  id_,
103  item.first.c_str(),
104  transportEndpointAddr_.str().c_str());
105  }
106  else if(item.first == "protocol")
107  {
108  std::string sProtocol{item.second[0].asString()};
109 
110  protocol_ = sProtocol == "udp" ?
113 
115  INFO_LEVEL,
116  "NEM %03hu TransportAdapterImpl::configure %s: %s",
117  id_,
118  item.first.c_str(),
119  sProtocol.c_str());
120  }
121  else
122  {
123  throw makeException<ConfigureException>("TransportAdapterImpl: "
124  "Unexpected configuration item %s",
125  item.first.c_str());
126  }
127  }
128 }
129 
131 {
132  if(!pTransport_)
133  {
134  throw makeException<StartException>("TransportAdapterImpl transport not set for NEM %hu",
135  id_);
136  }
137 
138  try
139  {
140  open(transportEndpointAddr_, platformEndpointAddr_,protocol_);
141  }
143  {
144  throw StartException(exp.what());
145  }
146 
147  pTransport_->start();
148 }
149 
151 {
152  pTransport_->postStart();
153 }
154 
156 {
157  pTransport_->stop();
158 }
159 
161  throw()
162 {
163  pTransport_->destroy();
164 }
165 
167  const EMANE::ControlMessages & msgs)
168 {
170  pkt.getVectorIO(),
171  pkt.length(),
172  msgs);
173 }
174 
175 void
177 {
178  sendControlMessage(msgs);
179 }
180 
181 void EMANE::Application::TransportAdapterImpl::doProcessPacketMessage(const PacketInfo & packetInfo,
182  const void * pPacketData,
183  size_t packetLength,
184  const ControlMessages & msgs)
185 {
186  UpstreamPacket pkt(packetInfo,
187  pPacketData,
188  packetLength);
189 
190  pTransport_->processUpstreamPacket(pkt,msgs);
191 }
192 
193 void EMANE::Application::TransportAdapterImpl::doProcessControlMessage(const ControlMessages & msgs)
194 {
195  pTransport_->processUpstreamControl(msgs);
196 }
A Packet class that allows upstream processing to strip layer headers as the packet travels up the st...
const PacketInfo & getPacketInfo() const
The Registrar interface provides access to all of the emulator registrars.
Definition: registrar.h:50
virtual ConfigurationRegistrar & configurationRegistrar()=0
std::list< const ControlMessage * > ControlMessages
void sendPacketMessage(const PacketInfo &packetInfo, const void *pPacketData, size_t packetLength, const ControlMessages &msgs)
void registerNonNumeric(const std::string &sName, const ConfigurationProperties &properties=ConfigurationProperties::NONE, const std::initializer_list< T > &values={}, const std::string &sUsage="", std::size_t minOccurs=1, std::size_t maxOccurs=1, const std::string &sRegexPattern={})
void open(const INETAddr &localAddress, const INETAddr &remoteAddress, Protocol protocol)
void initialize(Registrar &registrar) override
void sendControlMessage(const ControlMessages &msgs)
Store source, destination, creation time and priority information for a packet.
Definition: packetinfo.h:50
void processDownstreamPacket(DownstreamPacket &pkt, const ControlMessages &msgs)
const char * what() const
Definition: exception.h:62
Specialized packet the allows downstream processing to add layer specific headers as the packet trave...
std::string str(bool bWithPort=true) const
Definition: inetaddr.cc:409
void configure(const ConfigurationUpdate &update) override
void processDownstreamControl(const ControlMessages &msgs)
void setTransport(std::unique_ptr< NEMLayer > &pTransport) override
std::uint16_t NEMId
Definition: types.h:52
virtual void setUpstreamTransport(UpstreamTransport *pUpstreamTransport)
Transport Adapter interface. A Transport Adapter combines with a Transport to connect with its respec...
Exception thrown by the during BoundaryMessageManagerException to indicate connection failure...
Component start exception is used to indicate an exception during transition to the start state...
std::vector< ConfigurationNameAnyValues > ConfigurationUpdate
Utils::VectorIO getVectorIO() const
#define LOGGER_STANDARD_LOGGING(logger, level, fmt, args...)
static LogService * instance()
Definition: singleton.h:56