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
112
113
114
115
116
|
package org.openslx.dnbd3;
import java.io.DataOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.Filters;
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.newsclub.net.unix.AFUNIXSocket;
import org.newsclub.net.unix.AFUNIXSocketAddress;
public class DNBD3Server {
private final String UNIXSOCK = "/run/dnbd3-server.sock";
private static final int CMDSTOP = 0;
private static final int CMDRELOAD = 1;
private static final int CMDINFO = 2;
public void doReload() throws IOException {
AFUNIXSocket sock = connect();
sendCommand(sock, CMDRELOAD);
sock.close();
}
public void doShutdown() throws IOException {
AFUNIXSocket sock = connect();
sendCommand(sock, CMDSTOP);
sock.close();
}
public List<DNBD3Image> getImages() throws IOException {
List<DNBD3Image> images = new ArrayList<DNBD3Image>();
AFUNIXSocket sock = connect();
sendCommand(sock, CMDINFO);
try {
String atime, vid, rid, file;
InputStream is = sock.getInputStream();
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(is);
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp;
xp = xpfac.compile("//dnbd3-server/images/image", Filters.element());
for (Element e : xp.evaluate(document)) {
atime = e.getAttributeValue("atime");
vid = e.getAttributeValue("vid");
rid = e.getAttributeValue("rid");
file = e.getAttributeValue("file");
images.add(new DNBD3Image(atime, vid, rid, file));
}
is.close();
} catch (JDOMException ex) {
ex.printStackTrace();
}
sock.close();
return images;
}
public List<DNBD3Client> getClients() throws IOException {
List<DNBD3Client> clients = new ArrayList<DNBD3Client>();
AFUNIXSocket sock = connect();
sendCommand(sock, CMDINFO);
try {
String ip, image;
InputStream is = sock.getInputStream();
SAXBuilder builder = new SAXBuilder();
Document document = (Document) builder.build(is);
XPathFactory xpfac = XPathFactory.instance();
XPathExpression<Element> xp;
xp = xpfac.compile("//dnbd3-server/clients/client", Filters.element());
for (Element e : xp.evaluate(document)) {
ip = e.getAttributeValue("ip");
image = e.getAttributeValue("file");
clients.add(new DNBD3Client(ip, image));
}
is.close();
} catch (JDOMException ex) {
ex.printStackTrace();
}
sock.close();
return clients;
}
////////////////////////////////////////////////////////////////////////////////
private AFUNIXSocket connect() throws IOException {
AFUNIXSocket sock = null;
File socketFile = new File(UNIXSOCK);
sock = AFUNIXSocket.newInstance();
sock.connect(new AFUNIXSocketAddress(socketFile));
return sock;
}
private void sendCommand(AFUNIXSocket sock, int cmd) throws IOException {
OutputStream os = sock.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.writeInt(cmd);
os.close();
}
}
|