cbe268b022ac0d9e07b4c1bd808db4e35f184d7e
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / PCMMPepAgent.java
1 /**
2  @header@
3  */
4
5 package org.pcmm;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.umu.cops.prpep.COPSPepAgent;
10 import org.umu.cops.prpep.COPSPepConnection;
11 import org.umu.cops.prpep.COPSPepException;
12 import org.umu.cops.stack.*;
13 import org.umu.cops.stack.COPSHeader.OPCode;
14
15 import java.io.IOException;
16 import java.net.ServerSocket;
17 import java.net.Socket;
18
19 /**
20  * This is a provisioning COPS PEP. Responsible for making connection to the PDP
21  * and maintaining it
22  */
23 public class PCMMPepAgent extends COPSPepAgent implements Runnable {
24
25     public final static Logger logger = LoggerFactory.getLogger(PCMMPepAgent.class);
26
27     /** Well-known port for COPS */
28     public static final int WELL_KNOWN_CMTS_PORT = 3918;
29
30     /**
31      * PDP host IP
32      */
33     private ServerSocket serverSocket;
34
35     /**
36      * PDP host port
37      */
38     private int serverPort;
39
40     /**
41      * COPS error returned by PDP
42      */
43     private COPSError error;
44
45     /**
46      * Creates a PEP agent
47      *
48      * @param pepID
49      *            PEP-ID
50      * @param clientType
51      *            Client-type
52      */
53     public PCMMPepAgent(final String pepID, final short clientType) {
54         super(pepID, clientType);
55         serverPort = WELL_KNOWN_CMTS_PORT;
56     }
57
58     /**
59      * Creates a PEP agent with a PEP-ID equal to "noname"
60      *
61      * @param clientType
62      *            Client-type
63      */
64     public PCMMPepAgent(final short clientType) {
65         super(clientType);
66         serverPort = WELL_KNOWN_CMTS_PORT;
67     }
68
69     /**
70      * Runs the PEP process XXX - not sure of the exception throwing
71      */
72     public void run() {
73         try {
74
75             logger.info("Create Server Socket on Port " + serverPort);
76
77             serverSocket = new ServerSocket(serverPort);
78             // Loop through for Incoming messages
79
80             // server infinite loop
81             while (true) {
82
83                 // Wait for an incoming connection from a PEP
84                 Socket socket = serverSocket.accept();
85
86                 logger.info("New connection accepted " + socket.getInetAddress() + ":" + socket.getPort());
87
88                 processConnection(socket);
89                 /**
90                  * XXX - processConnection handles the open request from PEP And
91                  * a thread is created for conn = new
92                  * COPSPepConnection(_clientType, socket); the main processing
93                  * loop for PEP
94                  */
95
96             }
97         } catch (Exception e) {
98             logger.error("Error while processing the socket connection", e);
99         }
100     }
101
102     /**
103      * Establish connection to PDP's IP address
104      *
105      * <Client-Open> ::= <Common Header> <PEPID> [<ClientSI>] [<LastPDPAddr>]
106      * [<Integrity>]
107      *
108      * Not support [<ClientSI>], [<LastPDPAddr>], [<Integrity>]
109      *
110      * <Client-Accept> ::= <Common Header> <KA Timer> [<ACCT Timer>]
111      * [<Integrity>]
112      *
113      * Not send [<Integrity>]
114      *
115      * <Client-Close> ::= <Common Header> <Error> [<PDPRedirAddr>] [<Integrity>]
116      *
117      * Not send [<PDPRedirAddr>], [<Integrity>]
118      *
119      * @throws IOException
120      * @throws COPSException
121      * @throws COPSPepException
122      *
123      */
124     private COPSPepConnection processConnection(Socket socket) throws IOException, COPSException, COPSPepException {
125         // Build OPN
126         final COPSPepId pepId = new COPSPepId(new COPSData(getPepID()));
127         final COPSClientOpenMsg msg = new COPSClientOpenMsg(getClientType(), pepId, null, null, null);
128
129         // Create Socket and send OPN
130         /*
131          * InetAddress addr = InetAddress.getByName(psHost); Socket socket = new
132          * Socket(addr,psPort);
133          */
134         logger.info("Send COPSClientOpenMsg to PDP");
135         msg.writeData(socket);
136
137         // Receive the response
138         logger.info("Receive the resposne from PDP");
139         COPSMsg recvmsg = COPSTransceiver.receiveMsg(socket);
140
141         if (recvmsg.getHeader().getOpCode().equals(OPCode.CAT)) {
142             logger.info("isAClientAccept from PDP");
143             COPSClientAcceptMsg cMsg = (COPSClientAcceptMsg) recvmsg;
144
145             // Support
146             if (cMsg.getIntegrity() != null) {
147                 throw new COPSPepException("Unsupported object (Integrity)");
148             }
149
150             // Mandatory KATimer
151             COPSKATimer kt = cMsg.getKATimer();
152             if (kt == null)
153                 throw new COPSPepException(
154                     "Mandatory COPS object missing (KA Timer)");
155             short _kaTimeVal = kt.getTimerVal();
156
157             // ACTimer
158             COPSAcctTimer at = cMsg.getAcctTimer();
159             short _acctTimer = 0;
160             if (at != null)
161                 _acctTimer = at.getTimerVal();
162
163             // Create the connection manager
164             COPSPepConnection conn = new COPSPepConnection(getClientType(),
165                     socket);
166             conn.setKaTimer(_kaTimeVal);
167             conn.setAcctTimer(_acctTimer);
168             logger.info("Thread(conn).start");
169             new Thread(conn).start();
170
171             return conn;
172         } else if (recvmsg.getHeader().getOpCode().equals(OPCode.CC)) {
173             logger.info("isAClientClose from PDP");
174             COPSClientCloseMsg cMsg = (COPSClientCloseMsg) recvmsg;
175             error = cMsg.getError();
176             socket.close();
177             return null;
178         } else { // messages of other types are not expected
179             throw new COPSPepException(
180                 "Message not expected. Closing connection for "
181                 + socket.toString());
182         }
183     }
184
185     /**
186      * Gets the COPS error returned by the PDP
187      *
188      * @return <tt>COPSError</tt> returned by PDP
189      */
190     public COPSError getConnectionError() {
191         return error;
192     }
193
194     public void setConnectionError(COPSError _error) {
195         this.error = _error;
196     }
197
198     /**
199      * @return the serverSocket
200      */
201     public ServerSocket getServerSocket() {
202         return serverSocket;
203     }
204
205     /**
206      * @param serverSocket
207      *            the serverSocket to set
208      */
209     public void setServerSocket(ServerSocket serverSocket) {
210         this.serverSocket = serverSocket;
211     }
212
213     /**
214      * @return the serverPort
215      */
216     public int getServerPort() {
217         return serverPort;
218     }
219
220     /**
221      * @param serverPort
222      *            the serverPort to set
223      */
224     public void setServerPort(int serverPort) {
225         this.serverPort = serverPort;
226     }
227
228 }