2 * Copyright (c) 2004 University of Murcia. All rights reserved.
3 * --------------------------------------------------------------
4 * For more information, please see <http://www.umu.euro6ix.org/>.
7 package org.umu.cops.prpep;
9 import org.umu.cops.COPSMsgSender;
10 import org.umu.cops.stack.*;
11 import org.umu.cops.stack.COPSClientSI.CSIType;
12 import org.umu.cops.stack.COPSContext.RType;
13 import org.umu.cops.stack.COPSReason.ReasonCode;
14 import org.umu.cops.stack.COPSReportType.ReportType;
16 import java.io.IOException;
17 import java.net.Socket;
18 import java.util.HashSet;
23 * COPSPepMsgSender sends COPS messages to PDP.
25 * @version COPSPepMsgSender.java, v 2.00 2004
28 public class COPSPepMsgSender extends COPSMsgSender {
31 * Create a COPSPepMsgSender
33 * @param clientType client-type
34 * @param clientHandle client handle
35 * @param sock socket of PDP connection
37 public COPSPepMsgSender(short clientType, COPSHandle clientHandle, Socket sock) {
38 super(clientType, clientHandle, sock);
42 * Send Request to PDP.
43 * The PEP establishes a request state client handle for which the
44 * remote PDP may maintain state.
46 * @param clientSIs a Hashtable
48 * @throws COPSPepException
51 public void sendRequest(final Map<String, String> clientSIs) throws COPSPepException {
52 final Set<COPSClientSI> clientSISet = new HashSet<>();
54 for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
56 final COPSPrID prid = new COPSPrID();
57 prid.setData(new COPSData(entry.getKey()));
58 clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
61 final COPSPrEPD epd = new COPSPrEPD();
62 epd.setData(new COPSData(entry.getValue()));
63 clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
65 final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0),
66 null, null, null, clientSISet, null);
71 } catch (IOException e) {
72 throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
77 * Send Fail Report to PDP.
78 * The RPT message is used by the PEP to communicate to the PDP its
79 * success or failure in carrying out the PDP's decision, or to report
80 * an accounting related change in state.
82 * @throws COPSPepException
85 public void sendFailReport(final Map<String, String> clientSIs) throws COPSPepException {
86 sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
90 * Send Succes Report to PDP.
91 * The RPT message is used by the PEP to communicate to the PDP its
92 * success or failure in carrying out the PDP's decision, or to report
93 * an accounting related change in state.
95 * @throws COPSPepException
98 public void sendSuccessReport(final Map<String, String> clientSIs) throws COPSPepException {
99 sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
102 public void sendAcctReport(final Map<String, String> clientSIs) throws COPSPepException {
103 sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
106 private void sendReport(final Map<String, String> clientSIs, final COPSReportType reportType)
107 throws COPSPepException {
109 for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
111 final COPSPrID prid = new COPSPrID();
112 prid.setData(new COPSData(entry.getKey()));
114 final COPSReportMsg pridMsg = new COPSReportMsg(_clientType, _handle, reportType,
115 new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())), null);
117 pridMsg.writeData(_sock);
118 } catch (IOException e) {
119 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
123 final COPSPrEPD epd = new COPSPrEPD();
124 epd.setData(new COPSData(entry.getValue()));
125 final COPSReportMsg epdMsg = new COPSReportMsg(_clientType, _handle, reportType,
126 new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())), null);
128 pridMsg.writeData(_sock);
129 } catch (IOException e) {
130 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
136 * Send Sync State Complete to PDP.
137 * The Synchronize State Complete is sent by the PEP to the PDP after
138 * the PDP sends a synchronize state request to the PEP and the PEP has
139 * finished synchronization.
141 * @throws COPSPepException
144 public void sendSyncComplete() throws COPSPepException {
145 final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
147 msg.writeData(_sock);
148 } catch (IOException e) {
149 throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
154 * Send Delete Request to PDP.
155 * When sent from the PEP this message indicates to the remote PDP that
156 * the state identified by the client handle is no longer
157 * available/relevant.
159 * @throws COPSPepException
162 public void sendDeleteRequest() throws COPSPepException {
163 // *** TODO: send a real reason
164 final COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
165 final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
167 msg.writeData(_sock);
168 } catch (IOException e) {
169 throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());