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.InetAddress;
14 import java.net.Socket;
16 import java.util.concurrent.ConcurrentHashMap;
19 * This class starts a mock CMTS that can be used for testing.
21 public class CMTS extends AbstractPCMMServer implements ICMTS {
24 * Receives messages from the COPS client
26 private final Map<String, IPCMMClientHandler> handlerMap;
29 * The configured gates
31 private final Map<Direction, Set<String>> gateConfig;
34 * The connected CMTSs and whether or not they are up
36 private final Map<String, Boolean> cmStatus;
39 * Constructor for having the server port automatically assigned
40 * Call getPort() after startServer() is called to determine the port number of the server
42 public CMTS(final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
43 this(0, gateConfig, cmStatus);
47 * Constructor for starting the server to a pre-defined port number
48 * @param port - the port number on which to start the server.
50 public CMTS(final int port, final Map<Direction, Set<String>> gateConfig, final Map<String, Boolean> cmStatus) {
52 if (gateConfig == null || cmStatus == null) throw new IllegalArgumentException("Config must not be null");
53 this.gateConfig = Collections.unmodifiableMap(gateConfig);
54 this.cmStatus = Collections.unmodifiableMap(cmStatus);
55 handlerMap = new ConcurrentHashMap<>();
59 public void stopServer() {
60 for (final IPCMMClientHandler handler : handlerMap.values()) {
67 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) throws IOException {
68 final String key = socket.getLocalAddress().getHostName() + ':' + socket.getPort();
69 if (handlerMap.get(key) == null) {
70 final IPCMMClientHandler handler = new CmtsPcmmClientHandler(socket, gateConfig, cmStatus);
72 handlerMap.put(key, handler);
74 } else return handlerMap.get(key);
79 * @param args - the arguments which will contain configuration information
80 * @throws IOException - should the server fail to start for reasons such as port contention.
82 public static void main(final String[] args) throws IOException {
83 final Set<String> upGates = new HashSet<>();
84 upGates.add("extrm_up");
85 final Set<String> dnGates = new HashSet<>();
86 dnGates.add("extrm_dn");
87 final Map<Direction, Set<String>> gates = new HashMap<>();
88 gates.put(Direction.UPSTREAM, upGates);
89 gates.put(Direction.DOWNSTREAM, dnGates);
91 final Map<String, Boolean> cmStatus = new HashMap<>();
92 final InetAddress invalidCmAddrInet = InetAddress.getByAddress(new byte[] {99, 99, 99, 99});
93 cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 180}).getHostAddress(), true);
94 cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 179}).getHostAddress(), true);
95 cmStatus.put(InetAddress.getByAddress(new byte[]{10, 32, 110, (byte) 178}).getHostAddress(), true);
96 cmStatus.put(invalidCmAddrInet.getHostAddress(), false);
98 final CMTS cmts = new CMTS(PCMMProperties.get(PCMMConstants.PCMM_PORT, Integer.class), gates, cmStatus);