@@ -39,10 +39,12 @@ public class SyncServerBuilder {
39
39
final BoxStore boxStore ;
40
40
final String url ;
41
41
private final List <SyncCredentialsToken > credentials = new ArrayList <>();
42
- final List <PeerInfo > peers = new ArrayList <>();
43
42
44
43
private @ Nullable String certificatePath ;
45
44
SyncChangeListener changeListener ;
45
+ private @ Nullable String clusterId ;
46
+ private final List <ClusterPeerInfo > clusterPeers = new ArrayList <>();
47
+ private int clusterFlags ;
46
48
47
49
public SyncServerBuilder (BoxStore boxStore , String url , SyncCredentials authenticatorCredentials ) {
48
50
checkNotNull (boxStore , "BoxStore is required." );
@@ -98,21 +100,55 @@ public SyncServerBuilder changeListener(SyncChangeListener changeListener) {
98
100
}
99
101
100
102
/**
101
- * Adds a server peer, to which this server should connect to as a client using {@link SyncCredentials#none()}.
103
+ * Enables cluster mode (requires the Cluster feature) and associates this cluster peer with the given ID.
104
+ * <p>
105
+ * Cluster peers need to share the same ID to be in the same cluster.
106
+ */
107
+ public SyncServerBuilder clusterId (String id ) {
108
+ checkNotNull (id , "Cluster ID must not be null" );
109
+ this .clusterId = id ;
110
+ return this ;
111
+ }
112
+
113
+ /**
114
+ * @deprecated Use {@link #clusterPeer(String, SyncCredentials) clusterPeer(url, SyncCredentials.none())} instead.
102
115
*/
116
+ @ Deprecated
103
117
public SyncServerBuilder peer (String url ) {
104
- return peer (url , SyncCredentials .none ());
118
+ return clusterPeer (url , SyncCredentials .none ());
105
119
}
106
120
107
121
/**
108
- * Adds a server peer, to which this server should connect to as a client using the given credentials .
122
+ * @deprecated Use {@link #clusterPeer(String,SyncCredentials)} instead .
109
123
*/
124
+ @ Deprecated
110
125
public SyncServerBuilder peer (String url , SyncCredentials credentials ) {
126
+ return clusterPeer (url , credentials );
127
+ }
128
+
129
+ /**
130
+ * Adds a (remote) cluster peer, to which this server should connect to as a client using the given credentials.
131
+ * <p>
132
+ * To use this, must set a {@link #clusterId(String)}.
133
+ */
134
+ public SyncServerBuilder clusterPeer (String url , SyncCredentials credentials ) {
111
135
if (!(credentials instanceof SyncCredentialsToken )) {
112
136
throw new IllegalArgumentException ("Sync credentials of type " + credentials .getType ()
113
137
+ " are not supported" );
114
138
}
115
- peers .add (new PeerInfo (url , (SyncCredentialsToken ) credentials ));
139
+ clusterPeers .add (new ClusterPeerInfo (url , (SyncCredentialsToken ) credentials ));
140
+ return this ;
141
+ }
142
+
143
+ /**
144
+ * Sets bit flags to configure the cluster behavior of the Sync server (aka cluster peer).
145
+ * <p>
146
+ * To use this, must set a {@link #clusterId(String)}.
147
+ *
148
+ * @param flags One or more of {@link ClusterFlags}.
149
+ */
150
+ public SyncServerBuilder clusterFlags (int flags ) {
151
+ this .clusterFlags = flags ;
116
152
return this ;
117
153
}
118
154
@@ -125,6 +161,9 @@ public SyncServer build() {
125
161
if (credentials .isEmpty ()) {
126
162
throw new IllegalStateException ("At least one authenticator is required." );
127
163
}
164
+ if (!clusterPeers .isEmpty () || clusterFlags != 0 ) {
165
+ checkNotNull (clusterId , "Cluster ID must be set to use cluster features." );
166
+ }
128
167
return new SyncServerImpl (this );
129
168
}
130
169
@@ -159,6 +198,10 @@ byte[] buildSyncServerOptions() {
159
198
if (certificatePath != null ) {
160
199
certificatePathOffset = fbb .createString (certificatePath );
161
200
}
201
+ int clusterIdOffset = 0 ;
202
+ if (clusterId != null ) {
203
+ clusterIdOffset = fbb .createString (clusterId );
204
+ }
162
205
int authenticationMethodsOffset = buildAuthenticationMethods (fbb );
163
206
int clusterPeersVectorOffset = buildClusterPeers (fbb );
164
207
@@ -177,11 +220,15 @@ byte[] buildSyncServerOptions() {
177
220
// SyncServerOptions.addHistorySizeTargetKb();
178
221
// SyncServerOptions.addAdminUrl();
179
222
// SyncServerOptions.addAdminThreads();
180
- // SyncServerOptions.addClusterId();
223
+ if (clusterIdOffset > 0 ) {
224
+ SyncServerOptions .addClusterId (fbb , clusterIdOffset );
225
+ }
181
226
if (clusterPeersVectorOffset > 0 ) {
182
227
SyncServerOptions .addClusterPeers (fbb , clusterPeersVectorOffset );
183
228
}
184
- // SyncServerOptions.addClusterFlags();
229
+ if (clusterFlags > 0 ) {
230
+ SyncServerOptions .addClusterFlags (fbb , clusterFlags );
231
+ }
185
232
int offset = SyncServerOptions .endSyncServerOptions (fbb );
186
233
fbb .finish (offset );
187
234
@@ -216,13 +263,13 @@ private int buildCredentials(FlatBufferBuilder fbb, SyncCredentialsToken tokenCr
216
263
}
217
264
218
265
private int buildClusterPeers (FlatBufferBuilder fbb ) {
219
- if (peers .isEmpty ()) {
266
+ if (clusterPeers .isEmpty ()) {
220
267
return 0 ;
221
268
}
222
269
223
- int [] peersOffsets = new int [peers .size ()];
224
- for (int i = 0 ; i < peers .size (); i ++) {
225
- PeerInfo peer = peers .get (i );
270
+ int [] peersOffsets = new int [clusterPeers .size ()];
271
+ for (int i = 0 ; i < clusterPeers .size (); i ++) {
272
+ ClusterPeerInfo peer = clusterPeers .get (i );
226
273
227
274
int urlOffset = fbb .createString (peer .url );
228
275
int credentialsOffset = buildCredentials (fbb , peer .credentials );
0 commit comments