dimas 0.5.1

dimas - a framework for Distributed Multi Agent Systems
Documentation
//! `DiMAS` queryable example
//! Copyright © 2024 Stephan Kunz

use dimas::prelude::*;

#[derive(Debug)]
struct AgentProps {
	counter: u128,
}

async fn queryable(ctx: Context<AgentProps>, request: QueryMsg) -> Result<()> {
	let received: u128 = request.decode()?;
	let value = ctx.read()?.counter;
	let query = request.key_expr();
	println!(
		"Received query for {} with {}, responding with {}",
		&query, &received, &value
	);
	request.reply(value)?;

	ctx.write()?.counter += 1;
	Ok(())
}

#[dimas::main]
async fn main() -> Result<()> {
	// create & initialize agents properties
	let properties = AgentProps { counter: 0 };

	// create an agent with the properties and the prefix 'examples'
	let mut agent = Agent::new(properties)
		.prefix("examples")
		.name("queryable")
		.config(&Config::default())?;

	// add a queryable
	agent
		.queryable()
		.topic("query")
		.callback(queryable)
		.add()?;

	// activate liveliness
	agent.liveliness(true);
	// run agent
	agent.start().await?;

	Ok(())
}