1 package org.umu.cops.ospdp;
\r
3 import java.io.IOException;
\r
4 import java.net.Socket;
\r
5 import java.util.Enumeration;
\r
6 import java.util.Vector;
\r
8 import org.umu.cops.stack.COPSContext;
\r
9 import org.umu.cops.stack.COPSDecision;
\r
10 import org.umu.cops.stack.COPSDecisionMsg;
\r
11 import org.umu.cops.stack.COPSException;
\r
12 import org.umu.cops.stack.COPSHandle;
\r
13 import org.umu.cops.stack.COPSHeader;
\r
14 import org.umu.cops.stack.COPSSyncStateMsg;
\r
17 * COPS message transceiver class for outsourcing connections at the PDP side.
\r
19 public class COPSPdpOSMsgSender {
\r
21 * Socket connected to PEP
\r
23 protected Socket _sock;
\r
26 * COPS client-type that identifies the policy client
\r
28 protected short _clientType;
\r
31 * COPS client handle used to uniquely identify a particular
\r
32 * PEP's request for a client-type
\r
34 protected COPSHandle _handle;
\r
37 * Creates a COPSPepMsgSender
\r
39 * @param clientType COPS client-type
\r
40 * @param clientHandle Client handle
\r
41 * @param sock Socket to the PEP
\r
43 public COPSPdpOSMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {
\r
45 _handle = clientHandle;
\r
46 _clientType = clientType;
\r
52 * Gets the client handle
\r
53 * @return Client's <tt>COPSHandle</tt>
\r
55 public COPSHandle getClientHandle() {
\r
60 * Gets the client-type
\r
61 * @return Client-type value
\r
63 public short getClientType() {
\r
68 * Sends a decision message which was requested by the PEP
\r
69 * @param removeDecs Decisions to be removed
\r
70 * @param installDecs Decisions to be installed
\r
71 * @throws COPSPdpException
\r
73 public void sendSolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
\r
74 sendDecision(removeDecs, installDecs, true);
\r
78 * Sends a decision message which was not requested by the PEP
\r
79 * @param removeDecs Decisions to be removed
\r
80 * @param installDecs Decisions to be installed
\r
81 * @throws COPSPdpException
\r
83 public void sendUnsolicitedDecision(Vector removeDecs, Vector installDecs) throws COPSPdpException {
\r
84 sendDecision(removeDecs, installDecs, false);
\r
88 * Sends a decision message to the PEP
\r
89 * @param removeDecs Decisions to be removed
\r
90 * @param installDecs Decisions to be installed
\r
91 * @param solicited <tt>true</tt> if the PEP requested this decision, <tt>false</tt> otherwise
\r
92 * @throws COPSPdpException
\r
94 public void sendDecision(Vector removeDecs, Vector installDecs, boolean solicited) throws COPSPdpException {
\r
95 // Common Header holding the same ClientType as the request
\r
96 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
\r
99 hdr.setFlag(COPSHeader.COPS_FLAG_SOLICITED);
\r
101 // Client Handle with the same clientHandle as the request
\r
102 COPSHandle handle = new COPSHandle();
\r
103 handle.setId(getClientHandle().getId());
\r
105 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\r
107 decisionMsg.add(hdr);
\r
108 decisionMsg.add(handle);
\r
110 // Decisions (no flags supplied)
\r
112 COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);
\r
114 // Remove Decisions
\r
115 // <Decision: Flags>
\r
116 if (removeDecs.size() > 0) {
\r
117 COPSDecision rdec1 = new COPSDecision();
\r
118 rdec1.setCmdCode(COPSDecision.DEC_REMOVE);
\r
120 decisionMsg.addDecision(rdec1, cntxt);
\r
122 Enumeration removeDecsEnum = removeDecs.elements();
\r
123 while (removeDecsEnum.hasMoreElements())
\r
124 decisionMsg.addDecision((COPSDecision) removeDecsEnum.nextElement(), cntxt);
\r
127 // Install Decisions
\r
128 // <Decision: Flags>
\r
129 if (installDecs.size() > 0) {
\r
130 COPSDecision idec1 = new COPSDecision();
\r
131 idec1.setCmdCode(COPSDecision.DEC_INSTALL);
\r
133 decisionMsg.addDecision(idec1, cntxt);
\r
135 Enumeration installDecsEnum = installDecs.elements();
\r
136 while (installDecsEnum.hasMoreElements())
\r
137 decisionMsg.addDecision((COPSDecision) installDecsEnum.nextElement(), cntxt);
\r
139 COPSIntegrity intr = new COPSIntegrity();
\r
142 intr.setKeyDigest(new COPSData("KEY DIGEST"));
\r
143 decisionMsg.add(intr);
\r
146 } catch (COPSException e) {
\r
147 e.printStackTrace();
\r
148 throw new COPSPdpException("Error making Msg");
\r
154 decisionMsg.writeData(_sock);
\r
155 } catch (IOException e) {
\r
156 throw new COPSPdpException("Failed to send the decision, reason: " + e.getMessage());
\r
161 * Sends a message asking that the request state be deleted
\r
162 * @throws COPSPdpException
\r
164 public void sendDeleteRequestState() throws COPSPdpException {
\r
165 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
\r
169 * <Decision> ::= <Context>
\r
170 * <Decision: Flags>
\r
171 * <Decision: Flags> ::= Remove Request-State
\r
175 // Common Header with the same ClientType as the request (default UNSOLICITED)
\r
176 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
\r
178 // Client Handle with the same clientHandle as the request
\r
179 COPSHandle clienthandle = new COPSHandle();
\r
180 clienthandle.setId(_handle.getId());
\r
184 COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);
\r
185 // <Decision: Flags>
\r
186 COPSDecision dec = new COPSDecision();
\r
187 dec.setCmdCode(COPSDecision.DEC_REMOVE);
\r
188 dec.setFlags(COPSDecision.F_REQSTATE);
\r
190 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\r
192 decisionMsg.add(hdr);
\r
193 decisionMsg.add(clienthandle);
\r
194 decisionMsg.addDecision(dec, cntxt);
\r
195 } catch (COPSException e) {
\r
196 throw new COPSPdpException("Error making Msg");
\r
200 decisionMsg.writeData(_sock);
\r
201 } catch (IOException e) {
\r
202 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
\r
207 * Method sendOpenNewRequestState
\r
209 * @throws COPSPdpException
\r
213 public void sendOpenNewRequestState() throws COPSPdpException {
\r
214 /* <Decision Message> ::= <Common Header: Flag UNSOLICITED>
\r
218 * <Decision> ::= <Context>
\r
219 * <Decision: Flags>
\r
220 * <Decision: Flags> ::= Install Request-State
\r
224 // Common Header with the same ClientType as the request (default UNSOLICITED)
\r
225 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_DEC, getClientType());
\r
227 // Client Handle with the same clientHandle as the request
\r
228 COPSHandle clienthandle = new COPSHandle();
\r
229 clienthandle.setId(_handle.getId());
\r
233 COPSContext cntxt = new COPSContext(COPSContext.CONFIG, (short) 0);
\r
234 // <Decision: Flags>
\r
235 COPSDecision dec = new COPSDecision();
\r
236 dec.setCmdCode(COPSDecision.DEC_INSTALL);
\r
237 dec.setFlags(COPSDecision.F_REQSTATE);
\r
239 COPSDecisionMsg decisionMsg = new COPSDecisionMsg();
\r
241 decisionMsg.add(hdr);
\r
242 decisionMsg.add(clienthandle);
\r
243 decisionMsg.addDecision(dec, cntxt);
\r
244 } catch (COPSException e) {
\r
245 throw new COPSPdpException("Error making Msg");
\r
249 decisionMsg.writeData(_sock);
\r
250 } catch (IOException e) {
\r
251 throw new COPSPdpException("Failed to send the open new request state, reason: " + e.getMessage());
\r
256 * Sends a message asking for a COPS sync operation
\r
257 * @throws COPSPdpException
\r
259 public void sendSyncRequestState()
\r
260 throws COPSPdpException {
\r
261 /* <Synchronize State Request> ::= <Common Header>
\r
262 * [<Client Handle>]
\r
266 // Common Header with the same ClientType as the request
\r
267 COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSQ, getClientType());
\r
269 // Client Handle with the same clientHandle as the request
\r
270 COPSHandle clienthandle = new COPSHandle();
\r
271 clienthandle.setId(_handle.getId());
\r
273 COPSSyncStateMsg msg = new COPSSyncStateMsg();
\r
276 msg.add(clienthandle);
\r
277 } catch (Exception e) {
\r
278 throw new COPSPdpException("Error making Msg");
\r
282 msg.writeData(_sock);
\r
283 } catch (IOException e) {
\r
284 throw new COPSPdpException("Failed to send the sync state request, reason: " + e.getMessage());
\r