-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlThread.java
More file actions
99 lines (92 loc) · 2.35 KB
/
ControlThread.java
File metadata and controls
99 lines (92 loc) · 2.35 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
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
public class ControlThread extends Thread
{
private int finish = 0;
private double setpoint;
private Window gui;
private Socket socket;
private DataOutputStream out;
private DataInputStream in;
private volatile boolean alive = true;
private int threadId;
public ControlThread(Window _gui, Socket _socket, double _setpoint, int _threadId)
{
this.socket = _socket;
this.setpoint = _setpoint;
this.gui = _gui;
this.threadId = _threadId;
try
{
this.out = new DataOutputStream(this.socket.getOutputStream());
this.in = new DataInputStream(this.socket.getInputStream());
out.flush();
}
catch (IOException e)
{
System.out.println("Error obteniendo los streams de un cliente controlador");
e.printStackTrace();
return;
}
System.out.println("Hilo controlador creado");
}
public void run()
{
double output_process_val = 0;
double input_process_val = 0;
double time_value = 0;
while(this.alive)
{
try
{
output_process_val = this.in.readDouble();
input_process_val = this.in.readDouble();
time_value = this.in.readDouble();
out.writeInt(this.finish);
} catch (IOException e)
{
System.out.println("No se pudo hacer la lectura de la señal o la transferencia del finish");
e.printStackTrace();
}
if(this.threadId == 1)
{
this.gui.output_I.add(time_value, output_process_val);
this.gui.input_I.add(time_value, input_process_val);
this.gui.setpoint_I.add(time_value, this.setpoint);
}
else if(this.threadId == 2)
{
this.gui.output_II.add(time_value, output_process_val);
this.gui.input_II.add(time_value, input_process_val);
this.gui.setpoint_II.add(time_value, this.setpoint);
}
}
try
{
System.out.println("Esperando para cerrar los sockets");
Thread.sleep(2000);
} catch (InterruptedException e1)
{
System.out.println("Error esperando para cerrar los sockets");
e1.printStackTrace();
}
try
{
this.out.close();
this.in.close();
this.socket.close();
} catch (IOException e)
{
System.out.println("No se pudo cerrar el socket al dejar el hilo");
e.printStackTrace();
}
System.out.println("Hilo controlador terminado");
}
public void terminate()
{
this.alive = false;
this.finish = 1;
}
}