/*
* MammothCopyView.java
*/
package mammothcopy;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.util.Date;
import java.text.DateFormat;
import java.util.Locale;
import java.io.*;
import javax.swing.*;
import java.net.*;
import net.sbbi.upnp.messages.*;
import net.sbbi.upnp.impls.InternetGatewayDevice;
/**
* The application's main frame.
*/
public class MammothCopyView extends FrameView {
public static boolean AppRunning;
static String trgt;
// UPnP stuff
InternetGatewayDevice IGD;
static final int TIMEOUT_UPNPSEARCH = 1500;
// these could go in a settings place
static final int SETTINGS_CHUNKSIZE = 1024*1024;
static int loglevel;
// cross-thread stuff
static String logline, mystatus, partnerstatus;
static String pbMain, pbSub;
static int pbvMain, pbvSub;
static final int LEVEL_DEBUG = 50;
static final int LEVEL_NOTICE = 40;
static final int LEVEL_WARNING = 30;
static final int LEVEL_ERROR = 20;
static final int LEVEL_NONE = 0;
// Thread to update the UI from other threads.
class Logger extends Thread {
@Override public void run() {
while (AppRunning) {
try { Thread.sleep(100); } catch (InterruptedException e) {}
if (!logline.equals("")) {
Date t = new Date(System.currentTimeMillis());
Output.insert(DateFormat.getTimeInstance(DateFormat.MEDIUM, Locale.GERMANY).format(t)+": "+logline+"\n", busyIconIndex);
logline = "";
}
if (!mystatus.equals("")) {
edMyStatus.setText(mystatus);
mystatus = "";
}
if (!partnerstatus.equals("")) {
edPartnerStatus.setText(partnerstatus);
partnerstatus = "";
}
}
}
}
class UI extends Thread {
@Override public void run() {
while (AppRunning) {
try { Thread.sleep(100); } catch (InterruptedException e) {}
if (TheSocket != null && TheSocket.connected) {
// when connected, disable connect and start buttons,
// and enable cancel or SEND buttons
btnStart.setEnabled(false);
btnConnect.setEnabled(false);
btnGo.setEnabled(true);
btnCancel.setEnabled(true);
lblConnected.setText("Connected.");
} else {
btnStart.setEnabled(true);
btnConnect.setEnabled(true);
btnGo.setEnabled(false);
btnCancel.setEnabled(false);
lblConnected.setText("Not connected.");
}
// set button
if (server != null && server.ServerRunning == true) {
btnStart.setText("Stop");
} else {
btnStart.setText("Start");
}
if (ProgressBar.getValue() != pbvMain) {
ProgressBar.setValue(pbvMain);
}
if (!ProgressBar.getString().equals(pbMain)) {
ProgressBar.setString(pbMain);
}
if (SubProgressBar.getValue() != pbvSub) {
SubProgressBar.setValue(pbvSub);
}
if (!SubProgressBar.getString().equals(pbSub)) {
SubProgressBar.setString(pbSub);
}
// fetch the target string
trgt = edTarget.getText();
}
}
}
public static void setMyStatus(String s) {
mystatus = s;
}
public static void setPartnerStatus(String s) {
partnerstatus = s;
}
public static void SetProgressBar(String s, int value) {
pbvMain = value;
pbMain = s;
}
public static void SetSubProgressBar(String s, int value) {
pbvSub = value;
pbSub = s;
}
public static void Log(String s, int level) {
if (level <= loglevel) {
if (logline.equals("")) {
switch (level) {
case LEVEL_DEBUG: s = "(debug) "+s; break;
case LEVEL_NOTICE: s = "(notice) "+s; break;
case LEVEL_WARNING: s = "(warning) "+s; break;
case LEVEL_ERROR: s = "(error) "+s; break;
default: s = "(unknown) "+s; break;
}
logline = s;
} else {
try {
Thread.sleep(10);
Log(s, level);
}
catch (InterruptedException e) {}
}
}
}
private String VERSION = "0.4";
private String APPNAME = "MammothCopy";
private void Version() {
System.out.print(APPNAME+" v"+VERSION);
}
private void Usage() {
System.out.print(
APPNAME+" version "+VERSION+"\r\n" +
APPNAME+" is a tool to send files or directories immediately from one computer to another over the internet.\r\n" +
"It uses UPnP to get around firewall issues.\r\n" +
"\r\n" +
"Usage:\r\n" +
"MammothCopy [-s source] [-t target] [-p ip-address] [-clvh]\r\n" +
"-s source Source file/directory to use for sending\r\n" +
"-t target Target directory to use for receiving\r\n" +
"-p ip-address IP address to connect to\r\n"+
"-l Immediately start listening (convenient for quick receiver setup)\r\n" +
"-c Immediately try to connect (convenient for quick sender setup)\r\n"+
"-h This help\r\n" +
"-v Print version\r\n" +
"");
}
private void ParseCommandLineArgs() {
// MammothCopyApp.cl_args is a String[]
String lastarg = "(none)";
try {
for (int i = 0; i < MammothCopyApp.cl_args.length; i++) {
String arg = MammothCopyApp.cl_args[i];
lastarg = arg;
if (arg.charAt(0) == '-') {
switch (arg.charAt(1)) {
case 't': // target dir
Log("Using "+MammothCopyApp.cl_args[i+1]+" as target dir.", LEVEL_NOTICE);
edTarget.setText(MammothCopyApp.cl_args[i+1]);
break;
case 's':
Log("Using "+MammothCopyApp.cl_args[i+1]+" as source.", LEVEL_NOTICE);
edSource.setText(MammothCopyApp.cl_args[i+1]);
break;
case 'l':
System.out.println("Trying to open listener socket...");
Log("Will be listener.", LEVEL_NOTICE);
btnStart.doClick();
break;
case 'h':
Usage();
System.exit(1);
break;
case 'v':
Version();
System.exit(1);
break;
case 'p':
Log("Using "+MammothCopyApp.cl_args[i+1]+" as partner IP.", LEVEL_NOTICE);
edPartnerIP.setText(MammothCopyApp.cl_args[i+1]);
break;
case 'c':
System.out.println("Trying to connect to "+edPartnerIP.getText()+"...");
Log("Will be client", LEVEL_NOTICE);
btnConnect.doClick();
break;
}
}
}
} catch (IndexOutOfBoundsException e) {
Log("Command line arguments not all understood. Last one found: "+lastarg, LEVEL_WARNING);
}
}
public MammothCopyView(SingleFrameApplication app) {
super(app);
loglevel = LEVEL_DEBUG;
initComponents();
AppRunning = true;
logline = "";
mystatus = "";
partnerstatus = "";
IGD = null;
// our progress bar works with percentages
ProgressBar.setMaximum(100);
ProgressBar.setMinimum(0);
ProgressBar.setValue(0);
// default, we cannot start
btnGo.setEnabled(false);
// Find out our own IP address
// When using UPnP, this will be our routable internet address
try {
InetAddress myIP = InetAddress.getLocalHost();
edMyIP.setText(myIP.getHostAddress());
Log("Local IP address set to: "+myIP.getHostAddress(), MammothCopyView.LEVEL_NOTICE);
// Use a default port. No way of saving settings yet
} catch (UnknownHostException e) {
Log("Could not find my IP address.", MammothCopyView.LEVEL_WARNING);
edMyIP.setText("0.0.0.0");
}
edMyPort.setText("1976");
edPartnerPort.setText("1976");
l = new Logger();
l.start();