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 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.activemq.transport.stomp; 018 019import java.io.ByteArrayInputStream; 020import java.io.DataOutputStream; 021import java.io.EOFException; 022import java.io.IOException; 023import java.net.Socket; 024import java.net.URI; 025import java.net.UnknownHostException; 026import java.nio.ByteBuffer; 027import java.nio.channels.SelectionKey; 028import java.nio.channels.SocketChannel; 029 030import javax.net.SocketFactory; 031 032import org.apache.activemq.transport.Transport; 033import org.apache.activemq.transport.nio.NIOOutputStream; 034import org.apache.activemq.transport.nio.SelectorManager; 035import org.apache.activemq.transport.nio.SelectorSelection; 036import org.apache.activemq.transport.tcp.TcpTransport; 037import org.apache.activemq.util.IOExceptionSupport; 038import org.apache.activemq.util.ServiceStopper; 039import org.apache.activemq.wireformat.WireFormat; 040 041/** 042 * An implementation of the {@link Transport} interface for using Stomp over NIO 043 */ 044public class StompNIOTransport extends TcpTransport { 045 046 private SocketChannel channel; 047 private SelectorSelection selection; 048 049 private ByteBuffer inputBuffer; 050 StompCodec codec; 051 052 public StompNIOTransport(WireFormat wireFormat, SocketFactory socketFactory, URI remoteLocation, URI localLocation) throws UnknownHostException, IOException { 053 super(wireFormat, socketFactory, remoteLocation, localLocation); 054 } 055 056 public StompNIOTransport(WireFormat wireFormat, Socket socket) throws IOException { 057 super(wireFormat, socket); 058 } 059 060 public StompNIOTransport(WireFormat wireFormat, Socket socket, InitBuffer initBuffer) throws IOException { 061 super(wireFormat, socket, initBuffer); 062 } 063 064 @Override 065 protected void initializeStreams() throws IOException { 066 channel = socket.getChannel(); 067 channel.configureBlocking(false); 068 069 // listen for events telling us when the socket is readable. 070 selection = SelectorManager.getInstance().register(channel, new SelectorManager.Listener() { 071 @Override 072 public void onSelect(SelectorSelection selection) { 073 serviceRead(); 074 } 075 076 @Override 077 public void onError(SelectorSelection selection, Throwable error) { 078 if (error instanceof IOException) { 079 onException((IOException)error); 080 } else { 081 onException(IOExceptionSupport.create(error)); 082 } 083 } 084 }); 085 086 inputBuffer = ByteBuffer.allocate(8 * 1024); 087 NIOOutputStream outPutStream = new NIOOutputStream(channel, 8 * 1024); 088 this.dataOut = new DataOutputStream(outPutStream); 089 this.buffOut = outPutStream; 090 codec = new StompCodec(this); 091 092 try { 093 if (initBuffer != null) { 094 processBuffer(initBuffer.buffer, initBuffer.readSize); 095 } 096 } catch (IOException e) { 097 onException(e); 098 } catch (Throwable e) { 099 onException(IOExceptionSupport.create(e)); 100 } 101 } 102 103 private void serviceRead() { 104 try { 105 while (true) { 106 // read channel 107 int readSize = channel.read(inputBuffer); 108 109 // channel is closed, cleanup 110 if (readSize == -1) { 111 onException(new EOFException()); 112 selection.close(); 113 break; 114 } 115 116 // nothing more to read, break 117 if (readSize == 0) { 118 break; 119 } 120 121 processBuffer(inputBuffer, readSize); 122 } 123 } catch (IOException e) { 124 onException(e); 125 } catch (Throwable e) { 126 onException(IOExceptionSupport.create(e)); 127 } 128 } 129 130 protected void processBuffer(ByteBuffer buffer, int readSize) throws Exception { 131 receiveCounter += readSize; 132 133 buffer.flip(); 134 135 ByteArrayInputStream input = new ByteArrayInputStream(buffer.array()); 136 codec.parse(input, readSize); 137 138 // clear the buffer 139 buffer.clear(); 140 } 141 142 @Override 143 protected void doStart() throws Exception { 144 connect(); 145 selection.setInterestOps(SelectionKey.OP_READ); 146 selection.enable(); 147 } 148 149 @Override 150 protected void doStop(ServiceStopper stopper) throws Exception { 151 try { 152 if (selection != null) { 153 selection.close(); 154 } 155 } finally { 156 super.doStop(stopper); 157 } 158 } 159}