⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 yesnocounterfrontimpl.java

📁 Java the UML Way 书中所有源码
💻 JAVA
字号:
/*
 * YesNoCounterFrontImpl.java   E.L. 2001-08-25
 *
 * Here is the "front" implementation at the server side. The clients communicate with
 * instances of this class. These instances forward the messages on to instances
 * of the YesNoCounter class from ch. 4. The YesNoCounter class is not known
 * to the clients.
 */

import java.rmi.*;
import java.rmi.server.*;
import java.util.*;

class YesNoCounterFrontImpl
    extends UnicastRemoteObject implements YesNoCounterFront {
  private YesNoCounter theCounter = new YesNoCounter();
  private ArrayList allClients = new ArrayList();

  public YesNoCounterFrontImpl() throws RemoteException {
  }

  /* Adds a new client. */
  public synchronized void registerMe(Client theClient) throws RemoteException {
    try {
      allClients.add(theClient);
      System.out.println("Now " + theClient.getName() + " is registered.");
      theClient.printStatus(composeMessage());
    } catch (Exception e) {
      System.out.println("Error in registerMe: " + e);
    }
  }

  /* Resigns a client. Nothing happens if a client with the given name doesn't exist. */
  public synchronized void resignMe(String clientName) throws RemoteException {
    boolean found = false;
    int clientIndex = 0;
    while (clientIndex < allClients.size() && !found) {
      Client thisOne = (Client) allClients.get(clientIndex);
      if ((thisOne.getName()).equals(clientName)) {
        found = true;
        allClients.remove(clientIndex);
        System.out.println("Now " + clientName + " is removed.");
      } else clientIndex++;
    }
  }

  public synchronized void increaseNumberOfYes() throws RemoteException {
    System.out.println("The number of yes votes was increased by 1");
    theCounter.increaseNumberOfYes();
    alertAll();
  }

  public synchronized void increaseNumberOfNo() throws RemoteException {
    System.out.println("The number of no votes was increased by 1");
    theCounter.increaseNumberOfNo();
    alertAll();
  }

  public synchronized void increaseNumberOfYes(int increase) throws RemoteException {
    theCounter.increaseNumberOfYes(increase);
    System.out.println("The number of yes votes was increased by " + increase + ".");
    alertAll();
  }

  public synchronized void increaseNumberOfNo(int increase) throws RemoteException {
    theCounter.increaseNumberOfNo(increase);
    System.out.println("The number of no votes was increased by " + increase + ".");
    alertAll();
  }

  public synchronized int getNumberOfYes() throws RemoteException {
    return theCounter.getNumberOfYes();
  }

  public synchronized int getNumberOfNo() throws RemoteException {
    return theCounter.getNumberOfNo();
  }

  private synchronized String composeMessage() {
    java.util.Date now = new java.util.Date();
    java.text.DateFormat timeFormat =
                         java.text.DateFormat.getTimeInstance(); // see online API doc.
    return "Now the time is " + timeFormat.format(now) + ", the no. of Yes votes is " +
                            theCounter.getNumberOfYes() + ", the no. of No votes is " +
                                                      theCounter.getNumberOfNo() + ".";
  }

  private synchronized void alertAll() throws RemoteException {
    System.out.println("All clients will be notified of the changes");
    String message = composeMessage();
    int clientIndex = 0;
    while (clientIndex < allClients.size()){
      Client thisOne = (Client) allClients.get(clientIndex);
      try {
        thisOne.printStatus(message);
        clientIndex++; // updates index only if contact with the client
      } catch (ConnectException e) {  // the client program is shut down
        System.out.println("No contact with the client with index " + clientIndex + ": " + e);
        /*
         * Removes the client at this index. No index updating.
         */
        allClients.remove(clientIndex);
        System.out.println("This client is now removed from our list. We continue...");
      }
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -