1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.xley.lfosc;
22
23 import com.illposed.osc.OSCPortIn;
24 import com.xley.lfosc.impl.LightFactoryProxyThread;
25 import com.xley.lfosc.impl.OSCProxyListener;
26 import com.xley.lfosc.util.LogUtil;
27 import joptsimple.OptionSet;
28
29 import java.io.IOException;
30 import java.net.*;
31 import java.text.MessageFormat;
32 import java.util.Locale;
33 import java.util.ResourceBundle;
34
35
36
37
38 public class ProxyDaemon implements Runnable {
39
40
41
42
43 public static final ResourceBundle resources = ResourceBundle.getBundle(ProxyDaemon.class.getSimpleName(),
44 Locale.getDefault());
45 private final Object monitor = true;
46
47
48 private final OptionSet options;
49 OSCProxyListener listener = new OSCProxyListener();
50
51 private Boolean shutdown = false;
52 private Thread runner;
53 private int errorcode = 0;
54
55 private ServerSocket serverSocket;
56 private OSCPortIn receiver = null;
57
58
59
60
61
62
63
64 protected ProxyDaemon(final OptionSet optionSet) {
65 this.options = optionSet;
66 }
67
68
69
70
71
72
73 public final int errorcode() {
74 return this.errorcode;
75 }
76
77
78
79
80 public final void shutdown() {
81 synchronized (monitor) {
82 if (shutdown) {
83 return;
84 }
85 }
86 shutdown = true;
87 LogUtil.info(this.getClass(), resources.getString("shutdown.inprogress"));
88 runner.interrupt();
89 if (receiver != null) {
90 receiver.stopListening();
91 receiver.close();
92 }
93
94 if (listener != null) {
95 listener.shutdown();
96 }
97
98 if (serverSocket != null) {
99 try {
100 serverSocket.close();
101 serverSocket = null;
102 } catch (Throwable e) {
103 LogUtil.trace(e);
104 }
105 }
106 }
107
108 @Override
109 public final void run() {
110 runner = Thread.currentThread();
111
112 int portNumber = (int) options.valueOf("p");
113 int oscPortNumber = (int) options.valueOf("l");
114 int threads = (int) options.valueOf("t");
115 boolean oscEnabled = true;
116 boolean lfBridgeEnabled = true;
117
118 switch (String.valueOf(options.valueOf("m"))) {
119 case "osc":
120 oscEnabled = true;
121 lfBridgeEnabled = false;
122 break;
123 case "bridge":
124 oscEnabled = false;
125 lfBridgeEnabled = true;
126 break;
127 case "both":
128 break;
129 default:
130 LogUtil.error(this.getClass(), resources.getString("options.mode.invalid"));
131 errorcode = 1;
132 shutdown();
133 return;
134 }
135
136 String host = String.valueOf(options.valueOf("b"));
137 try {
138
139 InetSocketAddress binding = new InetSocketAddress(InetAddress.getByName(host), portNumber);
140 InetSocketAddress oscBinding = new InetSocketAddress(InetAddress.getByName(host), oscPortNumber);
141
142 if (oscEnabled) {
143 LogUtil.info(this.getClass(), MessageFormat.format(resources.getString("osc.listener.on"), host, oscPortNumber));
144 receiver = new OSCPortIn(new DatagramSocket(oscBinding));
145 receiver.addListener(resources.getString("osc.listener.binding"), listener);
146 receiver.startListening();
147
148
149 if (!lfBridgeEnabled) {
150 while (!shutdown) {
151 if (!receiver.isListening() || Thread.currentThread().isInterrupted()) {
152 break;
153 }
154 }
155 }
156
157 }
158 if (lfBridgeEnabled) {
159 try {
160 serverSocket = new ServerSocket(binding.getPort(), threads, binding.getAddress());
161 LogUtil.info(this.getClass(), MessageFormat.format(resources.getString("lf.listener.on"), host, portNumber));
162 while (!shutdown && !Thread.currentThread().isInterrupted()) {
163 new LightFactoryProxyThread(serverSocket.accept()).start();
164 }
165 } catch (IOException e) {
166 if (!shutdown) {
167 LogUtil.fatal(this.getClass(), MessageFormat.format(resources.getString("lf.listener.on.error"),
168 host, portNumber), e);
169 errorcode = 2;
170 }
171 }
172 }
173
174 } catch (UnknownHostException e) {
175 LogUtil.fatal(this.getClass(), MessageFormat.format(resources.getString("daemon.error.unknown.host"), host), e);
176 errorcode = 2;
177 } catch (SocketException e) {
178 LogUtil.fatal(this.getClass(), MessageFormat.format(resources.getString("daemon.error.socket"), host), e);
179 LogUtil.fatal(this.getClass(), "", e);
180 errorcode = 2;
181 } finally {
182 shutdown();
183 }
184 }
185 }