/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.mq.il.uil.multiplexor;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 *  Used to multiplex a socket's streams. With this this interface you can
 *  access the multiplexed streams of the socket. The multiplexed streams are
 *  identifed a stream id. Stream id 0 is reserved for internal use of the
 *  multiplexor.
 *
 * @author     Hiram Chirino (Cojonudo14@hotmail.com)
 * @created    August 16, 2001
 * @version    $Revision: 1.2.8.1 $
 */
public class SocketMultiplexor {
   StreamMux        mux;
   StreamDemux      demux;

   private Socket   socket;

   public SocketMultiplexor( Socket s )
      throws IOException {
      socket = s;
      mux = new StreamMux( s.getOutputStream() );
      demux = new StreamDemux( s.getInputStream() );
   }

   /**
    *  Creation date: (11/16/00 1:15:01 PM)
    *
    * @return    org.jboss.mq.connection.StreamDemux
    */
   public StreamDemux getDemux() {
      return demux;
   }

   public InputStream getInputStream( int id )
      throws IOException {
      return demux.getStream( ( short )id );
   }

   /**
    *  Creation date: (11/16/00 1:15:01 PM)
    *
    * @return    org.jboss.mq.connection.StreamMux
    */
   public StreamMux getMux() {
      return mux;
   }

   public OutputStream getOutputStream( int id )
      throws IOException {
      return mux.getStream( ( short )id );
   }

   /**
    *  Creation date: (11/16/00 1:14:41 PM)
    *
    * @return    java.net.Socket
    */
   public java.net.Socket getSocket() {
      return socket;
   }

   public void close()
      throws IOException {
      getSocket().close();
   }

}
