X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-akka-raft-example%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fcluster%2Fexample%2FMain.java;h=54ff92993a9ad23e2236b26c2944a28c944f9ba3;hb=0f02b7edeb1454c1a568f0f1b050757e7503ddf7;hp=0e5d643a643731277003781b75dd353c92697285;hpb=608760751ce7fcf4e84e86a8b33d43bc1d9984d6;p=controller.git diff --git a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/Main.java b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/Main.java index 0e5d643a64..54ff92993a 100644 --- a/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/Main.java +++ b/opendaylight/md-sal/sal-akka-raft-example/src/main/java/org/opendaylight/controller/cluster/example/Main.java @@ -12,18 +12,18 @@ import akka.actor.ActorRef; import akka.actor.ActorSystem; import akka.actor.PoisonPill; import com.google.common.base.Optional; -import org.opendaylight.controller.cluster.example.messages.KeyValue; -import org.opendaylight.controller.cluster.raft.ConfigParams; - import java.io.BufferedReader; import java.io.InputStreamReader; +import java.nio.charset.Charset; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; +import org.opendaylight.controller.cluster.example.messages.KeyValue; +import org.opendaylight.controller.cluster.raft.ConfigParams; -public class Main { - private static final ActorSystem actorSystem = ActorSystem.create(); +public final class Main { + private static final ActorSystem ACTOR_SYSTEM = ActorSystem.create(); // Create three example actors private static Map allPeers = new HashMap<>(); @@ -33,49 +33,55 @@ public class Main { allPeers.put("example-3", "akka://default/user/example-3"); } - public static void main(String[] args) throws Exception{ + private Main() { + } + + @SuppressWarnings("checkstyle:RegexpSingleLineJava") + public static void main(String[] args) throws Exception { ActorRef example1Actor = - actorSystem.actorOf(ExampleActor.props("example-1", + ACTOR_SYSTEM.actorOf(ExampleActor.props("example-1", withoutPeer("example-1"), Optional.absent()), "example-1"); ActorRef example2Actor = - actorSystem.actorOf(ExampleActor.props("example-2", + ACTOR_SYSTEM.actorOf(ExampleActor.props("example-2", withoutPeer("example-2"), Optional.absent()), "example-2"); ActorRef example3Actor = - actorSystem.actorOf(ExampleActor.props("example-3", + ACTOR_SYSTEM.actorOf(ExampleActor.props("example-3", withoutPeer("example-3"), Optional.absent()), "example-3"); List examples = Arrays.asList(example1Actor, example2Actor, example3Actor); - ActorRef clientActor = actorSystem.actorOf(ClientActor.props(example1Actor)); + ActorRef clientActor = ACTOR_SYSTEM.actorOf(ClientActor.props(example1Actor)); BufferedReader br = - new BufferedReader(new InputStreamReader(System.in)); + new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset())); System.out.println("Usage :"); System.out.println("s <1-3> to start a peer"); System.out.println("k <1-3> to kill a peer"); - while(true) { + while (true) { System.out.print("Enter command (0 to exit):"); try { - String s = br.readLine(); - String[] split = s.split(" "); - if(split.length > 1) { + String line = br.readLine(); + if (line == null) { + continue; + } + String[] split = line.split(" "); + if (split.length > 1) { String command = split[0]; String actor = split[1]; if ("k".equals(command)) { - int i = Integer.parseInt(actor); - examples.get(i - 1) - .tell(PoisonPill.getInstance(), null); + int num = Integer.parseInt(actor); + examples.get(num - 1).tell(PoisonPill.getInstance(), null); continue; } else if ("s".equals(command)) { - int i = Integer.parseInt(actor); - String actorName = "example-" + i; - examples.add(i - 1, - actorSystem.actorOf(ExampleActor.props(actorName, + int num = Integer.parseInt(actor); + String actorName = "example-" + num; + examples.add(num - 1, + ACTOR_SYSTEM.actorOf(ExampleActor.props(actorName, withoutPeer(actorName), Optional.absent()), actorName)); System.out.println("Created actor : " + actorName); @@ -83,11 +89,11 @@ public class Main { } } - int i = Integer.parseInt(s); - if(i == 0){ + int num = Integer.parseInt(line); + if (num == 0) { System.exit(0); } - clientActor.tell(new KeyValue("key " + i, "value " + i), null); + clientActor.tell(new KeyValue("key " + num, "value " + num), null); } catch (NumberFormatException nfe) { System.err.println("Invalid Format!"); }