View Javadoc
1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one or more
3    *  contributor license agreements.  See the NOTICE file distributed with
4    *  this work for additional information regarding copyright ownership.
5    *  The ASF licenses this file to You under the Apache License, Version 2.0
6    *  (the "License"); you may not use this file except in compliance with
7    *  the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  package org.apache.commons.daemon;
18  
19  import java.io.BufferedOutputStream;
20  import java.io.FileOutputStream;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.io.PrintStream;
25  import java.net.ServerSocket;
26  import java.net.Socket;
27  
28  class ProcrunDaemon {
29  
30      /* The server does nothing more than the following:
31       - 1 Just stop and do nothing more.
32       - 2 wait 60 second and stop.
33       */
34      public static void server() {
35          System.out.println("TestServ");
36          try (ServerSocket serverSocket = new ServerSocket(4444, 2)) {
37              /* Wait for clients */
38              for (;;) {
39                  Socket clientSocket = null;
40                  try {
41                      clientSocket = serverSocket.accept();
42                      ProcessClient(clientSocket);
43                  } catch (IOException e) {
44                      System.out.println("Accept or Client failed: 4444");
45                      System.exit(-1);
46                  }
47              }
48          } catch (IOException e) {
49              System.out.println("Could not listen on port: 4444");
50              System.exit(-1);
51          }
52  
53      }
54  
55      public static void ProcessClient(Socket clientSocket) {
56          byte[] buf = new byte[1024];
57          int num;
58          InputStream clientInput = null;
59          try {
60              clientInput = clientSocket.getInputStream();
61              while ((num = clientInput.read(buf)) != -1) {
62                  System.out.println(".");
63                  if (buf[0] == '0') {
64                      /* We just use 0 to check that the service is started */
65                      System.out.println("0");
66                      return;
67                  } else if (buf[0] == '1') {
68                      System.out.println("1");
69                      System.exit(0);
70                  } else if (buf[0] == '2') {
71                      System.out.println("2");
72                      try {
73                          Thread.sleep(60000);
74                      } catch (Exception ex) {
75                          System.out.println(ex);
76                      }
77                      System.exit(0);
78                  } else if (buf[0] == '5') {
79                      System.out.println("5");
80                      /* Note that System.out has been redirected to client.txt in main() */
81                      PrintStream myout = System.out;
82                      System.setOut(originalStdout);
83                      System.out.println("Using System.out");
84                      System.out.println("5");
85                      System.out.println("Back to redirection");
86                      System.setOut(myout);
87                  } else if (buf[0] == '6') {
88                      System.out.println("6");
89                      System.err.println("Using System.err");
90                      System.err.println("6");
91                  }
92              }
93              System.out.println("num " + num);
94          } catch (IOException e) {
95              System.out.println("Could not read from port: 4444");
96              System.exit(-1);
97          }
98  
99          try {
100             while ((num = clientInput.read(buf)) != -1) {
101                 System.out.println(".");
102             }
103             System.out.println("num " + num);
104         } catch (IOException e) {
105             System.out.println("Could not read from port: 4444");
106             System.exit(-1);
107         }
108 
109     }
110 
111     /* client piece */
112  /* The client processes the command and sends it to server */
113     public static void client(String string) {
114         System.out.println(string);
115         if (string.charAt(0) == '3' || string.charAt(0) == '4') {
116             /* Wait 60 seconds in the client, then sends the command to the server */
117             try {
118                 Thread.sleep(60000);
119             } catch (Exception ex) {
120                 System.out.println(ex);
121             }
122             if (string.charAt(0) == '3') {
123                 string = "1";
124             } else if (string.charAt(0) == '4') {
125                 string = "2"; /* The server will wait 60 seconds after the stop command */
126             }
127         }
128         try {
129             Socket connection = new Socket("127.0.0.1", 4444);
130             OutputStream os = connection.getOutputStream();
131             byte[] value = string.getBytes();
132             os.write(value);
133             os.flush();
134             connection.close();
135         } catch (Exception ex) {
136             System.out.println(ex);
137             System.exit(1);
138         }
139         System.exit(0);
140     }
141 
142     static PrintStream originalStdout;
143 
144     /* client server test for procrun */
145     public static void main(String[] argv) {
146         originalStdout = System.out;
147         if (argv.length != 0) {
148             /* Just send the command to the server */
149             try {
150                 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("client.txt")), true));
151             } catch (Exception ex) {
152                 System.out.println(ex);
153             }
154             client(argv[0]);
155         } else {
156             /* start server */
157             try {
158                 System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("server.txt")), true));
159             } catch (Exception ex) {
160                 System.out.println(ex);
161             }
162             server();
163         }
164     }
165 
166     /* For the jvm mode */
167     private static volatile Thread thrd; // start and stop are called from different threads
168 
169     public static void start(String[] argv) {
170         try {
171             System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("jvm.txt")), true));
172         } catch (Exception ex) {
173             System.out.println(ex);
174             System.exit(1);
175         }
176         if (argv.length != 0) {
177             System.out.println("start: " + argv[0]);
178         } else {
179             System.out.println("start no argv");
180         }
181         thrd = new Thread() {
182             @Override
183             public void run() {
184                 server();
185             }
186         };
187         thrd.start();
188         while (thrd.isAlive()) {
189             try {
190                 thrd.join();
191             } catch (InterruptedException ie) {
192                 System.out.println(ie);
193             }
194         }
195         System.out.println("start Thread finished");
196     }
197 
198     public static void stop(String[] argv) {
199         if (argv.length != 0) {
200             System.out.println("stop: " + argv[0]);
201             String string = argv[0];
202             if (string.charAt(0) == '3' || string.charAt(0) == '4') {
203                 /* Wait 60 seconds in the stop */
204                 try {
205                     Thread.sleep(60000);
206                 } catch (Exception ex) {
207                     System.out.println(ex);
208                 }
209             }
210         } else {
211             System.out.println("stop no argv");
212         }
213         /* just stop the thread! */
214         if (thrd != null) {
215             System.out.println("stop: interrupt Thread");
216             thrd.interrupt();
217         } else {
218             System.out.println("stop: Oops no Thread");
219         }
220     }
221 }