Example #1
0
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Thread#run()
  */
 @Override
 public void run() {
   try {
     tftp.setDefaultTimeout(0);
     tftp.open(udpPort);
     logger.debug("Now listening for TFTP packets on UDP port {}.", udpPort);
     running = true;
     while (true) {
       try {
         TFTPPacket packet = tftp.receive();
         if (packet instanceof TFTPWriteRequestPacket) {
           TFTPWriteRequestPacket requestPacket = (TFTPWriteRequestPacket) packet;
           synchronized (transfers) {
             Iterator<TftpTransfer> transferIterator = transfers.iterator();
             while (transferIterator.hasNext()) {
               TftpTransfer transfer = transferIterator.next();
               if (transfer.getStatus() != Status.PREPARED) {
                 transferIterator.remove();
                 continue;
               }
               if (transfer.match(requestPacket)) {
                 transfer.setRequestPacket(requestPacket);
               }
             }
           }
         }
       } catch (Exception e) {
       }
     }
   } catch (SocketException e) {
     logger.error("Couldn't start the TFTP server");
   }
   running = false;
 }
Example #2
0
 /**
  * Gets the packet.
  *
  * @return the packet
  * @throws InterruptedIOException the interrupted io exception
  * @throws SocketException the socket exception
  * @throws IOException Signals that an I/O exception has occurred.
  * @throws TFTPPacketException the tFTP packet exception
  */
 private TFTPPacket getPacket()
     throws InterruptedIOException, SocketException, IOException, TFTPPacketException {
   long maxTime = System.currentTimeMillis() + receiveTimeout * 1000;
   while (true) {
     TFTPPacket packet = tftp.bufferedReceive();
     if (packet.getAddress().equals(requestPacket.getAddress())
         && packet.getPort() == requestPacket.getPort()) {
       return packet;
     }
     if (System.currentTimeMillis() > maxTime) {
       throw new SocketTimeoutException();
     }
   }
 }
Example #3
0
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Thread#run()
  */
 @Override
 public void run() {
   while (requestPacket == null) {
     try {
       Thread.sleep(1000);
     } catch (InterruptedException e) {
       status = Status.ERROR;
       return;
     }
     if (checkTimeout()) {
       return;
     }
   }
   status = Status.RUNNING;
   tftp = new TFTP();
   tftp.beginBufferedOps();
   tftp.setDefaultTimeout(receiveTimeout * 1000);
   try {
     tftp.open();
   } catch (SocketException e) {
     status = Status.ERROR;
     return;
   }
   TFTPPacket packet = requestPacket;
   TFTPAckPacket ackPacket = new TFTPAckPacket(packet.getAddress(), packet.getPort(), 0);
   while (packet instanceof TFTPWriteRequestPacket) {
     try {
       tftp.bufferedSend(ackPacket);
       packet = this.getPacket();
     } catch (Exception e) {
     }
     if (checkTimeout()) {
       return;
     }
   }
   int lastBlock = 0;
   while (packet instanceof TFTPDataPacket) {
     TFTPDataPacket dataPacket = (TFTPDataPacket) packet;
     int block = dataPacket.getBlockNumber();
     if (block > lastBlock || (lastBlock == 65535 && block == 0)) {
       try {
         buffer.write(
             dataPacket.getData(), dataPacket.getDataOffset(), dataPacket.getDataLength());
       } catch (IOException e) {
         status = Status.ERROR;
         return;
       }
     }
     ackPacket = new TFTPAckPacket(requestPacket.getAddress(), requestPacket.getPort(), block);
     try {
       tftp.bufferedSend(ackPacket);
     } catch (IOException e) {
     }
     if (dataPacket.getDataLength() < TFTPDataPacket.MAX_DATA_LENGTH) {
       this.status = Status.FINISHED;
       for (int i = 0; i < 3; i++) {
         try {
           packet = this.getPacket();
           tftp.bufferedSend(ackPacket);
         } catch (Exception e) {
           return;
         }
       }
     }
     try {
       packet = this.getPacket();
     } catch (Exception e) {
     }
     if (checkTimeout()) {
       return;
     }
   }
   status = Status.ERROR;
   tftp.close();
 }