/**
*
* Copyright 2004 Protique Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.codehaus.activemq.transport.vm;
import EDU.oswego.cs.dl.util.concurrent.BoundedLinkedQueue;
import EDU.oswego.cs.dl.util.concurrent.Channel;
import EDU.oswego.cs.dl.util.concurrent.SynchronizedBoolean;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.activemq.message.Packet;
import org.codehaus.activemq.transport.AbstractTransportChannel;
import org.codehaus.activemq.transport.TransportChannel;
import javax.jms.JMSException;
/**
* A VM implementation of a TransportChannel
*
* @version $Revision: 1.17 $
*/
public class VmTransportChannel extends AbstractTransportChannel implements Runnable {
private static final Log log = LogFactory.getLog(VmTransportChannel.class);
private static final Object TERMINATE = new Object();
private Channel sendChannel;
private Channel receiveChannel;
private SynchronizedBoolean closed;
private SynchronizedBoolean started;
private Thread thread; //need to change this - and use a thread pool
private static int lastThreadId = 0; // To number the threads.
public VmTransportChannel(Channel sendChannel, Channel receiveChannel) {
this.sendChannel = sendChannel;
this.receiveChannel = receiveChannel;
closed = new SynchronizedBoolean(false);
started = new SynchronizedBoolean(false);
}
public VmTransportChannel() {
this(1000);
}
public VmTransportChannel(int capacity) {
this(new BoundedLinkedQueue(capacity), new BoundedLinkedQueue(capacity));
}
/**
* close the channel
*/
public void stop() {
if (closed.commit(false, true)) {
super.stop();
try {
// to close the channel, lets send a null
sendChannel.put(TERMINATE);
// lets wait for the receive thread to terminate
thread.join();
}
catch (Exception e) {
log.trace(toString() + " now closed with exception: " + e);
}
}
}
/**
* start listeneing for events
*
* @throws JMSException if an error occurs
*/
public void start() throws JMSException {
if (started.commit(false, true)) {
thread = new Thread(this, "VM Transport: " + getNextThreadId());
thread.setDaemon(true);
thread.start();
}
}
static synchronized public int getNextThreadId() {
return lastThreadId++;
}
/**
* Asynchronously send a Packet
*
* @param packet
* @throws JMSException
*/
public void asyncSend(Packet packet) throws JMSException {
while (true) {
try {
sendChannel.put(packet);
break;
}
catch (InterruptedException e) {
// continue
}
}
}
public boolean isMulticast() {
return false;
}
/**
* reads packets from a Socket
*/
public void run() {
while (!closed.get()) {
try {
Object answer = receiveChannel.take();
if (answer == TERMINATE) {
log.trace("The socket peer is now closed");
stop();
return;
}
else if (answer != null) {
Packet packet = (Packet) answer;
// we might have just got a packet in but we've already shut down
if (closed.get()) {
break;
}
doConsumePacket(packet);
}
}
catch (InterruptedException e) {
// continue
}
}
}
private void doClose(Exception ex) {
if (!closed.get()) {
JMSException jmsEx = new JMSException("Error reading socket: " + ex.getMessage());
jmsEx.setLinkedException(ex);
onAsyncException(jmsEx);
stop();
}
}
/**
* pretty print for object
*
* @return String representation of this object
*/
public String toString() {
return "VmTransportChannel: " + sendChannel;
}
/**
* Creates the server side version of this client side channel. On the server side
* the client's side sendChannel is the receiveChannel and vice versa
*
* @return
*/
public TransportChannel createServerSide() throws JMSException {
return new VmTransportChannel(receiveChannel, sendChannel);
}
}