Example usage for org.apache.commons.net.tftp TFTPClient sendFile

List of usage examples for org.apache.commons.net.tftp TFTPClient sendFile

Introduction

In this page you can find the example usage for org.apache.commons.net.tftp TFTPClient sendFile.

Prototype

public void sendFile(String filename, int mode, InputStream input, String hostname, int port)
        throws UnknownHostException, IOException 

Source Link

Document

Requests to send a file to a remote host, reads the file from an InputStream, sends the file to the remote host, and closes the connection.

Usage

From source file:com.t_oster.liblasercut.drivers.LaosCutter.java

@Override
public void sendJob(LaserJob job, ProgressListener pl, List<String> warnings)
        throws IllegalJobException, Exception {
    currentFrequency = -1;/*  w w  w  .j a v a2 s. c om*/
    currentPower = -1;
    currentSpeed = -1;
    currentFocus = 0;
    currentPurge = false;
    currentVentilation = false;
    pl.progressChanged(this, 0);
    BufferedOutputStream out;
    ByteArrayOutputStream buffer = null;
    pl.taskChanged(this, "checking job");
    checkJob(job);
    job.applyStartPoint();
    if (!useTftp) {
        pl.taskChanged(this, "connecting");
        Socket connection = new Socket();
        connection.connect(new InetSocketAddress(hostname, port), 3000);
        out = new BufferedOutputStream(connection.getOutputStream());
        pl.taskChanged(this, "sending");
    } else {
        buffer = new ByteArrayOutputStream();
        out = new BufferedOutputStream(buffer);
        pl.taskChanged(this, "buffering");
    }
    this.writeJobCode(job, out, pl);
    if (this.isUseTftp()) {
        pl.taskChanged(this, "connecting");
        TFTPClient tftp = new TFTPClient();
        tftp.setDefaultTimeout(5000);
        //open a local UDP socket
        tftp.open();
        pl.taskChanged(this, "sending");
        ByteArrayInputStream bain = new ByteArrayInputStream(buffer.toByteArray());
        tftp.sendFile(job.getName().replace(" ", "") + ".lgc", TFTP.BINARY_MODE, bain, this.getHostname(),
                this.getPort());
        tftp.close();
        bain.close();
        if (debugFilename != null && !"".equals(debugFilename)) {
            pl.taskChanged(this, "writing " + debugFilename);
            FileOutputStream o = new FileOutputStream(new File(debugFilename));
            o.write(buffer.toByteArray());
            o.close();
        }
        pl.taskChanged(this, "sent.");
    }
    pl.progressChanged(this, 100);
}

From source file:com.cablelabs.fsm.AutoProvState.java

private String autoGenerate(int msgIndexQueue) {
    Properties platform = SystemSettings.getSettings(SettingConstants.PLATFORM);
    Properties dut = SystemSettings.getSettings(SettingConstants.DUT);
    Boolean autoProv = SystemSettings.getBooleanSetting(SettingConstants.AUTO_GENERATE);
    Boolean autoGen = SystemSettings.getBooleanSetting(SettingConstants.AUTO_PROVISION);
    if (autoProv && autoGen) {
        if (pd != null && platform != null && dut != null) {
            String pcscfLabel = dut.getProperty(SettingConstants.PCSCF);
            String macAddr = dut.getProperty(SettingConstants.MAC_ADDRESS);
            String tftpIP = platform.getProperty(SettingConstants.TFTP_SERVER_IP);
            String tftpPort = platform.getProperty(SettingConstants.TFTP_SERVER_PORT);
            String phone1 = dut.getProperty(SettingConstants.PHONE_NUMBER_1);
            String phone2 = dut.getProperty(SettingConstants.PHONE_NUMBER_2);
            String cw = platform.getProperty(SettingConstants.CW_NUMBER);
            if (macAddr != null && pcscfLabel != null && tftpIP != null && tftpPort != null && cw != null) {

                // Next verify the port is not set to zero
                try {
                    int port = Integer.parseInt(tftpPort);
                    if (port > 0 && port <= 65535) {

                        // Next make sure the TFTP Server IP is not set to 0.0.0.0
                        if (tftpIP.equals("0.0.0.0")) {
                            logger.warn(PC2LogCategory.PCSim2, subCat,
                                    "The TFTP Server IP setting in the platform file is not valid. Ending auto generate operation.");
                            return null;
                        }// www  .j a  va  2s  .c o m

                        File input = new File(SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                + SettingConstants.CW + cw + File.separator + pd.getProvFileName());
                        if (input != null) {
                            ProvGen pg = new ProvGen(input);
                            if (phone1 != null)
                                pg.changePhoneNum(SettingConstants.AUTO_GENERATE_PHONE_NUMBER_1, phone1);
                            if (phone2 != null)
                                pg.changePhoneNum(SettingConstants.AUTO_GENERATE_PHONE_NUMBER_2, phone2);
                            Properties pcscf = SystemSettings.getSettings(pcscfLabel);
                            if (pcscf != null) {
                                String pcscfIP = pcscf.getProperty(SettingConstants.IP);
                                if (pcscfIP != null)
                                    pg.changePCSCF(pcscfIP);
                            }
                            String newFileName = macAddr + ".bin";
                            if (pg.output(SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                    + SettingConstants.CW + cw + File.separator + newFileName)) {
                                // Test system
                                //File output = new File(SettingConstants.AUTO_PROV_FILE_DIRECTORY + newFileName);
                                //File pact = new File(SettingConstants.AUTO_PROV_FILE_DIRECTORY + "chinmaya_base_ph1_pcscf.bin");
                                //pg.compare(pact, output);
                                // Create a data entry of the issued event
                                //ProvisioningData issuePD = new ProvisioningData(macAddr, pd.getPolicyFileName(), newFileName);
                                logger.info(PC2LogCategory.FSM, subCat,
                                        "AutoProvState beginning to TFTP the new provisioning file.");

                                // Next we need to TFTP the file to the server
                                TFTPClient client = new TFTPClient();
                                File binFile = new File(
                                        SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                                + SettingConstants.CW + cw + File.separator + newFileName);
                                if (binFile.isFile() && binFile.canRead()) {
                                    FileInputStream istrm = new FileInputStream(binFile);
                                    //InetAddress ia = InetAddress.getByName("10.4.1.37");
                                    client.open(); // client.open(20003, ia);
                                    client.sendFile(newFileName, TFTP.BINARY_MODE, istrm, tftpIP, port);
                                    client.close();
                                    logger.info(PC2LogCategory.FSM, subCat,
                                            "TFTP of the new provisioning file is complete.");
                                    super.processPrelude(msgIndexQueue);
                                } else {
                                    logger.warn(PC2LogCategory.FSM, subCat,
                                            "The " + macAddr + ".bin doesn't appear in the "
                                                    + SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                                    + SettingConstants.CW + cw
                                                    + " Ending auto generate operation.");
                                }
                            } else {
                                logger.error(PC2LogCategory.FSM, subCat,
                                        "AutoProvState could not locate provisioning template file["
                                                + input.getAbsolutePath() + "].");
                            }
                        }
                        //                  else {
                        //                     logger.info(PC2LogCategory.FSM, subCat, "AutoProvState is terminating because the input directory is null.");
                        //                  }
                    } else {
                        logger.info(PC2LogCategory.PCSim2, subCat,
                                "AutoProvState is terminating because the port(" + port
                                        + ") is less than 0 or greater than 65535.");
                    }
                } catch (NumberFormatException nfe) {
                    logger.warn(PC2LogCategory.FSM, subCat,
                            "AutoProvState is not auto generating a provisioning file because the "
                                    + "TFTP Server Port setting doesn't appear to be a number.");
                } catch (UnknownHostException uhe) {
                    logger.warn(PC2LogCategory.FSM, subCat,
                            "AutoProvState is not auto generating a provisioning file because the "
                                    + "system encountered an error when attempting to send the file to the TFTP Server.\n"
                                    + uhe.getMessage() + "\n" + uhe.getStackTrace());
                } catch (IOException ioe) {
                    logger.warn(PC2LogCategory.FSM, subCat,
                            "AutoProvState is not auto generating a provisioning file because the "
                                    + "system encountered an error when attempting to send the file to the TFTP Server.\n"
                                    + ioe.getMessage() + "\n" + ioe.getStackTrace());
                }

            } else {
                logger.info(PC2LogCategory.FSM, subCat,
                        "AutoProvState is stopping because one of the values is null.\n" + "macAddr=" + macAddr
                                + " pcscfLabel=" + pcscfLabel + " tftpIP=" + tftpIP + " tftpPort=" + tftpPort);
            }
        } else {
            if (pd != null)
                logger.info(PC2LogCategory.FSM, subCat,
                        "The provisioning data is null, terminating processing.");
            if (platform != null)
                logger.info(PC2LogCategory.FSM, subCat,
                        "The Platform settings is null, terminating processing.");
            if (dut != null)
                logger.info(PC2LogCategory.FSM, subCat, "The DUT settings is null, terminating processing.");
        }
    } else {
        Generate g = new Generate(EventConstants.AUTO_PROV_PROMPT, null, this.owner.getName());
        try {
            g.execute(super.api, 0);
        } catch (PC2Exception pce) {
            logger.error(PC2LogCategory.FSM, subCat,
                    name + " couldn't generate " + EventConstants.AUTO_PROV_PROMPT + " event to the FSM.");
        }
    }

    return null;
}

From source file:com.cablelabs.sim.PCSim2.java

/**
 * This method determines if the provisioning file used to conduct a test needs to
 * be generated from one of the templates prior to starting the test.
 * @return - the MAC address used for the name of the file
 *//*ww  w  . j ava  2  s. c om*/
private String autoGenerate(ProvisioningData pd) {
    Properties platform = SystemSettings.getSettings(SettingConstants.PLATFORM);
    Properties dut = SystemSettings.getSettings(SettingConstants.DUT);
    if (pd != null && platform != null && dut != null) {
        String pcscfLabel = dut.getProperty(SettingConstants.PCSCF);
        String macAddr = dut.getProperty(SettingConstants.MAC_ADDRESS);
        String tftpIP = platform.getProperty(SettingConstants.TFTP_SERVER_IP);
        String tftpPort = platform.getProperty(SettingConstants.TFTP_SERVER_PORT);
        String phone1 = dut.getProperty(SettingConstants.PHONE_NUMBER_1);
        String phone2 = dut.getProperty(SettingConstants.PHONE_NUMBER_2);
        String cw = platform.getProperty(SettingConstants.CW_NUMBER);
        if (macAddr != null && pcscfLabel != null && tftpIP != null && tftpPort != null && cw != null) {
            // First see if we have already issued a generated file.
            if (!provDB.issued(macAddr, pd)) {
                // Next verify the port is not set to zero
                try {
                    int port = Integer.parseInt(tftpPort);
                    if (port > 0 && port <= 65535) {

                        // Next make sure the TFTP Server IP is not set to 0.0.0.0
                        if (tftpIP.equals("0.0.0.0")) {
                            logger.warn(PC2LogCategory.PCSim2, subCat,
                                    "The TFTP Server IP setting in the platform file is not valid. Ending auto generate operation.");
                            return null;
                        }

                        File input = new File(SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                + SettingConstants.CW + cw + File.separator + pd.getProvFileName());
                        if (input != null) {
                            ProvGen pg = new ProvGen(input);
                            if (phone1 != null)
                                pg.changePhoneNum(SettingConstants.AUTO_GENERATE_PHONE_NUMBER_1, phone1);
                            if (phone2 != null)
                                pg.changePhoneNum(SettingConstants.AUTO_GENERATE_PHONE_NUMBER_2, phone2);
                            Properties pcscf = SystemSettings.getSettings(pcscfLabel);
                            if (pcscf != null) {
                                String pcscfIP = pcscf.getProperty(SettingConstants.IP);
                                if (pcscfIP != null)
                                    pg.changePCSCF(pcscfIP);
                            }
                            String newFileName = macAddr + ".bin";
                            if (pg.output(SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                    + SettingConstants.CW + cw + File.separator + newFileName)) {
                                // Test system
                                //File output = new File(SettingConstants.AUTO_PROV_FILE_DIRECTORY + newFileName);
                                //File pact = new File(SettingConstants.AUTO_PROV_FILE_DIRECTORY + "chinmaya_base_ph1_pcscf.bin");
                                //pg.compare(pact, output);
                                // Create a data entry of the issued event
                                //ProvisioningData issuePD = new ProvisioningData(macAddr, pd.getPolicyFileName(), newFileName);
                                logger.info(PC2LogCategory.PCSim2, subCat,
                                        "Beginning to TFTP the new provisioning file.");
                                provDB.setIssuedData(macAddr, pd);

                                // Next we need to TFTP the file to the server
                                TFTPClient client = new TFTPClient();
                                File binFile = new File(
                                        SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                                + SettingConstants.CW + cw + File.separator + newFileName);
                                if (binFile.isFile() && binFile.canRead()) {
                                    FileInputStream istrm = new FileInputStream(binFile);
                                    //InetAddress ia = InetAddress.getByName("10.4.1.37");
                                    client.open(); // client.open(20003, ia);
                                    client.sendFile(newFileName, TFTP.BINARY_MODE, istrm, tftpIP, port);
                                    client.close();
                                    logger.info(PC2LogCategory.PCSim2, subCat,
                                            "TFTP of the new provisioning file is complete.");
                                    return macAddr;
                                } else {
                                    logger.warn(PC2LogCategory.PCSim2, subCat,
                                            "The " + macAddr + ".bin doesn't appear in the "
                                                    + SettingConstants.AUTO_PROV_FILE_DIRECTORY + File.separator
                                                    + SettingConstants.CW + cw
                                                    + " Ending auto generate operation.");
                                }
                            } else {
                                logger.error(PC2LogCategory.PCSim2, subCat,
                                        "PCSim2 could not locate provisioning template file["
                                                + input.getAbsolutePath() + "].");
                            }
                        }
                        //                     else {
                        //                        logger.info(PC2LogCategory.PCSim2, subCat, "Auto provisioning is terminating because the input directory is null.");
                        //                     }
                    } else {
                        logger.info(PC2LogCategory.PCSim2, subCat,
                                "Auto provisioning is terminating because the port(" + port
                                        + ") is less than 0 or greater than 65535.");
                    }
                } catch (NumberFormatException nfe) {
                    logger.warn(PC2LogCategory.PCSim2, subCat,
                            "PCSim2 is not auto generating a provisioning file because the "
                                    + "TFTP Server Port setting doesn't appear to be a number.");
                } catch (UnknownHostException uhe) {
                    logger.warn(PC2LogCategory.PCSim2, subCat,
                            "PCSim2 is not auto generating a provisioning file because the "
                                    + "system encountered an error when attempting to send the file to the TFTP Server.\n"
                                    + uhe.getMessage() + "\n" + uhe.getStackTrace());
                } catch (IOException ioe) {
                    logger.warn(PC2LogCategory.PCSim2, subCat,
                            "PCSim2 is not auto generating a provisioning file because the "
                                    + "system encountered an error when attempting to send the file to the TFTP Server.\n"
                                    + ioe.getMessage() + "\n" + ioe.getStackTrace());
                }
            } else {
                logger.info(PC2LogCategory.PCSim2, subCat,
                        "Auto provisioning detected the same same provisioning template is already in use, skipping operation.");

            }
        } else {
            logger.info(PC2LogCategory.PCSim2, subCat,
                    "Auto provisioning is stopping because one of the values is null.\n" + "macAddr=" + macAddr
                            + " pcscfLabel=" + pcscfLabel + " tftpIP=" + tftpIP + " tftpPort=" + tftpPort);
        }
    } else {
        if (pd != null)
            logger.info(PC2LogCategory.PCSim2, subCat,
                    "The provisioning data is null, terminating processing.");
        if (platform != null)
            logger.info(PC2LogCategory.PCSim2, subCat,
                    "The Platform settings is null, terminating processing.");
        if (dut != null)
            logger.info(PC2LogCategory.PCSim2, subCat, "The DUT settings is null, terminating processing.");
    }

    return null;
}

From source file:org.apache.commons.net.examples.ftp.TFTPExample.java

private static boolean send(int transferMode, String hostname, String localFilename, String remoteFilename,
        TFTPClient tftp) {
    boolean closed;
    FileInputStream input = null;

    // Try to open local file for reading
    try {//  w  ww. jav  a2  s.c  o m
        input = new FileInputStream(localFilename);
    } catch (IOException e) {
        tftp.close();
        throw new RuntimeException("Error: could not open local file for reading.", e);
    }

    open(tftp);

    // Try to send local file via TFTP
    try {
        String[] parts = hostname.split(":");
        if (parts.length == 2) {
            tftp.sendFile(remoteFilename, transferMode, input, parts[0], Integer.parseInt(parts[1]));
        } else {
            tftp.sendFile(remoteFilename, transferMode, input, hostname);
        }
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Error: I/O exception occurred while sending file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and input file
        closed = close(tftp, input);
    }

    return closed;
}

From source file:person.daizhongde.virtue.util.test.TFTPExample.java

private static boolean send(int transferMode, String hostname, String localFilename, String remoteFilename,
        TFTPClient tftp) {
    boolean closed;
    FileInputStream input = null;

    // Try to open local file for reading
    try {/* www  .j a  v a  2 s  . c o  m*/
        input = new FileInputStream(localFilename);
    } catch (IOException e) {
        tftp.close();
        System.err.println("Error: could not open local file for reading.");
        System.err.println(e.getMessage());
        System.exit(1);
    }

    open(tftp);

    // Try to send local file via TFTP
    try {
        String[] parts = hostname.split(":");
        if (parts.length == 2) {
            tftp.sendFile(remoteFilename, transferMode, input, parts[0], Integer.parseInt(parts[1]));
        } else {
            tftp.sendFile(remoteFilename, transferMode, input, hostname);
        }
    } catch (UnknownHostException e) {
        System.err.println("Error: could not resolve hostname.");
        System.err.println(e.getMessage());
        System.exit(1);
    } catch (IOException e) {
        System.err.println("Error: I/O exception occurred while sending file.");
        System.err.println(e.getMessage());
        System.exit(1);
    } finally {
        // Close local socket and input file
        closed = close(tftp, input);
    }

    return closed;
}