Skip to content

Commit 1cc04e6

Browse files
committed
feat: add socket refinement interface with state and method specifications
1 parent f1d2090 commit 1cc04e6

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.barista.socket;
2+
3+
import java.io.InputStream;
4+
import java.io.OutputStream;
5+
import java.net.InetAddress;
6+
import java.net.Proxy;
7+
import java.net.Socket;
8+
import java.net.SocketAddress;
9+
import java.net.SocketOption;
10+
11+
import liquidjava.specification.Refinement;
12+
import liquidjava.specification.RefinementAlias;
13+
import liquidjava.specification.StateSet;
14+
import liquidjava.specification.StateRefinement;
15+
import liquidjava.specification.ExternalRefinementsFor;
16+
17+
@StateSet({"unconnected", "binded", "connected", "closed"})
18+
@RefinementAlias("Positive(int x) {x >= 0}")
19+
@ExternalRefinementsFor("java.net.Socket")
20+
public interface SocketRefinements {
21+
22+
// Constructors
23+
@StateRefinement(to="unconnected(this)")
24+
public void Socket();
25+
26+
@StateRefinement(to="unconnected(this)")
27+
public void Socket(Proxy proxy);
28+
29+
@StateRefinement(to="connected(this)")
30+
public void Socket(String host, @Refinement("Positive(_)") int port);
31+
32+
@StateRefinement(to="connected(this)")
33+
public void Socket(String host, @Refinement("Positive(_)") int port, InetAddress localAddr, @Refinement("Positive(_)") int localPort);
34+
35+
@StateRefinement(to="connected(this)")
36+
public void Socket(InetAddress address, @Refinement("Positive(_)") int port, InetAddress localAddr, @Refinement("Positive(_)") int localPort);
37+
38+
// Methods
39+
@StateRefinement(from="unconnected(this)", to="binded(this)")
40+
public void bind(SocketAddress bindpoint);
41+
42+
@StateRefinement(from="unconnected(this)", to="unconnected(this)")
43+
@StateRefinement(from="binded(this)", to="binded(this)")
44+
public void setPerformancePreferences(int connectionTime, int latency, int bandwith);
45+
46+
@StateRefinement(from="unconnected(this)", to="unconnected(this)")
47+
@StateRefinement(from="binded(this)", to="binded(this)")
48+
@StateRefinement(from="connected(this)", to="connected(this)")
49+
public <T> Socket setOption(SocketOption<T> name, T value);
50+
51+
@StateRefinement(from="unconnected(this)", to="unconnected(this)")
52+
@StateRefinement(from="binded(this)", to="binded(this)")
53+
@StateRefinement(from="connected(this)", to="connected(this)")
54+
public <T> T getOption(SocketOption<T> name);
55+
56+
@StateRefinement(from="connected(this)", to="connected(this)")
57+
public InputStream getInputStream();
58+
59+
@StateRefinement(from="connected(this)", to="connected(this)")
60+
public OutputStream getOutputStream();
61+
62+
@StateRefinement(from="connected(this)", to="connected(this)")
63+
public void sendUrgentData(int data);
64+
65+
@StateRefinement(from="unconnected(this) || binded(this)", to="connected(this)")
66+
public void connect(SocketAddress endpoint);
67+
68+
@StateRefinement(from="unconnected(this) || binded(this)", to="connected(this)")
69+
public void connect(SocketAddress endpoint, @Refinement("Positive(_)") int timeout);
70+
71+
@StateRefinement(from="unconnected(this)", to="unconnected(this)")
72+
@StateRefinement(from="binded(this)", to="binded(this)")
73+
@StateRefinement(from="connected(this)", to="connected(this)")
74+
public void setSoLinger(boolean on, @Refinement("Positive(_)") int linger);
75+
76+
@StateRefinement(from="unconnected(this) || connected(this) || binded(this)", to="closed(this)")
77+
public void close();
78+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.barista.socket;
2+
3+
import java.io.IOException;
4+
import java.net.Socket;
5+
6+
public interface SocketTest {
7+
8+
public static void test1() throws IOException {
9+
Socket s = new Socket();
10+
s.close();
11+
s.sendUrgentData(0);
12+
}
13+
14+
public static void test2() throws IOException{
15+
Socket s = new Socket("localhost", 8080);
16+
s.bind(null);
17+
}
18+
19+
20+
public static void test3() throws IOException{
21+
Socket s = new Socket();
22+
s.connect(null);
23+
s.connect(null);
24+
}
25+
26+
public static void test4() throws IOException{
27+
Socket s = new Socket();
28+
s.bind(null);
29+
s.bind(null);
30+
}
31+
}

0 commit comments

Comments
 (0)