View Javadoc
1   /*
2    * The MIT License
3    *
4    * Copyright (c) 2009-, Sun Microsystems, Inc.
5    *
6    * Permission is hereby granted, free of charge, to any person obtaining a copy
7    * of this software and associated documentation files (the "Software"), to deal
8    * in the Software without restriction, including without limitation the rights
9    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10   * copies of the Software, and to permit persons to whom the Software is
11   * furnished to do so, subject to the following conditions:
12   *
13   * The above copyright notice and this permission notice shall be included in
14   * all copies or substantial portions of the Software.
15   *
16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22   * THE SOFTWARE.
23   */
24  package com.sun.akuma;
25  
26  import java.net.ServerSocket;
27  import java.net.Socket;
28  import java.io.InputStream;
29  import java.io.OutputStream;
30  
31  import static com.sun.akuma.CLibrary.LIBC;
32  
33  /**
34   * Sample echo server.
35   *
36   * @author Kohsuke Kawaguchi
37   */
38  public class EchoServer extends NetworkServer {
39      public static void main(String[] args) throws Exception {
40          new EchoServer(args).run();
41      }
42  
43      public EchoServer(String[] args) {
44          super(args);
45      }
46  
47      /**
48       * Daemonize if something is given as arguments.
49       */
50      @Override
51      protected boolean shouldBeDaemonized() {
52          return !arguments.isEmpty();
53      }
54  
55      @Override
56      protected void frontend() throws Exception {
57          System.out.println("This is a simple echo server. Run with some argument to fork into a daemon, then try 'nc localhost 12345' from several terminals.");
58          super.frontend();
59      }
60  
61      @Override
62      protected void forkWorkers(JavaVMArguments args) throws Exception {
63          // TODO: parse arguments and decide how many to fork
64          forkWorkerThreads(args, 2);
65      }
66  
67      @Override
68      protected ServerSocket createServerSocket() throws Exception {
69          System.out.println("Listening on port 12345");
70          // TODO: parse arguments and decide port
71          return new ServerSocket(12345);
72      }
73  
74      @Override
75      protected void worker(ServerSocket ss) throws Exception {
76          byte[] buf = new byte[1024];
77          // run a simple echo server
78          while(true) {
79              Socket s = ss.accept();
80              System.out.println("PID:"+ LIBC.getpid()+" accepted a new connection");
81  
82              InputStream in = s.getInputStream();
83              OutputStream out = s.getOutputStream();
84  
85              int len;
86              while((len=in.read(buf))>=0)
87                  out.write(buf,0,len);
88  
89              s.close();
90          }
91      }
92  }