2 * Copyright (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.pcmm.rcd.impl;
11 import static com.google.common.base.Preconditions.checkNotNull;
13 import org.pcmm.gates.IGateSpec.Direction;
14 import org.pcmm.messages.impl.MessageFactory;
15 import org.pcmm.rcd.IPCMMServer.IPCMMClientHandler;
16 import org.slf4j.Logger;
17 import org.slf4j.LoggerFactory;
18 import org.umu.cops.prpep.COPSPepException;
19 import org.umu.cops.stack.*;
20 import org.umu.cops.stack.COPSHeader.OPCode;
22 import java.net.Socket;
23 import java.util.Collections;
25 import java.util.Properties;
27 import java.util.concurrent.Callable;
30 * This class was created by moving an anonymous inner class from CMTS.java and is responsible for creating a persistent
31 * connection with a PEP.
33 public class CmtsPcmmClientHandler extends AbstractPCMMClient implements IPCMMClientHandler {
35 private static final Logger logger = LoggerFactory.getLogger(CmtsPcmmClientHandler.class);
38 * The thread accepting PEP COPS messages
40 private transient Thread thread;
43 * Emulator configuration
45 private final CMTSConfig config;
48 * Constructor when a socket connection has not been established
49 * @param host - the host to connect
50 * @param port - the port to connect
51 * @param config - emulator configuration
53 public CmtsPcmmClientHandler(final String host, final int port, final CMTSConfig config) {
55 this.config = checkNotNull(config);
59 * Constructor with a connected socket.
60 * @param socket - the socket connection
61 * @param config - emulator configuration
63 public CmtsPcmmClientHandler(final Socket socket, final CMTSConfig config) {
65 this.config = checkNotNull(config);
69 if (thread != null && thread.isAlive())
76 logger.info("Send OPN message to the PS");
77 sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));
80 final COPSMsg recvMsg = readMessage();
82 switch (recvMsg.getHeader().getOpCode()) {
84 final COPSClientCloseMsg closeMsg = (COPSClientCloseMsg) recvMsg;
85 logger.info("PS requested Client-Close" + closeMsg.getError().getDescription());
86 // send a CC message and close the socket
90 logger.info("received Client-Accept from PS");
91 final COPSClientAcceptMsg acceptMsg = (COPSClientAcceptMsg) recvMsg;
93 if (acceptMsg.getIntegrity() != null) {
94 throw new COPSPepException("Unsupported object (Integrity)");
98 final COPSKATimer kt = acceptMsg.getKATimer();
100 throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
101 final short kaTimeVal = kt.getTimerVal();
104 final COPSAcctTimer at = acceptMsg.getAcctTimer();
107 acctTimer = at.getTimerVal();
109 logger.info("Send a REQ message to the PS");
110 final Properties prop = new Properties();
111 final COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
112 final COPSHandle handle = ((COPSReqMsg) reqMsg).getClientHandle();
115 // Create the connection manager
116 final PcmmCmtsConnection conn = new PcmmCmtsConnection(CLIENT_TYPE, getSocket(), config);
117 conn.addRequestState(handle, new CmtsDataProcessor());
118 conn.setKaTimer(kaTimeVal);
119 conn.setAcctTimer(acctTimer);
121 logger.info(getClass().getName() + " Thread(conn).start");
122 thread = new Thread(conn);
126 throw new COPSPepException("Message not expected. Closing connection for " + getSocket().toString());
128 } catch (Exception e) {
129 logger.error(e.getMessage());
134 public void task(Callable<?> c) {
135 // TODO Auto-generated method stub
140 public void shouldWait(int t) {
141 // TODO Auto-generated method stub
147 // TODO Auto-generated method stub