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.impl;
22
23 import com.xley.lfosc.util.LogUtil;
24
25 import java.io.*;
26 import java.net.Socket;
27 import java.nio.charset.Charset;
28 import java.text.MessageFormat;
29 import java.util.Locale;
30 import java.util.ResourceBundle;
31 import java.util.concurrent.atomic.AtomicInteger;
32
33
34
35
36 public class LightFactoryProxyThread extends Thread {
37
38
39
40 public static final ResourceBundle resources = ResourceBundle.getBundle(LightFactoryProxyThread.class.
41 getSimpleName(), Locale.getDefault());
42
43
44
45 private static final AtomicInteger count = new AtomicInteger();
46
47
48
49
50 private final Socket socket;
51
52
53
54
55
56
57 public LightFactoryProxyThread(final Socket connection) {
58 super("LightFactoryProxyThread - " + count.incrementAndGet());
59 this.socket = connection;
60 }
61
62 public final void run() {
63 LogUtil.info(this.getClass(), MessageFormat.format(resources.getString("osc.connection.established"),
64 socket.getInetAddress()));
65 try (
66 PrintWriter out = new PrintWriter(new OutputStreamWriter(socket.getOutputStream(),
67 Charset.defaultCharset()), true);
68 BufferedReader in = new BufferedReader(
69 new InputStreamReader(
70 socket.getInputStream(), Charset.defaultCharset()))
71 ) {
72 String inputLine, outputLine;
73 LightFactoryProtocol opp = new LightFactoryProtocol();
74 if (socket.isConnected() && (inputLine = in.readLine()) != null) {
75 inputLine = inputLine.trim();
76 LogUtil.trace(this.getClass(), ">> " + inputLine);
77 outputLine = opp.process(inputLine);
78 out.println(outputLine);
79 LogUtil.trace(this.getClass(), "<< " + outputLine);
80 }
81 } catch (IOException e) {
82 LogUtil.error(this.getClass(), resources.getString("osc.connection.error"), e);
83 } finally {
84 try {
85 socket.close();
86 } catch (IOException e) {
87
88 LogUtil.trace(this.getClass(), e);
89 }
90 LogUtil.info(this.getClass(), MessageFormat.format(resources.getString("osc.connection.disconnected"),
91 socket.getInetAddress().getHostAddress()));
92 }
93 }
94 }