1 /**
2 * AUTOHIT 2003
3 * Copyright Erich P Gatejen (c) 1989,1997,2003,2004
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at
8 * your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Additional license information can be found in the documentation.
19 * @author Erich P Gatejen
20 */
21 package autohit.server.invoker;
22
23 import autohit.server.BasicBootstrap;
24 import autohit.server.service.CLIService;
25 import autohit.server.service.CommandService;
26 import autohit.server.service.HttpCommandService;
27 import autohit.server.service.SocketRelayService;
28 import autohit.vm.VMProcess;
29
30 /**
31 * A very basic server context
32 *
33 * @author Erich P. Gatejen
34 * @version 1.0 <i>Version History</i><p>
35 * <code>EPG - Initial - 16Sep03<br>
36 * EPG - Add HTTP command service - 5Apr05
37 * </code>
38 */
39 public class BasicServer extends BasicBootstrap {
40
41 /*
42 * Command system
43 */
44 public SimTextCommand cmd;
45
46 /*
47 * Server mode
48 */
49 public boolean serverMode;
50
51 /**
52 * Default constructor. */
53 public BasicServer() throws Exception {
54 throw new Exception("Dont use the default constructor!");
55 }
56
57 /**
58 * Properties constructor. Give it a full path to the properties file. */
59 public BasicServer(String rootProps) throws Exception {
60
61 // CRITICAL TO CALL SUPER
62 super(rootProps);
63 }
64
65 /**
66 * Object main */
67 public void go() throws Exception {
68
69 SocketRelayService srs = new SocketRelayService();
70 VMProcess srsp;
71 CommandService cs = new CommandService();
72 VMProcess csp;
73 CLIService cli = new CLIService();
74 VMProcess clip = null;
75 HttpCommandService hc = new HttpCommandService();
76 VMProcess hcp = null;
77
78 try {
79
80 // START SERVICES
81 srs.loadcontext(sc);
82 srs.init(sc.getRootLogger().sinjector, "SocketRelayService");
83 srsp = this.runService(srs);
84
85 cs.loadcontext(sc);
86 cs.init(sc.getRootLogger().sinjector, "CommandService");
87 csp = this.runService(cs);
88
89 if (serverMode) {
90 hc.loadcontext(sc);
91 hc.init(sc.getRootLogger().sinjector, "HttpCommandService");
92 hcp = this.runService(hc);
93 } else {
94 cli.loadcontext(sc);
95 cli.init(sc.getRootLogger().sinjector, "CLIService");
96 clip = this.runService(cli);
97 }
98
99 } catch (Exception e) {
100 System.out.println("BasicServer startup failed due to exception. message=" + e.getMessage());
101 throw e;
102 }
103
104 // Server loop
105 //synchronized(this) {wait(1000); };
106 //while (true){
107 // if ((clip.getState() < VM.STATE_ACTIVE_THRESHOLD)||(hc.getState() < VM.STATE_ACTIVE_THRESHOLD)) break;
108 // synchronized(this) {wait(5000); };
109 //}
110
111 // Die
112 if (serverMode) {
113 hcp.joinIt();
114 System.out.println("HttpCommandService down.");
115 } else {
116 clip.joinIt();
117 System.out.println("CLIService down.");
118 }
119 System.out.println("Starting shutdown.");
120 srsp.kill();
121 srsp.joinIt();
122 System.out.println("SocketRelayService down.");
123 csp.kill();
124 csp.joinIt();
125 System.out.println("CommandService down.");
126
127 System.out.println("Goodbye.");
128 }
129
130 /**
131 * main interface */
132 public static void main(String[] args) {
133
134 // handle arguments
135 if (args.length == 0) {
136 System.out.println("No .prop specified");
137 return;
138 }
139
140 try {
141 BasicServer me = new BasicServer(args[0]);
142
143 if ((args.length > 1)&&(args[1].toLowerCase().equals("server"))) {
144 me.serverMode = true;
145 System.out.println("Starting in server mode.");
146 }
147
148 me.go();
149 } catch (Exception e) {
150 e.printStackTrace();
151 }
152 }
153
154 }
|