EMANE  1.0.1
nemimpl.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-2015 - Adjacent Link LLC, Bridgewater, New Jersey
3  * Copyright (c) 2011 - 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 "nemimpl.h"
35 #include "logservice.h"
37 #include "emane/startexception.h"
38 
39 #include <sstream>
40 #include <iomanip>
41 
43  std::unique_ptr<NEMLayerStack> & pNEMLayerStack,
44  bool bExternalTransport):
45  pNEMLayerStack_(std::move(pNEMLayerStack)),
46  id_{id},
47  bExternalTransport_{bExternalTransport},
48  NEMOTAAdapter_{id},
49  NEMNetworkAdapter_{id}
50 {
51  pNEMLayerStack_->connectLayers(&NEMNetworkAdapter_,&NEMOTAAdapter_);
52 }
53 
55 
57 {
58  if(bExternalTransport_)
59  {
60  auto & configRegistrar = registrar.configurationRegistrar();
61 
62  configRegistrar.registerNonNumeric<INETAddr>("platformendpoint",
64  {},
65  "IPv4 or IPv6 NEM Platform Service endpoint.");
66 
67  configRegistrar.registerNonNumeric<INETAddr>("transportendpoint",
69  {},
70  "IPv4 or IPv6 Transport endpoint.");
71  }
72 
73  pNEMLayerStack_->initialize(registrar);
74 }
75 
77 {
78  for(const auto & item : update)
79  {
80  if(item.first == "platformendpoint")
81  {
82  platformEndpointAddr_ = item.second[0].asINETAddr();
83 
85  INFO_LEVEL,
86  "NEM %03hu NEMImpl::configure platformendpoint: %s",
87  id_,
88  platformEndpointAddr_.str().c_str());
89 
90  }
91  else if(item.first == "transportendpoint")
92  {
93  transportEndpointAddr_ = item.second[0].asINETAddr();
94 
96  INFO_LEVEL,
97  "NEM %03hu NEMImpl::configure transportendpoint: %s",
98  id_,
99  transportEndpointAddr_.str().c_str());
100  }
101  else
102  {
103  throw makeException<ConfigureException>("NEMImpl::configure: "
104  "Unexpected configuration item %s",
105  item.first.c_str());
106  }
107  }
108 }
109 
111 {
112  NEMOTAAdapter_.open();
113 
114  if(bExternalTransport_)
115  {
116  try
117  {
118  NEMNetworkAdapter_.open(platformEndpointAddr_,transportEndpointAddr_);
119  }
120  catch(NetworkAdapterException & exp)
121  {
122  throw StartException(exp.what());
123  }
124  }
125 
126  pNEMLayerStack_->start();
127 }
128 
130 {
131  pNEMLayerStack_->postStart();
132 }
133 
135 {
136  if(bExternalTransport_)
137  {
138  NEMNetworkAdapter_.close();
139  }
140 
141  NEMOTAAdapter_.close();
142 
143  pNEMLayerStack_->stop();
144 }
145 
147  throw()
148 {
149  pNEMLayerStack_->destroy();
150 }
151 
153 {
154  return id_;
155 }
void postStart() override
Definition: nemimpl.cc:129
The Registrar interface provides access to all of the emulator registrars.
Definition: registrar.h:50
NEMId getNEMId() const override
Definition: nemimpl.cc:152
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:42
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
void open(const INETAddr &localAddress, const INETAddr &remoteAddress)
std::uint16_t NEMId
Definition: types.h:52
void initialize(Registrar &registrar) override
Definition: nemimpl.cc:56
void destroy() override
Definition: nemimpl.cc:146
void start() override
Definition: nemimpl.cc:110
void stop() override
Definition: nemimpl.cc:134
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:76
#define LOGGER_STANDARD_LOGGING(logger, level, fmt, args...)
static LogService * instance()
Definition: singleton.h:56