KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > orb > http > ServerConnection


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.orb.http;
22
23 import java.net.*;
24 import java.util.*;
25 import java.io.*;
26
27 import org.jacorb.util.*;
28 import org.jacorb.orb.*;
29 import org.jacorb.orb.giop.*;
30
31 //Note: HTTPServerConnections are good for one incoming Request only!
32

33 public final class ServerConnection
34     extends org.jacorb.orb.connection.ServerConnection
35 {
36     private boolean connected = true;
37     private int readcount = 0;
38     private boolean readyParsed = false;
39     Object JavaDoc oldrequest;
40     Object JavaDoc requestwait = new Object JavaDoc();
41     org.jacorb.orb.http.httpserver.ServeConnection realCon;
42     Socket mysock;
43
44     private ORB orb = null;
45     private int client_count = 0;
46
47     public ServerConnection( org.jacorb.orb.ORB orb,
48                              boolean is_ssl,
49                              Socket s,
50                              java.io.InputStream JavaDoc _in_stream)
51         throws IOException
52     {
53         this.orb = orb;
54     mysock = s;
55         isSSL = is_ssl;
56
57     in_stream = new BufferedInputStream(_in_stream);
58     realCon =
59             new org.jacorb.orb.http.httpserver.ServeConnection(s,_in_stream);
60     String JavaDoc ip = mysock.getInetAddress().getHostAddress();
61     if( ip.indexOf('/') > 0)
62         ip = ip.substring( ip.indexOf('/') + 1 );
63     String JavaDoc host_and_port = ip + ":"+ mysock.getPort();
64     String JavaDoc ssl = isSSL() ? "SSL " : ""; //bnv
65
connection_info = host_and_port;
66     client_count = 1;
67     org.jacorb.util.Debug.output(1,"Accepted HTTP connection from " + host_and_port);
68
69     }
70
71     protected void abort()
72     throws EOFException
73     {
74         org.jacorb.util.Debug.output(3,"HTTPServerConnection to " + connection_info + " aborting...");
75         throw new EOFException();
76
77     }
78
79     /**
80      * socket closing etc is done after each single HTTP Connection
81      */

82
83     public synchronized void closeConnection()
84     {
85         connected=false;
86         realCon.close();
87     }
88
89     public boolean connected()
90     {
91         return connected;
92     }
93
94     public byte[] readBuffer()
95         throws java.io.IOException JavaDoc
96     {
97         readcount++;
98         in_stream = realCon.getInputStream();
99         return super.readBuffer();
100     }
101
102     public synchronized void reconnect()
103     throws org.omg.CORBA.COMM_FAILURE JavaDoc
104     {
105     connected=true;
106     }
107
108     /**
109      * send a "close connection" message to the other side.
110      */

111
112     public synchronized void sendCloseConnection()
113         throws org.omg.CORBA.COMM_FAILURE JavaDoc
114     {
115         String JavaDoc iaddr=mysock.getInetAddress().toString()+mysock.getPort();
116         //BasicAdapter ba = orb.getBasicAdapter();
117

118         try
119         {
120             realCon.answerRequest(Messages.closeConnectionMessage());
121             realCon.close();
122         }
123         catch ( Exception JavaDoc e )
124         {
125             throw new org.omg.CORBA.COMM_FAILURE JavaDoc
126                 (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
127         }
128     }
129
130     /**
131      *
132      */

133
134     public void sendLocateReply( int request_id,
135                                  int status,
136                                  org.omg.CORBA.Object JavaDoc arg,
137                                  int giop_minor )
138         throws org.omg.CORBA.COMM_FAILURE JavaDoc
139     {
140     /**
141      * called from dsi/ServerRequest
142      */

143
144         synchronized( writeLock )
145     {
146             String JavaDoc iaddr=mysock.getInetAddress().toString()+mysock.getPort();
147             // BasicAdapter ba = orb.getBasicAdapter();
148
try
149         {
150
151         realCon.answerRequest(Messages.locateReplyMessage( request_id,
152                                                                    status,
153                                                                    arg,
154                                                                    giop_minor ));
155         realCon.close();
156
157         }
158         catch ( Exception JavaDoc e )
159         {
160         org.jacorb.util.Debug.output(2,e);
161         throw new org.omg.CORBA.COMM_FAILURE JavaDoc
162                     (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
163         }
164     }
165     }
166
167     public void sendReply( ReplyOutputStream os )
168         throws IOException
169     {
170     synchronized( writeLock )
171     {
172             String JavaDoc iaddr=mysock.getInetAddress().toString()+mysock.getPort();
173             // BasicAdapter ba=orb.getBasicAdapter();
174
try
175         {
176         byte buf [];
177                 // if( Environment.serverInterceptMessages())
178
// {
179
// buf = orb.server_messageIntercept_post(os.getBufferCopy());
180
// realCon.answerRequest(buf);
181
// }
182
// else
183
// {
184
// org.jacorb.util.Debug.output(3,"bypassing any message-level interceptors");
185
if (os!=null)
186                 {
187                     realCon.answerRequest(os.getBufferCopy());
188                     os.close();
189                 }
190                 else
191                 { //dummy reply
192
byte[] dummy={0};
193                     realCon.answerRequest(dummy);
194                 }
195                 //org.jacorb.util.Debug.output(1,"sendReply",os.getBuffer(true));
196

197                 // }
198
// org.jacorb.util.Debug.output(3,"bypassing any message-level interceptors");
199
realCon.close();
200         }
201         catch ( Exception JavaDoc e )
202         {
203         org.jacorb.util.Debug.output(2,e);
204         throw new org.omg.CORBA.COMM_FAILURE JavaDoc
205                     (0, org.omg.CORBA.CompletionStatus.COMPLETED_MAYBE);
206         }
207     }
208     }
209
210
211     public void setTimeOut(int timeout)
212     throws SocketException
213     {
214     //do nothing here
215
}
216 }
217
Popular Tags