-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorker.java
96 lines (94 loc) · 3.76 KB
/
Worker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package piper;
import javax.swing.*;
import java.net.*;
import java.io.*;
class Worker
{ private boolean sendFile;
private Thread[] t;
Worker(boolean host, boolean sendFile)
{ this.sendFile = sendFile;
try
{ if(!host)
configureClient();
else
configureServer();
System.exit(0);
}
catch(Exception e){JOptionPane.showMessageDialog(null, e.toString());}
}
private void configureClient()
throws Exception
{ String ip = JOptionPane.showInputDialog("Please enter the ip address of the host:");
if(sendFile)
{ File dir = new File(Settings.packetSendFile.substring(0, Settings.packetSendFile.length() - 4));
if(!dir.isDirectory())
dir.mkdir();
String fileLocation = JOptionPane.showInputDialog("Please enter the location of source file");
t = new Transmitter[Settings.noOfPackets];
Packets.divide(new File(fileLocation));
for(int i = 0;i<Settings.noOfPackets;i++)
{ Socket s = new Socket(ip, Settings.port);
t[i] = new Transmitter(s, i);
t[i].start();
}
for(int i = 0;i<Settings.noOfPackets;i++)
{ t[i].join();
}
}
else
{ File dir = new File(Settings.packetReceiveFile.substring(0, Settings.packetReceiveFile.length() - 4));
if(!dir.isDirectory())
dir.mkdir();
String fileLocation = JOptionPane.showInputDialog("Please enter the location of detination file");
t = new Receiver[Settings.noOfPackets];
for(int i = 0;i<Settings.noOfPackets;i++)
{ Socket s = new Socket(ip, Settings.port);
t[i] = new Receiver(s, i);
t[i].start();
}
for(int i = 0;i<Settings.noOfPackets;i++)
{ t[i].join();
}
Packets.conquer(new File(fileLocation));
}
JOptionPane.showMessageDialog(null, "Transfer done");
}
private void configureServer()
throws Exception
{ ServerSocket ss = new ServerSocket(Settings.port);
if(sendFile)
{ File dir = new File(Settings.packetSendFile.substring(0, Settings.packetSendFile.length() - 4));
if(!dir.isDirectory())
dir.mkdir();
String fileLocation = JOptionPane.showInputDialog("Please enter the location of source file");
t = new Transmitter[Settings.noOfPackets];
Packets.divide(new File(fileLocation));
for(int i = 0;i<Settings.noOfPackets;i++)
{ Socket s = ss.accept();
t[i] = new Transmitter(s, i);
t[i].start();
}
for(int i = 0;i<Settings.noOfPackets;i++)
{ t[i].join();
}
}
else
{ File dir = new File(Settings.packetReceiveFile.substring(0, Settings.packetReceiveFile.length() - 4));
if(!dir.isDirectory())
dir.mkdir();
String fileLocation = JOptionPane.showInputDialog("Please enter the location of detination file");
t = new Receiver[Settings.noOfPackets];
for(int i = 0;i<Settings.noOfPackets;i++)
{ Socket s = ss.accept();
t[i] = new Receiver(s, i);
t[i].start();
}
for(int i = 0;i<Settings.noOfPackets;i++)
{ t[i].join();
}
Packets.conquer(new File(fileLocation));
}
ss.close();
JOptionPane.showMessageDialog(null, "Transfer done");
}
}