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.stack.*;
10 import org.umu.cops.stack.COPSClientSI.CSIType;
11 import org.umu.cops.stack.COPSContext.RType;
12 import org.umu.cops.stack.COPSReason.ReasonCode;
13 import org.umu.cops.stack.COPSReportType.ReportType;
15 import java.io.IOException;
16 import java.net.Socket;
17 import java.util.HashSet;
22 * COPSPepMsgSender sends COPS messages to PDP.
24 * @version COPSPepMsgSender.java, v 2.00 2004
27 public class COPSPepMsgSender {
30 * Socket connection to PDP
32 protected Socket _sock;
35 * The client-type identifies the policy client
37 protected short _clientType;
40 * The client handle is used to uniquely identify a particular
41 * PEP's request for a client-type
43 protected COPSHandle _handle;
46 * Create a COPSPepMsgSender
48 * @param clientType client-type
49 * @param clientHandle client handle
50 * @param sock socket of PDP connection
52 public COPSPepMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
54 _handle = clientHandle;
55 _clientType = clientType;
61 * Return client handle
63 * @return a COPSHandle
66 public COPSHandle getClientHandle() {
76 public int getClientType() {
81 * Send Request to PDP.
82 * The PEP establishes a request state client handle for which the
83 * remote PDP may maintain state.
85 * @param clientSIs a Hashtable
87 * @throws COPSPepException
90 public void sendRequest(final Map<String, String> clientSIs) throws COPSPepException {
91 final Set<COPSClientSI> clientSISet = new HashSet<>();
93 for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
95 final COPSPrID prid = new COPSPrID();
96 prid.setData(new COPSData(entry.getKey()));
97 clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())));
100 final COPSPrEPD epd = new COPSPrEPD();
101 epd.setData(new COPSData(entry.getValue()));
102 clientSISet.add(new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())));
104 final COPSReqMsg msg = new COPSReqMsg(_clientType, _handle, new COPSContext(RType.CONFIG, (short)0),
105 null, null, null, clientSISet, null);
109 msg.writeData(_sock);
110 } catch (IOException e) {
111 throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());
116 * Send Fail Report to PDP.
117 * The RPT message is used by the PEP to communicate to the PDP its
118 * success or failure in carrying out the PDP's decision, or to report
119 * an accounting related change in state.
121 * @throws COPSPepException
124 public void sendFailReport(final Map<String, String> clientSIs) throws COPSPepException {
125 sendReport(clientSIs, new COPSReportType(ReportType.FAILURE));
129 * Send Succes Report to PDP.
130 * The RPT message is used by the PEP to communicate to the PDP its
131 * success or failure in carrying out the PDP's decision, or to report
132 * an accounting related change in state.
134 * @throws COPSPepException
137 public void sendSuccessReport(final Map<String, String> clientSIs) throws COPSPepException {
138 sendReport(clientSIs, new COPSReportType(ReportType.SUCCESS));
141 public void sendAcctReport(final Map<String, String> clientSIs) throws COPSPepException {
142 sendReport(clientSIs, new COPSReportType(ReportType.ACCOUNTING));
145 private void sendReport(final Map<String, String> clientSIs, final COPSReportType reportType)
146 throws COPSPepException {
148 for (final Map.Entry<String, String> entry : clientSIs.entrySet()) {
150 final COPSPrID prid = new COPSPrID();
151 prid.setData(new COPSData(entry.getKey()));
153 final COPSReportMsg pridMsg = new COPSReportMsg(_clientType, _handle, reportType,
154 new COPSClientSI(CSIType.NAMED, new COPSData(prid.getDataRep(), 0, prid.getDataLength())), null);
156 pridMsg.writeData(_sock);
157 } catch (IOException e) {
158 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
162 final COPSPrEPD epd = new COPSPrEPD();
163 epd.setData(new COPSData(entry.getValue()));
164 final COPSReportMsg epdMsg = new COPSReportMsg(_clientType, _handle, reportType,
165 new COPSClientSI(CSIType.NAMED, new COPSData(epd.getDataRep(), 0, epd.getDataLength())), null);
167 pridMsg.writeData(_sock);
168 } catch (IOException e) {
169 throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());
175 * Send Sync State Complete to PDP.
176 * The Synchronize State Complete is sent by the PEP to the PDP after
177 * the PDP sends a synchronize state request to the PEP and the PEP has
178 * finished synchronization.
180 * @throws COPSPepException
183 public void sendSyncComplete() throws COPSPepException {
184 final COPSSyncStateMsg msg = new COPSSyncStateMsg(_clientType, _handle, null);
186 msg.writeData(_sock);
187 } catch (IOException e) {
188 throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());
193 * Send Delete Request to PDP.
194 * When sent from the PEP this message indicates to the remote PDP that
195 * the state identified by the client handle is no longer
196 * available/relevant.
198 * @throws COPSPepException
201 public void sendDeleteRequest() throws COPSPepException {
202 // *** TODO: send a real reason
203 final COPSReason reason = new COPSReason(ReasonCode.UNSPECIFIED, ReasonCode.NA);
204 final COPSDeleteMsg msg = new COPSDeleteMsg(_clientType, _handle, reason, null);
206 msg.writeData(_sock);
207 } catch (IOException e) {
208 throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());