summaryrefslogtreecommitdiff
path: root/modules/remote-filesystem/c++/remote.hpp
blob: c5ca3972fac19f7f61bf7defe8230b45cf2342c3 (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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#pragma once

#include <filesystem>

#include <forstio/codec/schema_hash.hpp>
#include <forstio/remote/remote.hpp>

namespace saw {
namespace rmt {
struct File {};
}

template<>
class remote_address<rmt::File> {
private:
	std::filesystem::path path_;
public:
	remote_address(const std::filesystem::path& path__):
		path_{path__}
	{}

	std::string get_path_string() const {
		return path_.string();
	}

	const std::filesystem::path& get_path() const {
		return path_;
	}
};

template<>
class remote<rmt::File> {
private:
	SAW_FORBID_COPY(remote);
	SAW_FORBID_MOVE(remote);

	std::map<std::string, ptr<i_data_server<rmt::File>>> registered_data_servers_;
public:
	remote() = default;

	error_or<own<remote_address<rmt::File>>> parse_address(const std::string_view& path_v){
		return heap<remote_address<rmt::File>>(path_v);
	}

	template<typename Schema, typename Encode>
	error_or<own<data_server<Schema, Encode, rmt::File>>> data_listen(ref<remote_address<rmt::File>> addr){
		auto insert_res = registered_data_servers_.emplace(std::make_pair(addr().get_path_string(),ptr<i_data_server<rmt::File>>{}));
		if(!insert_res.second){
			return make_error<err::already_exists>();
		}

		auto dat_srv = heap<data_server<Schema, Encode, rmt::File>>(*this, addr);
		insert_res.first->second = ptr<i_data_server<rmt::File>>{*dat_srv};
		return dat_srv;
	}

	template<typename Schema, typename Encode>
	conveyor<own<data_client<Schema, Encode, rmt::File>>> data_connect(ref<remote_address<rmt::File>> addr){
		auto path_str = addr().get_path_string();
		auto find = registered_data_servers_.find(path_str);
		if(find == registered_data_servers_.end()){
			return {make_error<err::not_found>()};
		}

		return {heap<data_client<Schema,Encode,rmt::File>>({*this},{find->second})};
	}

	/**
	 * Internal deregister function
	 */
	error_or<void> deregister_data_server(const std::string& path){
		auto erase_op = registered_data_servers_.erase(path);
		if(erase_op == 0u){
			return make_error<err::not_found>();
		}
		return make_void();
	}
};

namespace rmt {
struct FileSystem {};
}

template<>
class remote_address<rmt::FileSystem> {
private:
	std::filesystem::path path_;
public:
	remote_address(const std::filesystem::path& path__):
		path_{path__}
	{}

	std::string get_path_string() const {
		return path_.string();
	}

	const std::filesystem::path& get_path() const {
		return path_;
	}
};

template<>
class remote<rmt::FileSystem> {
private:
	SAW_FORBID_COPY(remote);
	SAW_FORBID_MOVE(remote);

	struct key_t {
		std::string path;
		uint32_t sch_crc32;
		uint32_t enc_crc32;

		bool operator<(const key_t& rhs) const {
			if(path != rhs.path){
				return path < rhs.path;
			}

			if(sch_crc32 != rhs.sch_crc32 ){
				return sch_crc32 < rhs.sch_crc32;
			}
			
			if(enc_crc32 != rhs.enc_crc32 ){
				return enc_crc32 < rhs.enc_crc32;
			}

			return false;
		}
	};

	std::map<key_t, ptr<i_data_server<rmt::FileSystem>>> registered_data_servers_;
public:
	remote() = default;

	error_or<own<remote_address<rmt::FileSystem>>> parse_address(const std::string_view& path_v){
		return heap<remote_address<rmt::FileSystem>>(path_v);
	}

	template<typename Schema, typename Encode>
	error_or<own<data_server<Schema, Encode, rmt::FileSystem>>> data_listen(ref<remote_address<rmt::FileSystem>> addr){
		key_t k;
		k.path = addr().get_path_string();
		constexpr auto crc_pair = data_server<Schema,Encode,rmt::FileSystem>::class_id;
		k.sch_crc32 = crc_pair.first;
		k.enc_crc32 = crc_pair.second;

		auto insert_res = registered_data_servers_.emplace(std::make_pair(std::move(k),ptr<i_data_server<rmt::FileSystem>>{}));
		if(!insert_res.second){
			return make_error<err::already_exists>();
		}

		auto dat_srv = heap<data_server<Schema, Encode, rmt::FileSystem>>(*this, addr);
		insert_res.first->second = ptr<i_data_server<rmt::FileSystem>>{*dat_srv};
		return dat_srv;
	}

	/**
	 * Internal deregister function
	 */
	error_or<void> deregister_data_server(const std::string& addr, uint32_t sch_crc32, uint32_t enc_crc32){
		key_t k;
		k.path = addr;
		k.sch_crc32 = sch_crc32;
		k.enc_crc32 = enc_crc32;

		auto erase_op = registered_data_servers_.erase(k);
		if(erase_op == 0u){
			return make_error<err::not_found>();
		}
		return make_void();
	}
};
}