ae8ea822956b96fbb678c17332ec27e7fe30088e
[controller.git] / opendaylight / md-sal / sal-akka-raft-example / src / main / java / org / opendaylight / controller / cluster / example / Main.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.cluster.example;
9
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;
19 import java.util.Map;
20 import java.util.Optional;
21 import org.opendaylight.controller.cluster.example.messages.KeyValue;
22
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<>();
27
28     static {
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");
32     }
33
34     private Main() {
35     }
36
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");
42
43         ActorRef example2Actor =
44             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2",
45                 withoutPeer("example-2"), Optional.empty()), "example-2");
46
47         ActorRef example3Actor =
48             ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3",
49                 withoutPeer("example-3"), Optional.empty()), "example-3");
50
51
52         List<ActorRef> examples = Arrays.asList(example1Actor, example2Actor, example3Actor);
53
54         ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor));
55         BufferedReader br =
56             new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset()));
57
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");
61
62         while (true) {
63             System.out.print("Enter command (0 to exit):");
64             try {
65                 String line = br.readLine();
66                 if (line == null) {
67                     continue;
68                 }
69                 String[] split = line.split(" ");
70                 if (split.length > 1) {
71                     String command = split[0];
72                     String actor = split[1];
73
74                     if ("k".equals(command)) {
75                         int num = Integer.parseInt(actor);
76                         examples.get(num - 1).tell(PoisonPill.getInstance(), null);
77                         continue;
78                     } else if ("s".equals(command)) {
79                         int num = Integer.parseInt(actor);
80                         String actorName = "example-" + num;
81                         examples.add(num - 1,
82                             ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName,
83                                 withoutPeer(actorName), Optional.empty()), actorName));
84                         System.out.println("Created actor : " + actorName);
85                         continue;
86                     }
87                 }
88
89                 int num = Integer.parseInt(line);
90                 if (num == 0) {
91                     System.exit(0);
92                 }
93                 clientActor.tell(new KeyValue("key " + num, "value " + num), null);
94             } catch (NumberFormatException nfe) {
95                 System.err.println("Invalid Format!");
96             }
97         }
98     }
99
100     private static Map<String, String> withoutPeer(final String peerId) {
101         Map<String, String> without = new HashMap<>(allPeers);
102         without.remove(peerId);
103         return without;
104     }
105 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.