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 java.io.BufferedReader;
16 import java.io.InputStreamReader;
17 import java.util.Arrays;
18 import java.util.HashMap;
19 import java.util.List;
21 import org.opendaylight.controller.cluster.example.messages.KeyValue;
22 import org.opendaylight.controller.cluster.raft.ConfigParams;
24 public final class Main {
25 private static final ActorSystem ACTOR_SYSTEM = ActorSystem.create();
26 // Create three example actors
27 private static Map<String, String> allPeers = new HashMap<>();
30 allPeers.put("example-1", "akka://default/user/example-1");
31 allPeers.put("example-2", "akka://default/user/example-2");
32 allPeers.put("example-3", "akka://default/user/example-3");
38 @SuppressWarnings("checkstyle:RegexpSingleLineJava")
39 public static void main(String[] args) throws Exception {
40 ActorRef example1Actor =
41 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
42 withoutPeer("example-1"), Optional.<ConfigParams>absent()), "example-1");
44 ActorRef example2Actor =
45 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
46 withoutPeer("example-2"), Optional.<ConfigParams>absent()), "example-2");
48 ActorRef example3Actor =
49 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
50 withoutPeer("example-3"), Optional.<ConfigParams>absent()), "example-3");
53 List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
55 ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
57 new BufferedReader(new InputStreamReader(System.in));
59 System.out.println("Usage :");
60 System.out.println("s <1-3> to start a peer");
61 System.out.println("k <1-3> to kill a peer");
64 System.out.print("Enter command (0 to exit):");
66 String line = br.readLine();
67 String[] split = line.split(" ");
68 if (split.length > 1) {
69 String command = split[0];
70 String actor = split[1];
72 if ("k".equals(command)) {
73 int num = Integer.parseInt(actor);
74 examples.get(num - 1).tell(PoisonPill.getInstance(), null);
76 } else if ("s".equals(command)) {
77 int num = Integer.parseInt(actor);
78 String actorName = "example-" + num;
80 ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName,
81 withoutPeer(actorName), Optional.<ConfigParams>absent()),
83 System.out.println("Created actor : " + actorName);
88 int num = Integer.parseInt(line);
92 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
93 } catch (NumberFormatException nfe) {
94 System.err.println("Invalid Format!");
99 private static Map<String, String> withoutPeer(String peerId) {
100 Map<String, String> without = new HashMap<>(allPeers);
101 without.remove(peerId);