File: example.cpp

package info (click to toggle)
libarcus 4.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 396 kB
  • sloc: cpp: 1,703; python: 54; sh: 42; makefile: 11
file content (128 lines) | stat: -rw-r--r-- 3,534 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <vector>
#include <iostream>
#include <thread>

#include "../src/Socket.h"
#include "../src/SocketListener.h"
#include "../src/Error.h"

#include "example.pb.h"

struct Object
{
public:
    int id;
    std::string vertices;
    std::string normals;
    std::string indices;
};

class Listener : public Arcus::SocketListener
{
public:
    void stateChanged(Arcus::SocketState::SocketState new_state)
    {
        std::cout << "State Changed: " << new_state << std::endl;
    }

    void messageReceived()
    {
    }

    void error(const Arcus::Error& new_error)
    {
        std::cout << new_error << std::endl;
    }
};

std::vector<Object> objects;

void handleMessage(Arcus::Socket& socket, Arcus::MessagePtr message);

int main(int argc, char** argv)
{
    Arcus::Socket socket;

    socket.registerMessageType(&Example::ObjectList::default_instance());
    socket.registerMessageType(&Example::ProgressUpdate::default_instance());
    socket.registerMessageType(&Example::SlicedObjectList::default_instance());

    socket.addListener(new Listener());

    std::cout << "Connecting to server\n";
    socket.connect("127.0.0.1", 56789);

    while(socket.getState() != Arcus::SocketState::Connected)
    {
        std::this_thread::sleep_for(std::chrono::milliseconds(100));
    }

    std::cout << "Connected to server\n";

    while(true)
    {
        if(socket.getState() == Arcus::SocketState::Connected)
        {
            auto message = socket.takeNextMessage();
            if(message)
            {
                handleMessage(socket, message);
            }
            std::this_thread::sleep_for(std::chrono::milliseconds(250));
        }
        else if(socket.getState() == Arcus::SocketState::Closed || socket.getState() == Arcus::SocketState::Error)
        {
            break;
        }
    }

    socket.close();
    return 0;
}

void handleMessage(Arcus::Socket& socket, Arcus::MessagePtr message)
{
    // (Dynamicly) cast the message to one of our types. If this works (does not return a nullptr), we've found the right type.
    auto objectList = dynamic_cast<Example::ObjectList*>(message.get());
    if(objectList)
    {
        objects.clear();

        std::cout << "Received object list containing " << objectList->objects_size() << " objects" << std::endl;

        for(auto objectDesc : objectList->objects())
        {
            Object obj;
            obj.id = objectDesc.id();
            obj.vertices = objectDesc.vertices();
            obj.normals = objectDesc.normals();
            obj.indices = objectDesc.indices();
            objects.push_back(obj);
        }

        auto msg = std::make_shared<Example::SlicedObjectList>();
        int progress = 0;
        for(auto object : objects)
        {
            auto slicedObject = msg->add_objects();
            slicedObject->set_id(object.id);

            for(int i = 0; i < 1000; ++i)
            {
                auto polygon = slicedObject->add_polygons();
                polygon->set_type(i % 2 == 0 ? Example::Polygon_Type_InnerType : Example::Polygon_Type_OuterType);
                polygon->set_points(object.vertices);
            }

            auto update = std::make_shared<Example::ProgressUpdate>();
            update->set_objectid(object.id);
            update->set_amount((float(++progress) / float(objects.size())) * 100.f);
            socket.sendMessage(update);
        }

        std::cout << "Sending SlicedObjectList" << std::endl;
        socket.sendMessage(msg);

        return;
    }
}