-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathCheckNetworkThread.java
More file actions
103 lines (87 loc) · 3.91 KB
/
CheckNetworkThread.java
File metadata and controls
103 lines (87 loc) · 3.91 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
101
102
103
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.sniffer.udp;
import com.sniffer.list.device.DeviceThread;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.MulticastSocket;
import java.net.SocketException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONException;
/**
*
* @author eliseu
*/
public class CheckNetworkThread implements Runnable {
private final String listPath;
private final String snifferID;
private static final String REQUEST_SNIFFER = "anyonesniffer?";
private static final String RESPONSE_SNIFFER = "yes";
private static final String REQUEST_DEVICE = "newdevice";
private static final String RESPONSE_DEVICE = "startregist";
private static final int PACKET_SIZE = 300;
private final String mAddress;
private final int port;
public CheckNetworkThread(String mAddress, int port, String listPath, String snifferID) throws JSONException {
this.mAddress = mAddress;
this.port = port;
this.listPath = listPath;
this.snifferID = snifferID;
}
@Override
public void run() {
try {
System.out.println("\nStart checking new devices and sniffers in the network...");
InetAddress addr = InetAddress.getByName(mAddress);
MulticastSocket mcSock = new MulticastSocket(port);
mcSock.joinGroup(addr);
DatagramPacket rcPacket = new DatagramPacket(new byte[PACKET_SIZE], PACKET_SIZE);
while (true) {
mcSock.receive(rcPacket);
System.out.println("\nNew message.");
String request = new String(rcPacket.getData()).trim();
String deviceIP = rcPacket.getAddress().getHostAddress();
String[] splitRequest = request.split(":");
switch (splitRequest[0]) {
case REQUEST_SNIFFER:
System.out.println("New sniffer in the network.\nSniffer ip: " + deviceIP);
sendResponse(deviceIP, RESPONSE_SNIFFER);
break;
case REQUEST_DEVICE:
System.out.println("New device in the network.\nDevice ID: " + splitRequest[1]);
System.out.println("Device IP: " + deviceIP + ", Port: 1883");
sendResponse(deviceIP, RESPONSE_DEVICE);
(new Thread(new DeviceThread(listPath, snifferID, splitRequest[1], deviceIP, "1883", false), "DeviceThread")).start();
break;
default:
System.out.println("Message doesn't corresponds to the expected.");
break;
}
}
} catch (IOException ex) {
System.err.println("Error checking new network sniffers and devices.");
}
}
private void sendResponse(String host, String responseString) {
try {
InetAddress addr = InetAddress.getByName(host);
byte[] response = responseString.getBytes();
DatagramSocket sendSOCK = new DatagramSocket();
DatagramPacket responsePacket = new DatagramPacket(response, response.length);
responsePacket.setAddress(addr);
responsePacket.setPort(port + 1);
sendSOCK.send(responsePacket);
System.out.println("Sended packet to address: " + responsePacket.getAddress().getHostAddress());
} catch (SocketException ex) {
Logger.getLogger(CheckNetworkThread.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CheckNetworkThread.class.getName()).log(Level.SEVERE, null, ex);
}
}
}