EMANE  1.2.1
emaneeventserver.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 - Adjacent Link LLC, Bridgewater, New Jersey
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in
13  * the documentation and/or other materials provided with the
14  * distribution.
15  * * Neither the name of Adjacent Link LLC nor the names of its
16  * contributors may be used to endorse or promote products derived
17  * from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
25  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 #define _GLIBCXX_USE_NANOSLEEP
33 #include "emulator.h"
34 #include "eventdirector.h"
35 
36 #include "emane/types.h"
37 #include "emane/startexception.h"
39 
40 #include <thread>
41 #include <chrono>
42 #include <iostream>
43 
44 namespace EMANE
45 {
46  namespace Application
47  {
48  class App : public EMANE::Application::Emulator<EventGeneratorBuilder,
49  EventDirector,
50  EventGeneratorManager>
51  {
52  public:
53  App(const std::string & sName_):
54  Emulator(sName_),
55  secondsStart_{},
56  bNextDay_{}{}
57 
58  private:
59  std::vector<option> doGetOptions() const
60  {
61  return {{"starttime", 1, nullptr, 's'},
62  {"nextday", 0, nullptr, 'n'}};
63 
64  }
65 
66  std::string doGetOptString() const
67  {
68  return "s:n";
69  }
70 
71  bool doProcessOption(int iOptOpt, const char * pzOptArg)
72  {
73  switch(iOptOpt)
74  {
75  case 's':
76  {
77  unsigned hour, min, sec;
78 
79  if(sscanf(pzOptArg, "%02u:%02u:%02u", &hour, &min, &sec) == 3)
80  {
81  secondsStart_ = std::chrono::seconds{(hour * 3600) + (min * 60) + sec};
82  }
83  else
84  {
85  std::cerr<<"Error in format of start time "
86  <<pzOptArg
87  <<" --starttime HH:MM:SS"<<std::endl;
88  return false;
89  }
90  }
91  break;
92 
93  case 'n':
94  bNextDay_ = true;
95  break;
96 
97  default:
98  return false;
99  }
100 
101  return true;
102  }
103 
104  virtual std::vector<std::string> doGetOptionsUsage() const
105  {
106  return
107  {
108  " -s, --starttime TIME Set the start time HH:MM:SS",
109  " -n, --nextday Set the start time to the next day"};
110  }
111 
112 
113  void doStart() override
114  {
115  pManager_ = pDirector_->construct(getUUID());
116 
117  if(secondsStart_ != std::chrono::seconds::zero())
118  {
119  std::time_t t = std::time(nullptr);
120 
121  // current time broken down to mon,day,year,hr,min,sec ...
122  struct tm *tm_ptr = std::localtime(&t);
123 
124  // number of seconds passed so far today
125  std::chrono::seconds secondsPassed{tm_ptr->tm_hour * 3600 + tm_ptr->tm_min * 60 + tm_ptr->tm_sec};
126 
127  if(bNextDay_)
128  {
129  // add the remaining seconds left for today to the start time in
130  // localtime seconds since midnight
131  secondsStart_ += std::chrono::seconds{24 * 60 * 60} - secondsPassed;
132  }
133  else
134  {
135  if(secondsStart_ >= secondsPassed)
136  {
137  secondsStart_ -= secondsPassed;
138  }
139  else
140  {
141  throw makeException<StartException>("Startime in the past");
142  }
143  }
144 
145 
146  std::this_thread::sleep_for(secondsStart_);
147  }
148 
149 
150  pManager_->start();
151 
152  pManager_->postStart();
153  }
154 
155 
156  std::chrono::seconds secondsStart_;
157  bool bNextDay_;
158  };
159  }
160 }
161 DECLARE_APPLICATION(EMANE::Application::App,"emaneeventservice");
EMANE::Application::Emulator< EMANE::Application::NEMBuilder, EMANE::Application::NEMDirector, EMANE::Application::NEMManager > App
Definition: emane.cc:39
DECLARE_APPLICATION(EMANE::Application::App,"emaneeventservice")
Definition: agent.h:43