-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatListener.java
More file actions
100 lines (89 loc) · 2.95 KB
/
ChatListener.java
File metadata and controls
100 lines (89 loc) · 2.95 KB
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
97
98
99
100
package src2;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
class ChatListener implements Runnable
{
private DatagramSocket ds;
private FileEvent fileEvent = null;
private Client c;
// constructor
public ChatListener(DatagramSocket ds,Client c) {
this.ds = ds;
this.c = c;
}
@Override
public void run() {
try {
byte[] receive = new byte[65535];
DatagramPacket DpReceive = null;
while (true) {
DpReceive = new DatagramPacket(receive, receive.length);
ds.receive(DpReceive);
String message = new String (DpReceive.getData(), 0, DpReceive.getLength());
String[] peer = message.split(":");
boolean breakFlag = false;
for (ChatManager x : c.getChatManagerList()) {
if (x.getSocketAddress().equals(DpReceive.getSocketAddress().toString())) {
x.addTextFromOtherUser(message);
breakFlag = true;
break;
}
}
if (!breakFlag) {
String[] b = DpReceive.getSocketAddress().toString().substring(1).split(":");
String ipaddress = b[0];
String port = b[1];
ChatManager cm = new ChatManager(peer[0],ipaddress,port,ds,c.getUsername());
c.addChatManagerList(cm);
cm.start();
cm.addTextFromOtherUser(message);
}
if (message.contains("\\")) {
byte[] incomingData = new byte[1024 * 1000 * 50];
DatagramPacket incomingPacket = new DatagramPacket(incomingData, incomingData.length);
ds.receive(incomingPacket);
byte[] data = incomingPacket.getData();
ByteArrayInputStream in = new ByteArrayInputStream(data);
ObjectInputStream is = new ObjectInputStream(in);
fileEvent = (FileEvent) is.readObject();
if (fileEvent.getStatus().equalsIgnoreCase("Error")) {
System.out.println("Some issue happened while packing the data @ client side");
continue;
}
createAndWriteFile();
receive = new byte[65535];
}
}
} catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public void createAndWriteFile() {
String outputFile = fileEvent.getDestinationDirectory() + fileEvent.getFilename();
if (!new File(fileEvent.getDestinationDirectory()).exists()) {
new File(fileEvent.getDestinationDirectory()).mkdirs();
}
File dstFile = new File(outputFile);
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(dstFile);
fileOutputStream.write(fileEvent.getFileData());
fileOutputStream.flush();
fileOutputStream.close();
System.out.println("Output file : " + outputFile + " is successfully saved ");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}