Skip to content

Commit 0d2d547

Browse files
committed
Add DatagramSocket Refinements
1 parent 33acddb commit 0d2d547

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.datagramSocket;
2+
3+
import java.net.DatagramPacket;
4+
import java.net.DatagramSocket;
5+
import java.net.InetAddress;
6+
import java.net.SocketAddress;
7+
import java.net.SocketOption;
8+
9+
import liquidjava.specification.ExternalRefinementsFor;
10+
import liquidjava.specification.Refinement;
11+
import liquidjava.specification.RefinementAlias;
12+
import liquidjava.specification.StateRefinement;
13+
import liquidjava.specification.StateSet;
14+
15+
@RefinementAlias("Port(int n) { 0 <= n && n <= 65535 }")
16+
@RefinementAlias("NonNegative(int v) { v >= 0 }")
17+
@RefinementAlias("Positive(int v) { v > 0 }")
18+
@RefinementAlias("TrafficClass(int tc) { 0 <= tc && tc <= 255 }")
19+
@ExternalRefinementsFor("java.net.DatagramSocket")
20+
@StateSet({"unbound", "bound", "connected", "disconnected", "closed" })
21+
public interface DatagramSocketRefinements {
22+
23+
@StateRefinement(to="bound(this)") // "Constructs a datagram socket and binds it to any available port on the local host machine"
24+
public void DatagramSocket();
25+
26+
@StateRefinement(to="bound(this)")
27+
public void DatagramSocket(SocketAddress bindaddr); // cant check for null value to start with unbound state
28+
29+
@StateRefinement(to="bound(this)")
30+
public void DatagramSocket(@Refinement("Port(_)") int port);
31+
32+
@StateRefinement(from="unbound(this)", to="bound(this)")
33+
public void bind(SocketAddress addr);
34+
35+
@StateRefinement(from="bound(this)", to="connected(this)")
36+
@StateRefinement(from="unbound(this)", to="connected(this)") // "If this socket is not bound then this method will first cause the socket to be bound to an address that is assigned automatically (...)"
37+
public void connect(InetAddress address, @Refinement("Port(_)") int port); // cant check if address is null
38+
39+
@StateRefinement(from="bound(this)", to="connected(this)")
40+
@StateRefinement(from="unbound(this)", to="connected(this)")
41+
public void connect(SocketAddress addr); // "If given an InetSocketAddress, this method behaves as if invoking connect(InetAddress,int)"
42+
43+
@StateRefinement(from="connected(this)", to="disconnected(this)")
44+
public void disconnect();
45+
46+
@StateRefinement(from="connected(this)")
47+
public void send(DatagramPacket p);
48+
49+
@StateRefinement(from="connected(this)")
50+
public void receive(DatagramPacket p);
51+
52+
@StateRefinement(from="!closed(this)", to="closed(this)")
53+
public void close();
54+
55+
@StateRefinement(from="unbound(this)")
56+
@StateRefinement(from="bound(this)")
57+
@StateRefinement(from="connected(this)")
58+
@StateRefinement(from="disconnected(this)")
59+
public <T> DatagramSocket setOption(SocketOption<T> name, T value); // "IOException - if an I/O error occurs, or if the socket is closed."
60+
61+
@StateRefinement(from="unbound(this)")
62+
@StateRefinement(from="bound(this)")
63+
@StateRefinement(from="connected(this)")
64+
@StateRefinement(from="disconnected(this)")
65+
public <T> T getOption(SocketOption<T> name); // same as above
66+
67+
public void setSoTimeout(@Refinement("NonNegative(_)") int timeout);
68+
69+
@Refinement("NonNegative(_)")
70+
public int getSoTimeout();
71+
72+
public void setSendBufferSize(@Refinement("Positive(_)") int size);
73+
74+
@Refinement("Positive(_)")
75+
public int getSendBufferSize();
76+
77+
public void setReceiveBufferSize(@Refinement("Positive(_)") int size);
78+
79+
@Refinement("Positive(_)")
80+
public int getReceiveBufferSize();
81+
82+
public void setTrafficClass(@Refinement("TrafficClass(_)") int tc);
83+
84+
@Refinement("TrafficClass(_)")
85+
public int getTrafficClass();
86+
}
87+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.datagramSocket;
2+
3+
import java.net.DatagramSocket;
4+
5+
public class DatagramSocketTests {
6+
7+
public void testOk() throws Exception {
8+
DatagramSocket ds = new DatagramSocket(8080);
9+
ds.connect(null);
10+
ds.setSoTimeout(1000);
11+
ds.setSendBufferSize(100);
12+
ds.setReceiveBufferSize(1000);
13+
ds.setTrafficClass(50);
14+
ds.send(null);
15+
ds.send(null);
16+
ds.receive(null);
17+
ds.receive(null);
18+
ds.setOption(null, null);
19+
ds.getOption(null);
20+
ds.disconnect();
21+
ds.close();
22+
}
23+
24+
public void testSendingAfterDisconnecting() throws Exception {
25+
DatagramSocket ds = new DatagramSocket(8080);
26+
ds.connect(null);
27+
ds.disconnect();
28+
ds.send(null); // error
29+
}
30+
31+
public void testBindingAfterAlreadyBound() throws Exception {
32+
DatagramSocket ds = new DatagramSocket(8080);
33+
ds.bind(null); // error
34+
}
35+
}

0 commit comments

Comments
 (0)