Skip to content

Commit 2f2f6ad

Browse files
committed
Merge branch '220-sync-server-options' into dev
2 parents 5279983 + fbc68c7 commit 2f2f6ad

16 files changed

+822
-71
lines changed

objectbox-java/src/main/java/io/objectbox/BoxStoreBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ public BoxStoreBuilder initialDbFile(Factory<InputStream> initialDbFileFactory)
569569

570570
byte[] buildFlatStoreOptions(String canonicalPath) {
571571
FlatBufferBuilder fbb = new FlatBufferBuilder();
572-
// FlatBuffer default values are set in generated code, e.g. may be different from here, so always store value.
572+
// Always put values, even if they match the default values (defined in the generated classes)
573573
fbb.forceDefaults(true);
574574

575575
// Add non-integer values first...
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.sync;
20+
21+
import io.objectbox.flatbuffers.BaseVector;
22+
import io.objectbox.flatbuffers.BooleanVector;
23+
import io.objectbox.flatbuffers.ByteVector;
24+
import io.objectbox.flatbuffers.Constants;
25+
import io.objectbox.flatbuffers.DoubleVector;
26+
import io.objectbox.flatbuffers.FlatBufferBuilder;
27+
import io.objectbox.flatbuffers.FloatVector;
28+
import io.objectbox.flatbuffers.IntVector;
29+
import io.objectbox.flatbuffers.LongVector;
30+
import io.objectbox.flatbuffers.ShortVector;
31+
import io.objectbox.flatbuffers.StringVector;
32+
import io.objectbox.flatbuffers.Struct;
33+
import io.objectbox.flatbuffers.Table;
34+
import io.objectbox.flatbuffers.UnionVector;
35+
import java.nio.ByteBuffer;
36+
import java.nio.ByteOrder;
37+
38+
/**
39+
* Credentials consist of a type and the credentials data to perform authentication checks.
40+
* The data is either provided as plain-bytes, or as a list of strings.
41+
* Credentials can be used from the client and server side.
42+
* This depends on the type however:
43+
* for example, shared secrets are configured at both sides, but username/password is only provided at the client.
44+
*/
45+
@SuppressWarnings("unused")
46+
public final class Credentials extends Table {
47+
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
48+
public static Credentials getRootAsCredentials(ByteBuffer _bb) { return getRootAsCredentials(_bb, new Credentials()); }
49+
public static Credentials getRootAsCredentials(ByteBuffer _bb, Credentials obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
50+
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
51+
public Credentials __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
52+
53+
public long type() { int o = __offset(4); return o != 0 ? (long)bb.getInt(o + bb_pos) & 0xFFFFFFFFL : 0L; }
54+
/**
55+
* Credentials provided by plain bytes.
56+
* This is used for shared secrets (client & server).
57+
*/
58+
public int bytes(int j) { int o = __offset(6); return o != 0 ? bb.get(__vector(o) + j * 1) & 0xFF : 0; }
59+
public int bytesLength() { int o = __offset(6); return o != 0 ? __vector_len(o) : 0; }
60+
public ByteVector bytesVector() { return bytesVector(new ByteVector()); }
61+
public ByteVector bytesVector(ByteVector obj) { int o = __offset(6); return o != 0 ? obj.__assign(__vector(o), bb) : null; }
62+
public ByteBuffer bytesAsByteBuffer() { return __vector_as_bytebuffer(6, 1); }
63+
public ByteBuffer bytesInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 6, 1); }
64+
/**
65+
* Credentials provided by a string array.
66+
* For username/password (client-only), provide the username in strings[0] and the password in strings[1].
67+
* For GoogleAuth, you can provide a list of accepted IDs (server-only).
68+
*/
69+
public String strings(int j) { int o = __offset(8); return o != 0 ? __string(__vector(o) + j * 4) : null; }
70+
public int stringsLength() { int o = __offset(8); return o != 0 ? __vector_len(o) : 0; }
71+
public StringVector stringsVector() { return stringsVector(new StringVector()); }
72+
public StringVector stringsVector(StringVector obj) { int o = __offset(8); return o != 0 ? obj.__assign(__vector(o), 4, bb) : null; }
73+
74+
public static int createCredentials(FlatBufferBuilder builder,
75+
long type,
76+
int bytesOffset,
77+
int stringsOffset) {
78+
builder.startTable(3);
79+
Credentials.addStrings(builder, stringsOffset);
80+
Credentials.addBytes(builder, bytesOffset);
81+
Credentials.addType(builder, type);
82+
return Credentials.endCredentials(builder);
83+
}
84+
85+
public static void startCredentials(FlatBufferBuilder builder) { builder.startTable(3); }
86+
public static void addType(FlatBufferBuilder builder, long type) { builder.addInt(0, (int) type, (int) 0L); }
87+
public static void addBytes(FlatBufferBuilder builder, int bytesOffset) { builder.addOffset(1, bytesOffset, 0); }
88+
public static int createBytesVector(FlatBufferBuilder builder, byte[] data) { return builder.createByteVector(data); }
89+
public static int createBytesVector(FlatBufferBuilder builder, ByteBuffer data) { return builder.createByteVector(data); }
90+
public static void startBytesVector(FlatBufferBuilder builder, int numElems) { builder.startVector(1, numElems, 1); }
91+
public static void addStrings(FlatBufferBuilder builder, int stringsOffset) { builder.addOffset(2, stringsOffset, 0); }
92+
public static int createStringsVector(FlatBufferBuilder builder, int[] data) { builder.startVector(4, data.length, 4); for (int i = data.length - 1; i >= 0; i--) builder.addOffset(data[i]); return builder.endVector(); }
93+
public static void startStringsVector(FlatBufferBuilder builder, int numElems) { builder.startVector(4, numElems, 4); }
94+
public static int endCredentials(FlatBufferBuilder builder) {
95+
int o = builder.endTable();
96+
return o;
97+
}
98+
99+
public static final class Vector extends BaseVector {
100+
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
101+
102+
public Credentials get(int j) { return get(new Credentials(), j); }
103+
public Credentials get(Credentials obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
104+
}
105+
}
106+
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.sync;
20+
21+
/**
22+
* Credentials types for login at a sync server.
23+
*/
24+
@SuppressWarnings("unused")
25+
public final class CredentialsType {
26+
private CredentialsType() { }
27+
/**
28+
* Used to indicate an uninitialized variable. Should never be sent/received in a message.
29+
*/
30+
public static final int Invalid = 0;
31+
/**
32+
* No credentials required; do not use for public/production servers.
33+
* This is useful for testing and during development.
34+
*/
35+
public static final int None = 1;
36+
/**
37+
* Deprecated, replaced by SHARED_SECRET_SIPPED
38+
*/
39+
public static final int SharedSecret = 2;
40+
/**
41+
* Google Auth ID token
42+
*/
43+
public static final int GoogleAuth = 3;
44+
/**
45+
* Use shared secret to create a SipHash and make attacks harder than just copy&paste.
46+
* (At some point we may want to switch to crypto & challenge/response.)
47+
*/
48+
public static final int SharedSecretSipped = 4;
49+
/**
50+
* Use ObjectBox Admin users for Sync authentication.
51+
*/
52+
public static final int ObxAdminUser = 5;
53+
/**
54+
* Generic credential type suitable for ObjectBox admin (and possibly others in the future)
55+
*/
56+
public static final int UserPassword = 6;
57+
}
58+

objectbox-java/src/main/java/io/objectbox/sync/Sync.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.objectbox.sync;
1818

1919
import io.objectbox.BoxStore;
20+
import io.objectbox.sync.server.SyncServer;
2021
import io.objectbox.sync.server.SyncServerBuilder;
2122

2223
/**
@@ -50,12 +51,17 @@ public static SyncBuilder client(BoxStore boxStore, String url, SyncCredentials
5051
}
5152

5253
/**
53-
* Start building a sync server. Requires the BoxStore the server should use,
54-
* the URL and port the server should bind to and authenticator credentials to authenticate clients.
55-
* Additional authenticator credentials can be supplied using the builder.
54+
* Starts building a {@link SyncServer}. Once done, complete with {@link SyncServerBuilder#build() build()}.
5655
* <p>
57-
* For the embedded server, currently only {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none}
58-
* are supported.
56+
* Note: when also using Admin, make sure it is started before the server.
57+
*
58+
* @param boxStore The {@link BoxStore} the server should use.
59+
* @param url The URL of the Sync server on which the Sync protocol is exposed. This is typically a WebSockets URL
60+
* starting with {@code ws://} or {@code wss://} (for encrypted connections), for example
61+
* {@code ws://0.0.0.0:9999}.
62+
* @param authenticatorCredentials A list of enabled authentication methods available to Sync clients. Additional
63+
* authenticator credentials can be supplied using the builder. For the embedded server, currently only
64+
* {@link SyncCredentials#sharedSecret} and {@link SyncCredentials#none} are supported.
5965
*/
6066
public static SyncServerBuilder server(BoxStore boxStore, String url, SyncCredentials authenticatorCredentials) {
6167
return new SyncServerBuilder(boxStore, url, authenticatorCredentials);

objectbox-java/src/main/java/io/objectbox/sync/SyncBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2020 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import javax.annotation.Nullable;
2222

2323
import io.objectbox.BoxStore;
24-
import io.objectbox.annotation.apihint.Experimental;
2524
import io.objectbox.sync.internal.Platform;
2625
import io.objectbox.sync.listener.SyncChangeListener;
2726
import io.objectbox.sync.listener.SyncCompletedListener;
@@ -34,7 +33,6 @@
3433
* A builder to create a {@link SyncClient}; the builder itself should be created via
3534
* {@link Sync#client(BoxStore, String, SyncCredentials)}.
3635
*/
37-
@Experimental
3836
@SuppressWarnings({"unused", "WeakerAccess"})
3937
public class SyncBuilder {
4038

objectbox-java/src/main/java/io/objectbox/sync/SyncClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2019-2024 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -38,7 +38,6 @@
3838
* SyncClient is thread-safe.
3939
*/
4040
@SuppressWarnings("unused")
41-
@Experimental
4241
public interface SyncClient extends Closeable {
4342

4443
/**

objectbox-java/src/main/java/io/objectbox/sync/SyncCredentials.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,12 @@ public static SyncCredentials none() {
6060
}
6161

6262
public enum CredentialsType {
63-
// Note: this needs to match with CredentialsType in Core.
64-
65-
NONE(1),
66-
SHARED_SECRET(2),
67-
GOOGLE(3),
68-
SHARED_SECRET_SIPPED(4),
69-
OBX_ADMIN_USER(5),
70-
USER_PASSWORD(6);
63+
64+
NONE(io.objectbox.sync.CredentialsType.None),
65+
GOOGLE(io.objectbox.sync.CredentialsType.GoogleAuth),
66+
SHARED_SECRET_SIPPED(io.objectbox.sync.CredentialsType.SharedSecretSipped),
67+
OBX_ADMIN_USER(io.objectbox.sync.CredentialsType.ObxAdminUser),
68+
USER_PASSWORD(io.objectbox.sync.CredentialsType.UserPassword);
7169

7270
public final long id;
7371

objectbox-java/src/main/java/io/objectbox/model/SyncFlags.java renamed to objectbox-java/src/main/java/io/objectbox/sync/SyncFlags.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
// automatically generated by the FlatBuffers compiler, do not modify
1818

19-
package io.objectbox.model;
19+
package io.objectbox.sync;
2020

2121
/**
2222
* Flags to adjust sync behavior like additional logging.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.sync.server;
20+
21+
/**
22+
* Special bit flags used in cluster mode only.
23+
*/
24+
@SuppressWarnings("unused")
25+
public final class ClusterFlags {
26+
private ClusterFlags() { }
27+
/**
28+
* Indicates that this cluster always stays in the "follower" cluster role.
29+
* Thus, it does not participate in leader elections.
30+
* This is useful e.g. for weaker cluster nodes that should not become leaders.
31+
*/
32+
public static final int FixedFollower = 1;
33+
}
34+
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* Copyright 2024 ObjectBox Ltd. All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
// automatically generated by the FlatBuffers compiler, do not modify
18+
19+
package io.objectbox.sync.server;
20+
21+
import io.objectbox.flatbuffers.BaseVector;
22+
import io.objectbox.flatbuffers.BooleanVector;
23+
import io.objectbox.flatbuffers.ByteVector;
24+
import io.objectbox.flatbuffers.Constants;
25+
import io.objectbox.flatbuffers.DoubleVector;
26+
import io.objectbox.flatbuffers.FlatBufferBuilder;
27+
import io.objectbox.flatbuffers.FloatVector;
28+
import io.objectbox.flatbuffers.IntVector;
29+
import io.objectbox.flatbuffers.LongVector;
30+
import io.objectbox.flatbuffers.ShortVector;
31+
import io.objectbox.flatbuffers.StringVector;
32+
import io.objectbox.flatbuffers.Struct;
33+
import io.objectbox.flatbuffers.Table;
34+
import io.objectbox.flatbuffers.UnionVector;
35+
import java.nio.ByteBuffer;
36+
import java.nio.ByteOrder;
37+
38+
/**
39+
* Configuration to connect to another (remote) cluster peer.
40+
* If this server is started in cluster mode, it connects to other cluster peers.
41+
*/
42+
@SuppressWarnings("unused")
43+
public final class ClusterPeerConfig extends Table {
44+
public static void ValidateVersion() { Constants.FLATBUFFERS_23_5_26(); }
45+
public static ClusterPeerConfig getRootAsClusterPeerConfig(ByteBuffer _bb) { return getRootAsClusterPeerConfig(_bb, new ClusterPeerConfig()); }
46+
public static ClusterPeerConfig getRootAsClusterPeerConfig(ByteBuffer _bb, ClusterPeerConfig obj) { _bb.order(ByteOrder.LITTLE_ENDIAN); return (obj.__assign(_bb.getInt(_bb.position()) + _bb.position(), _bb)); }
47+
public void __init(int _i, ByteBuffer _bb) { __reset(_i, _bb); }
48+
public ClusterPeerConfig __assign(int _i, ByteBuffer _bb) { __init(_i, _bb); return this; }
49+
50+
public String url() { int o = __offset(4); return o != 0 ? __string(o + bb_pos) : null; }
51+
public ByteBuffer urlAsByteBuffer() { return __vector_as_bytebuffer(4, 1); }
52+
public ByteBuffer urlInByteBuffer(ByteBuffer _bb) { return __vector_in_bytebuffer(_bb, 4, 1); }
53+
public io.objectbox.sync.Credentials credentials() { return credentials(new io.objectbox.sync.Credentials()); }
54+
public io.objectbox.sync.Credentials credentials(io.objectbox.sync.Credentials obj) { int o = __offset(6); return o != 0 ? obj.__assign(__indirect(o + bb_pos), bb) : null; }
55+
56+
public static int createClusterPeerConfig(FlatBufferBuilder builder,
57+
int urlOffset,
58+
int credentialsOffset) {
59+
builder.startTable(2);
60+
ClusterPeerConfig.addCredentials(builder, credentialsOffset);
61+
ClusterPeerConfig.addUrl(builder, urlOffset);
62+
return ClusterPeerConfig.endClusterPeerConfig(builder);
63+
}
64+
65+
public static void startClusterPeerConfig(FlatBufferBuilder builder) { builder.startTable(2); }
66+
public static void addUrl(FlatBufferBuilder builder, int urlOffset) { builder.addOffset(0, urlOffset, 0); }
67+
public static void addCredentials(FlatBufferBuilder builder, int credentialsOffset) { builder.addOffset(1, credentialsOffset, 0); }
68+
public static int endClusterPeerConfig(FlatBufferBuilder builder) {
69+
int o = builder.endTable();
70+
return o;
71+
}
72+
73+
public static final class Vector extends BaseVector {
74+
public Vector __assign(int _vector, int _element_size, ByteBuffer _bb) { __reset(_vector, _element_size, _bb); return this; }
75+
76+
public ClusterPeerConfig get(int j) { return get(new ClusterPeerConfig(), j); }
77+
public ClusterPeerConfig get(ClusterPeerConfig obj, int j) { return obj.__assign(__indirect(__element(j), bb), bb); }
78+
}
79+
}
80+

0 commit comments

Comments
 (0)