Package network

Source Code of network.MessageProcessor

package network;

import java.io.File;
import java.io.FileNotFoundException;

import main.ui.ILog;

import file.LocalFileOperation;

/**
* Processes the Message and carries out the file operation
*/
public class MessageProcessor {

 
  public void process(Message inMessage,ILog log) {

    String fileCommand = inMessage.getCommand();
    String fileContents = inMessage.getFileContents();
    String fileId = inMessage.getFileId();
    LocalFileOperation   oFileOperation = new LocalFileOperation ();
    int numFiles = 0;
    File localFile;
   
    try {
     
    if (fileCommand.equals("add"))
    {
      oFileOperation.add(fileId,fileContents,log);
    }
    else
      //Some other node is looking for a file
      //so we send a message directly back to them
      //with file contents
      if   (fileCommand.equals ("get"))
      {
        //try and get the local file
        try{
          Message responseMessage = new Message();
          Transmitter directTransmit = new Transmitter(2002);
         
          responseMessage.setCommand("add");
          responseMessage.setFileId(fileId);
          responseMessage.setFileContents(new File(fileId));
          //responseMessage.setSenderHostName(myAddress);
          directTransmit.send(responseMessage,inMessage.getSenderHostName(),log);
         
          System.out.println("Add Request Sent Direct to Node:"+ "to other nodes");
        } catch (FileNotFoundException fnfe){
          log.log("Did not have copy of file:"+fileId+" not responding to get request");
        }
       
       
       
      }
      else
        if   (fileCommand.equals ("remove"))
        {
          oFileOperation.remove(fileId,log);
        }       
        else
          if   (fileCommand.equals( "update"))
          {
            oFileOperation.update(fileId,fileContents,log);
          }       
          else
            if   (fileCommand.equals("getFileCount"))
            {
              numFiles = LocalFileOperation.getFileCount();
            }
            else
              throw new Exception ("Unknown command "+ fileCommand);
    }
    catch (Exception e) {
      e.printStackTrace();
    }
   
   
  }

 

}
TOP

Related Classes of network.MessageProcessor

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.