View Javadoc

1   package org.apache.commons.net.time;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * 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, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import java.io.DataOutputStream;
21  import java.io.IOException;
22  import java.net.ServerSocket;
23  import java.net.Socket;
24  
25  /**
26   * The TimetSimpleServer class is a simple TCP implementation of a server
27   * for the Time Protocol described in RFC 868.
28   * <p>
29   * Listens for TCP socket connections on the time protocol port and writes
30   * the local time to socket outputStream as 32-bit integer of seconds
31   * since midnight on 1 January 1900 GMT.
32   * See <A HREF="ftp://ftp.rfc-editor.org/in-notes/rfc868.txt"> the spec </A> for
33   * details.
34   * <p>
35   * Note this is for <B>debugging purposes only</B> and not meant to be run as a realiable time service.
36   *
37   * @author Jason Mathews, MITRE Corporation
38   *
39   * @version $Revision: 1489361 $
40   */
41  public class TimeTestSimpleServer implements Runnable
42  {
43  
44      /**
45       * baseline time 1900-01-01T00:00:00 UTC
46       */
47      public static final long SECONDS_1900_TO_1970 = 2208988800L;
48  
49      /*** The default time port.  It is set to 37 according to RFC 868. ***/
50      public static final int DEFAULT_PORT = 37;
51  
52      private ServerSocket server;
53      private final int port;
54      private boolean running = false;
55  
56      /**
57       * Default constructor for TimetSimpleServer.
58       * Initializes port to defaul time port.
59       */
60      public TimeTestSimpleServer()
61      {
62          port = DEFAULT_PORT;
63      }
64  
65      /**
66       * Constructor for TimetSimpleServer given a specific port.
67       */
68      public TimeTestSimpleServer(int port)
69      {
70          this.port = port;
71      }
72  
73      public void connect() throws IOException
74      {
75          if (server == null)
76          {
77              server = new ServerSocket(port);
78          }
79      }
80  
81      public int getPort()
82      {
83          return server == null ? port : server.getLocalPort();
84      }
85  
86      public boolean isRunning()
87      {
88          return running;
89      }
90  
91      /**
92       * Start time service and provide time to client connections.
93       * @throws IOException
94       */
95      public void start() throws IOException
96      {
97          if (server == null)
98      {
99              connect();
100     }
101     if (!running)
102     {
103         running = true;
104         new Thread(this).start();
105     }
106     }
107 
108 //    @Override
109     public void run()
110     {
111         Socket socket = null;
112         while (running)
113         {
114             try
115             {
116                 socket = server.accept();
117                 DataOutputStream os = new DataOutputStream(socket.getOutputStream());
118                 // add 500 ms to round off to nearest second
119                 int time = (int) ((System.currentTimeMillis() + 500) / 1000 + SECONDS_1900_TO_1970);
120                 os.writeInt(time);
121                 os.flush();
122             } catch (IOException e)
123             {
124             } finally
125             {
126                 if (socket != null) {
127                     try
128                     {
129                         socket.close();  // force closing of the socket
130                     } catch (IOException e)
131                     {
132                         System.err.println("close socket error: " + e);
133                     }
134                 }
135             }
136         }
137     }
138 
139     /**
140      * Close server socket.
141      */
142     public void stop()
143     {
144         running = false;
145         if (server != null)
146         {
147             try
148             {
149                 server.close();  // force closing of the socket
150             } catch (IOException e)
151             {
152                 System.err.println("close socket error: " + e);
153             }
154             server = null;
155         }
156     }
157 
158     public static void main(String[] args)
159     {
160         TimeTestSimpleServer server = new TimeTestSimpleServer();
161         try
162         {
163             server.start();
164         } catch (IOException e)
165         {
166         }
167     }
168 
169 }