2. Multitier & Peer Setup
Table of contents
ScalaLoci is a distributed multitier language. This means that you can write distributed applications as if they were one single application. When compiling your program, it will be ‘sliced’ into multiple parts and the compiler will create an executable for each peer (the machines that are going to run parts of your program). All the network communication logic will be generated automatically by ScalaLoci.
This part of our tutorial will cover the peer setup of your application where you define the different types of nodes in your distributed application and their relationship (the topology of your network).
Imports
In the same directory as your build.sbt, create the folder src/main/scala with the file Chat.scala
. This will be our chat application.
Add the following imports to Chat.scala
make ScalaLoci available in your program:
import loci._ // the loci language
import loci.serializer.upickle._ // the selected serializer
import loci.communicator.tcp._ // the selected communication backend
In our example we will use upickle as our serializer and the tcp communication backend. Of course you can make different choices for your real world application. For more information on this see the the ScalaLoci Github repository.
This example will additionally make use of the REScala library for reactive and event driven programming in Scala. This allows us to model new chat messages as events.
import rescala.default._
The multitier environment
In a ScalaLoci application, all code has to live in the multitier environment which you specify with the @multitier
annotation.
@multitier
object Chat {
// server and client code lives in here
}
Peer Setup
Let’s continue with the definition of our peers. This can be done using the @peer
annotation.
Our example will use multiple Client peers and one Server peer which will manage the connections. Each client will only have a connection to the server while the server will be connected to all clients.
@multitier object Chat {
@peer type Server <: { type Tie <: Multiple[Client] }
@peer type Client <: { type Tie <: Single[Server] }
}
Notice how the keywords Multiple
and Single
specify the type and quantity of the ties between the peers.