001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Mikhail A. Markov
021: * @version $Revision: 1.1.2.3 $
022: */package org.apache.harmony.rmi.transport.proxy;
023:
024: import java.io.DataInputStream;
025: import java.io.DataOutputStream;
026: import java.io.IOException;
027: import java.net.Socket;
028: import java.rmi.RemoteException;
029: import java.rmi.UnmarshalException;
030: import java.rmi.server.UID;
031:
032: import org.apache.harmony.rmi.common.RMILog;
033: import org.apache.harmony.rmi.internal.nls.Messages;
034: import org.apache.harmony.rmi.server.ServerConnection;
035: import org.apache.harmony.rmi.server.ServerConnectionManager;
036:
037: /**
038: * Http extension of ServerConnection.
039: *
040: * @author Mikhail A. Markov
041: * @version $Revision: 1.1.2.3 $
042: */
043: public class HttpServerConnection extends ServerConnection implements
044: ProxyConstants {
045:
046: // If true then this connection was closed.
047: private boolean isClosed = false;
048:
049: /**
050: * Constructs HttpServerConnection working through socket specified.
051: *
052: * @param s Socket connected to the client
053: * @param mgr ConnectionManager managing this connection
054: *
055: * @throws IOException if an I/O error occurred during getting
056: * input/output streams from specified socket
057: */
058: public HttpServerConnection(Socket s, ServerConnectionManager mgr)
059: throws IOException {
060: super (s, mgr);
061: }
062:
063: /**
064: * @see ServerConnection.clientProtocolAck()
065: */
066: protected int clientProtocolAck() throws IOException {
067: byte data;
068: DataInputStream din = new DataInputStream(in);
069:
070: try {
071: // read RMI header
072: int header = din.readInt();
073:
074: if (header != RMI_HEADER) {
075: // rmi.82=Unknown header: {0}
076: throw new UnmarshalException(Messages.getString(
077: "rmi.82", header)); //$NON-NLS-1$
078: }
079:
080: // read RMI protocol version
081: short ver = din.readShort();
082:
083: if (ver != PROTOCOL_VER) {
084: // rmi.83=Unknown RMI protocol version: {0}
085: throw new UnmarshalException(Messages.getString(
086: "rmi.83", ver));//$NON-NLS-1$
087: }
088: } catch (IOException ioe) {
089: // rmi.84=Unable to read RMI protocol header
090: throw new UnmarshalException(Messages.getString("rmi.84"), //$NON-NLS-1$
091: ioe);
092: }
093:
094: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
095: // rmi.85=Using protocol version {0}
096: proxyTransportLog.log(RMILog.VERBOSE, Messages.getString(
097: "rmi.85", //$NON-NLS-1$
098: PROTOCOL_VER));
099: }
100:
101: // read protocol type
102: if (din.readByte() == SINGLEOP_PROTOCOL) {
103:
104: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
105: // rmi.86=Using singleop RMI protocol
106: proxyTransportLog.log(RMILog.VERBOSE, Messages
107: .getString("rmi.86")); //$NON-NLS-1$
108: }
109: } else {
110: return -1;
111: }
112:
113: // protocol is agreed
114: return SINGLEOP_PROTOCOL;
115: }
116:
117: /**
118: * @see ServerConnection.waitCallMsg()
119: */
120: protected int waitCallMsg() throws IOException {
121: if (isClosed) {
122: return -1;
123: }
124: int data;
125:
126: try {
127: data = in.read();
128: } catch (IOException ioe) {
129: data = -1;
130: }
131:
132: if (data == -1) {
133: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
134: // rmi.log.123=Connection [{0}] is closed
135: proxyTransportLog.log(RMILog.VERBOSE, Messages
136: .getString("rmi.log.123", toString())); //$NON-NLS-1$
137: }
138: return -1;
139: }
140: DataOutputStream dout = new DataOutputStream(out);
141:
142: if (data == PING_MSG) {
143: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
144: // rmi.log.124=Got ping request
145: proxyTransportLog.log(RMILog.VERBOSE, Messages
146: .getString("rmi.log.124")); //$NON-NLS-1$
147: }
148: releaseInputStream();
149:
150: // send ping ack
151: dout.writeByte(PING_ACK);
152: dout.close();
153: return -1;
154: } else if (data == DGCACK_MSG) {
155: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
156: // rmi.log.125=Got DGC ack request
157: proxyTransportLog.log(RMILog.VERBOSE, Messages
158: .getString("rmi.log.125")); //$NON-NLS-1$
159: }
160: dgcUnregisterUID(UID.read(new DataInputStream(in)));
161: releaseInputStream();
162: dout.close();
163: return -1;
164: } else if (data == CALL_MSG) {
165: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
166: // rmi.log.126=Got call request
167: proxyTransportLog.log(RMILog.VERBOSE, Messages
168: .getString("rmi.log.126")); //$NON-NLS-1$
169: }
170: return data;
171: } else {
172: if (proxyTransportLog.isLoggable(RMILog.VERBOSE)) {
173: // rmi.log.127=Unknown request got: {0}
174: proxyTransportLog.log(RMILog.VERBOSE, Messages
175: .getString("rmi.log.127", data)); //$NON-NLS-1$
176: }
177: // rmi.87=Unknown message got: {0}
178: throw new RemoteException(Messages
179: .getString("rmi.87", data)); //$NON-NLS-1$
180: }
181: }
182:
183: /**
184: * Closes output stream. After that call this connection is treated as
185: * closed and could be reused.
186: */
187: public synchronized void releaseOutputStream() throws IOException {
188: if (isClosed) {
189: return;
190: }
191: isClosed = true;
192: out.close();
193: }
194:
195: /**
196: * Returns string representation of this connection.
197: *
198: * @return string representation of this connection
199: */
200: public String toString() {
201: return "HttpServerConnection: remote endpoint:" + ep; //$NON-NLS-1$
202: }
203: }
|