b2fc19f6e0b5cb8ac26caf4f7213d452a7add8dc
[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 org.opendaylight.controller.cluster.example.messages.KeyValue;
13
14 import java.util.HashMap;
15 import java.util.Map;
16 import java.util.Random;
17
18 /**
19  * Created by kramesha on 7/16/14.
20  */
21 public class LogGenerator {
22     private Map<ActorRef, LoggingThread> clientToLoggingThread = new HashMap<ActorRef, LoggingThread>();
23
24     public void startLoggingForClient(ActorRef client) {
25         LoggingThread lt = new LoggingThread(client);
26         clientToLoggingThread.put(client, lt);
27         Thread t = new Thread(lt);
28         t.start();
29     }
30
31     public void stopLoggingForClient(ActorRef client) {
32         clientToLoggingThread.get(client).stopLogging();
33         clientToLoggingThread.remove(client);
34     }
35
36     public class LoggingThread implements Runnable {
37
38         private ActorRef clientActor;
39         private volatile boolean stopLogging = false;
40
41         public LoggingThread(ActorRef clientActor) {
42             this.clientActor = clientActor;
43         }
44
45         public void run() {
46             Random r = 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 random = r.nextInt(100);
54                 clientActor.tell(new KeyValue(key+"-key-" + random, "value-" + random), null);
55                 try {
56                     Thread.sleep((random%10) * 1000);
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
72     }
73
74
75 }