KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > rmi > iiop > server > SocketListener


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.rmi.iiop.server;
20
21 import gcc.*;
22 import gcc.properties.*;
23 import gcc.rmi.iiop.*;
24 import java.net.*;
25
26 public class SocketListener extends Thread
27 {
28     public static SocketListener getInstance()
29     {
30         return new SocketListener();
31     }
32
33     // private data
34

35     private String _name;
36
37     private String _host;
38
39     private int _port;
40
41     private int _listenBacklog;
42
43     private java.net.ServerSocket _listener;
44
45     // internal methods
46

47     protected void init()
48     {
49         _host = "localhost";
50         _port = 2000;
51         _listenBacklog = 10;
52         setDaemon(true);
53     }
54
55     // public methods
56

57     public void setHost(String host)
58     {
59         _host = host;
60     }
61
62     public void setPort(int port)
63     {
64         _port = port;
65     }
66
67     public void setListenBacklog(int backlog)
68     {
69         _listenBacklog = backlog;
70     }
71
72     public void run()
73     {
74         String iiopURL = "iiop://" + _host + ":" + _port;
75         ListenerInfo listenerInfo = new ListenerInfo();
76         listenerInfo.protocol = Protocol.IIOP; // TODO: other protocols (IIOPS etc.)
77
listenerInfo.host = _host;
78         listenerInfo.port = _port;
79         try
80         {
81             InetAddress addr = InetAddress.getByName(_host);
82             _listener = new java.net.ServerSocket(_port, _listenBacklog, addr);
83         }
84         catch (Exception ex)
85         {
86             System.out.println( "SocketListener: Error creating server socket." );
87             ex.printStackTrace();
88             try
89             {
90                 Socket socket = new Socket(_host, _port);
91                 socket.close();
92                 System.out.println( "SocketListener: Error server already running: " + iiopURL );
93                 ex.printStackTrace();
94             }
95             catch (Exception ignore)
96             {
97             }
98             return;
99         }
100         new CheckConnect().start();
101         for (;;)
102         {
103             java.net.Socket socket;
104             try
105             {
106                 socket = _listener.accept();
107             }
108             catch (Exception ex)
109             {
110                 throw new SystemException(ex); // TODO: log error message
111
}
112             MessageHandler.getInstance(listenerInfo, socket).start();
113         }
114     }
115
116     private class CheckConnect extends Thread
117     {
118         public void run()
119         {
120             try
121             {
122                 Socket socket = new Socket(_host, _port);
123                 socket.close();
124                 if (! SystemProperties.quiet())
125                 {
126                     System.out.println(formatAcceptingIiopConnections());
127                 }
128             }
129             catch (Exception ex)
130             {
131                 warnConnectFailed(_host, _port);
132             }
133         }
134     }
135
136     // format methods
137

138     protected String formatAcceptingIiopConnections()
139     {
140         String msg = "SocketListener.formatAcceptingIiopConnection(): ";
141         return msg;
142     }
143
144     // log methods
145

146     protected void warnConnectFailed(String host, int port)
147     {
148         System.out.println( "SocketListener.warnConnectFailed(): host = " + host + ", port = " + port );
149     }
150 }
151
Popular Tags