2 * Copyright (c) 2014, 2015 Cable Television Laboratories, Inc. and others. 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 org.pcmm.nio.PCMMChannel;
12 import org.pcmm.objects.MMVersionInfo;
13 import org.pcmm.rcd.IPCMMClient;
14 import org.slf4j.Logger;
15 import org.slf4j.LoggerFactory;
16 import org.umu.cops.stack.COPSHandle;
17 import org.umu.cops.stack.COPSMsg;
19 import java.io.IOException;
20 import java.net.InetAddress;
21 import java.net.Socket;
23 // import org.junit.Assert;
27 * default implementation for {@link IPCMMClient}
31 public class AbstractPCMMClient implements IPCMMClient {
33 private final static Logger logger = LoggerFactory.getLogger(AbstractPCMMClient.class);
35 private final String host;
37 private final int port;
39 // TODO - consider removing this attribute as it is never being used
40 private MMVersionInfo versionInfo;
42 // Following two attributes are set when connect() is called
44 * socket used to communicated with server.
46 private transient Socket socket;
48 private transient PCMMChannel channel;
50 // TODO - determine why this class holds a handle as it is not being used.
51 private transient COPSHandle clientHandle;
54 * When true, this means the socket object will be generated via the host name and port number vs. being injected.
55 * In this case, the socket will be closed on disconnect(), else, the client creating the socked object will be
58 private final boolean ownSocket;
60 public AbstractPCMMClient(final String host, final int port) {
63 this.ownSocket = true;
66 public AbstractPCMMClient(final Socket socket) {
67 this.host = socket.getInetAddress().getHostName();
68 this.port = socket.getPort();
70 this.ownSocket = false;
74 public void connect() throws IOException {
76 socket = new Socket(InetAddress.getByName(host), port);
78 channel = new PCMMChannel(this.socket);
82 public boolean disconnect() {
89 } catch (IOException e) {
90 logger.error(e.getMessage());
97 public void sendRequest(COPSMsg requestMessage) {
99 channel.sendMsg(requestMessage);
100 } catch (Exception e) {
101 logger.error(e.getMessage(), getSocket());
106 public COPSMsg readMessage() {
108 COPSMsg recvdMsg = channel.receiveMessage();
109 logger.debug("received message : " + recvdMsg.getHeader());
111 } catch (Exception e) {
112 logger.error(e.getMessage(), getSocket());
120 public Socket getSocket() {
125 public boolean isConnected() {
126 return socket != null && socket.isConnected();
130 * @return the versionInfo
132 public MMVersionInfo getVersionInfo() {
138 * the versionInfo to set
140 public void setVersionInfo(MMVersionInfo versionInfo) {
141 this.versionInfo = versionInfo;
145 public COPSHandle getClientHandle() {
150 public void setClientHandle(final COPSHandle handle) {
151 this.clientHandle = handle;