package org.umu.cops.prpep;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.umu.cops.stack.*;
+
import java.io.IOException;
import java.net.Socket;
-import java.util.Date;
-import java.util.Enumeration;
-import java.util.Hashtable;
-import java.util.Vector;
-
-import org.umu.cops.common.COPSDebug;
-import org.umu.cops.stack.COPSClientCloseMsg;
-import org.umu.cops.stack.COPSContext;
-import org.umu.cops.stack.COPSDecision;
-import org.umu.cops.stack.COPSDecisionMsg;
-import org.umu.cops.stack.COPSError;
-import org.umu.cops.stack.COPSException;
-import org.umu.cops.stack.COPSHandle;
-import org.umu.cops.stack.COPSHeader;
-import org.umu.cops.stack.COPSKAMsg;
-import org.umu.cops.stack.COPSMsg;
-import org.umu.cops.stack.COPSSyncStateMsg;
-import org.umu.cops.stack.COPSTransceiver;
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
/**
* COPSPepConnection represents a PEP-PDP Connection Manager.
*/
public class COPSPepConnection implements Runnable {
+ public final static Logger logger = LoggerFactory.getLogger(COPSPepConnection.class);
+
/** Socket connected to PDP */
protected Socket _sock;
/**
Maps a COPS Client Handle to a Request State Manager
*/
- protected Hashtable _managerMap;
+ protected final Map<String, COPSPepReqStateMan> _managerMap;
// map < String(COPSHandle), COPSPepReqStateMan>;
/**
_responseTime = 10000;
_lastmessage = COPSHeader.COPS_OP_CAT;
- _managerMap = new Hashtable(20);
+ _managerMap = new ConcurrentHashMap<>();
}
/**
return _acctTimer;
}
- /**
- * Gets active COPS handles
- * @return An <tt>Enumeration</tt> holding all active handles
- */
- protected Enumeration getHandles() {
- return _managerMap.keys();
- }
-
/**
* Gets all request state managers
* @return A <tt>Hashatable</tt> holding all request state managers
*/
protected Hashtable getReqStateMans() {
- return _managerMap;
+ return new Hashtable(_managerMap);
}
/**
*/
public void setResponseTime(int respTime) {
_responseTime = respTime;
- };
+ }
/**
* Sets keep-alive timer
int _startTime = (int) (_lastRecKa.getTime());
int cTime = (int) (new Date().getTime());
- if ((int)(cTime - _startTime) > _kaTimer*1000) {
+ if ((cTime - _startTime) > _kaTimer*1000) {
_sock.close();
// Notify all Request State Managers
notifyNoKAAllReqStateMan();
_startTime = (int) (_lastSendKa.getTime());
cTime = (int) (new Date().getTime());
- if ((int)(cTime - _startTime) > ((_kaTimer*3/4) * 1000)) {
+ if ((cTime - _startTime) > ((_kaTimer*3/4) * 1000)) {
COPSHeader hdr = new COPSHeader(COPSHeader.COPS_OP_KA);
COPSKAMsg msg = new COPSKAMsg();
int _startTime = (int) (_lastSendAcc.getTime());
int cTime = (int) (new Date().getTime());
- if ((int)(cTime - _startTime) > ((_acctTimer*3/4)*1000)) {
+ if ((cTime - _startTime) > ((_acctTimer*3/4)*1000)) {
// Notify all Request State Managers
notifyAcctAllReqStateMan();
_lastSendAcc = new Date();
try {
Thread.sleep(500);
- } catch (Exception e) {};
+ } catch (Exception e) {
+ logger.error("Exception thrown while sleeping", e);
+ }
}
} catch (Exception e) {
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_SOCKET, e);
+ logger.error("Error while processing socket messages", e);
}
// connection closed by server
// COPSDebug.out(getClass().getName(),"Connection closed by server");
try {
_sock.close();
- } catch (IOException e) {};
+ } catch (IOException e) {
+ logger.error("Error closing socket", e);
+ }
// Notify all Request State Managers
try {
notifyCloseAllReqStateMan();
- } catch (COPSPepException e) {};
+ } catch (COPSPepException e) {
+ logger.error("Error closing state managers");
+ }
}
/**
try {
// Support
if (cMsg.getIntegrity() != null) {
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
- "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+ logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
}
conn.close();
- } catch (Exception unae) { };
+ } catch (Exception unae) {
+ logger.error("Unexpected exception closing connection", unae);
+ }
}
/**
try {
// Support
if (cMsg.getIntegrity() != null) {
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
- "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+ logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
}
// should we do anything else?? ....
- } catch (Exception unae) { };
+ } catch (Exception unae) {
+ logger.error("Unexpected exception while writing COPS data", unae);
+ }
}
/**
COPSDecision decision = (COPSDecision) ee.nextElement();
// Get the associated manager
- COPSPepReqStateMan manager = (COPSPepReqStateMan) _managerMap.get(handle.getId().str());
+ COPSPepReqStateMan manager = _managerMap.get(handle.getId().str());
if (manager == null)
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
+ logger.warn("Unable to find state manager with key - " + handle.getId().str());
// Check message type
if (decision.getFlags() == COPSDecision.F_REQSTATE) {
private void handleOpenNewRequestStateMsg(Socket conn, COPSHandle handle)
throws COPSPepException {
- COPSPepReqStateMan manager = (COPSPepReqStateMan) _managerMap.get(handle.getId().str());
+ COPSPepReqStateMan manager = _managerMap.get(handle.getId().str());
if (manager == null)
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
+ logger.warn("Unable to find state manager with key - " + handle.getId().str());
manager.processOpenNewRequestState();
}
// Support
if (cMsg.getIntegrity() != null) {
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOSUPPORTED,
- "Unsupported objects (Integrity) to connection " + conn.getInetAddress());
+ logger.warn("Unsupported objects (Integrity) to connection " + conn.getInetAddress());
}
COPSPepReqStateMan manager = (COPSPepReqStateMan) _managerMap.get(cMsg.getClientHandle().getId().str());
if (manager == null) {
- COPSDebug.err(getClass().getName(), COPSDebug.ERROR_NOEXPECTEDMSG);
+ logger.warn("Unable to find state manager with key - " + cMsg.getClientHandle().getId().str());
} else {
manager.processSyncStateRequest(cMsg);
}
manager.finalizeRequestState();
}
- private void notifyCloseAllReqStateMan()
- throws COPSPepException {
- if (_managerMap.size() > 0) {
- for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
- String handle = (String) e.nextElement();
- COPSPepReqStateMan man = (COPSPepReqStateMan) _managerMap.get(handle);
-
- man.processClosedConnection(_error);
- }
+ private void notifyCloseAllReqStateMan() throws COPSPepException {
+ for (final COPSPepReqStateMan man: _managerMap.values()) {
+ man.processClosedConnection(_error);
}
}
- private void notifyNoKAAllReqStateMan()
- throws COPSPepException {
- if (_managerMap.size() > 0) {
- for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
- String handle = (String) e.nextElement();
- COPSPepReqStateMan man = (COPSPepReqStateMan) _managerMap.get(handle);
-
- man.processNoKAConnection();
- }
+ private void notifyNoKAAllReqStateMan() throws COPSPepException {
+ for (final COPSPepReqStateMan man: _managerMap.values()) {
+ man.processNoKAConnection();
}
}
- private void notifyAcctAllReqStateMan()
- throws COPSPepException {
- if (_managerMap.size() > 0) {
- for (Enumeration e = _managerMap.keys() ; e.hasMoreElements() ;) {
- String handle = (String) e.nextElement();
- COPSPepReqStateMan man = (COPSPepReqStateMan) _managerMap.get(handle);
-
- man.processAcctReport();
- }
+ private void notifyAcctAllReqStateMan() throws COPSPepException {
+ for (final COPSPepReqStateMan man: _managerMap.values()) {
+ man.processAcctReport();
}
}