-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSession.java
More file actions
273 lines (226 loc) · 9.51 KB
/
Session.java
File metadata and controls
273 lines (226 loc) · 9.51 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package mp.code;
import lombok.Getter;
import mp.code.exceptions.ControllerException;
import mp.code.proto.Config;
import mp.code.proto.SessionEvent;
import mp.code.proto.UserInfo;
import mp.code.proto.WorkspaceIdentifier;
import mp.code.exceptions.ConnectionException;
import mp.code.exceptions.ConnectionRemoteException;
import java.util.function.Consumer;
/**
* The main entrypoint of the library.
* This is the only object you are expected to hold yourself; unlike all the others,
* there are no copies of it managed exclusively by the library. When this is garbage
* collected, it will free the underlying memory.
* A Session is used to join and manage workspaces, and to obtain information about
* the current session.
*/
@Getter
public final class Session {
private final long ptr;
Session(long ptr) {
this.ptr = ptr;
Extensions.CLEANER.register(this, () -> free(ptr));
}
/**
* Connects to a remote CodeMP server and creates a {@link Session} instance
* for interacting with it.
* @param config a {@link Config} object containing the connection settings
* @return a holder for the Session's pointer
* @throws ConnectionException if an error occurs in communicating with the server
*/
public static native Session connect(Config config) throws ConnectionException;
private static native UserInfo current_user(long self);
/**
* Gets information about the current user.
* @return a {@link UserInfo} object representing the user
*/
public UserInfo currentUser() {
return current_user(this.ptr);
}
private static native Workspace attach_workspace(long self, String user, String workspace) throws ConnectionException;
/**
* Joins a {@link Workspace} and returns it.
* @param user the owner of the workspace
* @param workspace the identifier of the workspace
* @return the relevant {@link Workspace}
* @throws ConnectionException if an error occurs in communicating with the server
*/
public Workspace attachWorkspace(String user, String workspace) throws ConnectionException {
return attach_workspace(this.ptr, user, workspace);
}
private static native void accept_invite(long self, String user, String workspace) throws ConnectionRemoteException;
/**
* Accept an invitation to a workspace.
* @param user the owner of the workspace
* @param workspace the identifier of the workspace
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void acceptInvite(String user, String workspace) throws ConnectionRemoteException {
accept_invite(this.ptr, user, workspace);
}
private static native void reject_invite(long self, String user, String workspace) throws ConnectionRemoteException;
/**
* Rejects an invitation to a workspace
* @param user the owner of the workspace
* @param workspace the identifier of the workspace
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void rejectInvite(String user, String workspace) throws ConnectionRemoteException {
reject_invite(this.ptr, user, workspace);
}
private static native void quit_workspace(long self, String user, String workspace) throws ConnectionRemoteException;
/**
* Quits a workspace.
* @param user the owner of the workspace
* @param workspace the identifier of the workspace
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void quitWorkspace(String user, String workspace) throws ConnectionRemoteException {
quit_workspace(this.ptr, user, workspace);
}
private static native void create_workspace(long self, String workspace) throws ConnectionRemoteException;
/**
* Creates a workspace. You need to call {@link #attachWorkspace(String, String)} to actually join
* and interact with it.
* @param workspace the id of the new workspace
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void createWorkspace(String workspace) throws ConnectionRemoteException {
create_workspace(this.ptr, workspace);
}
private static native void delete_workspace(long self, String workspace) throws ConnectionRemoteException;
/**
* Deletes a workspace.
* @param workspace the id of the workspace to delete
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void deleteWorkspace(String workspace) throws ConnectionRemoteException {
delete_workspace(this.ptr, workspace);
}
private static native void invite_to_workspace(long self, String workspaceId, String user) throws ConnectionRemoteException;
/**
* Invites a user to a workspace.
* @param workspace the id of the new workspace
* @param user the name of the user to invite
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void inviteToWorkspace(String workspace, String user) throws ConnectionRemoteException {
invite_to_workspace(this.ptr, workspace, user);
}
private static native WorkspaceIdentifier[] fetch_owned_workspaces(long self) throws ConnectionRemoteException;
/**
* Lists workspaces owned by the current user.
* @return an array of {@link WorkspaceIdentifier}s
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public WorkspaceIdentifier[] fetchOwnedWorkspaces() throws ConnectionRemoteException {
return fetch_owned_workspaces(this.ptr);
}
private static native WorkspaceIdentifier[] fetch_joined_workspaces(long self) throws ConnectionRemoteException;
/**
* Lists workspaces the current user has joined.
* @return an array of {@link WorkspaceIdentifier}s
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public WorkspaceIdentifier[] fetchJoinedWorkspaces() throws ConnectionRemoteException {
return fetch_joined_workspaces(this.ptr);
}
private static native WorkspaceIdentifier[] active_workspaces(long self);
/**
* Lists the currently active workspaces (the ones the user has currently joined).
* @return an array of {@link WorkspaceIdentifier}s
*/
public WorkspaceIdentifier[] activeWorkspaces() {
return active_workspaces(this.ptr);
}
private static native boolean leave_workspace(long self, String user, String workspace);
/**
* Leaves a workspace.
* @param user the owner of the workspace
* @param workspace the identifier of the workspace
* @return true if it succeeded or wasn't in the workspace; false if there are still
* leftover references around
*/
public boolean leaveWorkspace(String user, String workspace) {
return leave_workspace(this.ptr, user, workspace);
}
private static native Workspace get_workspace(long self, String user, String workspace);
/**
* Gets an active workspace.
* @param user the owner of the workspace
* @param workspace the identifier of the workspace
* @return a {@link Workspace} with that name, if it was present and active, null otherwise
*/
public Workspace getWorkspace(String user, String workspace) {
return get_workspace(this.ptr, user, workspace);
}
private static native UserInfo get_user_info(long self, String user) throws ConnectionRemoteException;
/**
* Fetches information about a user by name.
* @param user the name of the user
* @return the {@link UserInfo} for the user
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public UserInfo getUserInfo(String user) throws ConnectionRemoteException {
return get_user_info(this.ptr, user);
}
private static native SessionEvent try_recv(long self) throws ControllerException;
/**
* Tries to get a {@link SessionEvent} from the queue if any were present, null otherwise.
* @return the first session event in queue, if any are present
* @throws ControllerException if the controller was stopped
*/
public SessionEvent tryRecv() throws ControllerException {
return try_recv(this.ptr);
}
private static native SessionEvent recv(long self) throws ControllerException;
/**
* Blocks until a {@link SessionEvent} is available and returns it.
* @return the session event that occurred
* @throws ControllerException if the controller was stopped
*/
public SessionEvent recv() throws ControllerException {
return recv(this.ptr);
}
private static native void callback(long self, Consumer<Session> cb);
/**
* Registers a callback to be invoked whenever a {@link SessionEvent} occurs.
* This will not work unless a Java thread has been dedicated to the event loop.
* @param cb a {@link Consumer} that receives the controller when the change occurs;
* you should probably spawn a new thread in here, to avoid deadlocking
* @see Extensions#drive(boolean)
*/
public void callback(Consumer<Session> cb) {
callback(this.ptr, cb);
}
private static native void clear_callback(long self);
/**
* Clears the registered callback.
* @see #callback(Consumer)
*/
public void clearCallback() {
clear_callback(this.ptr);
}
private static native void poll(long self) throws ControllerException;
/**
* Blocks until a {@link SessionEvent} is available.
* @throws ControllerException if the controller was stopped
*/
public void poll() throws ControllerException {
poll(this.ptr);
}
private static native void refresh(long self) throws ConnectionRemoteException;
/**
* Refreshes the current access token.
* @throws ConnectionRemoteException if an error occurs in communicating with the server
*/
public void refresh() throws ConnectionRemoteException {
refresh(this.ptr);
}
private static native void free(long self);
static {
NativeUtils.loadLibraryIfNeeded();
}
}