2 * (c) 2015 Cable Television Laboratories, Inc. All rights reserved.
5 package org.pcmm.rcd.impl;
7 import org.pcmm.nio.PCMMChannel;
8 import org.pcmm.objects.MMVersionInfo;
9 import org.pcmm.rcd.IPCMMClient;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.umu.cops.stack.COPSHandle;
13 import org.umu.cops.stack.COPSMsg;
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.net.Socket;
19 // import org.junit.Assert;
23 * default implementation for {@link IPCMMClient}
27 public class AbstractPCMMClient implements IPCMMClient {
29 private final static Logger logger = LoggerFactory.getLogger(AbstractPCMMClient.class);
31 private final String host;
33 private final int port;
35 // TODO - consider removing this attribute as it is never being used
36 private MMVersionInfo versionInfo;
38 // Following two attributes are set when connect() is called
40 * socket used to communicated with server.
42 private transient Socket socket;
44 private transient PCMMChannel channel;
46 // TODO - determine why this class holds a handle as it is not being used.
47 private transient COPSHandle clientHandle;
50 * When true, this means the socket object will be generated via the host name and port number vs. being injected.
51 * In this case, the socket will be closed on disconnect(), else, the client creating the socked object will be
54 private final boolean ownSocket;
56 public AbstractPCMMClient(final String host, final int port) {
59 this.ownSocket = true;
62 public AbstractPCMMClient(final Socket socket) {
63 this.host = socket.getInetAddress().getHostName();
64 this.port = socket.getPort();
66 this.ownSocket = false;
70 public void connect() throws IOException {
72 socket = new Socket(InetAddress.getByName(host), port);
74 channel = new PCMMChannel(this.socket);
78 public boolean disconnect() {
85 } catch (IOException e) {
86 logger.error(e.getMessage());
93 public void sendRequest(COPSMsg requestMessage) {
95 channel.sendMsg(requestMessage);
96 } catch (Exception e) {
97 logger.error(e.getMessage(), getSocket());
102 public COPSMsg readMessage() {
104 COPSMsg recvdMsg = channel.receiveMessage();
105 logger.debug("received message : " + recvdMsg.getHeader());
107 } catch (Exception e) {
108 logger.error(e.getMessage(), getSocket());
116 public Socket getSocket() {
121 public boolean isConnected() {
122 return socket != null && socket.isConnected();
126 * @return the versionInfo
128 public MMVersionInfo getVersionInfo() {
134 * the versionInfo to set
136 public void setVersionInfo(MMVersionInfo versionInfo) {
137 this.versionInfo = versionInfo;
141 public COPSHandle getClientHandle() {
146 public void setClientHandle(final COPSHandle handle) {
147 this.clientHandle = handle;