Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * 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.remoting.transport.multiplex;
 
 import  org.jboss.logging.Logger;
 
 import java.util.Map;

MultiplexClientInvoker is the client side of the Multiplex transport. For more information, see Remoting documentation on labs.jboss.org.

Deprecated:
As of release 2.4.0 the multiplex transport will no longer be actively supported.
Author(s):
Tom Elrod
Ron Sigal
 
 {
    private static final Logger log = Logger.getLogger(MultiplexClientInvoker.class);
    private static final boolean isTraceEnabled = .isTraceEnabled();
    
    private InetAddress connectAddress;
    private String connectHost;
    private int connectPort;
    private String socketGroupId;
    private AddressPair addressPair;
    private boolean readyToRun;
 
Create a new MultiplexClientInvoker.

Parameters:
locator
 
    public MultiplexClientInvoker(InvokerLocator locatorthrows IOException
    {
       super(locator);
    }


Create a new MultiplexClientInvoker.

Parameters:
locator
 
    public MultiplexClientInvoker(InvokerLocator locatorMap configurationthrows IOException
    {
       super(locatorconfiguration);
    }
 
 
    protected void handleConnect() throws ConnectionFailedException
    {
       try
       {
       .debug("configuring MultiplexClientInvoker for: " + );
       super.handleConnect();
 
      this. = InetAddress.getByName(.getHost());
      this. = .getHostName();
      this. = .getPort();
      if (getSocketFactory() != null)
      Map parameters = ;
      if (parameters != null)
      {
         configureSocketGroupParameters(parameters);
      }
      }
      catch (Exception e)
      {
         throw new ConnectionFailedException(e.getMessage());
      }
   }


Finishes the start up process, once adequate bind and connect information is made available. For more information, see the Multiplex subsystem documentation at labs.jboss.org.
   public void finishStart() throws IOException
   {
      if ( != null &&
          .getBindAddress() != null &&
           == null)
      {
         InetAddress bindAddress = .getBindAddress();
         int bindPort = .getBindPort();
          = new InetSocketAddress(bindAddressbindPort);
      }
      if ( != null &&
          .getBindAddress() != null &&
           == null)
      {
         String bindHost = .getBindAddress().getHostName();
         int bindPort = .getBindPort();
          = new AddressPair(bindHostbindPort);
      }
       = true;
   }
   public InvokerLocator getCallbackLocator(Map metadata)
   {
      Socket socket = .getPrimingSocket();
                   socket.getInetAddress().getHostAddress());
                   Integer.toString(socket.getPort()));
      metadata.put(.,
            socket.getInetAddress().getHostAddress());
      metadata.put(.,
            Integer.toString(socket.getPort()));
      InvokerLocator locator = new InvokerLocator("multiplex",
                                                  socket.getLocalAddress().getHostAddress(),
                                                  socket.getLocalPort(),
                                                  null,
                                                  metadata);
      return locator;
   }

Parameters:
parameters
Throws:
IOException
   protected void configureSocketGroupParameters(Map parametersthrows IOException
   {
      String bindHost;
      String bindPortString;
      int bindPort = -1;
      InetAddress bindAddress = null;
       = (Stringparameters.get(.);
      .debug("socketGroupId: " + );
      synchronized (MultiplexServerInvoker.SocketGroupInfo.class)
      {
         if ( != null)
             = (SocketGroupInfo) MultiplexServerInvoker.getSocketGroupMap().get();
         if ( != null && .getServerInvoker() != null)
         {
            .debug("client rule 1");
            // If we get here, it's because a MultiplexServerInvoker created a SocketGroupInfo with matching
            // group id.  We want to make sure that it didn't get a connect address or connect port different
            // than the ones passed in through the parameters map.
            InetAddress socketGroupConnectAddress = .getConnectAddress();
            int socketGroupConnectPort = .getConnectPort();
            if (socketGroupConnectAddress != null && !socketGroupConnectAddress.equals())
            {
               String message = "socket group connect address (" + socketGroupConnectAddress +
                                ") does not match connect address (" +  + ")";
               .error(message);
               throw new IOException(message);
            }
            if (socketGroupConnectPort > 0 && socketGroupConnectPort != )
            {
               String message = "socket group connect port (" + socketGroupConnectPort +
                                ") does not match connect port (" +  + ")";
               .error(message);
               throw new IOException(message);
            }
            bindAddress = .getBindAddress();
            bindPort = .getBindPort();
             = new InetSocketAddress(bindAddressbindPort);
            if (.getPrimingSocket() == null)
            {
               String connectHost = .getHostName();
               MultiplexServerInvoker.createPrimingSocket(connectHost,
                                                          bindAddressbindPort,
                                                          );
            }
            .addClientInvoker(this);
            // We got socketGroupInfo by socketGroupId.  Make sure it is also stored by AddressPair.
            bindHost = bindAddress.getHostName();
             = new AddressPair(bindHostbindPort);
            MultiplexServerInvoker.getAddressPairMap().put();
            MultiplexServerInvoker serverInvoker = .getServerInvoker();
            if (serverInvoker != null)
               serverInvoker.finishStart();
            finishStart();
            return;
         }
         bindHost = (Stringparameters.get(.);
         bindPortString = (Stringparameters.get(.);
         if (bindHost != null && bindPortString == null)
         {
            bindPortString = "0";
         }
         if (bindHost == null && bindPortString != null)
         {
            bindHost = "localhost";
         }
         if (bindHost != null)
         {
            .debug("client rule 2");
            try
            {
               bindPort = Integer.parseInt(bindPortString);
            }
            catch (NumberFormatException e)
            {
               throw new IOException("number format error for bindPort: " + bindPortString);
            }
            if (bindPort != 0)
            {
                = new InetSocketAddress(bindHostbindPort);
                = new AddressPair(bindHostbindPort);
                = (SocketGroupInfo) MultiplexServerInvoker.getAddressPairMap().get();
            }
            // If socketGroupInfo exists, it's because it was created, along with a priming socket, by a
            // MultiplexServerInvoker.  Note that we don't look for a match if the bind port was
            // anonymous.
            if ( != null)
            {
               .setConnectAddress();
               .setConnectPort();
               .addClientInvoker(this);
               // We got socketGroupInfo by AddressPair.  Make sure it is stored by socketGroupId, if we have one.
               if ( != null)
               {
                  String socketGroupSocketGroupId = .getSocketGroupId();
                  if (socketGroupSocketGroupId != null && !socketGroupSocketGroupId.equals())
                  {
                     String message = "socket group multiplexId (" + socketGroupSocketGroupId +
                                      ") does not match multiplexId (" +  + ")";
                     .error(message);
                     throw new IOException(message);
                  }
                  if (socketGroupSocketGroupId == null)
                  {
                     .setSocketGroupId();
                     MultiplexServerInvoker.getSocketGroupMap().put();
                  }
               }
               finishStart();
               return;
            }
            if (bindPort == 0 &&  == null)
            {
               String message = "Can never be found by any MultiplexServerInvoker: " +
                                "bind port == 0 and socketGroupId == null";
               .warn(message);
            }
            // Anonymous bind port
            if (bindPort == 0)
               bindPort = PortUtil.findFreePort(bindHost);
             = new SocketGroupInfo();
            .addClientInvoker(this);
            // Set bindAddress and bindPort to be able to test for inconsistencies with bind address
            // and bind port determined by companion MultiplexServerInvoker.
            bindAddress = InetAddress.getByName(bindHost);
            .setBindAddress(bindAddress);
            .setBindPort(bindPort);
            String connectHost = .getHostName();
            MultiplexServerInvoker.createPrimingSocket(connectHost,
                                                       bindAddressbindPort);
            MultiplexServerInvoker.getAddressPairMap().put();
            if ( != null)
            {
               .setSocketGroupId();
               MultiplexServerInvoker.getSocketGroupMap().put();
            }
            finishStart();
            return;
         }
         if ( != null)
         {
            .debug("client rule 3");
            if ( == null)
            {
                = new SocketGroupInfo();
               .setSocketGroupId();
               .setConnectAddress();
               .setConnectPort();
               MultiplexServerInvoker.getSocketGroupMap().put();
            }
            .addClientInvoker(this);
            return;
         }
         .debug("client rule 4");
         String connectHost = .getHostName();
          = new SocketGroupInfo();
         MultiplexServerInvoker.createPrimingSocket(connectHost,
                                                    );
         finishStart();
      }
   }

   

Parameters:
sessionId
invocation
marshaller
Returns:
Throws:
java.io.IOException
org.jboss.remoting.ConnectionFailedException
   protected Object transport(String sessionIdObject invocationMap metadataMarshaller marshallerUnMarshaller unmarshaller)
   {
      .debug("entering transport()");
      if (!)
         throw new IOException("connection to server has not been made");
      return super.transport(sessionIdinvocationmetadatamarshallerunmarshaller);
   }


   
subclasses must implement this method to provide a hook to disconnect from the remote server, if this applies to the specific transport. However, in some transport implementations, this may not make must difference since the connection is not persistent among invocations, such as SOAP. In these cases, the method should silently return without any processing.
   protected void handleDisconnect()
   {
      //TODO: -TME Should this be a no op or need to pool?
      .debug("entering handleDisconnect()");
      super.handleDisconnect();
      synchronized (MultiplexServerInvoker.SocketGroupInfo.class)
      {
         if ( != null)
         {
            .removeClientInvoker(this);
            if (.getClientInvokers().isEmpty() && .getServerInvoker() == null)
            {
               .debug("invoker group shutting down: " + .getSocketGroupId());
               if (.getPrimingSocket() != null)
               {
                  .debug("MultiplexClientInvoker: closing bind priming socket");
                  VirtualSocket ps = .getPrimingSocket();
                  if (ps != null)
                  {
                     try
                     {
                        // When the remote virtual MultiplexServerInvoker learns that the
                        // priming socket has closed, it will close its VirtualServerSocket,
                        // rendering unshareable the MultiplexingManager that underlies this
                        // socket group.  We mark it as unshareable immediately so that it will
                        // not be reused by any other socket group.
                        ps.getManager().unregisterShareable();
                        ps.close();
                     }
                     catch (IOException e)
                     {
                        .error("Error closing bind priming socket during cleanup upon stopping"e);
                     }
                  }
               }
                = .getSocketGroupId();
               if ( != null)
               {
                  MultiplexServerInvoker.getSocketGroupMap().remove();
               }
               // addressPair is set in finishStart().
               if ( != null)
               {
                  MultiplexServerInvoker.getAddressPairMap().remove();
               }
            }
             = null;  // Prevent from occurring a second time in Finalizer thread.
         }
      }
   }


Returns:
   {
      return ;
   }


Returns:
   {
      return ;
   }


   
Each implementation of the remote client invoker should have a default data type that is uses in the case it is not specified in the invoker locator uri.

Returns:
   protected String getDefaultDataType()
   {
      return .;
   }
   protected Socket createSocket(String addressint portint timeoutthrows IOException
   {
      .debug("MultiplexClientInvoker.createSocket()");
      
      if (timeout < 0)
      {
         timeout = getTimeout();
         if (timeout < 0)
            timeout = 0;
      }
      // If connection has been broken, try to connect to new server.
      if ( != null && .getPrimingSocket() != null)
      {
         VirtualSocket primingSocket = .getPrimingSocket();
         if (!primingSocket.isFunctional() || primingSocket.hasReceivedDisconnectMessage())
         {
            .info("Current server is inaccessible.  Will try to connect to new server");
            primingSocket.close();
            // Get new priming socket.
            if ( != null)
            {
               InetAddress bindAddress = .getAddress();
               int bindPort = PortUtil.findFreePort(.getHostName());
               .setBindPort(bindPort);
                = new InetSocketAddress(bindAddressbindPort);
               MultiplexServerInvoker.createPrimingSocket(,
                                                          ,
                                                          bindAddressbindPort,
                                                          timeout);
            }
            else
               MultiplexServerInvoker.createPrimingSocket(,
                                                          ,
                                                          port);
            MultiplexServerInvoker serverInvoker = .getServerInvoker();
            if (serverInvoker != null)
            {
               try
               {
                  // Restart callback server invoker with new server socket.
                  serverInvoker.stop();
                  .setServerInvoker(null);
                  serverInvoker.resetLocator(.getPort());
                  serverInvoker.refreshServerSocket();
                  serverInvoker.setup();
                  serverInvoker.start();
               }
               catch (Exception e)
               {
                  .error(e.getMessage(), e);
               }
            }
            VirtualSocket socket = new VirtualSocket();
            for (int i = 0; i < 3; i++)
            {
               try
               {
                  socket.connect(timeout);
                  return socket;
               }
               catch (Exception e)
               {
                  try
                  {
                     Thread.sleep(500);
                  }
                  catch (InterruptedException e1)
                  {
                  }
               }
            }
         }
      }
      VirtualSocket socket = new VirtualSocket();
      socket.connect(timeout);
      return socket;
   }
New to GrepCode? Check out our FAQ X