/**
  * processOpenAccount
  *
  * @param udpMessage is an instance of OpenAccountMessage
  * @throws Exception
  */
 private void processOpenAccount(UDPMessage udpMessage) throws Exception {
   if (udpMessage.getMessage() instanceof OpenAccountMessage) {
     OpenAccountMessage msg = (OpenAccountMessage) udpMessage.getMessage();
     if (!Env.getCurrentBank().toString().equalsIgnoreCase(msg.getBank())) {
       msg.setException(new Exception("Wrong Bank Name! Request denied. processOpenAccount"));
     } else {
       int accountNumber = -1;
       try {
         accountNumber =
             bank.openAccount(
                 msg.getFirstName(),
                 msg.getLastName(),
                 msg.getEmailAddress(),
                 msg.getPhoneNumber(),
                 msg.getPassword());
       } catch (Exception e) {
         msg.setException(e);
         accountNumber = -1;
       }
       msg.setResultAccountID(accountNumber);
       msg.setMachineName(Env.getMachineName());
     }
     this.send(udpMessage);
   } else {
     throw new Exception("Invalid Object processOpenAccount");
   }
 }
  @Test
  public void testChannel() throws IOException, InterruptedException {
    String data = "hello";
    InetAddress address = InetAddress.getByName("127.0.0.1");
    DatagramPacket packet = new DatagramPacket(data.getBytes(), data.length(), address, 5885);
    DatagramSocket senderSocket = new DatagramSocket();
    senderSocket.send(packet);
    senderSocket.close();

    // Wait at most 1 second, 0.1 seconds at a time
    boolean recievedMessage = false;
    for (int i = 0; i < 10; i++) {
      UDPMessage message = channel.popLatestMessage();
      if (message != null) {
        recievedMessage = true;
        assertEquals("Get the message that we sent", message.getMessage(), "hello");
        break;
      }
      Thread.sleep(100);
    }

    if (!recievedMessage) {
      fail("We didn't recieve the message after 1 seconds");
    }
  }
예제 #3
0
  public void initReceive() throws IOException {
    byte[] receiveData = new byte[256];
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    clientSide.receive(receivePacket);

    String data = new String(receivePacket.getData(), 0, receivePacket.getLength());
    JSONObject jObj = null;
    try {
      jObj = (JSONObject) new JSONParser().parse(data);
      UDPMessage message = new UDPMessage(jObj, getCallback());
      message.init();
    } catch (ParseException ex) {
      UDPMessage message = new UDPMessage(data.toString(), getCallback());
      message.init();
      // System.out.println("data is not json format");
    }
  }
 /**
  * processTransferLoan
  *
  * @param udpMessage is an instance of TransferLoanMessage
  * @throws Exception
  */
 private void processTransferLoan(UDPMessage udpMessage) throws Exception {
   if (udpMessage.getMessage() instanceof TransferLoanMessage) {
     TransferLoanMessage msg = (TransferLoanMessage) udpMessage.getMessage();
     if (!this.bank.getServerName().equalsIgnoreCase(msg.getCurrentBank())) {
       msg.setException(new Exception("Wrong Bank Name! Request denied. processTransferLoan"));
     } else {
       boolean isTransferred = false;
       try {
         isTransferred = bank.transferLoan(msg.getLoanID(), msg.getOtherBank());
       } catch (Exception e) {
         msg.setException(e);
       }
       msg.setTransferSuccessful(isTransferred);
       msg.setMachineName(Env.getMachineName());
     }
     this.send(udpMessage);
   } else {
     throw new Exception("Invalid Object processTransferLoan");
   }
 }
 /**
  * processGetLoan
  *
  * @param udpMessage is an instance of GetLoanMessage
  * @throws Exception
  */
 private void processGetLoan(UDPMessage udpMessage) throws Exception {
   if (udpMessage.getMessage() instanceof GetLoanMessage) {
     GetLoanMessage msg = (GetLoanMessage) udpMessage.getMessage();
     if (!this.bank.getServerName().equalsIgnoreCase(msg.getBank())) {
       msg.setException(new Exception("Wrong Bank Name! Request denied. processGetLoan"));
     } else {
       int loanNumber = -1;
       try {
         loanNumber = bank.getLoan(msg.getAccountNumber(), msg.getPassword(), msg.getLoanAmount());
       } catch (Exception e) {
         msg.setException(e);
       }
       msg.setResultLoanID(loanNumber);
       msg.setMachineName(Env.getMachineName());
     }
     this.send(udpMessage);
   } else {
     throw new Exception("Invalid Object processGetLoan");
   }
 }
 /**
  * processPrintCustomerInfo
  *
  * @param udpMessage is an instance of PrintCustomerInfoMessage
  * @throws Exception
  */
 private void processPrintCustomerInfo(UDPMessage udpMessage) throws Exception {
   if (udpMessage.getMessage() instanceof PrintCustomerInfoMessage) {
     PrintCustomerInfoMessage msg = (PrintCustomerInfoMessage) udpMessage.getMessage();
     if (!this.bank.getServerName().equalsIgnoreCase(msg.getBank())) {
       msg.setException(
           new Exception("Wrong Bank Name! Request denied. processPrintCustomerInfo"));
     } else {
       String result = "";
       try {
         result = bank.printCustomerInfo();
       } catch (Exception e) {
         msg.setException(e);
       }
       msg.setResult(result);
       msg.setMachineName(Env.getMachineName());
     }
     this.send(udpMessage);
   } else {
     throw new Exception("Invalid Object processPrintCustomerInfo");
   }
 }
 /**
  * processDelayPayment
  *
  * @param udpMessage is an instance of DelayPaymentMessage
  * @throws Exception
  */
 private void processDelayPayment(UDPMessage udpMessage) throws Exception {
   if (udpMessage.getMessage() instanceof DelayPaymentMessage) {
     DelayPaymentMessage msg = (DelayPaymentMessage) udpMessage.getMessage();
     if (!this.bank.getServerName().equalsIgnoreCase(msg.getBank())) {
       msg.setException(new Exception("Wrong Bank Name! Request denied. processDelayPayment"));
     } else {
       boolean isDelayed = false;
       try {
         isDelayed =
             bank.delayPayment(msg.getLoanID(), msg.getCurrentDueDate(), msg.getNewDueDate());
       } catch (Exception e) {
         msg.setException(e);
       }
       msg.setDelaySuccessful(isDelayed);
       msg.setMachineName(Env.getMachineName());
     }
     this.send(udpMessage);
   } else {
     throw new Exception("Invalid Object processDelayPayment");
   }
 }
 /**
  * Method can be overwritten for FE, Sequencer, Replica, ReplicaManager
  *
  * @param bank
  * @param udpMessage
  * @throws Exception
  */
 protected void processRequest(UDPMessage udpMessage) throws Exception {
   switch (udpMessage.getOperation()) {
     case OpenAccount:
       processOpenAccount(udpMessage);
       break;
     case GetLoan:
       processGetLoan(udpMessage);
       break;
     case DelayPayment:
       processDelayPayment(udpMessage);
       break;
     case PrintCustomerInfo:
       processPrintCustomerInfo(udpMessage);
       break;
     case TransferLoan:
       processTransferLoan(udpMessage);
       break;
     default:
       Env.log("Unknown regular processRequest! " + udpMessage.getOperation().toString());
       break;
   }
 }