View Javadoc
1   /*
2     (C) Copyright IBM Corp. 2007, 2009
3   
4     THIS FILE IS PROVIDED UNDER THE TERMS OF THE ECLIPSE PUBLIC LICENSE
5     ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THIS FILE
6     CONSTITUTES RECIPIENTS ACCEPTANCE OF THE AGREEMENT.
7   
8     You can obtain a current copy of the Eclipse Public License from
9     http://www.opensource.org/licenses/eclipse-1.0.php
10  
11    @author : Endre Bak, IBM, ebak@de.ibm.com
12   * 
13   * Change History
14   * Flag       Date        Prog         Description
15   *------------------------------------------------------------------------------- 
16   * 1804402    2007-09-28  ebak         IPv6 ready SLP
17   * 1892103    2008-02-12  ebak         SLP improvements
18   * 2003590    2008-06-30  blaschke-oss Change licensing from CPL to EPL
19   * 2524131    2009-01-21  raman_arora  Upgrade client to JDK 1.5 (Phase 1)
20   */
21  
22  package org.sentrysoftware.wbem.sblim.slp.internal.ua;
23  
24  /*-
25   * ╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲╱╲
26   * WBEM Java Client
27   * ჻჻჻჻჻჻
28   * Copyright (C) 2023 Sentry Software
29   * ჻჻჻჻჻჻
30   * This program is free software: you can redistribute it and/or modify
31   * it under the terms of the GNU Lesser General Public License as
32   * published by the Free Software Foundation, either version 3 of the
33   * License, or (at your option) any later version.
34   *
35   * This program is distributed in the hope that it will be useful,
36   * but WITHOUT ANY WARRANTY; without even the implied warranty of
37   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38   * GNU General Lesser Public License for more details.
39   *
40   * You should have received a copy of the GNU General Lesser Public
41   * License along with this program.  If not, see
42   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
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   * TCPRequester
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  	 * Ctor.
80  	 * 
81  	 * @param pResTable
82  	 * @param pDestination
83  	 * @param pReqMsg
84  	 * @param pAsThread
85  	 * @throws ServiceLocationException
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  		// FIXME: Is it safe to omit PreviousResopnder list for TCP request?
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 	 * waitFor
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 }