Merge "changed revision of flow-capable transaction"
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / ospep / COPSPepOSReqStateMan.java
1 package org.umu.cops.ospep;\r
2 \r
3 import org.umu.cops.stack.*;\r
4 \r
5 import java.net.Socket;\r
6 import java.util.Vector;\r
7 \r
8 /**\r
9  * State manager class for outsourcing requests, at the PEP side.\r
10  */\r
11 public class COPSPepOSReqStateMan {\r
12     /**\r
13      * Request State created\r
14      */\r
15     public final static short ST_CREATE = 1;\r
16     /**\r
17      * Request sent\r
18      */\r
19     public final static short ST_INIT = 2;\r
20     /**\r
21      * Decisions received\r
22      */\r
23     public final static short ST_DECS = 3;\r
24     /**\r
25      * Report sent\r
26      */\r
27     public final static short ST_REPORT = 4;\r
28     /**\r
29      * Request State finalized\r
30      */\r
31     public final static short ST_FINAL = 5;\r
32     /**\r
33      * New Request State solicited\r
34      */\r
35     public final static short ST_NEW = 6;\r
36     /**\r
37      * Delete Request State solicited\r
38      */\r
39     public final static short ST_DEL = 7;\r
40     /**\r
41      * SYNC request received\r
42      */\r
43     public final static short ST_SYNC = 8;\r
44     /**\r
45      * Sync completed\r
46      */\r
47     public final static short ST_SYNCALL = 9;\r
48     /**\r
49      * Close connection received\r
50      */\r
51     public final static short ST_CCONN = 10;\r
52     /**\r
53      * Keep-alive timeout\r
54      */\r
55     public final static short ST_NOKA = 11;\r
56     /**\r
57      * Accounting timeout\r
58      */\r
59     public final static short ST_ACCT = 12;\r
60 \r
61     /**\r
62      * COPS client-type that identifies the policy client\r
63      */\r
64     protected short _clientType;\r
65 \r
66     /**\r
67      *  COPS client handle used to uniquely identify a particular\r
68      *  PEP's request for a client-type\r
69      */\r
70     protected COPSHandle _handle;\r
71 \r
72     /**\r
73         Object for performing policy data processing\r
74      */\r
75     protected COPSPepOSDataProcess _process;\r
76 \r
77     /**\r
78      * ClientSI data from signaling.\r
79      */\r
80     protected Vector _clientSIs;\r
81 \r
82     /**\r
83      *  Current state of the request being managed\r
84      */\r
85     protected short _status;\r
86 \r
87     /**\r
88         COPS message transceiver used to send COPS messages\r
89      */\r
90     protected COPSPepOSMsgSender _sender;\r
91 \r
92     /**\r
93      * Sync state\r
94      */\r
95     protected boolean _syncState;\r
96 \r
97     /**\r
98      * Creates a state request manager\r
99      * @param    clientType Client-type\r
100      * @param   clientHandle    Client's <tt>COPSHandle</tt>\r
101      */\r
102     public COPSPepOSReqStateMan(short clientType, String clientHandle) {\r
103         // COPS Handle\r
104         _handle = new COPSHandle(new COPSData(clientHandle));\r
105         _clientType = clientType;\r
106         _syncState = true;\r
107         _status = ST_CREATE;\r
108         _clientSIs = null;\r
109     }\r
110 \r
111     /**\r
112      * Gets the client handle\r
113      * @return  Client's <tt>COPSHandle</tt>\r
114      */\r
115     public COPSHandle getClientHandle() {\r
116         return _handle;\r
117     }\r
118 \r
119     /**\r
120      * Sets the client SI data.\r
121      * @param someClientSIs Client SI data built by the event listener\r
122      */\r
123     public void setClientSI(Vector someClientSIs) {\r
124         _clientSIs = someClientSIs;\r
125     }\r
126 \r
127     /**\r
128      * Gets the client-type\r
129      * @return  Client-type value\r
130      */\r
131     public short getClientType() {\r
132         return _clientType;\r
133     }\r
134 \r
135     /**\r
136      * Gets the request status\r
137      * @return  Request status value\r
138      */\r
139     public short getStatus() {\r
140         return _status;\r
141     }\r
142 \r
143     /**\r
144      * Gets the policy data processing object\r
145      *\r
146      * @return   Policy data processing object\r
147      *\r
148      */\r
149     public COPSPepOSDataProcess getDataProcess() {\r
150         return _process;\r
151     }\r
152 \r
153     /**\r
154      * Sets the policy data processing object\r
155      *\r
156      * @param   process   Policy data processing object\r
157      *\r
158      */\r
159     public void setDataProcess(COPSPepOSDataProcess process) {\r
160         _process = process;\r
161     }\r
162 \r
163     /**\r
164      * Initializes a new request state over a socket\r
165      * @param sock  Socket to the PDP\r
166      * @throws COPSPepException\r
167      */\r
168     protected void initRequestState(Socket sock) throws COPSPepException {\r
169         // Inits an object for sending COPS messages to the PDP\r
170         _sender = new COPSPepOSMsgSender(_clientType, _handle, sock);\r
171 \r
172         // If an object exists for retrieving the PEP features,\r
173         // use it for retrieving them.\r
174         /*      Hashtable clientSIs;\r
175                 if (_process != null)\r
176                     clientSIs = _process.getClientData(this);\r
177                 else\r
178                     clientSIs = null;*/\r
179 \r
180         // Semd the request\r
181         _sender.sendRequest(_clientSIs);\r
182 \r
183         // Initial state\r
184         _status = ST_INIT;\r
185     }\r
186 \r
187     /**\r
188      * Deletes the request state\r
189      * @throws COPSPepException\r
190      */\r
191     protected void finalizeRequestState() throws COPSPepException {\r
192         _sender.sendDeleteRequest();\r
193         _status = ST_FINAL;\r
194     }\r
195 \r
196     /**\r
197      * Processes the decision message\r
198      * @param    dMsg Decision message from the PDP\r
199      * @throws   COPSPepException\r
200      */\r
201     protected void processDecision(COPSDecisionMsg dMsg) throws COPSPepException {\r
202         // COPSDebug.out(getClass().getName(), "ClientId:" + getClientHandle().getId().str());\r
203 \r
204         //Hashtable decisionsPerContext = dMsg.getDecisions();\r
205 \r
206         //** Applies decisions to the configuration\r
207         //_process.setDecisions(this, removeDecs, installDecs, errorDecs);\r
208         // second param changed to dMsg so that the data processor\r
209         // can check the 'solicited' flag\r
210         boolean isFailReport = _process.setDecisions(this, dMsg /*decisionsPerContext*/);\r
211         _status = ST_DECS;\r
212 \r
213         if (isFailReport) {\r
214             // COPSDebug.out(getClass().getName(),"Sending FAIL Report\n");\r
215             _sender.sendFailReport(_process.getReportData(this));\r
216         } else {\r
217             // COPSDebug.out(getClass().getName(),"Sending SUCCESS Report\n");\r
218             _sender.sendSuccessReport(_process.getReportData(this));\r
219         }\r
220         _status = ST_REPORT;\r
221 \r
222         if (!_syncState) {\r
223             _sender.sendSyncComplete();\r
224             _syncState = true;\r
225             _status = ST_SYNCALL;\r
226         }\r
227     }\r
228 \r
229 \r
230     /**\r
231      * Processes a COPS delete message\r
232      * @param dMsg  <tt>COPSDeleteMsg</tt> received from the PDP\r
233      * @throws COPSPepException\r
234      */\r
235     protected void processDeleteRequestState(COPSDecisionMsg dMsg) throws COPSPepException {\r
236         if (_process != null)\r
237             _process.closeRequestState(this);\r
238 \r
239         _status = ST_DEL;\r
240     }\r
241 \r
242     /**\r
243      * Processes the message SycnStateRequest.\r
244      * The message SycnStateRequest indicates that the remote PDP\r
245      * wishes the client (which appears in the common header)\r
246      * to re-send its state.\r
247      *\r
248      * @param    ssMsg               The sync request from the PDP\r
249      *\r
250      * @throws   COPSPepException\r
251      *\r
252      */\r
253     protected void processSyncStateRequest(COPSSyncStateMsg ssMsg) throws COPSPepException {\r
254         _syncState = false;\r
255         // If an object exists for retrieving the PEP features,\r
256         // use it for retrieving them.\r
257 \r
258         // Send the request\r
259         _sender.sendRequest(_clientSIs);\r
260 \r
261         _status = ST_SYNC;\r
262     }\r
263 \r
264     /**\r
265      * Called when connection is closed\r
266      * @param error Reason\r
267      * @throws COPSPepException\r
268      */\r
269     protected void processClosedConnection(COPSError error) throws COPSPepException {\r
270         if (_process != null)\r
271             _process.notifyClosedConnection(this, error);\r
272 \r
273         _status = ST_CCONN;\r
274     }\r
275 \r
276     /**\r
277      * Called when no keep-alive is received\r
278      * @throws COPSPepException\r
279      */\r
280     protected void processNoKAConnection() throws COPSPepException {\r
281         if (_process != null)\r
282             _process.notifyNoKAliveReceived(this);\r
283 \r
284         _status = ST_NOKA;\r
285     }\r
286 \r
287     /**\r
288      * Processes the accounting report\r
289      * @throws COPSPepException\r
290      */\r
291     protected void processAcctReport() throws COPSPepException {\r
292         Vector report = new Vector();\r
293 \r
294         if (_process != null)\r
295             report = _process.getAcctData(this);\r
296 \r
297         _sender.sendAcctReport(report);\r
298 \r
299         _status = ST_ACCT;\r
300     }\r
301 }\r