Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
// Class Page is defined separately. It stores and returns the Page name via getName()

public final boolean SUCCESS = true;
public final boolean FAILURE = false;
Page[] pageBuff = new Page[MAX_PAGE_SIZE];

public synchronized boolean sendPage(Socket socket, String pageName){
  try{
    // Get the output stream to write the Page to
    ObjectOutputStream out = new 
      ObjectOutputStream(socket.getOutputStream());

    Page targetPage = null;

    // Find the Page requested by the client
    for(Page p : pageBuff) {
      if(p.getName().compareTo(pageName) == 0) {
        targetPage = p;
      }
    }

    // Page requested does not exist
    if(targetPage == null)
      return FAILURE;

    // Send the Page to the client
    out.writeObject(targetPage);
    out.flush();
    out.close();
  } catch(IOException io){ /* forward to handler */ }
  return SUCCESS;
}

...

Code Block
bgColor#ccccff
public boolean sendReply(Socket socket, String pageName){
  Page targetPage = getPage(pageName);

  if(targetPage == null)
    return FAILURE;

  return sendPage(socket, targetPage);
}

private synchronized Page getPage(String pageName){
  Page targetPage = null;

  for(Page p : pageBuff) {
    if(p.getName().equals(pageName)) {
      targetPage = p;
    }
  }
  return targetPage;
}

public boolean sendPage(Socket socket, Page page){
  try{
    // Get the output stream to write the Page to
    ObjectOutputStream out = new 
      ObjectOutputStream(socket.getOutputStream());

    // Send the Page to the client
    out.writeObject(page);
    out.flush();
    out.close();
    return SUCCESS;
  }catch(IOException io){
     // handle exception    
  }
  return FAILURE;
}

...