EMANE  1.2.1
nemimpl.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015,2017 - Adjacent Link LLC, Bridgewater,
3  * New Jersey
4  * Copyright (c) 2011 - DRS CenGen, LLC, Columbia, Maryland
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  * * Neither the name of DRS CenGen, LLC nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include "nemimpl.h"
36 #include "logservice.h"
38 #include "emane/startexception.h"
39 
40 #include <sstream>
41 #include <iomanip>
42 
44  std::unique_ptr<NEMLayerStack> & pNEMLayerStack,
45  bool bExternalTransport):
46  pNEMLayerStack_(std::move(pNEMLayerStack)),
47  id_{id},
48  bExternalTransport_{bExternalTransport},
49  NEMOTAAdapter_{id},
50  NEMNetworkAdapter_{id}
51 {
52  pNEMLayerStack_->connectLayers(&NEMNetworkAdapter_,&NEMOTAAdapter_);
53 }
54 
56 
58 {
59  if(bExternalTransport_)
60  {
61  auto & configRegistrar = registrar.configurationRegistrar();
62 
63  configRegistrar.registerNonNumeric<INETAddr>("platformendpoint",
65  {},
66  "IPv4 or IPv6 NEM Platform Service endpoint.");
67 
68  configRegistrar.registerNonNumeric<INETAddr>("transportendpoint",
70  {},
71  "IPv4 or IPv6 Transport endpoint.");
72 
73  configRegistrar.registerNonNumeric<std::string>("protocol",
75  {"udp"},
76  "Defines the protocl used for communictation:"
77  " udp or tcp.",
78  1,
79  1,
80  "^(udp|tcp)$");
81  }
82 
83  pNEMLayerStack_->initialize(registrar);
84 }
85 
87 {
88  for(const auto & item : update)
89  {
90  if(item.first == "platformendpoint")
91  {
92  platformEndpointAddr_ = item.second[0].asINETAddr();
93 
95  INFO_LEVEL,
96  "NEM %03hu NEMImpl::configure platformendpoint: %s",
97  id_,
98  platformEndpointAddr_.str().c_str());
99 
100  }
101  else if(item.first == "transportendpoint")
102  {
103  transportEndpointAddr_ = item.second[0].asINETAddr();
104 
106  INFO_LEVEL,
107  "NEM %03hu NEMImpl::configure transportendpoint: %s",
108  id_,
109  transportEndpointAddr_.str().c_str());
110  }
111  else if(item.first == "protocol")
112  {
113  std::string sProtocol{item.second[0].asString()};
114 
115  protocol_ = sProtocol == "udp" ?
118 
120  INFO_LEVEL,
121  "NEM %03hu NEMImpl::configure %s: %s",
122  id_,
123  item.first.c_str(),
124  sProtocol.c_str());
125  }
126  else
127  {
128  throw makeException<ConfigureException>("NEMImpl::configure: "
129  "Unexpected configuration item %s",
130  item.first.c_str());
131  }
132  }
133 }
134 
136 {
137  NEMOTAAdapter_.open();
138 
139  if(bExternalTransport_)
140  {
141  try
142  {
143  NEMNetworkAdapter_.open(platformEndpointAddr_,
144  transportEndpointAddr_,
145  protocol_);
146  }
147  catch(NetworkAdapterException & exp)
148  {
149  throw StartException(exp.what());
150  }
151  }
152 
153  pNEMLayerStack_->start();
154 }
155 
157 {
158  pNEMLayerStack_->postStart();
159 }
160 
162 {
163  if(bExternalTransport_)
164  {
165  NEMNetworkAdapter_.close();
166  }
167 
168  NEMOTAAdapter_.close();
169 
170  pNEMLayerStack_->stop();
171 }
172 
174  throw()
175 {
176  pNEMLayerStack_->destroy();
177 }
178 
180 {
181  return id_;
182 }
void postStart() override
Definition: nemimpl.cc:156
The Registrar interface provides access to all of the emulator registrars.
Definition: registrar.h:50
NEMId getNEMId() const override
Definition: nemimpl.cc:179
virtual ConfigurationRegistrar & configurationRegistrar()=0
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={})
NEMImpl(NEMId id, std::unique_ptr< NEMLayerStack > &pNEMLayerStack, bool bExternalTransport)
Definition: nemimpl.cc:43
void open(const INETAddr &localAddress, const INETAddr &remoteAddress, Protocol protocol)
Exception thrown during open/establishment of network adapter connection.
const char * what() const
Definition: exception.h:62
std::string str(bool bWithPort=true) const
Definition: inetaddr.cc:409
std::uint16_t NEMId
Definition: types.h:52
void initialize(Registrar &registrar) override
Definition: nemimpl.cc:57
void destroy() override
Definition: nemimpl.cc:173
void start() override
Definition: nemimpl.cc:135
void stop() override
Definition: nemimpl.cc:161
Component start exception is used to indicate an exception during transition to the start state...
std::vector< ConfigurationNameAnyValues > ConfigurationUpdate
void configure(const ConfigurationUpdate &update) override
Definition: nemimpl.cc:86
#define LOGGER_STANDARD_LOGGING(logger, level, fmt, args...)
static LogService * instance()
Definition: singleton.h:56