aboutsummaryrefslogtreecommitdiffstats
path: root/src/wm_connection.hpp
blob: d7255f6ac88c12bc69ce77a1cf633c862165dc4c (plain)
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
/*
 * Insert Copyright if needed.
 */

#ifndef WM_CONNECTION_HPP
#define WM_CONNECTION_HPP

#include <functional>
#include <unordered_map>

struct json_object;

namespace wm
{

class SocketInfo
{
    public:
        SocketInfo() {};
        SocketInfo(std::string screen_name, std::string ip, int wm_port, bool master_mode) : _screen_name(screen_name),
            _ip(ip), _wm_port(wm_port), _master_mode(master_mode){}
        std::string screenName() const { return _screen_name; }
        std::string ip() const { return _ip; }
        int wmPort() const { return _wm_port; }
        bool masterMode() const { return _master_mode; }
        int mySocket() const { return _my_socket; }
        int connectedSocket() const { return _connected_socket; }
        void setMySocket(int socket) { this->_my_socket = socket; }
        void setConnectedSocket(int socket) { this->_connected_socket = socket; }

    private:
        std::string _screen_name;
        std::string _ip;
        int _wm_port;
        bool _master_mode;
        int _my_socket = -1;
        int _connected_socket = -1;
};

class WMConnection
{
  public:
    WMConnection();
    ~WMConnection() = default;

    using ReceivedHandler = std::function<void(json_object* j_out)>;

    enum ModeType
    {
        Mode_Standalone = 0,
        Mode_Connection,
    };

    int initialize();
    void registerCallback(ReceivedHandler on_received);
    int sendRequest(char const *req, char const *appid,
                    char const *drawing_name, char const *drawing_area);
    bool isRemoteArea(const char* area);
    bool isRemoteEcu(std::string appid);
    bool isConnecting(std::string ecu_name);
    bool isConnectionMode();
    std::string parseMasterArea(const char* area);
    bool isSyncDrawingForRemote(const char* role);
    void startSyncDrawForRemote(const char* role);
    void finishSyncDrawForRemote(const char* role);
    int getMySocket();
    int getConnectedSocket();
    void setConnectedSocket(int connected_socket);
    std::string getAreaToEcuName(const char* area);
    std::string getSocketToEcuName(int socket);
    std::string getMySocketToEcuName(int socket);
    std::string getAppIdToEcuName(std::string appid);
    std::string getIpToEcuName(std::string ip);
    std::string getEcuName();
    bool getEndInit();
    int getSleepTime();
    void setAppIdToEcuName(std::string appid, std::string ecu_name);
    void setAppIdToReceivedEcuName(std::string appid);
    void callOnReceivedHandler(json_object *j_out);
    int connectToEcu();
    int connectToServer(std::string ecu_name);
    int connectedToServer(int socket);
    void connectionTimeLimit();

    int serverAccept(int socket);
    int receive(json_object** j_out, int *j_cnt, int socket);

  private:
    std::string mode;
    std::string received_ecu_name;
    ReceivedHandler onReceived;
    std::string syndDrawingAppId;

    std::string ecu_name;
    std::string received_ecu;

    uint64_t times;
    uint64_t sleep;

    bool end_init;
    ModeType wm_mode;

    std::unordered_map<std::string, SocketInfo> wm_socket;
    std::unordered_map<std::string, std::string> appid2ecuName;

    int initializeServer();
    int initializeClient(std::string ecu_name);
    int loadTimeoutConfigFile();
    int loadConnectionConfigFile();

    int send(json_object* j_in, std::string ecu_name);

    uint64_t getNextTimerTime(uint64_t msec);
};

} // namespace wm

#endif // WM_CONNECTION_HPP