Skip to content

Commit

Permalink
Add location-set as a generic type to Transport
Browse files Browse the repository at this point in the history
  • Loading branch information
ihaveint committed Sep 18, 2023
1 parent 07730da commit 44c3478
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 101 deletions.
10 changes: 5 additions & 5 deletions chorus_lib/examples/bookseller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::thread;

use chrono::NaiveDate;

use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation};
use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, Projector};
use chorus_lib::transport::local::LocalTransport;
use chorus_lib::{projector, LocationSet};
use chorus_lib::LocationSet;

fn get_book(title: &str) -> Option<(i32, NaiveDate)> {
match title.trim() {
Expand Down Expand Up @@ -73,9 +73,9 @@ impl Choreography for BooksellerChoreography {
}

fn main() {
let transport = LocalTransport::from(&[Seller::name(), Buyer::name()]);
let seller_projector = projector!(LocationSet!(Buyer, Seller), Seller, transport.clone());
let buyer_projector = projector!(LocationSet!(Buyer, Seller), Buyer, transport.clone());
let transport = LocalTransport::<LocationSet!(Seller, Buyer)>::new();
let seller_projector = Projector::new(Seller, transport.clone());
let buyer_projector = Projector::new(Buyer, transport.clone());

let mut handles: Vec<thread::JoinHandle<()>> = Vec::new();
handles.push(thread::spawn(move || {
Expand Down
24 changes: 6 additions & 18 deletions chorus_lib/examples/bookseller2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ extern crate chorus_lib;
use std::thread;
use std::{collections::HashMap, sync::Arc};

use chorus_lib::LocationSet;
use chorus_lib::{
core::{ChoreoOp, Choreography, ChoreographyLocation, Located},
core::{ChoreoOp, Choreography, ChoreographyLocation, Located, Projector},
transport::local::LocalTransport,
};
use chorus_lib::{projector, LocationSet};
use chrono::NaiveDate;

#[derive(ChoreographyLocation)]
Expand Down Expand Up @@ -142,22 +142,10 @@ fn main() {
i
};

let transport = LocalTransport::from(&[Seller::name(), Buyer1::name(), Buyer2::name()]);
let seller_projector = Arc::new(projector!(
LocationSet!(Seller, Buyer1, Buyer2),
Seller,
transport.clone()
));
let buyer1_projector = Arc::new(projector!(
LocationSet!(Seller, Buyer1, Buyer2),
Buyer1,
transport.clone()
));
let buyer2_projector = Arc::new(projector!(
LocationSet!(Seller, Buyer1, Buyer2),
Buyer2,
transport.clone()
));
let transport = LocalTransport::<LocationSet!(Seller, Buyer1, Buyer2)>::new();
let seller_projector = Arc::new(Projector::new(Seller, transport.clone()));
let buyer1_projector = Arc::new(Projector::new(Buyer1, transport.clone()));
let buyer2_projector = Arc::new(Projector::new(Buyer2, transport.clone()));

println!("Tries to buy HoTT with one buyer");
type OneBuyerBooksellerChoreography = BooksellerChoreography<OneBuyerDecider>;
Expand Down
10 changes: 5 additions & 5 deletions chorus_lib/examples/hello.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ extern crate chorus_lib;

use std::thread;

use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation};
use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, Projector};
use chorus_lib::transport::local::LocalTransport;
use chorus_lib::{projector, LocationSet};
use chorus_lib::LocationSet;

// --- Define two locations (Alice and Bob) ---

Expand Down Expand Up @@ -40,20 +40,20 @@ impl Choreography for HelloWorldChoreography {
fn main() {
let mut handles: Vec<thread::JoinHandle<()>> = Vec::new();
// Create a local transport
let transport = LocalTransport::from(&[Alice::name(), Bob::name()]);
let transport = LocalTransport::<LocationSet!(Alice, Bob)>::new();

// Run the choreography in two threads
{
let transport = transport.clone();
handles.push(thread::spawn(move || {
let p = projector!(LocationSet!(Alice, Bob), Alice, transport);
let p = Projector::new(Alice, transport);
p.epp_and_run(HelloWorldChoreography);
}));
}
{
let transport = transport.clone();
handles.push(thread::spawn(move || {
let p = projector!(LocationSet!(Alice, Bob), Bob, transport);
let p = Projector::new(Bob, transport);
p.epp_and_run(HelloWorldChoreography);
}));
}
Expand Down
12 changes: 7 additions & 5 deletions chorus_lib/examples/loc-poly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ extern crate chorus_lib;
use std::fmt::Debug;
use std::thread;

use chorus_lib::core::{ChoreoOp, Choreography, ChoreographyLocation, Located, Portable};
use chorus_lib::core::{
ChoreoOp, Choreography, ChoreographyLocation, Located, Portable, Projector,
};
use chorus_lib::transport::local::LocalTransport;
use chorus_lib::{projector, LocationSet};
use chorus_lib::LocationSet;

#[derive(ChoreographyLocation)]
struct Alice;
Expand Down Expand Up @@ -55,20 +57,20 @@ impl Choreography<Located<i32, Alice>> for MainChoreography {
}

fn main() {
let transport = LocalTransport::from(&[Alice::name(), Bob::name(), Carol::name()]);
let transport = LocalTransport::<LocationSet!(Alice, Bob, Carol)>::new();
let mut handles = vec![];
{
let transport = transport.clone();
handles.push(thread::spawn(|| {
let p = projector!(LocationSet!(Alice, Bob), Alice, transport);
let p = Projector::new(Alice, transport);
let v = p.epp_and_run(MainChoreography);
assert_eq!(p.unwrap(v), 110);
}));
}
{
let transport = transport.clone();
handles.push(thread::spawn(|| {
let p = projector!(LocationSet!(Alice, Bob), Bob, transport);
let p = Projector::new(Bob, transport);
p.epp_and_run(MainChoreography);
}));
}
Expand Down
44 changes: 25 additions & 19 deletions chorus_lib/examples/tic-tac-toe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
extern crate chorus_lib;

use chorus_lib::{
core::{ChoreoOp, Choreography, ChoreographyLocation, Deserialize, Located, Serialize},
projector,
core::{
ChoreoOp, Choreography, ChoreographyLocation, Deserialize, Located, Projector, Serialize,
},
http_config,
transport::http::HttpTransport,
LocationSet,
};
use clap::Parser;
use std::{collections::HashMap, io::Write};
use std::io::Write;
use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor};

#[derive(Serialize, Deserialize, Debug)]
Expand Down Expand Up @@ -292,28 +294,32 @@ fn main() {

match args.player {
'X' => {
let mut config = HashMap::new();
config.insert(PlayerX::name(), (args.hostname.as_str(), args.port));
config.insert(
PlayerO::name(),
(args.opponent_hostname.as_str(), args.opponent_port),
);
let transport = HttpTransport::new(PlayerX::name(), &config);
let projector = projector!(LocationSet!(PlayerX, PlayerO), PlayerX, transport);
// let mut config = HttpConfig::<LocationSet!(PlayerX, PlayerO)>::new();
// config.insert(PlayerX, (args.hostname.as_str(), args.port));
// config.insert(
// PlayerO,
// (args.opponent_hostname.as_str(), args.opponent_port),
// );
let config = http_config!(PlayerX: (args.hostname.as_str(), args.port),
PlayerO: (args.opponent_hostname.as_str(), args.opponent_port));
let transport = HttpTransport::new(PlayerX, &config);
let projector = Projector::new(PlayerX, transport);
projector.epp_and_run(TicTacToeChoreography {
brain_for_x: projector.local(brain),
brain_for_o: projector.remote(PlayerO),
});
}
'O' => {
let mut config = HashMap::new();
config.insert(PlayerO::name(), (args.hostname.as_str(), args.port));
config.insert(
PlayerX::name(),
(args.opponent_hostname.as_str(), args.opponent_port),
);
let transport = HttpTransport::new(PlayerO::name(), &config);
let projector = projector!(LocationSet!(PlayerX, PlayerO), PlayerO, transport);
// let mut config = HttpConfig::<LocationSet!(PlayerX, PlayerO)>::new();
// config.insert(PlayerO, (args.hostname.as_str(), args.port));
// config.insert(
// PlayerX,
// (args.opponent_hostname.as_str(), args.opponent_port),
// );
let config = http_config!(PlayerO: (args.hostname.as_str(), args.port),
PlayerX: (args.opponent_hostname.as_str(), args.opponent_port));
let transport = HttpTransport::new(PlayerO, &config);
let projector = Projector::new(PlayerO, transport);
projector.epp_and_run(TicTacToeChoreography {
brain_for_x: projector.remote(PlayerX),
brain_for_o: projector.local(brain),
Expand Down
33 changes: 17 additions & 16 deletions chorus_lib/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,11 @@ pub trait HList {
fn to_string_list() -> Vec<&'static str>;
}
/// end of HList
#[derive(Clone)]
pub struct HNil;
/// An element of HList
#[derive(Clone)]
pub struct HCons<Head, Tail>(Head, Tail);

impl HList for HNil {
Expand All @@ -125,7 +128,7 @@ where
macro_rules! LocationSet {
() => { $crate::core::HNil };
($head:ty $(,)*) => { $crate::core::HCons<$head, $crate::core::HNil> };
($head:ty, $($tail:tt)*) => { $crate::core::HCons<$head, LocationSet!($($tail)*)> };
($head:ty, $($tail:tt)*) => { $crate::core::HCons<$head, $crate::LocationSet!($($tail)*)> };
}

/// Marker
Expand Down Expand Up @@ -261,7 +264,7 @@ pub trait Choreography<R = ()> {
/// Provides methods to send and receive messages.
///
/// The trait provides methods to send and receive messages between locations. Implement this trait to define a custom transport.
pub trait Transport {
pub trait Transport<L> {
/// Returns a list of locations.
fn locations(&self) -> Vec<String>;
/// Sends a message from `from` to `to`.
Expand All @@ -271,7 +274,7 @@ pub trait Transport {
}

/// Provides a method to perform end-point projection.
pub struct Projector<LS: HList, L1: ChoreographyLocation, T: Transport, Index>
pub struct Projector<LS: HList, L1: ChoreographyLocation, T: Transport<LS>, Index>
where
L1: Member<LS, Index>,
{
Expand All @@ -281,15 +284,7 @@ where
index: PhantomData<Index>,
}

/// Macro to make Projector
#[macro_export]
macro_rules! projector {
($al_type:ty, $target:expr, $transport:expr) => {
$crate::core::Projector::<$al_type, _, _, _>::new($target, $transport)
};
}

impl<LS: HList, L1: ChoreographyLocation, B: Transport, Index> Projector<LS, L1, B, Index>
impl<LS: HList, L1: ChoreographyLocation, B: Transport<LS>, Index> Projector<LS, L1, B, Index>
where
L1: Member<LS, Index>,
{
Expand Down Expand Up @@ -338,13 +333,16 @@ where
where
L: Subset<LS, IndexSet>,
{
struct EppOp<'a, L: HList, L1: ChoreographyLocation, B: Transport> {
struct EppOp<'a, L: HList, L1: ChoreographyLocation, LS: HList, B: Transport<LS>> {
target: PhantomData<L1>,
transport: &'a B,
locations: Vec<String>,
marker: PhantomData<L>,
location_set: PhantomData<LS>,
}
impl<'a, L: HList, T: ChoreographyLocation, B: Transport> ChoreoOp<L> for EppOp<'a, L, T, B> {
impl<'a, L: HList, T: ChoreographyLocation, LS: HList, B: Transport<LS>> ChoreoOp<L>
for EppOp<'a, L, T, LS, B>
{
fn locally<V, L1: ChoreographyLocation, Index>(
&self,
_location: L1,
Expand Down Expand Up @@ -406,11 +404,12 @@ where
where
M: HList + Subset<L, Index>,
{
let op: EppOp<'a, M, T, B> = EppOp {
let op: EppOp<'a, M, T, LS, B> = EppOp {
target: PhantomData::<T>,
transport: &self.transport,
locations: self.transport.locations(),
marker: PhantomData::<M>,
location_set: PhantomData::<LS>,
};
choreo.run(&op)
}
Expand All @@ -429,18 +428,20 @@ where
transport: self.transport,
locations: locs_vec.clone(),
marker: PhantomData::<S>,
location_set: PhantomData::<LS>,
};
return choreo.run(&op);
}
}
R::remote()
}
}
let op: EppOp<'a, L, L1, B> = EppOp {
let op: EppOp<'a, L, L1, LS, B> = EppOp {
target: PhantomData::<L1>,
transport: &self.transport,
locations: self.transport.locations(),
marker: PhantomData::<L>,
location_set: PhantomData::<LS>,
};
choreo.run(&op)
}
Expand Down
Loading

0 comments on commit 44c3478

Please sign in to comment.