27d083f2b38f60998721eb0025a949854ec1d394
[controller.git] / opendaylight / md-sal / sal-akka-raft / 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
9 package org.opendaylight.controller.cluster.example;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import org.opendaylight.controller.cluster.example.messages.KeyValue;
14
15 import java.io.BufferedReader;
16 import java.io.InputStreamReader;
17 import java.util.HashMap;
18 import java.util.Map;
19
20 public class Main {
21     private static final ActorSystem actorSystem = ActorSystem.create();
22     // Create three example actors
23     private static Map<String, String> allPeers = new HashMap<>();
24
25     static {
26         allPeers.put("example-1", "akka://default/user/example-1");
27         allPeers.put("example-2", "akka://default/user/example-2");
28         allPeers.put("example-3", "akka://default/user/example-3");
29     }
30
31     public static void main(String[] args) throws Exception{
32         ActorRef example1Actor =
33             actorSystem.actorOf(ExampleActor.props("example-1",
34                 withoutPeer("example-1")), "example-1");
35
36         ActorRef example2Actor =
37             actorSystem.actorOf(ExampleActor.props("example-2",
38                 withoutPeer("example-2")), "example-2");
39
40         ActorRef example3Actor =
41             actorSystem.actorOf(ExampleActor.props("example-3",
42                 withoutPeer("example-3")), "example-3");
43
44         ActorRef clientActor = actorSystem.actorOf(ClientActor.props(example1Actor));
45         BufferedReader br =
46             new BufferedReader(new InputStreamReader(System.in));
47
48         while(true) {
49             System.out.print("Enter Integer (0 to exit):");
50             try {
51                 int i = Integer.parseInt(br.readLine());
52                 if(i == 0){
53                     System.exit(0);
54                 }
55                 clientActor.tell(new KeyValue("key " + i, "value " + i), null);
56             } catch (NumberFormatException nfe) {
57                 System.err.println("Invalid Format!");
58             }
59         }
60     }
61
62     private static Map<String, String> withoutPeer(String peerId) {
63         Map<String, String> without = new HashMap<>(allPeers);
64         without.remove(peerId);
65         return without;
66     }
67 }