First version with monitoring.
[upnpmonitor] / monitor / src / main / java / org / wamblee / upnpmonitor / Monitor.java
1 package org.wamblee.upnpmonitor;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.util.concurrent.ScheduledThreadPoolExecutor;
6 import java.util.concurrent.TimeUnit;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9
10 import org.wamblee.io.SimpleProcess;
11
12 public class Monitor implements Runnable {
13
14     private static final Logger LOGGER = Logger.getLogger(Monitor.class
15         .getName());
16
17     private ScheduledThreadPoolExecutor executor;
18     private Config config;
19     private boolean serviceFound;
20     private UpnpStack stack;
21
22     public Monitor(Config aConfig) {
23         config = aConfig;
24         executor = new ScheduledThreadPoolExecutor(1);
25     }
26
27     public void start() {
28         serviceFound = true;
29         executor.scheduleWithFixedDelay(this, 0, config.getIntervalSeconds(),
30             TimeUnit.SECONDS);
31         executeCommand(config.getStartupCommand());
32     }
33
34     public synchronized void setServiceFound(boolean aServiceFound) {
35         serviceFound = aServiceFound;
36     }
37
38     public synchronized void run() {
39
40         if (!serviceFound) {
41             LOGGER.info("UPNP service is down, executing recovery");
42             // we need to execute recovery.
43             executeCommand(config.getShutdownCommand());
44             executeCommand(config.getStartupCommand());
45             setServiceFound(true);
46             // give process a chance to startup.
47             return;
48         }
49
50         shutdownStack();
51
52         setServiceFound(false);
53
54         stack = new UpnpStack(new UpnpStack.Listener() {
55             @Override
56             public void deviceAdded(String aDeviceString) {
57                 LOGGER.fine("Device added: " + aDeviceString);
58                 if (aDeviceString.toLowerCase().contains(
59                     config.getPattern().toLowerCase())) {
60                     synchronized (Monitor.this) {
61                         setServiceFound(true);
62                     }
63                 }
64             }
65
66             @Override
67             public void deviceRemoved(String aDeviceString) {
68                 LOGGER.fine("Device removed:" + aDeviceString);
69             }
70         });
71         stack.search();
72     }
73
74     private void shutdownStack() {
75         if (stack != null) {
76             stack.shutdown();
77             stack = null;
78         }
79     }
80
81     private void executeCommand(String command) {
82         LOGGER.info("Executing command: " + command);
83         SimpleProcess process = new SimpleProcess(new File("."), new String[] {
84             "sh", "-c", command });
85
86         try {
87             int ret = process.run();
88             LOGGER.info(process.getStdout());
89             LOGGER.info(process.getStderr());
90             LOGGER.info("Exit status: " + ret);
91         } catch (IOException e) {
92             LOGGER.log(Level.INFO, "Problem executing command", e);
93         }
94     }
95
96     public void stop() {
97         executor.shutdown();
98         shutdownStack();
99         executeCommand(config.getShutdownCommand());
100     }
101 }