Add missing copyright text
[controller.git] / opendaylight / md-sal / sal-akka-raft-example / src / main / java / org / opendaylight / controller / cluster / example / LogGenerator.java
1 package org.opendaylight.controller.cluster.example;
2
3 import akka.actor.ActorRef;
4 import org.opendaylight.controller.cluster.example.messages.KeyValue;
5
6 import java.util.HashMap;
7 import java.util.Map;
8 import java.util.Random;
9
10 /**
11  * Created by kramesha on 7/16/14.
12  */
13 public class LogGenerator {
14     private Map<ActorRef, LoggingThread> clientToLoggingThread = new HashMap<ActorRef, LoggingThread>();
15
16     public void startLoggingForClient(ActorRef client) {
17         LoggingThread lt = new LoggingThread(client);
18         clientToLoggingThread.put(client, lt);
19         Thread t = new Thread(lt);
20         t.start();
21     }
22
23     public void stopLoggingForClient(ActorRef client) {
24         clientToLoggingThread.get(client).stopLogging();
25         clientToLoggingThread.remove(client);
26     }
27
28     public class LoggingThread implements Runnable {
29
30         private ActorRef clientActor;
31         private volatile boolean stopLogging = false;
32
33         public LoggingThread(ActorRef clientActor) {
34             this.clientActor = clientActor;
35         }
36
37         public void run() {
38             Random r = new Random();
39             while (true) {
40                 if (stopLogging) {
41                     System.out.println("Logging stopped for client:" + clientActor.path());
42                     break;
43                 }
44                 String key = clientActor.path().name();
45                 int random = r.nextInt(100);
46                 clientActor.tell(new KeyValue(key+"-key-" + random, "value-" + random), null);
47                 try {
48                     Thread.sleep((random%10) * 1000);
49                 } catch (InterruptedException e) {
50                     e.printStackTrace();
51                 }
52             }
53         }
54
55         public void stopLogging() {
56             stopLogging = true;
57         }
58
59         public void startLogging() {
60             stopLogging = false;
61         }
62
63
64     }
65
66
67 }