Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Networked swift bindings #181

Open
wants to merge 28 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
15fe377
Move FFI callback to a global
za-creature Jan 6, 2025
93e60ad
Better mac build system
za-creature Jan 7, 2025
50044ab
Merge branch 'service' into darwin
za-creature Jan 14, 2025
a0be495
Expose LogLevel via FFI
za-creature Jan 14, 2025
70c7380
Move logInit to before Server.start
za-creature Jan 14, 2025
900150d
Remove old swift bindings
za-creature Jan 14, 2025
58d68cc
Add ouisync-service ffi bindings
za-creature Jan 14, 2025
21c95e4
Set explicit enum values
za-creature Jan 15, 2025
147958a
More explicit enum values
za-creature Jan 15, 2025
a3de4e7
Manually add missing enums
za-creature Jan 15, 2025
9ba2434
Remove no longer used channel errors
za-creature Jan 16, 2025
a181f85
Type-safe bindings
za-creature Jan 17, 2025
2e17b34
`swift build` compatibility
za-creature Jan 21, 2025
eee0b1d
Add trivial tests
za-creature Jan 21, 2025
7987aa4
Merge branch 'service' into darwin
za-creature Jan 21, 2025
1ec34c3
Add tests
za-creature Jan 24, 2025
e3e02cd
Merge branch 'develop' into darwin
za-creature Jan 24, 2025
3becae2
Finish tests
za-creature Jan 24, 2025
ee38525
Remove unsafe swift code
za-creature Jan 28, 2025
ba0d3fa
Include version hash in exported EntryType (now called StatEntry)
za-creature Jan 28, 2025
e0dca71
Add `root/target` to shared rust artifact pool on darwin
za-creature Jan 30, 2025
b4bddb6
Remove superflous logInit call
za-creature Jan 30, 2025
93ae842
Merge branch 'develop' into darwin
za-creature Feb 2, 2025
c1580d5
fix `cargo test` on darwin
za-creature Jan 30, 2025
69d332e
Readability
za-creature Jan 31, 2025
7fe9fc0
Update gradle
za-creature Feb 1, 2025
32448a5
Rename `listenerAddrs` property to mirror upstream
za-creature Feb 2, 2025
ea65398
Disable cli tests on macos without breaking other platforms
za-creature Feb 2, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions bindings/dart/lib/bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,35 @@ import 'dart:ffi';
import 'dart:io';

import 'package:flutter/foundation.dart';
import 'package:ouisync/exception.dart';
import 'package:path/path.dart';

export 'bindings.g.dart';

sealed class EntryType {
static EntryType decode(Object foo) {
if (foo is! Map || foo.length != 1) { throw InvalidData("Not a one entry map"); }
return switch (foo.entries.first.key) {
"File" => EntryType_File(foo.entries.first.value),
"Directory" => EntryType_Directory(foo.entries.first.value),
final key => throw InvalidData("Unknown EntryType: $key")
};
}
}

// ignore: camel_case_types
class EntryType_File extends EntryType {
final Uint8List version;
EntryType_File(this.version);
}

// ignore: camel_case_types
class EntryType_Directory extends EntryType {
final Uint8List version;
EntryType_Directory(this.version);
}


/// Callback for `start_service` and `stop_service`.
typedef StatusCallback = Void Function(Pointer<Void>, Uint16);

Expand Down
22 changes: 0 additions & 22 deletions bindings/dart/lib/bindings.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,6 @@ enum AccessMode {

}

enum EntryType {
file,
directory,
;

static EntryType decode(int n) {
switch (n) {
case 1: return EntryType.file;
case 2: return EntryType.directory;
default: throw ArgumentError('invalid value: $n');
}
}

int encode() {
switch (this) {
case EntryType.file: return 1;
case EntryType.directory: return 2;
}
}

}

enum ErrorCode {
ok,
permissionDenied,
Expand Down
8 changes: 5 additions & 3 deletions bindings/dart/lib/ouisync.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export 'bindings.dart'
show
AccessMode,
EntryType,
EntryType_File,
EntryType_Directory,
ErrorCode,
LogLevel,
NetworkEvent,
Expand Down Expand Up @@ -415,7 +417,7 @@ class Repository {
/// Returns the type (file, directory, ..) of the entry at [path]. Returns `null` if the entry
/// doesn't exists.
Future<EntryType?> entryType(String path) async {
final raw = await _client.invoke<int?>('repository_entry_type', {
final raw = await _client.invoke<Object?>('repository_entry_type', {
'repository': _handle,
'path': path,
});
Expand Down Expand Up @@ -641,13 +643,13 @@ class DirEntry {
static DirEntry decode(Object? raw) {
final map = raw as List<Object?>;
final name = map[0] as String;
final type = map[1] as int;
final type = map[1] as Object;

return DirEntry(name, EntryType.decode(type));
}

@override
String toString() => '$name (${entryType.name})';
String toString() => '$name ($entryType)';
}

/// A reference to a directory (folder) in a [Repository].
Expand Down
2 changes: 1 addition & 1 deletion bindings/dart/test/ouisync_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ void main() {
expect(await repo.entryType('dir'), isNull);

await Directory.create(repo, 'dir');
expect(await repo.entryType('dir'), equals(EntryType.directory));
expect(await repo.entryType('dir'), isA<EntryType_Directory>());

await Directory.remove(repo, 'dir');
expect(await repo.entryType('dir'), isNull);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,10 @@ fun FolderScreen(
path = path,
onEntryClicked = { entry ->
when (entry.entryType) {
EntryType.FILE -> {
is EntryType.File -> {
navController.navigate(FileRoute(repositoryName, "$path/${entry.name}"))
}
EntryType.DIRECTORY -> {
is EntryType.Directory -> {
navController.navigate(FolderRoute(repositoryName, "$path/${entry.name}"))
}
}
Expand Down Expand Up @@ -324,8 +324,8 @@ fun FolderDetail(
modifier = Modifier.padding(PADDING).fillMaxWidth(),
) {
when (entry.entryType) {
EntryType.FILE -> Icon(Icons.Default.Description, "File")
EntryType.DIRECTORY -> Icon(Icons.Default.Folder, "Folder")
is EntryType.File -> Icon(Icons.Default.Description, "File")
is EntryType.Directory -> Icon(Icons.Default.Folder, "Folder")
}

Text(
Expand Down
Binary file modified bindings/kotlin/gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion bindings/kotlin/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.12.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
21 changes: 12 additions & 9 deletions bindings/kotlin/gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down Expand Up @@ -145,15 +148,15 @@ if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
case $MAX_FD in #(
max*)
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
MAX_FD=$( ulimit -H -n ) ||
warn "Could not query maximum file descriptor limit"
esac
case $MAX_FD in #(
'' | soft) :;; #(
*)
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
# shellcheck disable=SC3045
# shellcheck disable=SC2039,SC3045
ulimit -n "$MAX_FD" ||
warn "Could not set maximum file descriptor limit to $MAX_FD"
esac
Expand Down Expand Up @@ -202,11 +205,11 @@ fi
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'

# Collect all arguments for the java command;
# * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of
# shell script including quotes and variable substitutions, so put them in
# double quotes to make sure that they get re-expanded; and
# * put everything else in single quotes, so that it's not re-expanded.
# Collect all arguments for the java command:
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
# and any embedded shellness will be escaped.
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
# treated as '${Hostname}' itself on the command line.

set -- \
"-Dorg.gradle.appname=$APP_BASE_NAME" \
Expand Down
22 changes: 12 additions & 10 deletions bindings/kotlin/gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package org.equalitie.ouisync.lib
* A directory entry
*
* @property name name of the entry.
* @property entryType type of the entry (i.e., file or directory).
* @property entryType type of the entry (i.e., file or directory) and version.
*/
data class DirectoryEntry(val name: String, val entryType: EntryType)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ sealed class PeerState {
class Active(val runtimeId: String) : PeerState()
}

sealed class EntryType {
class File(var version: ByteArray) : EntryType()
class Directory(var version: ByteArray) : EntryType()
}

data class Progress(val value: Long, val total: Long)

internal sealed interface Response {
Expand Down Expand Up @@ -73,7 +78,7 @@ internal sealed interface Response {
"bytes" -> unpacker.unpackByteArray()
"directory" -> unpacker.unpackDirectory()
// Duration(Duration),
"entry_type" -> EntryType.decode(unpacker.unpackByte())
"entry_type" -> unpacker.unpackEntryType()
"file", "repository", "u64" -> unpacker.unpackLong()
"network_event" -> NetworkEvent.decode(unpacker.unpackByte())
// NetworkStats(Stats),
Expand Down Expand Up @@ -217,13 +222,36 @@ private fun MessageUnpacker.unpackDirectory(): Directory {
return Directory(entries)
}

internal fun MessageUnpacker.unpackEntryType(): EntryType {
val type = getNextFormat().getValueType()
return when (type) {
ValueType.NIL -> {
unpackNil()
throw IllegalArgumentException() // this is awkward but that's how bindgen does it too
}
ValueType.MAP -> {
val size = unpackMapHeader()
if (size != 1) {
throw Error.InvalidData("invalid EntryType payload: expected map of size 1, was $size")
}
val key = unpackString()
when (key) {
"File" -> EntryType.File(unpackByteArray())
"Directory" -> EntryType.Directory(unpackByteArray())
else -> throw Error.InvalidData("invalid EntryType case: '$key'")
}
}
else -> throw Error.InvalidData("invalid EntryType payload: expected NIL or MAP, was $type")
}
}

internal fun MessageUnpacker.unpackDirectoryEntry(): DirectoryEntry {
if (unpackArrayHeader() < 2) {
throw Error.InvalidData("invalid DirectoryEntry: too few elements")
}

val name = unpackString()
val entryType = EntryType.decode(unpackByte())
val entryType = unpackEntryType()

return DirectoryEntry(name, entryType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ class RepositoryTest {
@Test
fun entryType() = runTest {
withRepo {
assertEquals(EntryType.DIRECTORY, it.entryType("/"))
assertTrue(it.entryType("/") is EntryType.Directory)
assertNull(it.entryType("missing.txt"))
}
}
Expand All @@ -171,7 +171,7 @@ class RepositoryTest {
File.create(repo, "foo.txt").close()

repo.moveEntry("foo.txt", "bar.txt")
assertEquals(EntryType.FILE, repo.entryType("bar.txt"))
assertTrue(repo.entryType("bar.txt") is EntryType.File)
assertNull(repo.entryType("foo.txt"))
}
}
Expand Down Expand Up @@ -204,7 +204,7 @@ class RepositoryTest {

val file = File.create(repo, name)
file.close()
assertEquals(EntryType.FILE, repo.entryType(name))
assertTrue(repo.entryType(name) is EntryType.File)

File.remove(repo, name)
assertNull(repo.entryType(name))
Expand Down Expand Up @@ -259,7 +259,7 @@ class RepositoryTest {
assertNull(repo.entryType(dirName))

Directory.create(repo, dirName)
assertEquals(EntryType.DIRECTORY, repo.entryType(dirName))
assertTrue(repo.entryType(dirName) is EntryType.Directory)

val dir0 = Directory.read(repo, dirName)
assertEquals(0, dir0.size)
Expand All @@ -269,7 +269,7 @@ class RepositoryTest {
val dir1 = Directory.read(repo, dirName)
assertEquals(1, dir1.size)
assertEquals(fileName, dir1.elementAt(0).name)
assertEquals(EntryType.FILE, dir1.elementAt(0).entryType)
assertTrue(dir1.elementAt(0).entryType is EntryType.File)

Directory.remove(repo, dirName, recursive = true)
assertNull(repo.entryType(dirName))
Expand Down
9 changes: 9 additions & 0 deletions bindings/swift/Ouisync/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/OuisyncService.xcframework
/config.sh
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm
.netrc
14 changes: 14 additions & 0 deletions bindings/swift/Ouisync/Package.resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"pins" : [
{
"identity" : "messagepack.swift",
"kind" : "remoteSourceControl",
"location" : "https://github.com/a2/MessagePack.swift.git",
"state" : {
"revision" : "27b35fd49e92fcae395bf8ccb233499d89cc7890",
"version" : "4.0.0"
}
}
],
"version" : 2
}
Loading
Loading