completed setup for installation, fixed robustness problem.
[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         stack = new UpnpStack(new UpnpStack.Listener() {
30             @Override
31             public void deviceAdded(String aDeviceString) {
32                 LOGGER.fine("Device added: " + aDeviceString);
33                 if (aDeviceString.toLowerCase().contains(
34                     config.getPattern().toLowerCase())) {
35                     synchronized (Monitor.this) {
36                         setServiceFound(true);
37                     }
38                 }
39             }
40
41             @Override
42             public void deviceRemoved(String aDeviceString) {
43                 LOGGER.fine("Device removed:" + aDeviceString);
44             }
45         });
46         executor.scheduleWithFixedDelay(this, 0, config.getIntervalSeconds(),
47             TimeUnit.SECONDS);
48         executeCommand(config.getStartupCommand());
49     }
50
51     public synchronized void setServiceFound(boolean aServiceFound) {
52         serviceFound = aServiceFound;
53     }
54
55     public synchronized void run() {
56
57         if (!serviceFound) {
58             LOGGER.info("UPNP service is down, executing recovery");
59             // we need to execute recovery.
60             executeCommand(config.getShutdownCommand());
61             executeCommand(config.getStartupCommand());
62             setServiceFound(true);
63             // give process a chance to startup.
64             return;
65         }
66
67         setServiceFound(false);
68         stack.search();
69     }
70
71     private void shutdownStack() {
72         if (stack != null) {
73             stack.shutdown();
74             stack = null;
75         }
76     }
77
78     private void executeCommand(String command) {
79         LOGGER.info("Executing command: " + command);
80         SimpleProcess process = new SimpleProcess(new File("."), new String[] {
81             "sh", "-c", command });
82
83         try {
84             int ret = process.run();
85             LOGGER.info(process.getStdout());
86             LOGGER.info(process.getStderr());
87             LOGGER.info("Exit status: " + ret);
88         } catch (IOException e) {
89             LOGGER.log(Level.INFO, "Problem executing command", e);
90         }
91     }
92
93     public void stop() {
94         executor.shutdown();
95         shutdownStack();
96         executeCommand(config.getShutdownCommand());
97     }
98 }