1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.sentrysoftware.wbem.sblim.slp.internal.ua;
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46 import java.io.IOException;
47 import java.io.OutputStream;
48 import java.net.InetAddress;
49 import java.net.Socket;
50
51 import org.sentrysoftware.wbem.sblim.slp.ServiceLocationException;
52 import org.sentrysoftware.wbem.sblim.slp.internal.SLPConfig;
53 import org.sentrysoftware.wbem.sblim.slp.internal.TRC;
54 import org.sentrysoftware.wbem.sblim.slp.internal.msg.MsgFactory;
55 import org.sentrysoftware.wbem.sblim.slp.internal.msg.ReplyMessage;
56 import org.sentrysoftware.wbem.sblim.slp.internal.msg.RequestMessage;
57
58
59
60
61
62 public class TCPRequester implements Runnable {
63
64 private InetAddress iDestination;
65
66 private Thread iThread;
67
68 private ResultTable iResTable;
69
70 private RequestMessage iReqMsg;
71
72 private byte[] iRequestBytes;
73
74 private int iPort = SLPConfig.getGlobalCfg().getPort();
75
76 private final int iTCPTimeOut = SLPConfig.getGlobalCfg().getTCPTimeout();
77
78
79
80
81
82
83
84
85
86
87 public TCPRequester(ResultTable pResTable, InetAddress pDestination, RequestMessage pReqMsg,
88 boolean pAsThread) throws ServiceLocationException {
89 this.iResTable = pResTable;
90 this.iDestination = pDestination;
91 this.iReqMsg = pReqMsg;
92 this.iRequestBytes = pReqMsg.serializeWithoutResponders(false, false, true);
93
94 if (pAsThread) {
95 this.iThread = new Thread(this);
96 this.iThread.start();
97 } else {
98 this.iThread = null;
99 run();
100 }
101 }
102
103
104
105
106 public void waitFor() {
107 if (this.iThread == null) return;
108 try {
109 this.iThread.join();
110 } catch (InterruptedException e) {
111 TRC.error(e);
112 }
113 }
114
115 public void run() {
116 Socket socket = null;
117 try {
118 socket = new Socket(this.iDestination, this.iPort);
119 socket.setSoTimeout(this.iTCPTimeOut);
120 OutputStream os = socket.getOutputStream();
121 TRC.debug("sendTCP");
122 os.write(this.iRequestBytes);
123 os.flush();
124 handleResponse(socket);
125 TRC.debug("recievedOnTCP");
126 } catch (Exception e) {
127 TRC.error(e.getMessage());
128 } finally {
129 if (socket != null) {
130 try {
131 socket.close();
132 } catch (IOException e) {
133 TRC.error(e);
134 }
135 }
136 }
137
138 }
139
140 private void handleResponse(Socket pSocket) {
141 ReplyMessage replyMsg;
142 try {
143 replyMsg = (ReplyMessage) MsgFactory.parse(pSocket);
144 } catch (Exception e) {
145 this.iResTable.addException(e);
146 return;
147 }
148 if (this.iReqMsg.getXID() == replyMsg.getXID()
149 && this.iReqMsg.isAllowedResponseType(replyMsg)) this.iResTable
150 .addResults(replyMsg);
151 }
152
153 }