Expanded CMTS emulator to respond properly to gate add requests.
[packetcable.git] / packetcable-driver / src / main / java / org / pcmm / rcd / impl / AbstractPCMMClient.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.pcmm.rcd.impl;
6
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;
14
15 import java.io.IOException;
16 import java.net.InetAddress;
17 import java.net.Socket;
18
19 // import org.junit.Assert;
20
21 /**
22  *
23  * default implementation for {@link IPCMMClient}
24  *
25  *
26  */
27 public class AbstractPCMMClient implements IPCMMClient {
28
29     private final static Logger logger = LoggerFactory.getLogger(AbstractPCMMClient.class);
30
31     private final String host;
32
33     private final int port;
34
35     // TODO - consider removing this attribute as it is never being used
36     private MMVersionInfo versionInfo;
37
38     // Following two attributes are set when connect() is called
39     /**
40      * socket used to communicated with server.
41      */
42     private transient Socket socket;
43
44     private transient PCMMChannel channel;
45
46     // TODO - determine why this class holds a handle as it is not being used.
47     private transient COPSHandle clientHandle;
48
49     /**
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
52      * responsible.
53      */
54     private final boolean ownSocket;
55
56     public AbstractPCMMClient(final String host, final int port) {
57         this.host = host;
58         this.port = port;
59         this.ownSocket = true;
60     }
61
62     public AbstractPCMMClient(final Socket socket) {
63         this.host = socket.getInetAddress().getHostName();
64         this.port = socket.getPort();
65         this.socket = socket;
66         this.ownSocket = false;
67     }
68
69     @Override
70     public void connect() throws IOException {
71         if (socket == null) {
72             socket = new Socket(InetAddress.getByName(host), port);
73         }
74         channel = new PCMMChannel(this.socket);
75     }
76
77     @Override
78     public boolean disconnect() {
79         if (isConnected()) {
80             try {
81                 if (ownSocket) {
82                     socket.close();
83                 }
84                 channel = null;
85             } catch (IOException e) {
86                 logger.error(e.getMessage());
87             }
88         }
89         return true;
90     }
91
92     @Override
93     public void sendRequest(COPSMsg requestMessage) {
94         try {
95             channel.sendMsg(requestMessage);
96         } catch (Exception e) {
97             logger.error(e.getMessage(), getSocket());
98         }
99     }
100
101     @Override
102     public COPSMsg readMessage() {
103         try {
104             COPSMsg recvdMsg = channel.receiveMessage();
105             logger.debug("received message : " + recvdMsg.getHeader());
106             return recvdMsg;
107         } catch (Exception e) {
108             logger.error(e.getMessage(), getSocket());
109         }
110         return null;
111     }
112
113     /**
114      * @return the socket
115      */
116     public Socket getSocket() {
117         return socket;
118     }
119
120     @Override
121     public boolean isConnected() {
122         return socket != null && socket.isConnected();
123     }
124
125     /**
126      * @return the versionInfo
127      */
128     public MMVersionInfo getVersionInfo() {
129         return versionInfo;
130     }
131
132     /**
133      * @param versionInfo
134      *            the versionInfo to set
135      */
136     public void setVersionInfo(MMVersionInfo versionInfo) {
137         this.versionInfo = versionInfo;
138     }
139
140     @Override
141     public COPSHandle getClientHandle() {
142         return clientHandle;
143     }
144
145     @Override
146     public void setClientHandle(final COPSHandle handle) {
147         this.clientHandle = handle;
148     }
149
150 }