001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.mq.il.oil;
023:
024: import java.io.BufferedInputStream;
025: import java.io.BufferedOutputStream;
026: import java.io.IOException;
027: import java.io.ObjectInputStream;
028: import java.io.ObjectOutputStream;
029: import java.net.InetAddress;
030: import java.net.Socket;
031: import java.rmi.RemoteException;
032:
033: import org.jboss.logging.Logger;
034: import org.jboss.mq.ReceiveRequest;
035: import org.jboss.mq.SpyDestination;
036: import org.jboss.mq.il.ClientIL;
037:
038: /**
039: * The RMI implementation of the ConnectionReceiver object
040: *
041: * @author Norbert Lataille (Norbert.Lataille@m4x.org)
042: * @author Hiram Chirino (Cojonudo14@hotmail.com)
043: * @version $Revision: 57198 $
044: * @created August 16, 2001
045: */
046: public final class OILClientIL implements ClientIL,
047: java.io.Serializable {
048: static final long serialVersionUID = 7812173621233374692L;
049: private final static Logger log = Logger
050: .getLogger(OILClientIL.class);
051:
052: private InetAddress addr;
053: private int port;
054: /**
055: * If the TcpNoDelay option should be used on the socket.
056: */
057: protected boolean enableTcpNoDelay = false;
058:
059: private transient ObjectInputStream in;
060: private transient ObjectOutputStream out;
061: private transient Socket socket;
062:
063: OILClientIL(InetAddress addr, int port, boolean enableTcpNoDelay) {
064: this .addr = addr;
065: this .port = port;
066: this .enableTcpNoDelay = enableTcpNoDelay;
067: }
068:
069: /**
070: * #Description of the Method
071: *
072: * @exception Exception Description of Exception
073: */
074: public synchronized void close() throws Exception {
075: if (log.isTraceEnabled())
076: log.trace("Closing OILClientIL");
077: checkSocket();
078: out.writeByte(OILConstants.CLOSE);
079: waitAnswer();
080: try {
081: socket.close();
082: in.close();
083: out.close();
084: } catch (Exception e) {
085: if (log.isDebugEnabled())
086: log.debug("Error closing the socket connection", e);
087: }
088: }
089:
090: /**
091: * #Description of the Method
092: *
093: * @param dest Description of Parameter
094: * @exception Exception Description of Exception
095: */
096: public synchronized void deleteTemporaryDestination(
097: SpyDestination dest) throws Exception {
098: checkSocket();
099: out.writeByte(OILConstants.DELETE_TEMPORARY_DESTINATION);
100: out.writeObject(dest);
101: waitAnswer();
102: }
103:
104: /**
105: * #Description of the Method
106: *
107: * @param serverTime Description of Parameter
108: * @exception Exception Description of Exception
109: */
110: public synchronized void pong(long serverTime) throws Exception {
111: checkSocket();
112: out.writeByte(OILConstants.PONG);
113: out.writeLong(serverTime);
114: waitAnswer();
115: }
116:
117: /**
118: * #Description of the Method
119: *
120: * @param messages Description of Parameter
121: * @exception Exception Description of Exception
122: */
123: public synchronized void receive(ReceiveRequest messages[])
124: throws Exception {
125: boolean trace = log.isTraceEnabled();
126: if (trace)
127: log.trace("Checking socket");
128: checkSocket();
129: if (trace)
130: log.trace("Writing request");
131: out.writeByte(OILConstants.RECEIVE);
132: out.writeInt(messages.length);
133: for (int i = 0; i < messages.length; ++i) {
134: messages[i].writeExternal(out);
135: }
136: if (trace)
137: log.trace("Waiting for anwser");
138: waitAnswer();
139: if (trace)
140: log.trace("Done");
141: }
142:
143: /**
144: * #Description of the Method
145: *
146: * @exception Exception Description of Exception
147: */
148: private void checkSocket() throws RemoteException {
149: if (socket == null) {
150: createConnection();
151: }
152: }
153:
154: /**
155: * #Description of the Method
156: *
157: * @exception RemoteException Description of Exception
158: */
159: private void createConnection() throws RemoteException {
160: try {
161: if (log.isDebugEnabled()) {
162: log
163: .debug("ConnectionReceiverOILClient is connecting to: "
164: + addr.getHostAddress() + ":" + port);
165: }
166:
167: socket = new Socket(addr, port);
168: out = new ObjectOutputStream(new BufferedOutputStream(
169: socket.getOutputStream()));
170: out.flush();
171: in = new ObjectInputStream(new BufferedInputStream(socket
172: .getInputStream()));
173: } catch (Exception e) {
174: log.error(
175: "Cannot connect to the ConnectionReceiver/Server",
176: e);
177: throw new RemoteException(
178: "Cannot connect to the ConnectionReceiver/Server");
179: }
180: }
181:
182: /**
183: * #Description of the Method
184: *
185: * @exception Exception Description of Exception
186: */
187: private void waitAnswer() throws Exception {
188: Exception throwException = null;
189: try {
190: out.reset();
191: out.flush();
192: int val = in.readByte();
193: switch (val) {
194: case OILConstants.EXCEPTION:
195: Exception e = (Exception) in.readObject();
196: throwException = new RemoteException("", e);
197: break;
198: }
199: } catch (IOException e) {
200: throw new RemoteException(
201: "Cannot contact the remote object", e);
202: }
203:
204: if (throwException != null) {
205: throw throwException;
206: }
207: }
208: }
209: // vim:expandtab:tabstop=3:shiftwidth=3
|