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;
18 import java.util.List;
20 import java.util.Optional;
21 import org.opendaylight.controller.cluster.example.messages.KeyValue;
23 public final class Main {
24 private static final ActorSystem ACTOR_SYSTEM = 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");
37 @SuppressWarnings("checkstyle:RegexpSingleLineJava")
38 public static void main(final String[] args) throws Exception {
39 ActorRef example1Actor =
40 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1",
41 withoutPeer("example-1"), Optional.empty()), "example-1");
43 ActorRef example2Actor =
44 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
45 withoutPeer("example-2"), Optional.empty()), "example-2");
47 ActorRef example3Actor =
48 ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
49 withoutPeer("example-3"), Optional.empty()), "example-3");
52 List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
54 ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
56 new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
58 System.out.println("Usage :");
59 System.out.println("s <1-3> to start a peer");
60 System.out.println("k <1-3> to kill a peer");
63 System.out.print("Enter command (0 to exit):");
65 String line = br.readLine();
69 String[] split = line.split(" ");
70 if (split.length > 1) {
71 String command = split[0];
72 String actor = split[1];
74 if ("k".equals(command)) {
75 int num = Integer.parseInt(actor);
76 examples.get(num - 1).tell(PoisonPill.getInstance(), null);
78 } else if ("s".equals(command)) {
79 int num = Integer.parseInt(actor);
80 String actorName = "example-" + num;
82 ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName,
83 withoutPeer(actorName), Optional.empty()), actorName));
84 System.out.println("Created actor : " + actorName);
89 int num = Integer.parseInt(line);
93 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
94 } catch (NumberFormatException nfe) {
95 System.err.println("Invalid Format!");
100 private static Map<String, String> withoutPeer(final String peerId) {
101 Map<String, String> without = new HashMap<>(allPeers);
102 without.remove(peerId);