ClientConnector.cpp 8.83 KB
Newer Older
1
2
3
4
#include "ClientConnector.hpp"
#include "ClientsManager.hpp"
#include "ofThread.h"
#include "ofxGui.h"
5
#include "ofxTimeMeasurements.h"
6
7
8
9
10
11
12

namespace ofxCloudLib
{

ClientConnector::ClientConnector(int start_port, int tcp_port, int osc_port,
                                 std::string client_name, ofxCloudLib::ClientsManager &manager,
                                 int index)
13
    : ofxTCPClient(), AbstractClient(client_name, index), ofThread()
14
15
16
17
18
19
20
21
22
{
    xyz_receiver_.setCodec(ofxSquash::Codec(CLOUDCLIENT_DESIREDCODEC));
    rgb_receiver_.setCodec(ofxSquash::Codec(CLOUDCLIENT_DESIREDCODEC));
    depth_receiver_.setCodec(ofxSquash::Codec(CLOUDCLIENT_DESIREDCODEC));

    xyz_receiver_.init(start_port);
    rgb_receiver_.init(start_port + 1);
    depth_receiver_.init(start_port + 2);

23
24
    tcp_port_ = tcp_port;
    osc_port_ = osc_port;
25
26
27

    last_mesh_.setMode(OF_PRIMITIVE_POINTS);
    sticky_mesh_.setMode(OF_PRIMITIVE_POINTS);
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

    startThread();
}

ClientConnector::~ClientConnector()
{
    aggregated_.close();
    waitForThread(true);
}

void ClientConnector::threadedFunction()
{
    ofMesh fresh_mesh;
    while (1) {
        xyz_receiver_.update();
        if (xyz_receiver_.isFrameNew()) {
            fresh_mesh.clear();
            cloud_counter_++;
            xyz_receiver_.receive(fresh_mesh);
47
48
49
            if (fresh_mesh.getNumVertices()) {
                fresh_mesh.removeVertex(0);
            }
50
51
52
            TS_START("Client::send channel thread");
            aggregated_.send(std::move(fresh_mesh));
            TS_STOP("Client::send channel thread");
53
54
            // std::cout << "NEW CLOUD IN THREAD " << client_name_ << "#" << cloud_counter_ << std::endl;
        } else {
55
56
57
58
59
60
61
62

            handle_connection();

            // TS_START("Client::sleep()");

            // std::this_thread::sleep_for(std::chrono::milliseconds(10));

            // TS_STOP("Client::sleep()");
63
64
65
66
67
68
69
70
71
72
73
        }
    }
}

bool ClientConnector::is_xyz_new()
{
    if (is_new_) {
        is_new_ = false;
        return true;
    }
    return false;
74
75
76
77
}

void ClientConnector::setup()
{
78
    AbstractClient::setup();
79
80
81
82
83
84
85
    // de l'extérieur!
    gui_.add(parameters_);
    gui_.add(cropper_parameters_);
}

void ClientConnector::receive_mesh()
{
86
87

    while (aggregated_.tryReceive(last_mesh_)) {
88
        // std::cout << "...NEW FRAME IN THREAD A " << client_name_ << std::endl;
89
90
91
92
        is_new_ = true;
    }

    /*
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    ofMesh tmp_mesh;
    //            tmp_mesh.append(sticky_mesh_);
    //            sticky_mesh_.clear();
    if (xyz_receiver_.isFrameNew()) {

        cloud_counter_++;
        for (const auto &v : last_mesh_.getVertices()) {
            if (v != zero_ && ofRandom(100) < 10) {
                tmp_mesh.addVertex(v);
                tmp_mesh.addColor(cloud_color_);
            }
        }
        xyz_receiver_.receive(last_mesh_);
        last_mesh_.removeVertex(0);
    }

109
110

*/
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
    //            for (unsigned int i=0;i<tmp_mesh.getNumVertices();i++) {
    //                auto color = tmp_mesh.getColors()[i];
    //                color.a = color.a * .55;
    //                if (color.a > .05) {
    //                    if (ofRandom(100)<1) {
    //                        color.a = color.a+0.5;
    //                    }
    //                    auto v = tmp_mesh.getVertices()[i];
    //                    v.x = v.x+ofRandom(-.1,.1);
    //                    v.y = v.y+ofRandom(-.1,.1);
    //                    v.z = v.z+ofRandom(-.1,.1);
    //                    sticky_mesh_.addVertex(v);
    //                    sticky_mesh_.addColor(color);
    //                }
    //            }
}

void ClientConnector::draw()
{
    ofSetColor(cloud_color_);
    ofDrawSphere(zero_, .075);

    ofPushMatrix();
    ofTranslate(zero_);
    ofDrawBitmapStringHighlight(etiquette_, 0, 0);
    ofTranslate(ofVec3f(0, -.15, 0));
    ofDrawBitmapStringHighlight(ofToString(cloud_counter_) + ": " +
                                    ofToString(last_mesh_.getNumVertices()),
                                0, 0);
    ofPopMatrix();

    last_mesh_.drawWireframe();

    //        ofEnableBlendMode(OF_BLENDMODE_ALPHA); //TODO trouver bug...
    //        sticky_mesh_.drawWireframe();
}

void ClientConnector::update()
{
150
151
152
153
154
155
156
    // handle_connection();
}

void ClientConnector::handle_connection()
{


157
    //        if (dead_client_) return;
158
159
160
161
162
163
164
    TS_START("ClientConnector::handle_connection()");

    bool res = false;

    res = isConnected();

    if (res) {
165
166


167
      //SYNC  if (sync_active_) osc_sync_.update();
168

169
170
171
172
        // xyz_receiver_.update();

        // rgb_receiver_.update();
        // depth_receiver_.update();
173
174
175
176

        string str = receive();
        if (str.length() > 0) {

177
            // std::cout << "PROCESSING TCP STREAM FROM " << client_name_ << ":" << str << std::endl;
178
179
180
181
182
183
184
185

            boost::char_separator<char> sep(" ");
            boost::tokenizer<boost::char_separator<char>> tokens(str, sep);

            vector<std::string> message;
            for (const auto &t : tokens) {
                message.push_back(t);

186
                // std::cout << "  tokens:" << t << std::endl;
187
            }
188

189
            lock();
190

191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
            if (message.size() == 0) {
                std::cout << "TCP erreur: message vide" << std::endl;
            } else if (message[0] == "zero") {

                if (message.size() == 5) {

                    zero_.x = std::atof(message[2].c_str());
                    zero_.y = std::atof(message[3].c_str());
                    zero_.z = std::atof(message[4].c_str());

                } else {
                    std::cout << "TCP erreur: pas 5 elems dans message zero" << std::endl;
                }
            } else if (message[0] == "parameter") {

206
           //SYNC     osc_sync_.setup((ofParameterGroup &)gui_.getParameter(), 6666, client_name_, 6667);
207
208
209
210
211
212
213

                // de l'extérieur??

                if (!parameters_.update_parameter_from_strings(message)) {
                    cropper_parameters_.update_parameter_from_strings(message);
                }

214
          //SYNC      osc_sync_.setup((ofParameterGroup &)gui_.getParameter(), 6666, client_name_, OSCCLIENT_SYNCPORT);
215
216
217
218
219
220
221
222
223
224

            } else if (message[0] == "sync_ready") {

                send("trigger_request");

            } else if (message[0] == "etiquette") {

                if (message.size() == 3) {

                    etiquette_ = message[2];
225
                    AbstractClient::update_gui_name(etiquette_);
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247

                } else {
                    std::cout << "TCP erreur: pas 3 elems dans message etiquette"
                              << std::endl;
                }
            } else if (message[0] == "color") {

                if (message.size() == 5) {

                    cloud_color_.r = std::atoi(message[2].c_str());
                    cloud_color_.g = std::atoi(message[3].c_str());
                    cloud_color_.b = std::atoi(message[4].c_str());
                    cloud_color_.a = 255;

                } else {
                    std::cout << "TCP erreur: pas 5 elems dans message couleur"
                              << std::endl;
                }
            } else {
                std::cout << "got TCP unknown msg from " << client_name_ << ":" << str
                          << std::endl;
            }
248
            unlock();
249
            // std::cout << "DONE PROCESSING TCP STREAM FROM " << client_name_ << std::endl;
250
        }
251

252
    } else {
253

254
255
256
257
258
259
260
261
262
263
        delta_time_ = ofGetElapsedTimeMillis() - connect_time_;

        if (delta_time_ > reconnect_time_) {
            if (ofxTCPClient::setup(client_name_, tcp_port_)) {
                setMessageDelimiter("\n");
                std::string info = "ports = " + ofToString(xyz_receiver_.getPort()) +
                                   " " + ofToString(rgb_receiver_.getPort()) + " " +
                                   ofToString(depth_receiver_.getPort());
                send("hello from server");
                send(info);
264
         //SYNC       osc_sync_.setup((ofParameterGroup &)gui_.getParameter(), 6666,   client_name_, OSCCLIENT_SYNCPORT);
265
266
267
268
269
270
271
272
                //                    osc_sync_.setup((ofParameterGroup&)gui_.getParameter(),osc_port_,client_name_,OSCCLIENT_SYNCPORT);

                send("sync : " + ofToString(osc_port_));
                std::cout << "synching " << client_name_ << ":" << osc_port_
                          << std::endl;
                sync_active_ = true;

            } else {
273
                std::cout << "ClientConnector:: problem with TCP connexion to " << client_name_ << ":" << tcp_port_ << std::endl;
274
275
276
277
278
279
280
281
282
                //                    sync_active_ = false;
                //                    std::cout << "can't setup -- destroy?" <<
                //                    std::endl;
                //                    dead_client_ = true;
                //                    close();
            }
            connect_time_ = ofGetElapsedTimeMillis();
        }
    }
283
    TS_STOP("ClientConnector::handle_connection()");
284
285
}
}