2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.cluster.example;
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.actor.PoisonPill;
14 import org.opendaylight.controller.cluster.example.messages.KeyValue;
16 import java.io.BufferedReader;
17 import java.io.InputStreamReader;
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.List;
24 private static final ActorSystem actorSystem = ActorSystem.create();
25 // Create three example actors
26 private static Map<String, String> allPeers = new HashMap<>();
29 allPeers.put("example-1", "akka://default/user/example-1");
30 allPeers.put("example-2", "akka://default/user/example-2");
31 allPeers.put("example-3", "akka://default/user/example-3");
34 public static void main(String[] args) throws Exception{
35 ActorRef example1Actor =
36 actorSystem.actorOf(ExampleActor.props("example-1",
37 withoutPeer("example-1")), "example-1");
39 ActorRef example2Actor =
40 actorSystem.actorOf(ExampleActor.props("example-2",
41 withoutPeer("example-2")), "example-2");
43 ActorRef example3Actor =
44 actorSystem.actorOf(ExampleActor.props("example-3",
45 withoutPeer("example-3")), "example-3");
48 List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
50 ActorRef clientActor = actorSystem.actorOf(ClientActor.props(example1Actor));
52 new BufferedReader(new InputStreamReader(System.in));
54 System.out.println("Usage :");
55 System.out.println("s <1-3> to start a peer");
56 System.out.println("k <1-3> to kill a peer");
59 System.out.print("Enter command (0 to exit):");
61 String s = br.readLine();
62 String[] split = s.split(" ");
63 if(split.length > 1) {
64 String command = split[0];
65 String actor = split[1];
67 if ("k".equals(command)) {
68 int i = Integer.parseInt(actor);
70 .tell(PoisonPill.getInstance(), null);
72 } else if ("s".equals(command)) {
73 int i = Integer.parseInt(actor);
74 String actorName = "example-" + i;
76 actorSystem.actorOf(ExampleActor.props(actorName,
77 withoutPeer(actorName)), actorName));
78 System.out.println("Created actor : " + actorName);
83 int i = Integer.parseInt(s);
87 clientActor.tell(new KeyValue("key " + i, "value " + i), null);
88 } catch (NumberFormatException nfe) {
89 System.err.println("Invalid Format!");
94 private static Map<String, String> withoutPeer(String peerId) {
95 Map<String, String> without = new HashMap<>(allPeers);
96 without.remove(peerId);