Babel  1
The voip software that only works on your local network
Loading...
Searching...
No Matches
ThreadCapsule.cpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2024
3** epitech-ratrappage-babel (Workspace)
4** File description:
5** ThreadCapsule.cpp
6*/
7
14
16 : _running(false)
17{
18
19}
20
22{
23 if (_running || _thread.joinable()) {
24 stopThread();
25 }
26}
27
32{
33 if (!_running) {
34 _running = true;
35 _thread = std::thread(&Controls::UserControls::spamUserChoice, &_userControls);
36 std::cout << "Thread started" << std::endl;
37 }
38}
39
43void Controls::ThreadCapsule::stopThread(const unsigned int delay)
44{
45 // if (_running) {
46 // _running = false;
47
48 // if (_thread.joinable()) {
49 // std::chrono::steady_clock::time_point start_time = std::chrono::steady_clock::now();
50
51 // while (std::chrono::steady_clock::now() - start_time < std::chrono::seconds(delay)) {
52 // if (!_thread.joinable()) {
53 // return;
54 // }
55 // std::this_thread::sleep_for(std::chrono::milliseconds(100));
56 // }
57
58 // PRETTY_INFO << "Thread did not stop in time. Forcibly terminating." << std::endl;
59 // try {
60 // PRETTY_INFO << "Injecting the 'q' key to stop the thread." << std::endl;
61 // std::cin.putback('q');
62 // std::cin.putback('\n');
63 // throw CustomExceptions::ThreadFound();
64 // }
65 // catch (const std::exception &e) {
66 // std::cerr << "Error: " << e.what() << std::endl;
67 // }
68 // catch (...) {
69 // std::cerr << "Unknown error occurred while stopping the thread." << std::endl;
70 // }
71 // }
72 // }
73 if (_running) {
74 _running = false;
75 PRETTY_INFO << "Injecting the 'q' key to stop the thread." << std::endl;
76 _userControls.setLooping(false);
77 _userControls.setPlaying(false);
78 std::cin.putback('q');
79 std::cin.putback('\n');
80 std::cin.putback('\n');
81 std::cin.putback('\n');
82 std::cin.putback('\n');
83 if (_thread.joinable()) {
84 _thread.join();
85 }
86 }
87 // if (_running) {
88 // _running = false;
89 // if (_thread.joinable()) {
90 // _thread.join();
91 // }
92 // }
93}
94
101{
102 return _running;
103}
104
111{
112 _userControls.setEcho(echo);
113}
114
121{
122 _userControls.setHelp(help);
123}
124
131{
132 _userControls.setPlaying(playing);
133}
134
141{
142 _userControls.setLooping(looping);
143}
144
151{
152 _userControls.setRecording(recording);
153}
154
160void Controls::ThreadCapsule::setUserChoice(const std::string &userChoice)
161{
162 _userControls.setUserChoice(userChoice);
163}
164
171{
172 return _userControls.isEcho();
173}
174
181{
182 return _userControls.isHelp();
183}
184
191{
192 return _userControls.isPlaying();
193}
194
201{
202 return _userControls.isLooping();
203}
204
211{
212 return _userControls.isRecording();
213}
214
221{
222 return _userControls.hangUpTheCall();
223}
224
229{
230 _userControls.showPrompt();
231}
232
237{
238 _userControls.toggleEcho();
239}
240
245{
246 _userControls.toggleHelp();
247}
248
253{
254 _userControls.togglePlaying();
255}
256
261{
262 _userControls.toggleLooping();
263}
264
269{
270 _userControls.toggleRecording();
271}
272
279const std::string Controls::ThreadCapsule::getInfo(const unsigned int indent) const
280{
281 std::string indentation = "";
282 for (unsigned int i = 0; i < indent; ++i) {
283 indentation += "\t";
284 }
285 std::string result = indentation + "Thread Capsule:\n";
286 result += indentation + "- Running: '" + Recoded::myToString(_running) + "'\n";
287 result += indentation + "- User Controls: {\n" + _userControls.getInfo(indent + 1) + indentation + "}\n";
288 return result;
289}
290
298std::ostream &Controls::operator<<(std::ostream &os, const Controls::ThreadCapsule &network)
299{
300 os << network.getInfo();
301 return os;
302}
#define PRETTY_INFO
Info log with details and colour.
This file contains the definition of the ThreadCapsule class, which manages a thread for user control...
Manages a thread for user controls.
const bool isRunning() const
Checks if the thread is running.
void toggleHelp()
Toggles the help state.
void toggleRecording()
Toggles the recording state.
const bool isLooping() const
Checks if looping is enabled.
void startThread()
Starts the thread to run the spamUserChoice function.
void toggleLooping()
Toggles the looping state.
void stopThread(const unsigned int delay=2)
Stops the thread safely.
void togglePlaying()
Toggles the playing state.
const bool isPlaying() const
Checks if playing is enabled.
void setUserChoice(const std::string &userChoice)
Processes the user choice.
const bool isHelp() const
Checks if help is enabled.
const bool isRecording() const
Checks if recording is enabled.
void setEcho(const bool echo)
Sets the echo state.
const std::string getInfo(const unsigned int indent=0) const
Dumps the current state of the variables for debugging purposes.
void setRecording(const bool recording)
Sets the recording state.
const bool hangUpTheCall() const
Checks if the call should be hung up.
void setPlaying(const bool playing)
Sets the playing state.
void setLooping(const bool looping)
Sets the looping state.
const bool isEcho() const
Checks if echo is enabled.
void toggleEcho()
Toggles the echo state.
void showPrompt() const
Displays the prompt to the user.
void setHelp(const bool help)
Sets the help state.
void spamUserChoice()
Continuously prompts the user for input and processes it.
std::ostream & operator<<(std::ostream &os, const ThreadCapsule &network)
Overloads the stream insertion operator for ThreadCapsule.
const std::string myToString(bool value)
Converts a boolean value to its string representation.
Definition ToString.cpp:22