Skip to content

Commit d860f78

Browse files
committed
refactor: Start supporting Java in addition to Kotlin.
1 parent fe06f91 commit d860f78

File tree

9 files changed

+131
-73
lines changed

9 files changed

+131
-73
lines changed

BUILD.bazel

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library")
2-
load("@rules_java//java:defs.bzl", "java_proto_library")
2+
load("@rules_java//java:defs.bzl", "java_proto_library", "java_test")
33
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")
44
load("@rules_proto//proto:defs.bzl", "proto_library")
55
load("//tools/project:build_defs.bzl", "project")
@@ -96,3 +96,15 @@ kt_jvm_test(
9696
"@maven//:org_jetbrains_kotlinx_kotlinx_coroutines_core",
9797
],
9898
)
99+
100+
java_test(
101+
name = "ToxCoreJavaTest",
102+
size = "small",
103+
srcs = ["src/test/java/im/tox/tox4j/core/ToxCoreJavaTest.java"],
104+
jvm_flags = ["-Djava.library.path=jvm-toxcore-c"],
105+
test_class = "im.tox.tox4j.core.ToxCoreJavaTest",
106+
deps = [
107+
":jvm-toxcore-c",
108+
"@maven//:junit_junit",
109+
],
110+
)

src/main/java/im/tox/tox4j/core/ToxCore.kt

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ interface ToxCore : Closeable {
3131
*
3232
* @return a byte array containing the serialised tox instance.
3333
*/
34-
val getSavedata: ByteArray
34+
val savedata: ByteArray
3535

3636
/**
3737
* Create a new [[ToxCore]] instance with different options. The implementation may choose to
@@ -43,7 +43,7 @@ interface ToxCore : Closeable {
4343
* instance will operate correctly.
4444
*
4545
* If the [[ToxOptions.saveData]] field is not empty, this function will load the Tox instance
46-
* from a byte array previously filled by [[getSavedata]].
46+
* from a byte array previously filled by [[savedata]].
4747
*
4848
* If loading failed or succeeded only partially, an exception will be thrown.
4949
*
@@ -78,6 +78,8 @@ interface ToxCore : Closeable {
7878
*/
7979
// @Throws(ToxBootstrapException::class)
8080
fun bootstrap(address: String, port: Port, publicKey: ToxPublicKey): Unit
81+
fun bootstrap(address: String, port: Int, publicKey: ByteArray): Unit =
82+
bootstrap(address, Port(port.toUShort()), ToxPublicKey(publicKey))
8183

8284
/**
8385
* Connect to a TCP relay to forward traffic.
@@ -91,14 +93,17 @@ interface ToxCore : Closeable {
9193
*/
9294
// @Throws(ToxBootstrapException::class)
9395
fun addTcpRelay(address: String, port: Port, publicKey: ToxPublicKey): Unit
96+
fun addTcpRelay(address: String, port: Int, publicKey: ByteArray): Unit =
97+
addTcpRelay(address, Port(port.toUShort()), ToxPublicKey(publicKey))
9498

9599
/**
96100
* Get the UDP port this instance is bound to.
97101
*
98102
* @return a port number between 1 and 65535.
99103
*/
100104
// @Throws(ToxGetPortException::class)
101-
val getUdpPort: Port
105+
val udpPort: Port
106+
fun getUdpPort(): Int = udpPort.value.toInt()
102107

103108
/**
104109
* Return the TCP port this Tox instance is bound to. This is only relevant if the instance is
@@ -107,20 +112,22 @@ interface ToxCore : Closeable {
107112
* @return a port number between 1 and 65535.
108113
*/
109114
// @Throws(ToxGetPortException::class)
110-
val getTcpPort: Port
115+
val tcpPort: Port
116+
fun getTcpPort(): Int = tcpPort.value.toInt()
111117

112118
/**
113119
* Writes the temporary DHT public key of this instance to a byte array.
114120
*
115121
* This can be used in combination with an externally accessible IP address and the bound port
116-
* (from [[getUdpPort]]}) to run a temporary bootstrap node.
122+
* (from [[udpPort]]}) to run a temporary bootstrap node.
117123
*
118124
* Be aware that every time a new instance is created, the DHT public key changes, meaning this
119125
* cannot be used to run a permanent bootstrap node.
120126
*
121127
* @return a byte array of size [[ToxCoreConstants.PublicKeySize]]
122128
*/
123-
val getDhtId: ToxPublicKey
129+
val dhtId: ToxPublicKey
130+
fun getDhtId(): ByteArray = dhtId.value
124131

125132
/**
126133
* Get the time in milliseconds until [[iterate]] should be called again for optimal performance.
@@ -141,27 +148,24 @@ interface ToxCore : Closeable {
141148
*
142149
* @return a byte array of size [[ToxCoreConstants.PublicKeySize]]
143150
*/
144-
val getPublicKey: ToxPublicKey
151+
val publicKey: ToxPublicKey
152+
fun getPublicKey(): ByteArray = publicKey.value
145153

146154
/**
147155
* Copy the Tox Secret Key from the Tox object.
148156
*
149157
* @return a byte array of size [[ToxCoreConstants.SecretKeySize]]
150158
*/
151-
val getSecretKey: ToxSecretKey
159+
val secretKey: ToxSecretKey
160+
fun getSecretKey(): ByteArray = secretKey.value
152161

153162
/**
154-
* Set the 4-byte nospam part of the address.
163+
* The 4-byte nospam part of the address.
155164
*
156165
* Setting the nospam makes it impossible for others to send us friend requests that contained the
157166
* old nospam number.
158-
*
159-
* @param nospam the new nospam number.
160167
*/
161-
fun setNospam(nospam: Int): Unit
162-
163-
/** Get our current nospam number. */
164-
val getNospam: Int
168+
var nospam: Int
165169

166170
/**
167171
* Get our current tox address to give to friends.
@@ -175,43 +179,28 @@ interface ToxCore : Closeable {
175179
*
176180
* @return a byte array of size [[ToxCoreConstants.AddressSize]]
177181
*/
178-
val getAddress: ToxFriendAddress
182+
val address: ToxFriendAddress
179183

180184
/**
181-
* Set the nickname for the Tox client.
185+
* The nickname for the Tox client.
182186
*
183187
* Cannot be longer than [[ToxCoreConstants.MaxNameLength]] bytes. Can be empty (zero-length).
184-
*
185-
* @param name A byte array containing the new nickname..
186188
*/
187189
// @Throws(ToxSetInfoException::class)
188-
fun setName(name: ToxNickname): Unit
189-
190-
/** Get our own nickname. */
191-
val getName: ToxNickname
190+
var name: ToxNickname
192191

193192
/**
194-
* Set our status message.
193+
* Our status message.
195194
*
196195
* Cannot be longer than [[ToxCoreConstants.MaxStatusMessageLength]] bytes.
197-
*
198-
* @param message the status message to set.
199196
*/
200197
// @Throws(ToxSetInfoException::class)
201-
fun setStatusMessage(message: ToxStatusMessage): Unit
202-
203-
/** Gets our own status message. May be null if the status message was empty. */
204-
val getStatusMessage: ToxStatusMessage
198+
var statusMessage: ToxStatusMessage
205199

206200
/**
207-
* Set our status.
208-
*
209-
* @param status status to set.
201+
* Our status.
210202
*/
211-
fun setStatus(status: ToxUserStatus): Unit
212-
213-
/** Get our status. */
214-
val getStatus: ToxUserStatus
203+
var status: ToxUserStatus
215204

216205
/**
217206
* Add a friend to the friend list and send a friend request.
@@ -240,6 +229,8 @@ interface ToxCore : Closeable {
240229
*/
241230
// @Throws(ToxFriendAddException::class, IllegalArgumentException::class)
242231
fun addFriend(address: ToxFriendAddress, message: ToxFriendRequestMessage): ToxFriendNumber
232+
fun addFriend(address: ByteArray, message: ByteArray): Int =
233+
addFriend(ToxFriendAddress(address), ToxFriendRequestMessage(message)).value
243234

244235
/**
245236
* Add a friend without sending a friend request.
@@ -257,6 +248,8 @@ interface ToxCore : Closeable {
257248
*/
258249
// @Throws(ToxFriendAddException::class, IllegalArgumentException::class)
259250
fun addFriendNorequest(publicKey: ToxPublicKey): ToxFriendNumber
251+
fun addFriendNorequest(publicKey: ByteArray): Int =
252+
addFriendNorequest(ToxPublicKey(publicKey)).value
260253

261254
/**
262255
* Remove a friend from the friend list.
@@ -308,7 +301,7 @@ interface ToxCore : Closeable {
308301
* @return an array containing the currently valid friend numbers, the empty int array if there
309302
* are no friends.
310303
*/
311-
val getFriendList: IntArray
304+
val friendList: IntArray
312305

313306
/**
314307
* Get an array of [[ToxFriendNumber]] objects with the same values as [[getFriendList]].
@@ -317,8 +310,8 @@ interface ToxCore : Closeable {
317310
*
318311
* @return [[getFriendList]] mapped to [[ToxFriendNumber]].
319312
*/
320-
val getFriendNumbers: List<ToxFriendNumber>
321-
get() = getFriendList.map { ToxFriendNumber(it) }
313+
val friendNumbers: List<ToxFriendNumber>
314+
get() = friendList.map { ToxFriendNumber(it) }
322315

323316
/**
324317
* Tell friend number whether or not we are currently typing.
@@ -347,7 +340,7 @@ interface ToxCore : Closeable {
347340
* incremented by 1 each time a message is sent. If [[Int.MaxValue]] messages were sent, the next
348341
* message ID is [[Int.MinValue]].
349342
*
350-
* Message IDs are not stored in the array returned by [[getSavedata]].
343+
* Message IDs are not stored in the array returned by [[savedata]].
351344
*
352345
* @param friendNumber The friend number of the friend to send the message to.
353346
* @param messageType Message type (normal, action, ...).

src/main/java/im/tox/tox4j/core/callbacks/FriendConnectionStatusCallback.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,10 @@ interface FriendConnectionStatusCallback<ToxCoreState> {
2020
connectionStatus: ToxConnection,
2121
state: ToxCoreState
2222
): ToxCoreState = state
23+
24+
fun friendConnectionStatus(
25+
friendNumber: Int,
26+
connectionStatus: ToxConnection,
27+
state: ToxCoreState
28+
): ToxCoreState = friendConnectionStatus(ToxFriendNumber(friendNumber), connectionStatus, state)
2329
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package im.tox.tox4j.core.callbacks
22

3-
class ToxCoreEventAdapter<ToxCoreState> : ToxCoreEventListener<ToxCoreState>
3+
abstract class ToxCoreEventAdapter<ToxCoreState> : ToxCoreEventListener<ToxCoreState>

src/main/java/im/tox/tox4j/core/options/ToxOptions.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,8 @@ final data class ToxOptions(
4242
val tcpPort: UShort = ToxCoreConstants.DefaultTcpPort,
4343
val saveData: SaveDataOptions.Type = SaveDataOptions.None,
4444
val fatalErrors: Boolean = true,
45-
)
45+
) {
46+
companion object {
47+
val defaultInstance = ToxOptions()
48+
}
49+
}

src/main/java/im/tox/tox4j/impl/jni/ToxCoreEventDispatch.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ object ToxCoreEventDispatch {
103103
state,
104104
{ next, ev ->
105105
handler.friendConnectionStatus(
106-
ToxFriendNumber(ev.getFriendNumber()), convert(ev.getConnectionStatus()), next)
106+
ev.getFriendNumber(), convert(ev.getConnectionStatus()), next)
107107
})
108108

109109
private fun <S> dispatchFriendTyping(

src/main/java/im/tox/tox4j/impl/jni/ToxCoreImpl.kt

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import im.tox.tox4j.core.exceptions.*
99
import im.tox.tox4j.core.options.ToxOptions
1010

1111
/**
12-
* Initialises the new Tox instance with an optional save-data received from [[getSavedata]].
12+
* Initialises the new Tox instance with an optional save-data received from [[savedata]].
1313
*
1414
* @param options Connection options object with optional save-data.
1515
*/
@@ -50,16 +50,16 @@ final class ToxCoreImpl(val options: ToxOptions) : ToxCore {
5050
ToxCoreJni.toxAddTcpRelay(instanceNumber, address, port.value.toInt(), publicKey.value)
5151
}
5252

53-
override val getSavedata: ByteArray
53+
override val savedata: ByteArray
5454
get() = ToxCoreJni.toxGetSavedata(instanceNumber)
5555

56-
override val getUdpPort: Port
56+
override val udpPort: Port
5757
get() = Port(ToxCoreJni.toxSelfGetUdpPort(instanceNumber).toUShort())
5858

59-
override val getTcpPort: Port
59+
override val tcpPort: Port
6060
get() = Port(ToxCoreJni.toxSelfGetTcpPort(instanceNumber).toUShort())
6161

62-
override val getDhtId: ToxPublicKey
62+
override val dhtId: ToxPublicKey
6363
get() = ToxPublicKey(ToxCoreJni.toxSelfGetDhtId(instanceNumber))
6464

6565
override val iterationInterval: Int
@@ -68,38 +68,30 @@ final class ToxCoreImpl(val options: ToxOptions) : ToxCore {
6868
override fun <S> iterate(handler: ToxCoreEventListener<S>, state: S): S =
6969
ToxCoreEventDispatch.dispatch(handler, ToxCoreJni.toxIterate(instanceNumber), state)
7070

71-
override val getPublicKey: ToxPublicKey
71+
override val publicKey: ToxPublicKey
7272
get() = ToxPublicKey(ToxCoreJni.toxSelfGetPublicKey(instanceNumber))
7373

74-
override val getSecretKey: ToxSecretKey
74+
override val secretKey: ToxSecretKey
7575
get() = ToxSecretKey(ToxCoreJni.toxSelfGetSecretKey(instanceNumber))
7676

77-
override fun setNospam(nospam: Int): Unit = ToxCoreJni.toxSelfSetNospam(instanceNumber, nospam)
78-
79-
override val getNospam: Int
77+
override var nospam: Int
8078
get() = ToxCoreJni.toxSelfGetNospam(instanceNumber)
79+
set(value) = ToxCoreJni.toxSelfSetNospam(instanceNumber, value)
8180

82-
override val getAddress: ToxFriendAddress
81+
override val address: ToxFriendAddress
8382
get() = ToxFriendAddress(ToxCoreJni.toxSelfGetAddress(instanceNumber))
8483

85-
override fun setName(name: ToxNickname): Unit =
86-
ToxCoreJni.toxSelfSetName(instanceNumber, name.value)
87-
88-
override val getName: ToxNickname
84+
override var name: ToxNickname
8985
get() = ToxNickname(ToxCoreJni.toxSelfGetName(instanceNumber))
86+
set(value) = ToxCoreJni.toxSelfSetName(instanceNumber, value.value)
9087

91-
override fun setStatusMessage(message: ToxStatusMessage): Unit {
92-
ToxCoreJni.toxSelfSetStatusMessage(instanceNumber, message.value)
93-
}
94-
95-
override val getStatusMessage: ToxStatusMessage
88+
override var statusMessage: ToxStatusMessage
9689
get() = ToxStatusMessage(ToxCoreJni.toxSelfGetStatusMessage(instanceNumber))
90+
set(value) = ToxCoreJni.toxSelfSetStatusMessage(instanceNumber, value.value)
9791

98-
override fun setStatus(status: ToxUserStatus): Unit =
99-
ToxCoreJni.toxSelfSetStatus(instanceNumber, status.ordinal)
100-
101-
override val getStatus: ToxUserStatus
92+
override var status: ToxUserStatus
10293
get() = ToxUserStatus.values()[ToxCoreJni.toxSelfGetStatus(instanceNumber)]
94+
set(value) = ToxCoreJni.toxSelfSetStatus(instanceNumber, value.ordinal)
10395

10496
override fun addFriend(
10597
address: ToxFriendAddress,
@@ -126,7 +118,7 @@ final class ToxCoreImpl(val options: ToxOptions) : ToxCore {
126118
override fun friendExists(friendNumber: ToxFriendNumber): Boolean =
127119
ToxCoreJni.toxFriendExists(instanceNumber, friendNumber.value)
128120

129-
override val getFriendList: IntArray
121+
override val friendList: IntArray
130122
get() = ToxCoreJni.toxSelfGetFriendList(instanceNumber)
131123

132124
override fun setTyping(friendNumber: ToxFriendNumber, typing: Boolean): Unit =

0 commit comments

Comments
 (0)