use alloc::{string::String, vec::Vec};
use core::{fmt, hash::{Hash, Hasher}};
#[cfg(feature = "serde")]
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AgentId(pub u64);
impl AgentId {
pub fn new() -> Self {
static mut COUNTER: u64 = 0;
unsafe {
COUNTER += 1;
Self(COUNTER)
}
}
pub fn raw(&self) -> u64 {
self.0
}
}
impl fmt::Display for AgentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "agent-{}", self.0)
}
}
impl From<u64> for AgentId {
fn from(id: u64) -> Self {
Self(id)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct TaskId(pub u64);
impl TaskId {
pub fn new() -> Self {
static mut COUNTER: u64 = 0;
unsafe {
COUNTER += 1;
Self(COUNTER)
}
}
pub fn raw(&self) -> u64 {
self.0
}
}
impl fmt::Display for TaskId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "task-{}", self.0)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct RegionId(pub u64);
impl RegionId {
pub fn new() -> Self {
static mut COUNTER: u64 = 0;
unsafe {
COUNTER += 1;
Self(COUNTER)
}
}
pub fn raw(&self) -> u64 {
self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum SwarmTopology {
Centralized,
Mesh,
Hierarchical,
Ring,
Star,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Capability {
pub name: String,
pub version: u32,
pub resources: ResourceRequirements,
}
impl Capability {
pub fn new(name: String, version: u32) -> Self {
Self {
name,
version,
resources: ResourceRequirements::default(),
}
}
pub fn with_resources(mut self, resources: ResourceRequirements) -> Self {
self.resources = resources;
self
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct ResourceRequirements {
pub memory: u64,
pub cpu_cycles: u64,
pub bandwidth: u64,
}
impl Default for ResourceRequirements {
fn default() -> Self {
Self {
memory: 1024,
cpu_cycles: 1000,
bandwidth: 0,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Message {
pub from: AgentId,
pub to: AgentId,
pub msg_type: MessageType,
pub payload: MessagePayload,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum MessageType {
TaskAssignment,
TaskComplete,
StatusUpdate,
ResourceRequest,
Coordination,
Heartbeat,
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MessagePayload {
pub data: Vec<u8>,
pub metadata: BTreeMap<String, String>,
}
impl MessagePayload {
pub fn empty() -> Self {
Self {
data: Vec::new(),
metadata: BTreeMap::new(),
}
}
pub fn with_data(data: Vec<u8>) -> Self {
Self {
data,
metadata: BTreeMap::new(),
}
}
pub fn with_metadata(mut self, key: String, value: String) -> Self {
self.metadata.insert(key, value);
self
}
}
use alloc::collections::BTreeMap;
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SwarmConfig {
pub name: String,
pub max_agents: usize,
pub topology: SwarmTopology,
pub fault_tolerance: bool,
pub heartbeat_interval: u64,
}
impl Default for SwarmConfig {
fn default() -> Self {
Self {
name: String::from("default-swarm"),
max_agents: 256,
topology: SwarmTopology::Mesh,
fault_tolerance: true,
heartbeat_interval: 1000,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SchedulerConfig {
pub max_concurrent_tasks: usize,
pub task_timeout: u64,
pub priority_scheduling: bool,
pub load_balancing: bool,
}
impl Default for SchedulerConfig {
fn default() -> Self {
Self {
max_concurrent_tasks: 4,
task_timeout: 60000,
priority_scheduling: true,
load_balancing: true,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct MemoryConfig {
pub pool_size: usize,
pub min_allocation: usize,
pub max_allocation: usize,
pub compression: bool,
}
impl Default for MemoryConfig {
fn default() -> Self {
Self {
pool_size: 1024 * 1024, min_allocation: 64,
max_allocation: 65536,
compression: false,
}
}
}