x
1
// Buyer-Seller, Basic
2
s->b:Descr .
3
s->b:Price .
4
(b->s:Acc+b->s:Rej)
Basic protocol for the Buyer-Seller example
The code below is a possible implementation of a process that follows this protocol.
def seller(s: S$State$Init): S$State$Final = s
.send(B, Descr)
.send(B, Price)
.recv(
(_, _, s) => { println("offer accepted"); s },
(_, _, s) => { println("offer rejected"); s }
)
def buyer(s: B$State$Init): B$State$Final = s
.recv(
(_, _, s) => s.recv(
(_, _, s) => s.send(S, Rej) // or s.send(S, Acc)
)
)
val pr = new Protocol
pr.run(seller)
pr.run(buyer)
To quickly run this example you can:
- open https://scastie.scala-lang.org to run Scala code online
- Copy the code in the "All" tab of the "Scala APIs" widget and paste it on the Scastie
- Copy the process implementations above and paste it on the bottom of the same Sastie
- Press "Run" to run the process.