NIST Biometric Evaluation Framework
Software components for biometric technology evaluations
be_process_commandcenter.h
Go to the documentation of this file.
1/*
2 * This software was developed at the National Institute of Standards and
3 * Technology (NIST) by employees of the Federal Government in the course
4 * of their official duties. Pursuant to Title 17 Section 105 of the
5 * United States Code, this software is not subject to copyright protection
6 * and is in the public domain. NIST assumes no responsibility whatsoever for
7 * its use by other parties, and makes no guarantees, expressed or implied,
8 * about its quality, reliability, or any other characteristic.
9 */
10
11#ifndef __BE_PROCESS_COMMANDCENTER_H__
12#define __BE_PROCESS_COMMANDCENTER_H__
13
14#include <cstdio>
15#include <string>
16#include <vector>
17
19#include <be_memory_autoarray.h>
22#include <be_text.h>
23
24namespace BiometricEvaluation
25{
26 namespace Process
27 {
29 template <typename T,
30 typename = typename std::enable_if<std::is_enum<T>::value>>
32 {
33 public:
35 class Command
36 {
37 public:
39 uint32_t clientID;
43 std::vector<std::string> arguments;
44 };
45
46 static_assert(std::is_enum<T>::value,
47 "Invalid templatization of CommandCenter.");
48
57 uint16_t port = MessageCenter::DEFAULT_PORT) :
58 _messageCenter(port)
59 {
60
61 }
62
64 ~CommandCenter() = default;
65
79 inline bool
81 {
82 return (this->_messageCenter.
83 hasUnseenMessages());
84 }
85
103 inline bool
105 Command &command,
106 int numSeconds = -1,
107 std::string invalidCommandResponse = "")
108 {
109 Memory::uint8Array buffer;
110 if (!this->_messageCenter.getNextMessage(
111 command.clientID, buffer, numSeconds))
112 return (false);
113
114 /* Arguments are space separated */
115 command.arguments = Text::split(
116 to_string(buffer), ' ');
117 if (command.arguments.size() == 0)
118 return (false);
119
120 /* Remove newline from last argument */
121 std::for_each(command.arguments.begin(),
122 command.arguments.end(),
123 [](std::string &i) {
124 i = Text::trimWhitespace(i);
125 });
126
127 /* Split actual command off of arguments */
128 try {
129 command.command = BiometricEvaluation::
130 Framework::Enumeration::to_enum<T>(
131 command.arguments[0]);
132 } catch (const Error::ObjectDoesNotExist&) {
133 /*
134 * Send implementation specific usage
135 * if set.
136 */
137 if (invalidCommandResponse != "")
138 this->sendResponse(
139 command.clientID,
140 invalidCommandResponse);
141 else {
142 static const std::string
143 INVALID = ": "
144 "command not recognized";
145 this->sendResponse(
146 command.clientID,
147 command.arguments[0] +
148 INVALID);
149 }
150 return (false);
151 }
152 command.arguments.erase(
153 command.arguments.begin());
154
155 return (true);
156 }
157
171 inline void
173 uint32_t clientID,
174 const std::string &response,
175 const std::string prefix = ">> ",
176 const std::string suffix = "\n")
177 {
178 Memory::uint8Array message;
180 prefix + response + suffix);
181 this->_messageCenter.sendResponse(clientID,
182 message);
183 }
184
192 inline void
194 uint32_t clientID)
195 {
196 this->sendResponse(clientID, "Goodbye");
197 this->_messageCenter.disconnectClient(clientID);
198 }
199
200 private:
202 MessageCenter _messageCenter;
203 };
204
206 template <typename T>
207 class CommandParser : public CommandCenter<T>
208 {
209 public:
217 virtual void
219 const typename
220 CommandCenter<T>::Command &command) = 0;
221
236 inline bool
238 typename CommandCenter<T>::Command &command,
239 int numSeconds = -1)
240 {
242 command, numSeconds, this->getUsage()));
243 }
244
255 inline void
257 const std::string &usage)
258 {
259 this->_usage = usage;
260 }
261
263 inline std::string
265 const
266 {
267 return (this->_usage);
268 }
269
278 uint16_t port = MessageCenter::DEFAULT_PORT) :
279 CommandCenter<T>(port),
280 _usage("")
281 {
282
283 }
284
286 virtual ~CommandParser() = default;
287
288 private:
290 std::string _usage;
291 };
292 }
293}
294
295#endif /* __BE_PROCESS_COMMANDCENTER_H__ */
std::string to_string(const BiometricEvaluation::Memory::AutoArray< T > &aa)
Convert a uint8_t or char AutoArray to a string.
std::vector< std::string > arguments
Arguments passed to command (optional).
Receive enumerations as commands over the network.
CommandCenter(uint16_t port=MessageCenter::DEFAULT_PORT)
Constructor.
void sendResponse(uint32_t clientID, const std::string &response, const std::string prefix=">> ", const std::string suffix="\n")
Send a string response to a client.
void disconnectClient(uint32_t clientID)
Break the connection with a client.
bool getNextCommand(Command &command, int numSeconds=-1, std::string invalidCommandResponse="")
Get the next command.
bool hasPendingCommands()
Determine if there are commands waiting.
~CommandCenter()=default
Destructor (default).
Abstraction to parse messages received via CommandCenter.
CommandParser(uint16_t port=MessageCenter::DEFAULT_PORT)
Constructor.
virtual void parse(const typename CommandCenter< T >::Command &command)=0
Parse command.
bool getNextCommand(typename CommandCenter< T >::Command &command, int numSeconds=-1)
Get the next command.
void setUsage(const std::string &usage)
String sent when an invalid command is received.
virtual ~CommandParser()=default
Virtual destructor (default).
Convenience for asynchronous TCP socket message passing.
void disconnectClient(uint32_t clientID)
Break the connection with a client.
bool getNextMessage(uint32_t &clientID, Memory::uint8Array &message, int numSeconds=-1)
Get the next available message.
static const uint16_t DEFAULT_PORT
Default port used for messages.
void sendResponse(uint32_t clientID, const Memory::uint8Array &message) const
Send a message to a client.
void setString(AutoArray< T > &aa, const std::string &str)
Copy a string into an AutoAray of uint8_t or char.
std::vector< std::string > split(const std::string &str, const char delimiter, bool escape=true)
Return tokens bound by delimiters and the beginning and end of a string.
This software was developed at the National Institute of Standards and Technology (NIST) by employees...