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