1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package org.netbeans.mobilecenter;
  27 
  28 import java.io.File;
  29 import java.io.IOException;
  30 import java.io.ObjectOutputStream;
  31 import java.net.DatagramPacket;
  32 import java.net.DatagramSocket;
  33 import java.net.InetAddress;
  34 import java.net.Socket;
  35 import java.net.SocketTimeoutException;
  36 import java.nio.file.Files;
  37 
  38 public final class Redeploy {
  39     private Redeploy() {
  40     }
  41 
  42     public static void main(String... args) {
  43         if (args.length != 1) {
  44             System.err.println("Usage: <jar_file_to_deploy>");
  45             System.exit(5);
  46         }
  47         File f = new File(args[0]);
  48         if (!f.exists()) {
  49             System.err.println("File " + f + " does not exist");
  50             System.exit(1);
  51         }
  52 
  53         deploy(f);
  54         System.err.println("Success!");
  55         System.exit(0);
  56     }
  57 
  58     public static final String MULTICAST_DISCOVERY = "NetBeansMulticastDiscovery";
  59     public static final String GROUP_ADDRESS = "225.10.10.0";
  60     public static final int PORT = 44445;
  61     public static final int PACKET_LENGTH = 1024;
  62     public static final int TIMEOUT = 5000;
  63 
  64     private static void deploy(File f) {
  65         DatagramSocket datagramSocket = null;
  66 
  67         try {
  68             byte[] buf = new byte[PACKET_LENGTH];
  69             buf = MULTICAST_DISCOVERY.getBytes();
  70             datagramSocket = new DatagramSocket();
  71             InetAddress group = InetAddress.getByName(GROUP_ADDRESS);
  72             DatagramPacket packet = new DatagramPacket(buf, buf.length, group, PORT);
  73             datagramSocket.send(packet);
  74 
  75             buf = new byte[PACKET_LENGTH];
  76             datagramSocket.setSoTimeout(TIMEOUT);
  77             packet = new DatagramPacket(buf, buf.length);
  78             datagramSocket.receive(packet);
  79             String received = new String(packet.getData()).trim();
  80             if (!received.equals("firstRun")) {
  81                 Thread.sleep(1000);
  82                 sendFile(f, packet.getAddress(), Integer.parseInt(received));
  83             }
  84             //JOptionPane.showMessageDialog(null, "Found iOS device on IP: " + received);
  85         } catch (SocketTimeoutException ex) {
  86             System.err.println("No iOS devices were found");
  87             System.exit(2);
  88         } catch (IOException | InterruptedException ex) {
  89             ex.printStackTrace();
  90             System.exit(3);
  91         } finally {
  92             if (datagramSocket != null) {
  93                 datagramSocket.close();
  94             }
  95         }
  96     }
  97 
  98     private static void sendFile(File f, InetAddress iOSAddress, int port) throws IOException {
  99         System.err.println("Deploying " + f + " to " + iOSAddress + ":" + port);
 100         try (Socket socket = new Socket(iOSAddress, port)) {
 101             byte[] buffer = Files.readAllBytes(f.toPath());
 102             try (ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream())) {
 103                 oos.writeObject(buffer);
 104             }
 105         }
 106     }
 107 
 108 }