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
8 package org.opendaylight.controller.cluster.example;
10 import akka.actor.ActorRef;
11 import akka.actor.ActorSystem;
12 import akka.actor.PoisonPill;
13 import java.io.BufferedReader;
14 import java.io.InputStreamReader;
15 import java.nio.charset.Charset;
16 import java.util.Arrays;
17 import java.util.HashMap;
19 import java.util.Optional;
20 import org.opendaylight.controller.cluster.example.messages.KeyValue;
22 public final class Main {
23 private static final ActorSystem ACTOR_SYSTEM = ActorSystem.create();
24 // Create three example actors
25 private static Map<String, String> allPeers = new HashMap<>();
28 allPeers.put("example-1", "akka://default/user/example-1");
29 allPeers.put("example-2", "akka://default/user/example-2");
30 allPeers.put("example-3", "akka://default/user/example-3");
36 @SuppressWarnings("checkstyle:RegexpSingleLineJava")
37 public static void main(final String[] args) throws Exception {
38 ActorRef example1Actor =
39 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
40 withoutPeer("example-1"), Optional.empty()), "example-1");
42 ActorRef example2Actor =
43 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
44 withoutPeer("example-2"), Optional.empty()), "example-2");
46 ActorRef example3Actor =
47 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
48 withoutPeer("example-3"), Optional.empty()), "example-3");
51 final var examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
53 final var clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
54 final var br = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
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 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.empty()), actorName));
82 System.out.println("Created actor : " + actorName);
87 int num = Integer.parseInt(line);
91 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
92 } catch (NumberFormatException nfe) {
93 System.err.println("Invalid Format!");
98 private static Map<String, String> withoutPeer(final String peerId) {
99 Map<String, String> without = new HashMap<>(allPeers);
100 without.remove(peerId);