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;
22  
23  import com.xley.lfosc.util.LogUtil;
24  import joptsimple.OptionParser;
25  import joptsimple.OptionSet;
26  import org.apache.log4j.Level;
27  
28  import java.io.IOException;
29  import java.util.Locale;
30  import java.util.ResourceBundle;
31  
32  /**
33   * The primary OSC proxy class.
34   */
35  public class OSCProxy {
36  
37      /**
38       * The constant resources.
39       */
40      public static final ResourceBundle resources = ResourceBundle.getBundle(OSCProxy.class.getSimpleName(),
41              Locale.getDefault());
42  
43      /**
44       * Instantiates a new OSC proxy.
45       */
46      public OSCProxy() {
47      }
48  
49      /**
50       * The entry point of application.
51       *
52       * @param args the input arguments
53       */
54      public static void main(final String[] args) {
55          System.exit(new OSCProxy().execute(args));
56      }
57  
58      /**
59       * Execution workflow for LightFactory-OSC Proxy.
60       *
61       * @param args the args normally from the command line
62       * @return the error code of the exiting process
63       */
64      public final int execute(final String[] args) {
65          OptionParser parser = new OptionParser() {
66              {
67                  accepts("p").withOptionalArg().ofType(Integer.class)
68                          .describedAs(resources.getString("option.lf.port.desc"))
69                          .defaultsTo(Integer.parseInt(resources.getString("option.lf.port.default")));
70                  accepts("l").withOptionalArg().ofType(Integer.class)
71                          .describedAs(resources.getString("option.osc.port.desc"))
72                          .defaultsTo(Integer.parseInt(resources.getString("option.osc.port.default")));
73                  accepts("m").withOptionalArg().ofType(String.class)
74                          .describedAs(resources.getString("option.mode.desc"))
75                          .defaultsTo(resources.getString("option.mode.default"));
76                  accepts("b").withOptionalArg().ofType(String.class)
77                          .describedAs(resources.getString("option.bind.address.desc"))
78                          .defaultsTo(resources.getString("option.bind.address.default"));
79                  accepts("t").withOptionalArg().ofType(Integer.class)
80                          .describedAs(resources.getString("option.socket.threads.desc"))
81                          .defaultsTo(Integer.parseInt(resources.getString("option.socket.threads.default")));
82                  accepts("d").withOptionalArg().ofType(String.class)
83                          .describedAs(resources.getString("option.verbosity.desc"));
84                  accepts("?").withOptionalArg().describedAs(resources.getString("option.help.desc"));
85              }
86          };
87          OptionSet options = parser.parse(args);
88          if (options.has("?")) {
89              System.out.println(resources.getString("console.header.1"));
90              try {
91                  parser.printHelpOn(System.out);
92              } catch (IOException e) {
93                  LogUtil.error(e);
94              }
95              return 0;
96          }
97  
98          System.out.println(resources.getString("console.header.1"));
99          System.out.println(resources.getString("console.header.2"));
100 
101         if (options.has("d")) {
102             LogUtil.setLevel(Level.toLevel(((String) options.valueOf("d")).toUpperCase()));
103         }
104 
105         //start the main thread
106         ProxyDaemon daemon = new ProxyDaemon(options);
107         Thread mainThread = new Thread(daemon, "OSCProxy - Daemon");
108         mainThread.setDaemon(true);
109         mainThread.start();
110 
111         while (mainThread.isAlive()) {
112             if (Thread.interrupted()) {
113                 daemon.shutdown();
114                 break;
115             }
116         }
117 
118         LogUtil.info(resources.getString("shutdown.complete"));
119         return daemon.errorcode();
120     }
121 
122 }