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.nio.charset.Charset;
18 import java.util.Arrays;
19 import java.util.HashMap;
20 import java.util.List;
22 import org.opendaylight.controller.cluster.example.messages.KeyValue;
23 import org.opendaylight.controller.cluster.raft.ConfigParams;
25 public final class Main {
26 private static final ActorSystem ACTOR_SYSTEM = 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");
39 @SuppressWarnings("checkstyle:RegexpSingleLineJava")
40 public static void main(String[] args) throws Exception {
41 ActorRef example1Actor =
42 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
43 withoutPeer("example-1"), Optional.<ConfigParams>absent()), "example-1");
45 ActorRef example2Actor =
46 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
47 withoutPeer("example-2"), Optional.<ConfigParams>absent()), "example-2");
49 ActorRef example3Actor =
50 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
51 withoutPeer("example-3"), Optional.<ConfigParams>absent()), "example-3");
54 List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
56 ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
58 new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
60 System.out.println("Usage :");
61 System.out.println("s <1-3> to start a peer");
62 System.out.println("k <1-3> to kill a peer");
65 System.out.print("Enter command (0 to exit):");
67 String line = br.readLine();
71 String[] split = line.split(" ");
72 if (split.length > 1) {
73 String command = split[0];
74 String actor = split[1];
76 if ("k".equals(command)) {
77 int num = Integer.parseInt(actor);
78 examples.get(num - 1).tell(PoisonPill.getInstance(), null);
80 } else if ("s".equals(command)) {
81 int num = Integer.parseInt(actor);
82 String actorName = "example-" + num;
84 ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName,
85 withoutPeer(actorName), Optional.<ConfigParams>absent()),
87 System.out.println("Created actor : " + actorName);
92 int num = Integer.parseInt(line);
96 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
97 } catch (NumberFormatException nfe) {
98 System.err.println("Invalid Format!");
103 private static Map<String, String> withoutPeer(String peerId) {
104 Map<String, String> without = new HashMap<>(allPeers);
105 without.remove(peerId);