View Javadoc

1   /*
2    * Copyright (c) 2014. Peter Crossley (xley.com)
3    *  Licensed to the Apache Software Foundation (ASF) under one
4    *  or more contributor license agreements.  See the NOTICE file
5    *  distributed with this work for additional information
6    *  regarding copyright ownership.  The ASF licenses this file
7    *  to you under the Apache License, Version 2.0 (the
8    *  "License"); you may not use this file except in compliance
9    *  with the License.  You may obtain a copy of the License at
10   *
11   *    http://www.apache.org/licenses/LICENSE-2.0
12   *
13   *  Unless required by applicable law or agreed to in writing,
14   *  software distributed under the License is distributed on an
15   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   *  KIND, either express or implied.  See the License for the
17   *  specific language governing permissions and limitations
18   *  under the License.
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   * The main OSC proxy thread.
35   */
36  public class LightFactoryProxyThread extends Thread {
37      /**
38       * The constant resources.
39       */
40      public static final ResourceBundle resources = ResourceBundle.getBundle(LightFactoryProxyThread.class.
41              getSimpleName(), Locale.getDefault());
42      /**
43       * An atomic id for each thread instance.
44       */
45      private static final AtomicInteger count = new AtomicInteger();
46  
47      /**
48       * Socket for this thread.
49       */
50      private final Socket socket;
51  
52      /**
53       * Instantiates a new OSC proxy thread.
54       *
55       * @param connection the socket
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                  //do nothing
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  }