2 * Copyright (c) 2004 University of Murcia. All rights reserved.
\r
3 * --------------------------------------------------------------
\r
4 * For more information, please see <http://www.umu.euro6ix.org/>.
\r
7 package org.umu.cops.prpep;
\r
9 import java.io.IOException;
\r
10 import java.net.Socket;
\r
11 import java.util.Enumeration;
\r
12 import java.util.Hashtable;
\r
14 import org.umu.cops.stack.COPSClientSI;
\r
15 import org.umu.cops.stack.COPSContext;
\r
16 import org.umu.cops.stack.COPSData;
\r
17 import org.umu.cops.stack.COPSDeleteMsg;
\r
18 import org.umu.cops.stack.COPSException;
\r
19 import org.umu.cops.stack.COPSHandle;
\r
20 import org.umu.cops.stack.COPSHeader;
\r
21 import org.umu.cops.stack.COPSPrEPD;
\r
22 import org.umu.cops.stack.COPSPrID;
\r
23 import org.umu.cops.stack.COPSReason;
\r
24 import org.umu.cops.stack.COPSReportMsg;
\r
25 import org.umu.cops.stack.COPSReportType;
\r
26 import org.umu.cops.stack.COPSReqMsg;
\r
27 import org.umu.cops.stack.COPSSyncStateMsg;
\r
30 * COPSPepMsgSender sends COPS messages to PDP.
\r
32 * @version COPSPepMsgSender.java, v 2.00 2004
\r
35 public class COPSPepMsgSender {
\r
38 * Socket connection to PDP
\r
40 protected Socket _sock;
\r
43 * The client-type identifies the policy client
\r
45 protected short _clientType;
\r
48 * The client handle is used to uniquely identify a particular
\r
49 * PEP's request for a client-type
\r
51 protected COPSHandle _handle;
\r
54 * Create a COPSPepMsgSender
\r
56 * @param clientType client-type
\r
57 * @param clientHandle client handle
\r
58 * @param sock socket of PDP connection
\r
60 public COPSPepMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
\r
62 _handle = clientHandle;
\r
63 _clientType = clientType;
\r
69 * Return client handle
\r
71 * @return a COPSHandle
\r
74 public COPSHandle getClientHandle() {
\r
79 * Return client-type
\r
84 public short getClientType() {
\r
89 * Send Request to PDP.
\r
90 * The PEP establishes a request state client handle for which the
\r
91 * remote PDP may maintain state.
\r
93 * @param clientSIs a Hashtable
\r
95 * @throws COPSPepException
\r
98 public void sendRequest(Hashtable clientSIs)
\r
99 throws COPSPepException {
\r
100 // Create COPS Message
\r
101 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_REQ, _clientType);
\r
102 COPSContext cntxt = new COPSContext(COPSContext.CONFIG , (short) 0);
\r
104 COPSHandle handle = _handle;
\r
106 // Add the clientSIs
\r
107 COPSReqMsg msg = new COPSReqMsg();
\r
113 if (clientSIs.size() > 0) {
\r
114 for (Enumeration e = clientSIs.keys() ; e.hasMoreElements() ;) {
\r
115 String strprid = (String) e.nextElement();
\r
116 String strepd = (String) clientSIs.get(strprid);
\r
119 COPSClientSI cSi = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
120 COPSPrID prid = new COPSPrID();
\r
121 prid.setData(new COPSData(strprid));
\r
122 cSi.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
\r
125 COPSClientSI cSi2 = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
126 COPSPrEPD epd = new COPSPrEPD();
\r
127 epd.setData(new COPSData(strepd));
\r
128 cSi2.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));
\r
135 } catch (COPSException e) {
\r
136 throw new COPSPepException("Error making Request Msg, reason: " + e.getMessage());
\r
141 msg.writeData(_sock);
\r
142 } catch (IOException e) {
\r
143 throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
\r
148 * Send Fail Report to PDP.
\r
149 * The RPT message is used by the PEP to communicate to the PDP its
\r
150 * success or failure in carrying out the PDP's decision, or to report
\r
151 * an accounting related change in state.
\r
153 * @throws COPSPepException
\r
156 public void sendFailReport(Hashtable clientSIs)
\r
157 throws COPSPepException {
\r
158 COPSReportMsg msg = new COPSReportMsg();
\r
161 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);
\r
162 COPSHandle hnd = _handle;
\r
164 COPSReportType report = new COPSReportType(COPSReportType.FAILURE);
\r
169 if (clientSIs.size() > 0) {
\r
170 for (Enumeration e = clientSIs.keys() ; e.hasMoreElements() ;) {
\r
171 String strprid = (String) e.nextElement();
\r
172 String strepd = (String) clientSIs.get(strprid);
\r
175 COPSClientSI cSi = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
176 COPSPrID prid = new COPSPrID();
\r
177 prid.setData(new COPSData(strprid));
\r
178 cSi.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
\r
181 COPSClientSI cSi2 = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
182 COPSPrEPD epd = new COPSPrEPD();
\r
183 epd.setData(new COPSData(strepd));
\r
184 cSi2.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));
\r
191 } catch (COPSException ex) {
\r
192 throw new COPSPepException("Error making Msg");
\r
196 msg.writeData(_sock);
\r
197 } catch (IOException e) {
\r
198 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
\r
203 * Send Succes Report to PDP.
\r
204 * The RPT message is used by the PEP to communicate to the PDP its
\r
205 * success or failure in carrying out the PDP's decision, or to report
\r
206 * an accounting related change in state.
\r
208 * @throws COPSPepException
\r
211 public void sendSuccessReport(Hashtable clientSIs)
\r
212 throws COPSPepException {
\r
213 COPSReportMsg msg = new COPSReportMsg();
\r
216 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);
\r
217 COPSHandle hnd = _handle;
\r
219 COPSReportType report = new COPSReportType(COPSReportType.SUCCESS);
\r
225 if (clientSIs.size() > 0) {
\r
226 for (Enumeration e = clientSIs.keys() ; e.hasMoreElements() ;) {
\r
227 String strprid = (String) e.nextElement();
\r
228 String strepd = (String) clientSIs.get(strprid);
\r
231 COPSClientSI cSi = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
232 COPSPrID prid = new COPSPrID();
\r
233 prid.setData(new COPSData(strprid));
\r
234 cSi.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
\r
237 COPSClientSI cSi2 = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
238 COPSPrEPD epd = new COPSPrEPD();
\r
239 epd.setData(new COPSData(strepd));
\r
240 cSi2.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));
\r
247 } catch (COPSException ex) {
\r
248 throw new COPSPepException("Error making Msg");
\r
252 msg.writeData(_sock);
\r
253 } catch (IOException e) {
\r
254 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
\r
258 public void sendAcctReport(Hashtable clientSIs)
\r
259 throws COPSPepException {
\r
260 COPSReportMsg msg = new COPSReportMsg();
\r
263 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);
\r
264 COPSHandle hnd = _handle;
\r
266 COPSReportType report = new COPSReportType(COPSReportType.ACCT);
\r
272 if (clientSIs.size() > 0) {
\r
273 for (Enumeration e = clientSIs.keys() ; e.hasMoreElements() ;) {
\r
274 String strprid = (String) e.nextElement();
\r
275 String strepd = (String) clientSIs.get(strprid);
\r
278 COPSClientSI cSi = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
279 COPSPrID prid = new COPSPrID();
\r
280 prid.setData(new COPSData(strprid));
\r
281 cSi.setData(new COPSData(prid.getDataRep(), 0, prid.getDataLength()));
\r
284 COPSClientSI cSi2 = new COPSClientSI(COPSClientSI.CSI_NAMED);
\r
285 COPSPrEPD epd = new COPSPrEPD();
\r
286 epd.setData(new COPSData(strepd));
\r
287 cSi2.setData(new COPSData(epd.getDataRep(), 0, epd.getDataLength()));
\r
294 } catch (COPSException ex) {
\r
295 throw new COPSPepException("Error making Msg");
\r
299 msg.writeData(_sock);
\r
300 } catch (IOException e) {
\r
301 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
\r
306 * Send Sync State Complete to PDP.
\r
307 * The Synchronize State Complete is sent by the PEP to the PDP after
\r
308 * the PDP sends a synchronize state request to the PEP and the PEP has
\r
309 * finished synchronization.
\r
311 * @throws COPSPepException
\r
314 public void sendSyncComplete()
\r
315 throws COPSPepException {
\r
316 // Common Header with the same ClientType as the request
\r
317 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSC, _clientType);
\r
319 // Client Handle with the same clientHandle as the request
\r
320 COPSHandle clienthandle = _handle;
\r
322 COPSSyncStateMsg msg = new COPSSyncStateMsg();
\r
325 msg.add(clienthandle);
\r
326 } catch (Exception e) {
\r
327 throw new COPSPepException("Error making Msg");
\r
331 msg.writeData(_sock);
\r
332 } catch (IOException e) {
\r
333 throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
\r
338 * Send Delete Request to PDP.
\r
339 * When sent from the PEP this message indicates to the remote PDP that
\r
340 * the state identified by the client handle is no longer
\r
341 * available/relevant.
\r
343 * @throws COPSPepException
\r
346 public void sendDeleteRequest()
\r
347 throws COPSPepException {
\r
348 COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DRQ, _clientType);
\r
349 COPSHandle handle = _handle;
\r
351 // *** TODO: send a real reason
\r
352 COPSReason reason = new COPSReason((short) 234, (short) 345);
\r
354 COPSDeleteMsg msg = new COPSDeleteMsg();
\r
359 } catch (COPSException ex) {
\r
360 throw new COPSPepException("Error making Msg");
\r
363 msg.writeData(_sock);
\r
364 } catch (IOException e) {
\r
365 throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());
\r