Skip to content

Commit

Permalink
refactor: Start supporting Java in addition to Kotlin.
Browse files Browse the repository at this point in the history
  • Loading branch information
iphydf committed Feb 20, 2024
1 parent fe06f91 commit 2cd5651
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 73 deletions.
14 changes: 13 additions & 1 deletion BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library")
load("@rules_java//java:defs.bzl", "java_proto_library")
load("@rules_java//java:defs.bzl", "java_proto_library", "java_test")
load("@rules_kotlin//kotlin:jvm.bzl", "kt_jvm_library", "kt_jvm_test")
load("@rules_proto//proto:defs.bzl", "proto_library")
load("//tools/project:build_defs.bzl", "project")
Expand Down Expand Up @@ -96,3 +96,15 @@ kt_jvm_test(
"@maven//:org_jetbrains_kotlinx_kotlinx_coroutines_core",
],
)

java_test(
name = "ToxCoreJavaTest",
size = "small",
srcs = ["src/test/java/im/tox/tox4j/core/ToxCoreJavaTest.java"],
jvm_flags = ["-Djava.library.path=jvm-toxcore-c"],
test_class = "im.tox.tox4j.core.ToxCoreJavaTest",
deps = [
":jvm-toxcore-c",
"@maven//:junit_junit",
],
)
97 changes: 56 additions & 41 deletions src/main/java/im/tox/tox4j/core/ToxCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ interface ToxCore : Closeable {
*
* @return a byte array containing the serialised tox instance.
*/
val getSavedata: ByteArray
val savedata: ByteArray

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

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

/**
* Get the UDP port this instance is bound to.
*
* @return a port number between 1 and 65535.
*/
// @Throws(ToxGetPortException::class)
val getUdpPort: Port
val udpPort: Port
fun getUdpPort(): Int = udpPort.value.toInt()

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

/**
* Writes the temporary DHT public key of this instance to a byte array.
*
* This can be used in combination with an externally accessible IP address and the bound port
* (from [[getUdpPort]]}) to run a temporary bootstrap node.
* (from [[udpPort]]}) to run a temporary bootstrap node.
*
* Be aware that every time a new instance is created, the DHT public key changes, meaning this
* cannot be used to run a permanent bootstrap node.
*
* @return a byte array of size [[ToxCoreConstants.PublicKeySize]]
*/
val getDhtId: ToxPublicKey
val dhtId: ToxPublicKey
fun getDhtId(): ByteArray = dhtId.value

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

/**
* Copy the Tox Secret Key from the Tox object.
*
* @return a byte array of size [[ToxCoreConstants.SecretKeySize]]
*/
val getSecretKey: ToxSecretKey
val secretKey: ToxSecretKey
fun getSecretKey(): ByteArray = secretKey.value

/**
* Set the 4-byte nospam part of the address.
* The 4-byte nospam part of the address.
*
* Setting the nospam makes it impossible for others to send us friend requests that contained the
* old nospam number.
*
* @param nospam the new nospam number.
*/
fun setNospam(nospam: Int): Unit

/** Get our current nospam number. */
val getNospam: Int
var nospam: Int

/**
* Get our current tox address to give to friends.
Expand All @@ -175,43 +179,33 @@ interface ToxCore : Closeable {
*
* @return a byte array of size [[ToxCoreConstants.AddressSize]]
*/
val getAddress: ToxFriendAddress
val address: ToxFriendAddress
fun getAddress(): ByteArray = address.value

/**
* Set the nickname for the Tox client.
* The nickname for the Tox client.
*
* Cannot be longer than [[ToxCoreConstants.MaxNameLength]] bytes. Can be empty (zero-length).
*
* @param name A byte array containing the new nickname..
*/
// @Throws(ToxSetInfoException::class)
fun setName(name: ToxNickname): Unit

/** Get our own nickname. */
val getName: ToxNickname
var name: ToxNickname
fun getName(): ByteArray = name.value
fun setName(value: ByteArray) { name = ToxNickname(value) }

/**
* Set our status message.
* Our status message.
*
* Cannot be longer than [[ToxCoreConstants.MaxStatusMessageLength]] bytes.
*
* @param message the status message to set.
*/
// @Throws(ToxSetInfoException::class)
fun setStatusMessage(message: ToxStatusMessage): Unit

/** Gets our own status message. May be null if the status message was empty. */
val getStatusMessage: ToxStatusMessage
var statusMessage: ToxStatusMessage
fun getStatusMessage(): ByteArray = statusMessage.value
fun setStatusMessage(value: ByteArray) { statusMessage = ToxStatusMessage(value) }

/**
* Set our status.
*
* @param status status to set.
* Our status.
*/
fun setStatus(status: ToxUserStatus): Unit

/** Get our status. */
val getStatus: ToxUserStatus
var status: ToxUserStatus

/**
* Add a friend to the friend list and send a friend request.
Expand Down Expand Up @@ -240,6 +234,8 @@ interface ToxCore : Closeable {
*/
// @Throws(ToxFriendAddException::class, IllegalArgumentException::class)
fun addFriend(address: ToxFriendAddress, message: ToxFriendRequestMessage): ToxFriendNumber
fun addFriend(address: ByteArray, message: ByteArray): Int =
addFriend(ToxFriendAddress(address), ToxFriendRequestMessage(message)).value

/**
* Add a friend without sending a friend request.
Expand All @@ -257,6 +253,8 @@ interface ToxCore : Closeable {
*/
// @Throws(ToxFriendAddException::class, IllegalArgumentException::class)
fun addFriendNorequest(publicKey: ToxPublicKey): ToxFriendNumber
fun addFriendNorequest(publicKey: ByteArray): Int =
addFriendNorequest(ToxPublicKey(publicKey)).value

/**
* Remove a friend from the friend list.
Expand All @@ -268,6 +266,8 @@ interface ToxCore : Closeable {
*/
// @Throws(ToxFriendDeleteException::class)
fun deleteFriend(friendNumber: ToxFriendNumber): Unit
fun deleteFriend(friendNumber: Int): Unit =
deleteFriend(ToxFriendNumber(friendNumber))

/**
* Gets the friend number for the specified Public Key.
Expand All @@ -277,6 +277,8 @@ interface ToxCore : Closeable {
*/
// @Throws(ToxFriendByPublicKeyException::class)
fun friendByPublicKey(publicKey: ToxPublicKey): ToxFriendNumber
fun friendByPublicKey(publicKey: ByteArray): Int =
friendByPublicKey(ToxPublicKey(publicKey)).value

/**
* Gets the Public Key for the specified friend number.
Expand All @@ -286,6 +288,8 @@ interface ToxCore : Closeable {
*/
// @Throws(ToxFriendGetPublicKeyException::class)
fun getFriendPublicKey(friendNumber: ToxFriendNumber): ToxPublicKey
fun getFriendPublicKey(friendNumber: Int): ByteArray =
getFriendPublicKey(ToxFriendNumber(friendNumber)).value

/**
* Checks whether a friend with the specified friend number exists.
Expand All @@ -298,6 +302,8 @@ interface ToxCore : Closeable {
* @return true if such a friend exists.
*/
fun friendExists(friendNumber: ToxFriendNumber): Boolean
fun friendExists(friendNumber: Int): Boolean =
friendExists(ToxFriendNumber(friendNumber))

/**
* Get an array of currently valid friend numbers.
Expand All @@ -308,7 +314,7 @@ interface ToxCore : Closeable {
* @return an array containing the currently valid friend numbers, the empty int array if there
* are no friends.
*/
val getFriendList: IntArray
val friendList: IntArray

/**
* Get an array of [[ToxFriendNumber]] objects with the same values as [[getFriendList]].
Expand All @@ -317,8 +323,8 @@ interface ToxCore : Closeable {
*
* @return [[getFriendList]] mapped to [[ToxFriendNumber]].
*/
val getFriendNumbers: List<ToxFriendNumber>
get() = getFriendList.map { ToxFriendNumber(it) }
val friendNumbers: List<ToxFriendNumber>
get() = friendList.map { ToxFriendNumber(it) }

/**
* Tell friend number whether or not we are currently typing.
Expand All @@ -330,6 +336,8 @@ interface ToxCore : Closeable {
*/
// @Throws(ToxSetTypingException::class)
fun setTyping(friendNumber: ToxFriendNumber, typing: Boolean): Unit
fun setTyping(friendNumber: Int, typing: Boolean): Unit =
setTyping(ToxFriendNumber(friendNumber), typing)

/**
* Send a text chat message to an online friend.
Expand All @@ -347,7 +355,7 @@ interface ToxCore : Closeable {
* incremented by 1 each time a message is sent. If [[Int.MaxValue]] messages were sent, the next
* message ID is [[Int.MinValue]].
*
* Message IDs are not stored in the array returned by [[getSavedata]].
* Message IDs are not stored in the array returned by [[savedata]].
*
* @param friendNumber The friend number of the friend to send the message to.
* @param messageType Message type (normal, action, ...).
Expand All @@ -363,6 +371,13 @@ interface ToxCore : Closeable {
timeDelta: Int,
message: ToxFriendMessage
): Int
fun friendSendMessage(
friendNumber: Int,
messageType: ToxMessageType,
timeDelta: Int,
message: ByteArray
): Int =
friendSendMessage(ToxFriendNumber(friendNumber), messageType, timeDelta, ToxFriendMessage(message))

/**
* Sends a file control command to a friend for a given file transfer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ interface FriendConnectionStatusCallback<ToxCoreState> {
connectionStatus: ToxConnection,
state: ToxCoreState
): ToxCoreState = state

fun friendConnectionStatus(
friendNumber: Int,
connectionStatus: ToxConnection,
state: ToxCoreState
): ToxCoreState = friendConnectionStatus(ToxFriendNumber(friendNumber), connectionStatus, state)
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
package im.tox.tox4j.core.callbacks

class ToxCoreEventAdapter<ToxCoreState> : ToxCoreEventListener<ToxCoreState>
abstract class ToxCoreEventAdapter<ToxCoreState> : ToxCoreEventListener<ToxCoreState>
6 changes: 5 additions & 1 deletion src/main/java/im/tox/tox4j/core/options/ToxOptions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ final data class ToxOptions(
val tcpPort: UShort = ToxCoreConstants.DefaultTcpPort,
val saveData: SaveDataOptions.Type = SaveDataOptions.None,
val fatalErrors: Boolean = true,
)
) {
companion object {
val defaultInstance = ToxOptions()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ object ToxCoreEventDispatch {
state,
{ next, ev ->
handler.friendConnectionStatus(
ToxFriendNumber(ev.getFriendNumber()), convert(ev.getConnectionStatus()), next)
ev.getFriendNumber(), convert(ev.getConnectionStatus()), next)
})

private fun <S> dispatchFriendTyping(
Expand Down
Loading

0 comments on commit 2cd5651

Please sign in to comment.