-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
-
143
-
144
-
145
-
146
-
147
-
148
-
149
-
150
-
151
-
152
-
153
-
154
-
155
-
156
-
157
-
158
-
159
-
160
-
161
-
162
-
163
-
164
-
165
-
166
-
167
-
168
-
169
-
170
-
171
-
172
-
173
-
174
-
175
-
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
-
184
-
185
-
186
-
187
-
188
-
189
-
190
-
191
-
192
-
193
-
194
-
195
-
196
-
197
-
198
-
199
-
200
-
201
-
202
-
203
-
204
-
205
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
const std = @import("std");
const sood = @import("sood");
const Self = @This();
const discovery = @import("../roon/discovery.zig");
const callback = @import("./callback.zig");
pub const Entry = @import("./ServerSelector/Entry.zig");
const OnChange = callback.Callback(struct {});
allocator: std.mem.Allocator,
on_change_callbacks: OnChange.Store,
capi_lock: std.Thread.Mutex,
has_loaded_once: bool = false,
/// Acts as backing buffer for `CApi.entries`. For this reason, `CApi` manages this
/// struct's memory.
scan_result: ?std.StringHashMap(Entry.CApi) = null,
pub fn init(allocator: std.mem.Allocator) Self {
return .{
.allocator = allocator,
.on_change_callbacks = OnChange.Store.init(allocator),
.capi_lock = std.Thread.Mutex{},
};
}
pub fn deinit(self: *const Self) void {
self.on_change_callbacks.deinit();
}
pub const CApi = extern struct {
internal: *Self,
state: State = .not_loaded,
entries: [*]*Entry.CApi,
entries_len: usize,
pub const State = enum(c_int) {
not_loaded = 0,
loading = 1,
loaded = 2,
refreshing = 3,
err_unexpected = 4,
err_network_unavailable = 5,
err_socket_permission = 6,
err_out_of_memory = 7,
err_socket = 8,
err_thread_spawn = 9,
};
pub fn init(allocator: std.mem.Allocator) std.mem.Allocator.Error!CApi {
const internal = try allocator.create(Self);
errdefer allocator.destroy(internal);
internal.* = Self.init(allocator);
errdefer internal.deinit();
return .{
.internal = internal,
.entries = undefined,
.entries_len = 0,
};
}
pub fn deinit(self: *CApi, allocator: std.mem.Allocator) void {
self.deinitEntries();
self.internal.deinit();
allocator.destroy(self.internal);
self.internal = undefined;
}
inline fn deinitEntries(self: *CApi) void {
const entries = self.entries[0..self.entries_len];
for (entries) |entry| {
entry.deinit();
}
self.internal.allocator.free(entries);
self.entries_len = 0;
if (self.internal.scan_result) |*old_map| {
old_map.deinit();
}
}
pub fn reset(capi_ptr: ?*CApi) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.capi_lock.lock();
defer capi.internal.capi_lock.unlock();
capi.state = .not_loaded;
capi.deinitEntries();
capi.internal.has_loaded_once = false;
}
pub fn load(capi_ptr: ?*CApi) callconv(.C) void {
const capi = capi_ptr orelse return;
const thread = std.Thread.spawn(.{}, loadInternal, .{capi}) catch {
capi.internal.capi_lock.lock();
defer capi.internal.capi_lock.unlock();
defer capi.internal.on_change_callbacks.runAll(.{});
capi.state = .err_thread_spawn;
return;
};
thread.detach();
}
fn loadInternal(self: *CApi) void {
{
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.state = if (self.internal.has_loaded_once) .refreshing else .loading;
self.internal.on_change_callbacks.runAll(.{});
}
const entries, const map = scan(self.internal.allocator) catch |err| {
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.state = switch (err) {
error.NetworkUnavailable => State.err_network_unavailable,
error.SocketPermissionDenied => State.err_socket_permission,
error.UDPRecvError,
error.UDPSendError,
error.SocketCreationError,
=> State.err_socket,
error.OutOfMemory => State.err_out_of_memory,
else => State.err_unexpected,
};
self.internal.on_change_callbacks.runAll(.{});
return;
};
self.internal.capi_lock.lock();
defer self.internal.capi_lock.unlock();
self.deinitEntries();
self.entries = entries.ptr;
self.entries_len = entries.len;
self.state = .loaded;
self.internal.has_loaded_once = true;
self.internal.scan_result = map;
self.internal.on_change_callbacks.runAll(.{});
}
/// Caller owns returned slice.
fn scan(allocator: std.mem.Allocator) discovery.Error(Entry.CApi)!struct { []*Entry.CApi, std.StringHashMap(Entry.CApi) } {
var servers = try discovery.scan(Entry.CApi, allocator, .{});
errdefer servers.deinit();
const slice = try allocator.alloc(*Entry.CApi, servers.count());
var i: usize = 0;
var iter = servers.valueIterator();
while (iter.next()) |server| {
std.log.debug("Found server ({s})", .{server.*.getName()});
slice[i] = server;
i += 1;
}
return .{ slice, servers };
}
pub fn lock(capi_ptr: ?*CApi) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.capi_lock.lock();
}
pub fn unlock(capi_ptr: ?*CApi) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.capi_lock.unlock();
}
pub fn onChange(capi_ptr: ?*CApi, cb: OnChange.Fn, user_data: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
// TODO: Notify error to caller
capi.internal.on_change_callbacks.add(OnChange.init(cb, user_data)) catch |err| {
std.log.err("Failed to register callback: {s}", .{@errorName(err)});
};
}
pub fn onChangeDisarm(capi_ptr: ?*CApi, cb: OnChange.Fn, userdata: callback.UserData) callconv(.C) void {
const capi = capi_ptr orelse return;
capi.internal.on_change_callbacks.remove(OnChange.init(cb, userdata));
}
};