-
Notifications
You must be signed in to change notification settings - Fork 2
/
UDPDataGraph.java
111 lines (94 loc) · 3.76 KB
/
UDPDataGraph.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.net.*;
import java.io.*;
import java.util.*;
import java.awt.event.*;
import graph.*;
public class UDPDataGraph extends JFrame {
static public final long serialVersionUID = 1L;
InetSocketAddress destination_address;
static Hashtable<String,String> datasets = new Hashtable<String,String>();
public static void main( String[] args ) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new UDPDataGraph();
}
});
}
public UDPDataGraph() {
super("Send Text via UDP");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel content = new JPanel( );
content.setLayout( new BoxLayout( content, BoxLayout.Y_AXIS) );
SocketAddressPanel svr = new SocketAddressPanel();
content.add( svr );
GraphPanel pots = new GraphPanel("potentiometers",
new Dimension(300,100),
new Scale(0,1));
pots.dataSource(datasets);
content.add(pots);
GraphPanel acc = new GraphPanel("Acellerometer",
new Dimension(300,200),
new Scale(-1.5,1.5));
acc.dataSource(datasets);
content.add(acc);
this.setContentPane(content);
this.pack();
this.setVisible(true);
pots.addTrace("POT 1",0.25,Color.blue);
pots.addTrace("POT 2",0.5,Color.green.darker());
acc.addTrace("X", 0, Color.red.darker());
acc.addTrace("Y", 0, Color.green.darker());
acc.addTrace("Z", 0, Color.blue);
new Thread( svr ).start();
new javax.swing.Timer(20, pots).start();
new javax.swing.Timer(20, acc).start();
}
static class SocketAddressPanel extends JPanel implements Runnable {
public static final long serialVersionUID = 1L;
static public JLabel ip;
static private JLabel port;
static InetAddress Iam;
static public InetSocketAddress getSocketAddress() {
return new InetSocketAddress(
ip.getText(),
Integer.parseInt( port.getText() )
);
}
public SocketAddressPanel() {
super( new FlowLayout(FlowLayout.LEFT, 5, 0) );
try{
setBorder( BorderFactory.createTitledBorder("Internet Socket Address (listening)") );
add( new JLabel("IP:") );
ip = new JLabel("192.168.");
Iam = InetAddress.getLocalHost();
ip.setText(Iam.getHostAddress());
add(ip);
add( new JLabel(" port:") );
port = new JLabel("65500");
add(port);
}catch(Exception e){}}
public void run(){
try{
byte[] buffer = new byte[512];
DatagramSocket socket = new DatagramSocket(getSocketAddress());
while(true){
DatagramPacket msg = new DatagramPacket(buffer, buffer.length);
socket.receive(msg);
String message = new String(msg.getData());
String[] lines = message.trim().split("\n");
for(String l : lines ) {
if( l.length()>0 ) {
String[] pair = l.trim().split(":");
if( pair.length==2 ) datasets.put(pair[0], pair[1]);
}
}
}
}catch(Exception e){
System.err.println(e);
}
}
}
}