Basic fixes to make build work :)
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / ospep / COPSPepOSMsgSender.java
1 package org.umu.cops.ospep;\r
2 \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
7 \r
8 import org.umu.cops.stack.COPSClientSI;\r
9 import org.umu.cops.stack.COPSContext;\r
10 import org.umu.cops.stack.COPSDeleteMsg;\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.COPSReason;\r
15 import org.umu.cops.stack.COPSReportMsg;\r
16 import org.umu.cops.stack.COPSReportType;\r
17 import org.umu.cops.stack.COPSReqMsg;\r
18 import org.umu.cops.stack.COPSSyncStateMsg;\r
19 \r
20 /**\r
21  * COPS message transceiver class for outsourcing connections at the PEP side.\r
22  */\r
23 public class COPSPepOSMsgSender {\r
24     /**\r
25      * Socket connection to PDP\r
26      */\r
27     protected Socket _sock;\r
28 \r
29     /**\r
30      * COPS client-type that identifies the policy client\r
31      */\r
32     protected short _clientType;\r
33 \r
34     /**\r
35      * COPS client handle used to uniquely identify a particular\r
36      * PEP's request for a client-type\r
37      */\r
38     protected COPSHandle _handle;\r
39 \r
40     /**\r
41      * Creates a COPSPepMsgSender\r
42      *\r
43      * @param clientType        Client-type\r
44      * @param clientHandle      Client handle\r
45      * @param sock              Socket connected to the PDP\r
46      */\r
47     public COPSPepOSMsgSender (short clientType, COPSHandle clientHandle, Socket sock) {\r
48         // COPS Handle\r
49         _handle = clientHandle;\r
50         _clientType = clientType;\r
51 \r
52         _sock = sock;\r
53     }\r
54 \r
55     /**\r
56      * Gets the client handle\r
57      * @return  Client's <tt>COPSHandle</tt>\r
58      */\r
59     public COPSHandle getClientHandle() {\r
60         return _handle;\r
61     }\r
62 \r
63     /**\r
64      * Gets the client-type\r
65      * @return  Client-type value\r
66      */\r
67     public short getClientType() {\r
68         return _clientType;\r
69     }\r
70 \r
71     /**\r
72      * Sends a request to the PDP.\r
73      * The PEP establishes a request state client handle for which the\r
74      * remote PDP may maintain state.\r
75      * @param    clientSIs              Client data\r
76      * @throws   COPSPepException\r
77      */\r
78     public void sendRequest(Vector clientSIs) throws COPSPepException {\r
79         // Create COPS Message\r
80         COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_REQ, _clientType);\r
81 \r
82         COPSContext cntxt = new COPSContext(COPSContext.CONFIG , (short) 0);\r
83 \r
84         COPSHandle handle = _handle;\r
85 \r
86         COPSReqMsg msg = new COPSReqMsg();\r
87         try {\r
88             msg.add(hdr) ;\r
89             msg.add(handle) ;\r
90             msg.add(cntxt) ;\r
91 \r
92             Enumeration clientSIEnum = clientSIs.elements();\r
93             while (clientSIEnum.hasMoreElements())\r
94                 msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
95         } catch (COPSException e) {\r
96             throw new COPSPepException("Error making Request Msg, reason: " + e.getMessage());\r
97         }\r
98 \r
99         // Send message\r
100         try {\r
101             msg.writeData(_sock);\r
102         } catch (IOException e) {\r
103             throw new COPSPepException("Failed to send the request, reason: " + e.getMessage());\r
104         }\r
105     }\r
106 \r
107     /**\r
108      * Sends a failure report to the PDP. This report message notifies the PDP\r
109      * of failure when carrying out the PDP's decision, or when reporting\r
110      *  an accounting related state change.\r
111      * @param clientSIs Report data\r
112      * @throws   COPSPepException\r
113      */\r
114     public void sendFailReport(Vector clientSIs) throws COPSPepException {\r
115         COPSReportMsg msg = new COPSReportMsg();\r
116         // Report FAIL\r
117         try {\r
118             COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);\r
119             COPSHandle hnd = _handle;\r
120 \r
121             COPSReportType report = new COPSReportType(COPSReportType.FAILURE);\r
122 \r
123             msg.add(hdr);\r
124             msg.add(hnd);\r
125             msg.add(report);\r
126 \r
127             Enumeration clientSIEnum = clientSIs.elements();\r
128             while (clientSIEnum.hasMoreElements())\r
129                 msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
130         } catch (COPSException ex) {\r
131             throw new COPSPepException("Error making Msg");\r
132         }\r
133 \r
134         try {\r
135             msg.writeData(_sock);\r
136         } catch (IOException e) {\r
137             throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());\r
138         }\r
139     }\r
140 \r
141     /**\r
142      * Sends a success report to the PDP. This report message notifies the PDP\r
143      * of success when carrying out the PDP's decision, or when reporting\r
144      *  an accounting related state change.\r
145      * @param   clientSIs   Report data\r
146      * @throws  COPSPepException\r
147      */\r
148     public void sendSuccessReport(Vector clientSIs) throws COPSPepException {\r
149         COPSReportMsg msg = new COPSReportMsg();\r
150         // Report SUCESS\r
151         try {\r
152             COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);\r
153             COPSHandle hnd = _handle;\r
154 \r
155             COPSReportType report = new COPSReportType(COPSReportType.SUCCESS);\r
156 \r
157             msg.add(hdr);\r
158             msg.add(hnd);\r
159             msg.add(report);\r
160 \r
161             Enumeration clientSIEnum = clientSIs.elements();\r
162             while (clientSIEnum.hasMoreElements())\r
163                 msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
164         } catch (COPSException ex) {\r
165             throw new COPSPepException("Error making Msg");\r
166         }\r
167 \r
168         try {\r
169             msg.writeData(_sock);\r
170         } catch (IOException e) {\r
171             throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());\r
172         }\r
173     }\r
174 \r
175     /**\r
176      * Sends an accounting report to the PDP\r
177      * @param clientSIs Report data\r
178      * @throws COPSPepException\r
179      */\r
180     public void sendAcctReport(Vector clientSIs) throws COPSPepException {\r
181         COPSReportMsg msg = new COPSReportMsg();\r
182         // Report SUCCESS\r
183         try {\r
184             COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_RPT, _clientType);\r
185             COPSHandle hnd = _handle;\r
186 \r
187             COPSReportType report = new COPSReportType(COPSReportType.ACCT);\r
188 \r
189             msg.add(hdr);\r
190             msg.add(hnd);\r
191             msg.add(report);\r
192 \r
193             Enumeration clientSIEnum = clientSIs.elements();\r
194             while (clientSIEnum.hasMoreElements())\r
195                 msg.add( (COPSClientSI) clientSIEnum.nextElement());\r
196         } catch (COPSException ex) {\r
197             throw new COPSPepException("Error making Msg");\r
198         }\r
199 \r
200         try {\r
201             msg.writeData(_sock);\r
202         } catch (IOException e) {\r
203             throw new COPSPepException("Failed to send the report, reason: " + e.getMessage());\r
204         }\r
205     }\r
206 \r
207     /**\r
208      * Sends a sync-complete message to the PDP. This indicates the\r
209      * end of a synchronization requested by the PDP.\r
210      * @throws   COPSPepException\r
211      */\r
212     public void sendSyncComplete() throws COPSPepException {\r
213         // Common Header with the same ClientType as the request\r
214         COPSHeader hdr = new COPSHeader (COPSHeader.COPS_OP_SSC, _clientType);\r
215 \r
216         // Client Handle with the same clientHandle as the request\r
217         COPSHandle clienthandle = _handle;\r
218 \r
219         COPSSyncStateMsg msg = new COPSSyncStateMsg();\r
220         try {\r
221             msg.add(hdr);\r
222             msg.add(clienthandle);\r
223         } catch (Exception e) {\r
224             throw new COPSPepException("Error making Msg");\r
225         }\r
226 \r
227         try {\r
228             msg.writeData(_sock);\r
229         } catch (IOException e) {\r
230             throw new COPSPepException("Failed to send the sync state request, reason: " + e.getMessage());\r
231         }\r
232     }\r
233 \r
234     /**\r
235      * Sends a delete request to the PDP.\r
236      * When sent from the PEP this message indicates to the remote PDP that\r
237      * the state identified by the client handle is no longer\r
238      * available/relevant.\r
239      * @throws   COPSPepException\r
240      */\r
241     public void sendDeleteRequest() throws COPSPepException {\r
242         COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_DRQ, _clientType);\r
243         COPSHandle handle = _handle;\r
244 \r
245         // *** TODO: use real reason codes\r
246         COPSReason reason = new COPSReason((short) 234, (short) 345);\r
247 \r
248         COPSDeleteMsg msg = new COPSDeleteMsg();\r
249         try {\r
250             msg.add(hdr);\r
251             msg.add(handle);\r
252             msg.add(reason);\r
253             msg.writeData(_sock);\r
254         } catch (COPSException ex) {\r
255             throw new COPSPepException("Error making Msg");\r
256         } catch (IOException e) {\r
257             throw new COPSPepException("Failed to send the delete request, reason: " + e.getMessage());\r
258         }\r
259     }\r
260 \r
261 }\r