Fix findbugs violations in md-sal - part 2
[controller.git] / opendaylight / md-sal / sal-akka-raft-example / src / main / java / org / opendaylight / controller / cluster / example / LogGenerator.java
1 /*
2  * Copyright (c) 2014, 2015 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 java.util.HashMap;
13 import java.util.Map;
14 import java.util.Random;
15 import org.opendaylight.controller.cluster.example.messages.KeyValue;
16
17 /**
18  * Created by kramesha on 7/16/14.
19  */
20 public class LogGenerator {
21     private final Map<ActorRef, LoggingThread> clientToLoggingThread = new HashMap<>();
22
23     public void startLoggingForClient(ActorRef client) {
24         LoggingThread lt = new LoggingThread(client);
25         clientToLoggingThread.put(client, lt);
26         new Thread(lt).start();
27     }
28
29     public void stopLoggingForClient(ActorRef client) {
30         clientToLoggingThread.get(client).stopLogging();
31         clientToLoggingThread.remove(client);
32     }
33
34     public static class LoggingThread implements Runnable {
35
36         private final ActorRef clientActor;
37         private volatile boolean stopLogging = false;
38
39         public LoggingThread(ActorRef clientActor) {
40             this.clientActor = clientActor;
41         }
42
43         @Override
44         @SuppressWarnings("checkstyle:RegexpSingleLineJava")
45         public void run() {
46             Random random = new Random();
47             while (true) {
48                 if (stopLogging) {
49                     System.out.println("Logging stopped for client:" + clientActor.path());
50                     break;
51                 }
52                 String key = clientActor.path().name();
53                 int randomInt = random.nextInt(100);
54                 clientActor.tell(new KeyValue(key + "-key-" + randomInt, "value-" + randomInt), null);
55                 try {
56                     Thread.sleep(randomInt % 10 * 1000L);
57                 } catch (InterruptedException e) {
58                     e.printStackTrace();
59                 }
60             }
61         }
62
63         public void stopLogging() {
64             stopLogging = true;
65         }
66
67         public void startLogging() {
68             stopLogging = false;
69         }
70     }
71 }