worker.h 4.08 KB
Newer Older
Stanislav Bohm's avatar
Stanislav Bohm committed
1
2
3
4
5
#ifndef LOOM_WORKER_H
#define LOOM_WORKER_H

#include "interconnect.h"
#include "taskinstance.h"
6
#include "unpacking.h"
Stanislav Bohm's avatar
Stanislav Bohm committed
7
#include "taskfactory.h"
8
#include "dictionary.h"
Stanislav Bohm's avatar
Stanislav Bohm committed
9
10
11
12
13
14
15
16
17
18
19

#include <uv.h>

#include <string>
#include <vector>
#include <unordered_map>
#include <memory>

namespace loom {

class Worker;
20
class DataUnpacker;
Stanislav Bohm's avatar
Stanislav Bohm committed
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

class ServerConnection : public SimpleConnectionCallback {
    
public:
    ServerConnection(Worker &worker);

    bool is_connected() {
        return connection.get_state() == Connection::ConnectionOpen;
    }

protected:
    Worker &worker;

    void on_connection();
    void on_close();
    void on_message(const char *data, size_t size);
    void on_error(int error_code);
};


class Worker {    
    friend class ServerConnection;

public:
    Worker(uv_loop_t* loop,
           const std::string& server_address,
           int server_port,
           const std::string& work_dir_root);

    uv_loop_t *get_loop() {
        return loop;
    }
    
    void new_task(std::unique_ptr<Task> task);
55
56
    void send_data(const std::string &address, Id id, std::shared_ptr<Data> &data, bool with_size);
    bool send_data(const std::string &address, Id id, bool with_size) {
Stanislav Bohm's avatar
Stanislav Bohm committed
57
58
59
60
        auto& data = public_data[id];
        if (data.get() == nullptr) {
            return false;
        }
61
        send_data(address, id, data, with_size);
Stanislav Bohm's avatar
Stanislav Bohm committed
62
63
64
65
        return true;
    }

    void task_finished(TaskInstance &task_instance);
66
    void publish_data(Id id, std::unique_ptr<Data> data);
67
    void remove_data(loom::Id id);
Stanislav Bohm's avatar
Stanislav Bohm committed
68
69
70
71
72
73
74
75
76
77
78
79
80
81

    bool has_data(Id id) const
    {
        return public_data.find(id) != public_data.end();
    }

    std::shared_ptr<Data>& get_data(Id id)
    {
        auto it = public_data.find(id);
        assert(it != public_data.end());
        return it->second;
    }

    void add_task_factory(std::unique_ptr<TaskFactory> factory)
82
83
    {        
        unregistered_task_factories.push_back(std::move(factory));
Stanislav Bohm's avatar
Stanislav Bohm committed
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
    }

    InterConnection &get_connection(const std::string &address);

    void close_all();

    int get_listen_port() const
    {
        return listen_port;
    }

    void add_connection(std::unique_ptr<InterConnection> connection)
    {
        nonregistered_connections.push_back(std::move(connection));
    }

    void register_connection(InterConnection &connection);
    void unregister_connection(InterConnection &connection);

    const std::string& get_server_address() {
        return server_address;
    }

    const std::string& get_work_dir() {
        return work_dir;
    }

    std::string get_run_dir(Id id);

    void check_waiting_tasks();
    void check_ready_tasks();

    void set_cpus(int value);
117
118
    void add_unpacker(DataTypeId type_id, std::unique_ptr<UnpackFactory> factory);    
    std::unique_ptr<DataUnpacker> unpack(DataTypeId id);
Stanislav Bohm's avatar
Stanislav Bohm committed
119

120
121
122
123
124
125
    Dictionary& get_dictionary() {
        return dictionary;
    }

    void on_dictionary_updated();

Stanislav Bohm's avatar
Stanislav Bohm committed
126
127
128
129
private:
    void register_worker();
    void start_listen();

130
    void remove_task(TaskInstance &task);    
Stanislav Bohm's avatar
Stanislav Bohm committed
131
132
133
134
135
136
137
138
139
140
    void start_task(std::unique_ptr<Task> task);
    //int get_listen_port();

    
    uv_loop_t *loop;

    int resource_cpus;
    std::vector<std::unique_ptr<TaskInstance>> active_tasks;
    std::vector<std::unique_ptr<Task>> ready_tasks;
    std::vector<std::unique_ptr<Task>> waiting_tasks;
141
    std::unordered_map<Id, std::unique_ptr<TaskFactory>> task_factories;
142

Stanislav Bohm's avatar
Stanislav Bohm committed
143
    std::unordered_map<int, std::shared_ptr<Data>> public_data;
144
    std::string work_dir;
Stanislav Bohm's avatar
Stanislav Bohm committed
145

146
    std::unordered_map<DataTypeId, std::unique_ptr<UnpackFactory>> unpack_factories;
Stanislav Bohm's avatar
Stanislav Bohm committed
147
148
149
150
151

    ServerConnection server_conn;
    std::unordered_map<std::string, std::unique_ptr<InterConnection>> connections;
    std::vector<std::unique_ptr<InterConnection>> nonregistered_connections;

152
153
    Dictionary dictionary;

Stanislav Bohm's avatar
Stanislav Bohm committed
154
155
156
157
158
159
    std::string server_address;
    int server_port;

    uv_tcp_t listen_socket;
    int listen_port;

160
161
    std::vector<std::unique_ptr<TaskFactory>> unregistered_task_factories;

Stanislav Bohm's avatar
Stanislav Bohm committed
162
163
164
165
166
167
168
    static void _on_new_connection(uv_stream_t *stream, int status);
    static void _on_getaddrinfo(uv_getaddrinfo_t* handle, int status, struct addrinfo* response);
};

}

#endif // LOOM_WORKER_H