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.
 */
 
 /*
  * Created on Feb 21, 2006
  */
 package org.jboss.remoting.samples.chat.client;
 
 
 import  org.jboss.logging.Logger;
A RemoteStrategyRemoting.

Author(s):
Ron Sigal
Version:
$Revision: 735 $

Copyright (c) 2005

 
 
 public class RemoteStrategyRemoting implements RemoteStrategy
 {
    protected static final Logger log = Logger.getLogger(RemoteStrategyRemoting.class);
    private static String chatClientLocatorDefault = "socket://localhost:0";
    private static String chatManagerLocatorDefault = "socket://localhost:1969";
    
    private Client managerClient;
    private Connector receiverConnector;
    private boolean locallyShuttingDown = false;
    private boolean remotelyShuttingDown = false;
    
    
    public RemoteStrategyRemoting() throws Exception
    {
       String chatManagerUriString = Parameters.getParameter("chatManagerUri");
       
       if (chatManagerUriString == null)
          chatManagerUriString = ;
  
       InvokerLocator chatManagerLocator = new InvokerLocator(chatManagerUriString);
        = new Client(chatManagerLocator);
       .connect();
    }
 
    
    {
       RemoteInvocation invocation = new RemoteInvocation("list"null);
       
       try
       {
          return (ArrayList.invoke(invocation);
       }
       catch (Throwable t)
       {
          .error(t);
          t.printStackTrace();
          throw new RemoteConnectionException();
       }
    }
 
 
    public ChatServer createChat(String description,
                                 ChatMember owner,
                                 TalkFrame talkFrame,
                                ReadWriteArrayList outgoingLines)
   {
      Client serverClient = null;
      
      // Create new chat room and create Client for new chat room server.
      try
      {
         Object[] args = new Object[] {descriptionowner};
         RemoteInvocation invocation = new RemoteInvocation("createChat"args);
         InvokerLocator serverLocator = (InvokerLocator.invoke(invocation);
         serverClient = new Client(serverLocator);
         serverClient.connect();
      }
      catch (Throwable e)
      {
         .error(e);
         e.printStackTrace();
         throw new RemoteConnectionException();
      }
         
      // Create callback handler to process incoming messages.
      try
      {
          = new Connector();
         String receiverLocatorString = Parameters.getParameter("clientUriString");
         InvokerLocator receiverLocator = new InvokerLocator(receiverLocatorString);
         .setInvokerLocator(receiverLocator.getLocatorURI());
         .info(receiverLocator.getLocatorURI());
         .start();
         receiverLocator = .getLocator();
         .info(.getInvokerLocator());
         InvokerCallbackHandler receiverHandler = new ChatReceiverHandler(talkFrame);
         serverClient.addListener(receiverHandlerreceiverLocatorowner);
      }
      catch (MalformedURLException e)
      {
         .error(e);
         e.printStackTrace();
         throw new RemoteConnectionException();
      }
      catch (Throwable e)
      {
         .error(e);
         e.printStackTrace();
         throw new RemoteConnectionException();
      }
      
      // Create stub for new chat room server.
      ChatServer chatServer = new ChatServerStub(serverClient);
      
      // Create thread to send new outgoing messages to chat room server.
      SendThread sendThread = new SendThread(chatServeroutgoingLines);
      sendThread.start();
      
      return chatServer;
   }
   
   
   public ChatServer join(String keyChatMember newMemberTalkFrame talkFrameReadWriteArrayList outgoingLines)
   {
      Client serverClient = null;
      
      // Join chat room and create Client for chat room server.
      try
      {
         Object[] args = new Object[] {keynewMember};
         RemoteInvocation invocation = new RemoteInvocation("join"args);
         .invoke(invocation);
         InvokerLocator serverLocator = new InvokerLocator(key);
         serverClient = new Client(serverLocator);
         serverClient.connect();
      }
      catch (Throwable e)
      {
         .error(e);
         e.printStackTrace();
         throw new RemoteConnectionException();
      }
         
      // Create callback handler to process incoming messages.
      try
      {
          = new Connector();
         String receiverLocatorString = Parameters.getParameter("clientUriString");
         InvokerLocator receiverLocator = new InvokerLocator(receiverLocatorString);
         .setInvokerLocator(receiverLocator.getLocatorURI());
         .info(receiverLocator.getLocatorURI());
         .start();
         receiverLocator = .getLocator();
         InvokerCallbackHandler receiverHandler = new ChatReceiverHandler(talkFrame);
         serverClient.addListener(receiverHandlerreceiverLocatornewMember);
      }
      catch (MalformedURLException e)
      {
         .error(e);
         e.printStackTrace();
         throw new RemoteConnectionException();
      }
      catch (Throwable e)
      {
         .error(e);
         e.printStackTrace();
         throw new RemoteConnectionException();
      }
      
      
      // Create stub for new chat room server.
      ChatServer chatServer = new ChatServerStub(serverClient);
      
      // Create thread to send new outgoing messages to chat room server.
      SendThread sendThread = new SendThread(chatServeroutgoingLines);
      sendThread.start();
  
      return chatServer;
   }
   public void setShuttingDown()
   {
       = true;
   }
   
   
   class SendThread extends Thread
   {
      private ChatServer chatServer;
      private ReadWriteArrayList outgoingLines;
      
      SendThread(ChatServer chatServerReadWriteArrayList outgoingLines)
      {
         this. = chatServer;
         this. = outgoingLines;
      }
      
      public void run() {
         while (! && !)
         {
           try {
             .send( (ChatMessage.firstElement());
             .remove(0);
           } catch (RemoteConnectionException re)
           {
             ..println("RemoteStrategyImpl.createChat(): unable to send next line:");
//             System.out.println("  " + (String) outgoingLines.firstElement());
           } catch (ShuttingDownException sde) {
             ..println("RemoteStrategyImpl.createChat(): ChatServer is shutting down");
              = true;
           }
         }
       }
     }
   
   
   class ChatServerStub implements ChatServer
   {
      private Client serverClient;
    
      public ChatServerStub(Client serverClient)
      {
         this. = serverClient;
      }
     
      {
         RemoteInvocation invocation = new RemoteInvocation("getBackChat"null);
         
         try
         {
            return (ArrayList.invoke(invocation);
         }
         catch (Throwable e)
         {
            .error(e);
            throw new RemoteConnectionException();
         }
      }
      
      {
         RemoteInvocation invocation = new RemoteInvocation("getChatInfo"null);
         
         try
         {
            return (ChatInfo.invoke(invocation);
         }
         catch (Throwable e)
         {
            .error(e);
            throw new RemoteConnectionException();
         }
      }
      
      public void leave(ChatMember memberthrows RemoteConnectionExceptionShuttingDownException
      {
         RemoteInvocation invocation = new RemoteInvocation("leave"new Object[] {member});
         
         try
         {
            .invoke(invocation);
         }
         catch (Throwable e)
         {
            .error(e);
            throw new RemoteConnectionException();
         }
      }
      
      public void send(ChatMessage mesgthrows RemoteConnectionExceptionShuttingDownException
      {
         RemoteInvocation invocation = new RemoteInvocation("send"new Object[] {mesg});
         
         try
         {
            .invoke(invocation);
         }
         catch (Throwable e)
         {
            .error(e);
            throw new RemoteConnectionException();
         }
      }
   }
New to GrepCode? Check out our FAQ X