2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.rcd.impl;
7 import org.pcmm.PCMMConstants;
8 import org.pcmm.PCMMProperties;
9 import org.pcmm.gates.IGateSpec.Direction;
10 import org.pcmm.rcd.ICMTS;
12 import java.io.IOException;
13 import java.net.Socket;
15 import java.util.concurrent.ConcurrentHashMap;
18 * This class starts a mock CMTS that can be used for testing.
20 public class CMTS extends AbstractPCMMServer implements ICMTS {
23 * Receives messages from the COPS client
25 private final Map<String, IPCMMClientHandler> handlerMap;
28 * The configured gates
30 private final Map<Direction, Set<String>> gateConfig;
33 * The connected CMTSs and whether or not they are up
35 private final Map<String, Boolean> cmStatus;
38 * Constructor for having the server port automatically assigned
39 * Call getPort() after startServer() is called to determine the port number of the server
41 public CMTS(final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
42 this(0, gateConfig, cmStatus);
46 * Constructor for starting the server to a pre-defined port number
47 * @param port - the port number on which to start the server.
49 public CMTS(final int port, final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
51 if (gateConfig == null || cmStatus == null) throw new IllegalArgumentException("Config must not be null");
52 this.gateConfig = Collections.unmodifiableMap(gateConfig);
53 this.cmStatus = Collections.unmodifiableMap(cmStatus);
54 handlerMap = new ConcurrentHashMap<>();
58 public void stopServer() {
59 for (final IPCMMClientHandler handler : handlerMap.values()) {
66 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) throws IOException {
67 final String key = socket.getLocalAddress().getHostName() + ':' + socket.getPort();
68 if (handlerMap.get(key) == null) {
69 final IPCMMClientHandler handler = new CmtsPcmmClientHandler(socket, gateConfig, cmStatus);
71 handlerMap.put(key, handler);
73 } else return handlerMap.get(key);
78 * @param args - the arguments which will contain configuration information
79 * @throws IOException - should the server fail to start for reasons such as port contention.
81 public static void main(final String[] args) throws IOException {
82 final CMTS cmts = new CMTS(PCMMProperties.get(PCMMConstants.PCMM_PORT, Integer.class),
83 new HashMap<Direction, Set<String>>(), new HashMap<String, Boolean>());