summaryrefslogtreecommitdiff
path: root/include/erebos/message.h
blob: 508aa23c85faf3562db037d5683f38a6966d529a (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
#pragma once

#include <erebos/merge.h>
#include <erebos/service.h>

#include <condition_variable>
#include <functional>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <tuple>

namespace erebos {

using std::condition_variable;
using std::mutex;
using std::tuple;
using std::unique_ptr;

class Contact;
class Identity;
struct DirectMessageState;

class DirectMessage
{
public:
	const std::optional<Identity> & from() const;
	const std::optional<struct ZonedTime> & time() const;
	std::string text() const;

private:
	friend class DirectMessageThread;
	friend class DirectMessageService;
	struct Priv;
	DirectMessage(Priv *);
	std::shared_ptr<Priv> p;
};

class DirectMessageThread
{
public:
	class Iterator
	{
		struct Priv;
		Iterator(Priv *);
	public:
		using iterator_category = std::forward_iterator_tag;
		using value_type = DirectMessage;
		using difference_type = ssize_t;
		using pointer = const DirectMessage *;
		using reference = const DirectMessage &;

		Iterator(const Iterator &);
		~Iterator();
		Iterator & operator=(const Iterator &);
		Iterator & operator++();
		value_type operator*() const;
		bool operator==(const Iterator &) const;
		bool operator!=(const Iterator &) const;

	private:
		friend DirectMessageThread;
		std::unique_ptr<Priv> p;
	};

	Iterator begin() const;
	Iterator end() const;

	size_t size() const;
	DirectMessage at(size_t) const;

	const Identity & peer() const;

private:
	friend class DirectMessageService;
	friend class DirectMessageThreads;
	struct Priv;
	DirectMessageThread(Priv *);
	std::shared_ptr<Priv> p;
};

class DirectMessageThreads
{
public:
	DirectMessageThreads();
	DirectMessageThreads(Stored<DirectMessageState>);
	DirectMessageThreads(vector<Stored<DirectMessageState>>);

	static DirectMessageThreads load(const vector<Ref> & refs);
	vector<Ref> store() const;
	vector<Stored<DirectMessageState>> data() const;

	bool operator==(const DirectMessageThreads &) const;
	bool operator!=(const DirectMessageThreads &) const;

	DirectMessageThread thread(const Identity &) const;

private:
	vector<Stored<DirectMessageState>> state;

	friend class DirectMessageService;
};

DECLARE_SHARED_TYPE(DirectMessageThreads)

template<> struct Mergeable<DirectMessageThreads>
{
	using Component = DirectMessageState;
	static vector<Stored<DirectMessageState>> components(const DirectMessageThreads &);
	static Contact merge(vector<Stored<DirectMessageState>>);
};

class DirectMessageService : public Service
{
public:
	using ThreadWatcher = std::function<void(const DirectMessageThread &, ssize_t, ssize_t)>;

	class Config
	{
	public:
		Config & onUpdate(ThreadWatcher);

	private:
		friend class DirectMessageService;
		vector<ThreadWatcher> watchers;
	};

	DirectMessageService(Config &&, const Server &);
	virtual ~DirectMessageService();

	UUID uuid() const override;
	void handle(Context &) override;

	DirectMessageThread thread(const Identity &);

	static DirectMessage send(const Head<LocalState> &, const Identity &, const std::string &);
	static DirectMessage send(const Head<LocalState> &, const Contact &, const std::string &);
	static DirectMessage send(const Head<LocalState> &, const Peer &, const std::string &);

	DirectMessage send(const Identity &, const std::string &);
	DirectMessage send(const Contact &, const std::string &);
	DirectMessage send(const Peer &, const std::string &);

private:
	void updateHandler(const DirectMessageThreads &);
	void peerWatcher(size_t, const class Peer *);
	void syncWithPeer(const DirectMessageThread &, const Peer &);
	void doSyncWithPeers();
	void doSyncWithPeer(const DirectMessageThread &, const Peer &);

	const Config config;
	const Server & server;

	vector<Stored<DirectMessageState>> prevState;
	mutex stateMutex;

	mutex peerSyncMutex;
	condition_variable peerSyncCond;
	bool peerSyncRun;
	vector<tuple<DirectMessageThread, Peer>> peerSyncQueue;
	std::thread peerSyncThread;

	Watched<DirectMessageThreads> watched;
};

}