ab ovo
This commit is contained in:
158
src/p2p/event_handling.zig
Normal file
158
src/p2p/event_handling.zig
Normal file
@@ -0,0 +1,158 @@
|
||||
//! P2P Event Handling Implementation
|
||||
//!
|
||||
//! This module contains implementations for P2P event processing methods
|
||||
//! as free functions that take `*P2P` as the first parameter.
|
||||
//!
|
||||
//! Functions included:
|
||||
//! - registerCallbackImpl: Register event callback
|
||||
//! - unregisterCallbackImpl: Unregister event callback
|
||||
//! - getEventCountImpl: Get event queue size
|
||||
//! - getNextEventImpl: Get next event from queue
|
||||
//! - processNextEventImpl: Process next event
|
||||
//! - processAllEventsImpl: Process all pending events
|
||||
//! - getStatsImpl: Get P2P statistics
|
||||
//! - isOperationalImpl: Check if operational
|
||||
//! - getStateImpl: Get current state
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// Core components
|
||||
const core = @import("core");
|
||||
const Error = core.errors.Error;
|
||||
|
||||
// Import P2P type
|
||||
const P2P = @import("p2p.zig").P2P;
|
||||
|
||||
// Import sibling types
|
||||
const events = @import("events.zig");
|
||||
const P2PEvent = events.P2PEvent;
|
||||
const EventCallback = events.EventCallback;
|
||||
const UserContext = events.UserContext;
|
||||
|
||||
const stats = @import("stats.zig");
|
||||
const P2PStats = stats.P2PStats;
|
||||
|
||||
const types = @import("types.zig");
|
||||
const P2PState = types.P2PState;
|
||||
|
||||
/// Register an event callback
|
||||
pub fn registerCallbackImpl(p2p: *P2P, callback: EventCallback, context_data: ?*anyopaque) Error!void {
|
||||
const user_context = UserContext{
|
||||
.callback = callback,
|
||||
.context_data = context_data,
|
||||
};
|
||||
|
||||
p2p.callbacks_mutex.lock();
|
||||
defer p2p.callbacks_mutex.unlock();
|
||||
p2p.user_contexts.append(p2p.allocator, user_context) catch return Error.OutOfMemory;
|
||||
}
|
||||
|
||||
/// Unregister an event callback
|
||||
pub fn unregisterCallbackImpl(p2p: *P2P, callback: EventCallback) Error!void {
|
||||
p2p.callbacks_mutex.lock();
|
||||
defer p2p.callbacks_mutex.unlock();
|
||||
|
||||
var i: usize = 0;
|
||||
while (i < p2p.user_contexts.items.len) {
|
||||
if (p2p.user_contexts.items[i].callback == callback) {
|
||||
_ = p2p.user_contexts.orderedRemove(i);
|
||||
} else {
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the number of events in the queue
|
||||
pub fn getEventCountImpl(p2p: *P2P) usize {
|
||||
p2p.event_mutex.lock();
|
||||
defer p2p.event_mutex.unlock();
|
||||
return p2p.event_queue.items.len;
|
||||
}
|
||||
|
||||
/// Get the next event from the queue
|
||||
pub fn getNextEventImpl(p2p: *P2P) ?P2PEvent {
|
||||
p2p.event_mutex.lock();
|
||||
defer p2p.event_mutex.unlock();
|
||||
|
||||
if (p2p.event_queue.items.len == 0) return null;
|
||||
return p2p.event_queue.orderedRemove(0);
|
||||
}
|
||||
|
||||
/// Process the next event
|
||||
pub fn processNextEventImpl(p2p: *P2P) bool {
|
||||
var evt: P2PEvent = undefined;
|
||||
{
|
||||
p2p.event_mutex.lock();
|
||||
defer p2p.event_mutex.unlock();
|
||||
|
||||
if (p2p.event_queue.items.len == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
evt = p2p.event_queue.orderedRemove(0);
|
||||
}
|
||||
|
||||
defer evt.deinit();
|
||||
|
||||
var callbacks_copy: [64]UserContext = undefined;
|
||||
var callback_count: usize = 0;
|
||||
{
|
||||
p2p.callbacks_mutex.lock();
|
||||
defer p2p.callbacks_mutex.unlock();
|
||||
callback_count = @min(p2p.user_contexts.items.len, 64);
|
||||
for (p2p.user_contexts.items[0..callback_count], 0..) |context, i| {
|
||||
callbacks_copy[i] = context;
|
||||
}
|
||||
}
|
||||
|
||||
for (callbacks_copy[0..callback_count]) |context| {
|
||||
context.callback(evt);
|
||||
}
|
||||
_ = p2p.stats.events_processed.fetchAdd(1, .monotonic);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// Process all pending events
|
||||
pub fn processAllEventsImpl(p2p: *P2P) usize {
|
||||
var processed: usize = 0;
|
||||
|
||||
while (processNextEventImpl(p2p)) {
|
||||
processed += 1;
|
||||
}
|
||||
|
||||
return processed;
|
||||
}
|
||||
|
||||
/// Get current library statistics
|
||||
pub fn getStatsImpl(p2p: *P2P) P2PStats {
|
||||
if (p2p.file_manager) |manager| {
|
||||
const file_stats = manager.getStats();
|
||||
const max_u32: usize = std.math.maxInt(u32);
|
||||
p2p.stats.active_downloads.store(@intCast(@min(file_stats.downloading_sessions, max_u32)), .monotonic);
|
||||
p2p.stats.active_uploads.store(@intCast(@min(file_stats.uploading_sessions, max_u32)), .monotonic);
|
||||
p2p.stats.total_downloaded.store(file_stats.total_downloaded, .monotonic);
|
||||
p2p.stats.total_uploaded.store(file_stats.total_uploaded, .monotonic);
|
||||
}
|
||||
|
||||
p2p.stats.event_queue_size = getEventCountImpl(p2p);
|
||||
|
||||
p2p.state_mutex.lock();
|
||||
p2p.stats.state = p2p.state;
|
||||
p2p.state_mutex.unlock();
|
||||
|
||||
return p2p.stats;
|
||||
}
|
||||
|
||||
/// Check if the library is operational
|
||||
pub fn isOperationalImpl(p2p: *P2P) bool {
|
||||
p2p.state_mutex.lock();
|
||||
defer p2p.state_mutex.unlock();
|
||||
return p2p.running.load(.acquire) and p2p.state == .Running;
|
||||
}
|
||||
|
||||
/// Get the library's current state
|
||||
pub fn getStateImpl(p2p: *P2P) P2PState {
|
||||
p2p.state_mutex.lock();
|
||||
defer p2p.state_mutex.unlock();
|
||||
return p2p.state;
|
||||
}
|
||||
Reference in New Issue
Block a user