-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJavaMultiServer.java
More file actions
104 lines (79 loc) · 2.71 KB
/
JavaMultiServer.java
File metadata and controls
104 lines (79 loc) · 2.71 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
104
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioFactory;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.PinState;
import com.pi4j.io.gpio.RaspiPin;
class JavaMultiServer {
public static void main(String[] args) throws IOException {
final int portNumber = 8000;
final boolean listening = true;
System.out.println("Port: " + portNumber);
try (ServerSocket serverSocket = new ServerSocket(portNumber)) {
// create gpio controller
final GpioController gpio = GpioFactory.getInstance();
final GpioPinDigitalOutput[] pins = new GpioPinDigitalOutput[3];
// provision gpio pin #0 to #6 as an output pin and turn on
pins[1] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_01, "MyLED", PinState.HIGH);
pins[2] = gpio.provisionDigitalOutputPin(RaspiPin.GPIO_02, "MyLED", PinState.HIGH);
while (listening) {
new ServerThread(serverSocket.accept(), pins).start();
}
} catch (final IOException e) {
System.err.println("Could not listen on port " + portNumber);
System.exit(-1);
}
}
}
class ServerThread extends Thread {
private Socket socket = null;
private final GpioPinDigitalOutput[] pins;
public ServerThread(Socket socket, GpioPinDigitalOutput[] pins) {
super("ServerThread");
this.socket = socket;
this.pins = pins;
}
@Override
public void run() {
System.out.println("New client");
try (PrintWriter printWriter = new PrintWriter(this.socket.getOutputStream(), true);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(this.socket.getInputStream()));) {
String inputLine;
final String outputLine;
while ((inputLine = bufferedReader.readLine()) != null) {
System.out.println(inputLine);
/*if(inputLine.equals("DISCONNECT")) {
break;
}*/
final String[] splited = inputLine.split("\\s+");
if ((splited.length == 2) && splited[0].equals("GPIO") && isInteger(splited[1])) {
final int GPIONumber = Integer.parseInt(splited[1]);
lightLED(GPIONumber, this.pins[GPIONumber]);
}
printWriter.println(inputLine); // echo back to sender
}
this.socket.close();
System.out.println("Client disconnected");
} catch (final IOException e) {
e.printStackTrace();
}
}
public static void lightLED(int GPIONumber, GpioPinDigitalOutput pin) {
System.out.println("Light LED : " + GPIONumber);
pin.toggle();
}
public static boolean isInteger(String str) {
try {
final int d = Integer.parseInt(str);
} catch (final NumberFormatException nfe) {
return false;
}
return true;
}
}