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 com.google.common.base.Optional;
15 import org.opendaylight.controller.cluster.example.messages.KeyValue;
16 import org.opendaylight.controller.cluster.raft.ConfigParams;
18 import java.io.BufferedReader;
19 import java.io.InputStreamReader;
20 import java.util.Arrays;
21 import java.util.HashMap;
22 import java.util.List;
26 private static final ActorSystem actorSystem = ActorSystem.create();
27 // Create three example actors
28 private static Map<String, String> allPeers = new HashMap<>();
31 allPeers.put("example-1", "akka://default/user/example-1");
32 allPeers.put("example-2", "akka://default/user/example-2");
33 allPeers.put("example-3", "akka://default/user/example-3");
36 public static void main(String[] args) throws Exception{
37 ActorRef example1Actor =
38 actorSystem.actorOf(ExampleActor.props("example-1",
39 withoutPeer("example-1"), Optional.<ConfigParams>absent()), "example-1");
41 ActorRef example2Actor =
42 actorSystem.actorOf(ExampleActor.props("example-2",
43 withoutPeer("example-2"), Optional.<ConfigParams>absent()), "example-2");
45 ActorRef example3Actor =
46 actorSystem.actorOf(ExampleActor.props("example-3",
47 withoutPeer("example-3"), Optional.<ConfigParams>absent()), "example-3");
50 List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
52 ActorRef clientActor = actorSystem.actorOf(ClientActor.props(example1Actor));
54 new BufferedReader(new InputStreamReader(System.in));
56 System.out.println("Usage :");
57 System.out.println("s <1-3> to start a peer");
58 System.out.println("k <1-3> to kill a peer");
61 System.out.print("Enter command (0 to exit):");
63 String s = br.readLine();
64 String[] split = s.split(" ");
65 if(split.length > 1) {
66 String command = split[0];
67 String actor = split[1];
69 if ("k".equals(command)) {
70 int i = Integer.parseInt(actor);
72 .tell(PoisonPill.getInstance(), null);
74 } else if ("s".equals(command)) {
75 int i = Integer.parseInt(actor);
76 String actorName = "example-" + i;
78 actorSystem.actorOf(ExampleActor.props(actorName,
79 withoutPeer(actorName), Optional.<ConfigParams>absent()),
81 System.out.println("Created actor : " + actorName);
86 int i = Integer.parseInt(s);
90 clientActor.tell(new KeyValue("key " + i, "value " + i), null);
91 } catch (NumberFormatException nfe) {
92 System.err.println("Invalid Format!");
97 private static Map<String, String> withoutPeer(String peerId) {
98 Map<String, String> without = new HashMap<>(allPeers);
99 without.remove(peerId);