winss
control.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2016-2017 Morgan Stanley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIB_WINSS_CONTROL_HPP_
18 #define LIB_WINSS_CONTROL_HPP_
19 
20 #include <string>
21 #include <vector>
22 #include <map>
23 #include <set>
24 #include "pipe_client.hpp"
25 #include "wait_multiplexer.hpp"
26 #include "not_owning_ptr.hpp"
27 
28 namespace winss {
32 class ControlItem {
33  protected:
34  const std::string name;
41  explicit ControlItem(std::string name);
42 
43  public:
49  virtual const std::string& GetName() const;
50 
54  virtual void Init() {}
55 
59  virtual void Start() {}
60 
64  virtual bool Completed() const = 0;
65 
69  virtual ~ControlItem() {}
70 };
71 
78 class Control {
79  private:
80  static const int kTimeoutExitCode;
82  static const char kTimeoutGroup[];
86  std::map<std::string, winss::NotOwningPtr<ControlItem>> items;
87  std::set<std::string> ready;
88  bool started = false;
89  const DWORD timeout;
90  const int timeout_exit_code;
91  const bool finish_all;
93  public:
104  DWORD timeout = INFINITE, int timeout_exit_code = 1,
105  bool finish_all = true);
106  Control(const Control&) = delete;
107  Control(Control&&) = delete;
114  bool IsStarted() const;
115 
121  void Add(winss::NotOwningPtr<ControlItem> item);
122 
128  void Ready(std::string name);
129 
135  void Remove(std::string name);
136 
140  int Start();
141 
142  Control& operator=(const Control&) = delete;
143  Control& operator=(Control&&) = delete;
144 };
145 
152  public ControlItem,
154  private:
155  static const int kNoSendExitCode;
159  const std::vector<char>& commands;
160  bool written = false;
161 
162  public:
166  const std::vector<char>& commands, std::string name);
167  OutboundControlItem(const OutboundControlItem&) = delete;
173  void Init();
174 
178  void Start();
179 
185  bool Completed() const;
186 
192  bool Connected();
193 
199  bool WriteComplete();
200 
206  bool Disconnected();
207 
209  OutboundControlItem& operator=(const OutboundControlItem&) = delete;
211  OutboundControlItem& operator=(OutboundControlItem&&) = delete;
212 };
213 
218  public:
224  virtual bool IsEnabled() = 0;
225 
231  virtual bool CanStart() = 0;
232 
236  virtual void HandleConnected() {}
237 
243  virtual bool HandleReceived(const std::vector<char>& message) = 0;
244 
249 };
250 
257  public ControlItem,
259  private:
264  bool complete = false;
265 
266  public:
271  std::string name);
272  InboundControlItem(const InboundControlItem&) = delete;
278  void Init();
279 
283  void Start();
284 
290  bool Completed() const;
291 
297  bool Connected();
298 
304  bool Received(const std::vector<char>& message);
305 
311  bool Disconnected();
312 
314  InboundControlItem& operator=(const InboundControlItem&) = delete;
316  InboundControlItem& operator=(InboundControlItem&&) = delete;
317 };
318 } // namespace winss
319 
320 #endif // LIB_WINSS_CONTROL_HPP_
virtual ~ControlItem()
Default destructor.
Definition: control.hpp:69
Inbound control item.
Definition: control.hpp:256
ControlItem(std::string name)
Constructs a control item with the given name.
Definition: control.cpp:24
virtual ~InboundControlItemListener()
Default destructor.
Definition: control.hpp:248
A listener for pipe client received data events.
Definition: pipe_client.hpp:239
A listener for inbound control item events.
Definition: control.hpp:217
Outbound control item.
Definition: control.hpp:151
Base class for inbound and outbound control item.
Definition: control.hpp:32
Definition: case_ignore.hpp:23
virtual void Init()
Initializes the control item.
Definition: control.hpp:54
virtual void Start()
Signals the start of either sending events or listening to events.
Definition: control.hpp:59
const std::string name
The name of the control item.
Definition: control.hpp:34
virtual const std::string & GetName() const
Gets the name of the control item.
Definition: control.cpp:26
Orchestrates control.
Definition: control.hpp:78
A listener for pipe client send complete events.
Definition: pipe_client.hpp:223
virtual void HandleConnected()
Call back for connected event.
Definition: control.hpp:236
virtual bool Completed() const =0
Signals that the events have been sent or all have been received.