Hotwheels-Cluster 1.2
Creation of Cluster APP for SEA:ME project.
 
Loading...
Searching...
No Matches
ClientThread.cpp
Go to the documentation of this file.
1
15
16#include "ClientThread.hpp"
17
23ClientThread::ClientThread(QObject *parent) : QObject(parent) {}
24
25
32 if (communicator) {
33 communicator->shutdown();
34 communicator->destroy();
35 }
36}
37
49void ClientThread::runClient(int argc, char* argv[]) {
50 try {
51 // Initialize Ice communicator
52 communicator = Ice::initialize(argc, argv);
53
54 // Connect to the server by specifying the proxy endpoint
55 base = communicator->stringToProxy("carData:tcp -h 127.0.0.1 -p 10000");
56
57 // Cast the base proxy to the correct type (carDataPrx)
58 carData = Data::CarDataPrx::checkedCast(base);
59 if (!carData) {
60 throw std::runtime_error("Invalid proxy, failed to cast.");
61 }
62
63 // Set the connected flag to true once the connection is established
64 {
65 std::lock_guard<std::mutex> lock(mtx);
66 connected = true;
67 }
68
69 // Notify the main thread that the client is connected
70 cv.notify_all();
71
72 // Keep the client running and processing requests
73 while (running) {
74 std::this_thread::sleep_for(std::chrono::milliseconds(100));
75 }
76
77 // Gracefully shutdown the communicator
78 communicator->destroy();
79 } catch (const Ice::Exception& e) {
80 std::cerr << "Ice Exception: " << e.what() << std::endl;
81 }
82}
83
94 // Wait until the client is connected to the server
95 std::unique_lock<std::mutex> lock(mtx);
96 cv.wait(lock, [this] { return connected; });
97
98 // Ensure the carData proxy is valid before making the request
99 if (carData) {
100 try {
101 carData->setJoystickValue(value);
102 } catch (const Ice::Exception& e) {
103 std::cerr << "Error setting joystick value: " << e.what() << std::endl;
104 }
105 } else {
106 std::cerr << "Joystick proxy is not valid!" << std::endl;
107 }
108}
109
120 // Wait until the client is connected to the server
121 std::unique_lock<std::mutex> lock(mtx);
122 cv.wait(lock, [this] { return connected; });
123
124 // Ensure the carData proxy is valid before making the request
125 if (carData) {
126 try {
127 bool state = carData->getJoystickValue();
128 return state;
129 } catch (const Ice::Exception& e) {
130 std::cerr << "Error getting joystick value: " << e.what() << std::endl;
131 return false; // Return default value on error
132 }
133 } else {
134 std::cerr << "Joystick proxy is not valid!" << std::endl;
135 return false; // Return default value if the proxy is invalid
136 }
137}
138
146void ClientThread::setRunning(bool value) {
147 std::lock_guard<std::mutex> lock(mtx);
148 this->running = value;
149}
File containing the ClientThread class.
Ice::CommunicatorPtr communicator
void setJoystickValue(bool value)
Set the joystick value on the server.
std::condition_variable cv
Ice::ObjectPrx base
void setRunning(bool value)
Sets the running flag of the client thread.
ClientThread(QObject *parent=nullptr)
Construct a new Client Thread:: Client Thread object.
Data::CarDataPrx carData
void runClient(int argc, char *argv[])
Run the client by initializing the Ice communicator and connecting to the server.
~ClientThread()
Destructor for the ClientThread class.
std::mutex mtx
bool getJoystickValue()
Get the joystick value from the server.