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 com.google.common.collect.Maps;
14 import java.io.IOException;
15 import java.net.Socket;
17 import org.pcmm.rcd.ICMTS;
20 * Mock CMTS that can be used for testing. startServer() is called to start required threads after instantiation.
22 public class CMTS extends AbstractPCMMServer implements ICMTS {
25 * Emulator configuration
27 private final CMTSConfig config;
30 * Receives messages from the COPS client
32 private final Map<String, IPCMMClientHandler> handlerMap;
35 * Constructor for having the server port automatically assigned
36 * Call getPort() after startServer() is called to determine the port number of the server
38 public CMTS(final CMTSConfig config) {
39 super(checkNotNull(config, "config must not be null").getPort());
41 handlerMap = Maps.newConcurrentMap();
45 public void stopServer() {
46 for (final IPCMMClientHandler handler : handlerMap.values()) {
53 protected IPCMMClientHandler getPCMMClientHandler(final Socket socket) throws IOException {
54 final String key = socket.getLocalAddress().getHostName() + ':' + socket.getPort();
55 if (handlerMap.get(key) == null) {
56 final IPCMMClientHandler handler = new CmtsPcmmClientHandler(socket, config);
58 handlerMap.put(key, handler);
61 return handlerMap.get(key);
67 * @param args - the arguments which will contain configuration information
68 * @throws IOException - should the server fail to start for reasons such as port contention.
70 public static void main(final String[] args) throws IOException {
71 if (args.length != 1) {
72 throw new IllegalArgumentException("expected arguments: <cmts_yaml_config_file>");
75 final CMTSConfig config = CMTSConfig.loadConfig(args[0]);
76 final CMTS cmts = new CMTS(config);