TT Client Library 1.0.0
C++ library for Tandem Tales clients.
Loading...
Searching...
No Matches
ClientFactory.h
1#pragma once
2
3#include <string>
4#include <deque>
5#include <mutex>
6#include <atomic>
7#include <functional>
8#include <tt/Client.h>
9#include <tt/util/BlockingQueue.h>
10#include <tt/util/ThreadPool.h>
11#include <tt/util/CompletionEvent.h>
12
13#ifndef CLIENTFACTORY_H
14#define CLIENTFACTORY_H
15
16namespace tt {
17 // forward declaration to avoid circular inheritance
18 class Client;
19
21 using client_ptr = std::unique_ptr<Client>;
22
47 public:
56
62
68 virtual std::string toString() const;
69
70 friend std::ostream& operator<<(std::ostream& os, const ClientFactory& a);
71
112 void execute();
113
122 void close();
123
133 void onStart(const Client* client);
134
139 const int maxClients;
140
141 virtual ~ClientFactory();
142
143 protected:
152 virtual void onStart() const{
153 // This is meant to be overridden
154 }
155
164 virtual client_ptr create() const = 0;
165
179 virtual void onClose() const{
180 // This is meant to be overridden
181 }
182
195 virtual void onStop() const{
196 // This is meant to be overridden
197 };
198
199 private:
207 class Operation {
208 public:
209 explicit Operation(std::function<void()> function, ClientFactory& factory)
210 : function_(std::move(function)), factory_(factory)
211 {
212 }
213
214 void execute()
215 {
216 try {
217 function_();
218 }
219 catch (...) {
220 if (!uncaught_) {
221 uncaught_ = std::current_exception();
222 }
223
224 factory_.close();
225 }
226 }
227
228 private:
229 std::function<void()> function_;
230 std::exception_ptr uncaught_;
231 ClientFactory& factory_;
232 };
233
235 BlockingQueue<std::unique_ptr<Operation>> queue;
236
238 std::deque<client_ptr> waiting;
239
241 std::deque<client_ptr> running;
242
244 ThreadPool<Operation> tp;
245
250 std::exception_ptr uncaught = nullptr;
251
253 std::mutex uncaughtMutex;
254
260 void captureException(const char* stage);
261
263 bool closed = false;
264
266 CompletionEvent completion;
267
275 void createNewClient();
276
283 void onStop(const Client* client);
284
285 /* A boolean to make sure closing multiple times doesn't break anything */
286 std::atomic<bool> closeRequested_{false};
287 };
288
296 std::ostream& operator<<(std::ostream& os, const ClientFactory& a);
297}
298
299#endif
A client factory continuously creates clients to connect to a server and is useful for deploying auto...
Definition ClientFactory.h:46
virtual client_ptr create() const =0
This method creates and returns a new client of the type produced by this factory.
ClientFactory()
Creates a new client factory with no upper limit on the number of clients that may run simultaneously...
Definition ClientFactory.cpp:16
virtual void onClose() const
This method is called once after this factory stops creating new clients and has begun shutting down.
Definition ClientFactory.h:179
void close()
Causes the factory to shut down gracefully, closing any waiting clients and waiting for any running c...
Definition ClientFactory.cpp:83
void execute()
This method starts the client factory, creates the first client, periodically creates new clients as ...
Definition ClientFactory.cpp:29
virtual void onStop() const
This method is called once at the end of this factory's shutdown process after all clients have stopp...
Definition ClientFactory.h:195
virtual std::string toString() const
Gives a string representation of this client factory.
Definition ClientFactory.cpp:21
friend std::ostream & operator<<(std::ostream &os, const ClientFactory &a)
Override stream insertion operator to allow printing the ClientFactory class.
virtual void onStart() const
This method is called once when this factory begins running.
Definition ClientFactory.h:152
ClientFactory(int maxClients)
Creates a new client factory with an upper limit on the number of clients that it will create simulta...
Definition ClientFactory.cpp:7
const int maxClients
The maximum number of clients that this factory will create to run simultaneously.
Definition ClientFactory.h:139
A client connects to a server to find a partner and play a role in a storytelling session.
Definition Client.h:51
Tandem Tales is a platform that enables short, paired storytelling exercises between two agents over ...
Definition Client.h:22
std::unique_ptr< Client > client_ptr
Alias for a pointer to a client to clean up code.
Definition ClientFactory.h:21
std::ostream & operator<<(std::ostream &os, const Client &a)
Override stream insertion operator to allow printing the Client class.
Definition Client.cpp:556