/*** * Creates a TFTP instance with a default timeout of DEFAULT_TIMEOUT, * a null socket, and buffered operations disabled. ***/ public TFTP() { setDefaultTimeout(DEFAULT_TIMEOUT); __receiveBuffer = null; __receiveDatagram = null; }
/*** * This method synchronizes a connection by discarding all packets that * may be in the local socket buffer. This method need only be called * when you implement your own TFTP client or server. * * @throws IOException if an I/O error occurs. ***/ public final void discardPackets() throws IOException { int to; DatagramPacket datagram; datagram = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE); to = getSoTimeout(); setSoTimeout(1); try { while (true) { _socket_.receive(datagram); } } catch (SocketException e) { // Do nothing. We timed out so we hope we're caught up. } catch (InterruptedIOException e) { // Do nothing. We timed out so we hope we're caught up. } setSoTimeout(to); }
trc.fine("Transfer started in thread: " + Thread.currentThread().getName()); transferTftp_ = new TFTP(); transferTftp_.beginBufferedOps(); transferTftp_.setDefaultTimeout(socketTimeout_); transferTftp_.open(); if (transferTftp_ != null && transferTftp_.isOpen()) { transferTftp_.endBufferedOps(); transferTftp_.close();
public void raActive() { serverTftp_ = new TFTP(); // This is the value used in response to each client. socketTimeout_ = serverTftp_.getDefaultTimeout(); // we want the server thread to listen forever. serverTftp_.setDefaultTimeout(0); if (trc.isFineEnabled()) trc.fine(String.format( "TFTP-server RA starting, listening on port %d, timeout %d", port_, socketTimeout_)); try { serverTftp_.open(port_); serverThread = new Thread(this); serverThread.setDaemon(true); serverThread.start(); } catch (Exception e) { String msg = "Error initializing tftp server: "; trc.severe(msg, e); throw new RuntimeException(msg, e); } }
public void sendError(int errorCode, String reason) { try { if (transferTftp_ != null && transferTftp_.isOpen()) { if (trc.isFineEnabled()) trc.fine(String.format("Send error - code[%d] reason[%s]", errorCode, reason)); transferTftp_.bufferedSend(new TFTPErrorPacket(tftpPacket_.getAddress(), tftpPacket_.getPort(), errorCode, reason)); transferTftp_.endBufferedOps(); } } catch (Exception e) { } shutdown(); }
/** * The actual tftp server-thread. * Fork a transfer-thread for each request received. */ public void run() { try { while (!shutdownServer) { TFTPPacket tftpPacket; tftpPacket = serverTftp_.receive(); TFTPTransfer tt = createTransferActivity(tftpPacket); Thread thread = new Thread(tt); thread.setDaemon(true); thread.start(); } } catch (Exception e) { if (!shutdownServer) { trc.severe("Unexpected Error in TFTP Server - Server shut down! + " + e); } } finally { shutdownServer = true; // set this to true, so the launching thread can check to see if it started. if (serverTftp_ != null && serverTftp_.isOpen()) { serverTftp_.close(); } } }
try { if (mode_ == ServerMode.GET_ONLY) { transferTftp_.bufferedSend(new TFTPErrorPacket(twrp.getAddress(), twrp .getPort(), TFTPErrorPacket.ILLEGAL_OPERATION, "Write not allowed by server.")); transferTftp_.bufferedSend(lastSentAck); transferTftp_.bufferedSend(new TFTPErrorPacket(dataPacket.getAddress(), dataPacket.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port")); dataPacket = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) { if (timeoutCount >= maxTimeoutRetries_) { transferTftp_.bufferedSend(lastSentAck); timeoutCount++; continue; transferTftp_.bufferedSend(lastSentAck); } else if (dataPacket == null || !(dataPacket instanceof TFTPDataPacket)) { if (!shutdownTransfer) { transferTftp_.bufferedSend(lastSentAck); if (dataLength < TFTPDataPacket.MAX_DATA_LENGTH) { dataPacket = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) {
public void shutdown() { if (trc.isFineEnabled()) trc.fine("Transfer ended for thread: " + Thread.currentThread().getName()); shutdownTransfer = true; try { transferTftp_.close(); } catch (RuntimeException e) { // noop } resume(); }
private void fireEvent(TFTPPacket packet) throws Exception { RequestEvent event = new RequestEventImpl(packet, this); try { ra_.fireEvent(event, activity_, packet.getAddress().toString()); } catch (Exception e) { transferTftp_.bufferedSend(new TFTPErrorPacket(packet.getAddress(), packet .getPort(), TFTPErrorPacket.UNDEFINED, e.getMessage())); throw e; } }
trc.fine("Transfer started in thread: " + Thread.currentThread().getName()); transferTftp_ = new TFTP(); transferTftp_.beginBufferedOps(); transferTftp_.setDefaultTimeout(socketTimeout_); transferTftp_.open(); if (transferTftp_ != null && transferTftp_.isOpen()) { transferTftp_.endBufferedOps(); transferTftp_.close();
public void raActive() { serverTftp_ = new TFTP(); // This is the value used in response to each client. socketTimeout_ = serverTftp_.getDefaultTimeout(); // we want the server thread to listen forever. serverTftp_.setDefaultTimeout(0); if (trc.isFineEnabled()) trc.fine(String.format( "TFTP-server RA starting, listening on port %d, timeout %d", port_, socketTimeout_)); try { serverTftp_.open(port_); serverThread = new Thread(this); serverThread.setDaemon(true); serverThread.start(); } catch (Exception e) { String msg = "Error initializing tftp server: "; trc.severe(msg, e); throw new RuntimeException(msg, e); } }
public void sendError(int errorCode, String reason) { try { if (transferTftp_ != null && transferTftp_.isOpen()) { if (trc.isFineEnabled()) trc.fine(String.format("Send error - code[%d] reason[%s]", errorCode, reason)); transferTftp_.bufferedSend(new TFTPErrorPacket(tftpPacket_.getAddress(), tftpPacket_.getPort(), errorCode, reason)); transferTftp_.endBufferedOps(); } } catch (Exception e) { } shutdown(); }
/** * The actual tftp server-thread. * Fork a transfer-thread for each request received. */ public void run() { try { while (!shutdownServer) { TFTPPacket tftpPacket; tftpPacket = serverTftp_.receive(); TFTPTransfer tt = createTransferActivity(tftpPacket); Thread thread = new Thread(tt); thread.setDaemon(true); thread.start(); } } catch (Exception e) { if (!shutdownServer) { trc.severe("Unexpected Error in TFTP Server - Server shut down! + " + e); } } finally { shutdownServer = true; // set this to true, so the launching thread can check to see if it started. if (serverTftp_ != null && serverTftp_.isOpen()) { serverTftp_.close(); } } }
try { if (mode_ == ServerMode.GET_ONLY) { transferTftp_.bufferedSend(new TFTPErrorPacket(twrp.getAddress(), twrp .getPort(), TFTPErrorPacket.ILLEGAL_OPERATION, "Write not allowed by server.")); transferTftp_.bufferedSend(lastSentAck); transferTftp_.bufferedSend(new TFTPErrorPacket(dataPacket.getAddress(), dataPacket.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port")); dataPacket = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) { if (timeoutCount >= maxTimeoutRetries_) { transferTftp_.bufferedSend(lastSentAck); timeoutCount++; continue; transferTftp_.bufferedSend(lastSentAck); } else if (dataPacket == null || !(dataPacket instanceof TFTPDataPacket)) { if (!shutdownTransfer) { transferTftp_.bufferedSend(lastSentAck); if (dataLength < TFTPDataPacket.MAX_DATA_LENGTH) { dataPacket = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) {
public void shutdown() { if (trc.isFineEnabled()) trc.fine("Transfer ended for thread: " + Thread.currentThread().getName()); shutdownTransfer = true; try { transferTftp_.close(); } catch (RuntimeException e) { // noop } resume(); }
private void fireEvent(TFTPPacket packet) throws Exception { RequestEvent event = new RequestEventImpl(packet, this); try { ra_.fireEvent(event, activity_, packet.getAddress().toString()); } catch (Exception e) { transferTftp_.bufferedSend(new TFTPErrorPacket(packet.getAddress(), packet .getPort(), TFTPErrorPacket.UNDEFINED, e.getMessage())); throw e; } }
/*** * This method synchronizes a connection by discarding all packets that * may be in the local socket buffer. This method need only be called * when you implement your own TFTP client or server. * <p> * @exception IOException if an I/O error occurs. ***/ public final void discardPackets() throws IOException { int to; DatagramPacket datagram; datagram = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE); to = getSoTimeout(); setSoTimeout(1); try { while (true) _socket_.receive(datagram); } catch (SocketException e) { // Do nothing. We timed out so we hope we're caught up. } catch (InterruptedIOException e) { // Do nothing. We timed out so we hope we're caught up. } setSoTimeout(to); }
/*** * Creates a TFTP instance with a default timeout of DEFAULT_TIMEOUT, * a null socket, and buffered operations disabled. ***/ public TFTP() { setDefaultTimeout(DEFAULT_TIMEOUT); __receiveBuffer = null; __receiveDatagram = null; }
try { if (mode_ == ServerMode.PUT_ONLY) { transferTftp_.bufferedSend(new TFTPErrorPacket(trrp.getAddress(), trrp.getPort(), TFTPErrorPacket.ILLEGAL_OPERATION, "Read not allowed by server.")); transferTftp_.bufferedSend(lastSentData); totalBytesSent += readLength; transferTftp_.bufferedSend(new TFTPErrorPacket(answer.getAddress(), answer.getPort(), TFTPErrorPacket.UNKNOWN_TID, "Unexpected Host or Port")); answer = transferTftp_.bufferedReceive(); } catch (SocketTimeoutException e) { if (timeoutCount >= maxTimeoutRetries_) { transferTftp_.bufferedSend(lastSentData); continue;
/** * Stop the tftp server (and any currently running transfers) and release all * opened network resources. */ public void shutdown() { shutdownServer = true; for (TransferActivityImpl xfer : activities.values()) xfer.getSource().shutdown(); try { serverTftp_.close(); } catch (RuntimeException e) { // noop } try { serverThread.join(); } catch (InterruptedException e) { // we've done the best we could, return } } }