summaryrefslogtreecommitdiff
path: root/docs/examples/actors/counter/CounterUserTcp.scala
blob: b6c304bb1c9a7498d9f23b7289f09abc322fcf21 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package actors.examples.counter

import actors.multi.Pid
import actors.distributed.RemoteActor
import actors.distributed.TCP
import actors.distributed.TcpNode
import actors.distributed.TcpService

class CounterUser extends RemoteActor {
  override def run(): unit = {
    alive(TCP())

    spawn(TcpNode("127.0.0.1", 9090), "actors.examples.counter.Counter")

    receive {
      case p: Pid =>
        // communicate with counter
        Console.println("" + node + ": Sending Incr() to remote Counter (" + p + ")...")
        p ! Incr()
        p ! Incr()
        p ! Value(self)
        receive {
          case Result(v) =>
            Console.println("Received result: " + v)
        }
    }
  }
}

object Main {
  def main(args: Array[String]): unit = {
    val serv = new TcpService(9090)
    serv.start()

    val cu = new CounterUser
    cu.start()
  }
}