6 import java.io.IOException;
7 import java.io.InputStream;
8 import java.net.Socket;
9 import java.nio.ByteBuffer;
10 import java.util.Date;
12 import org.pcmm.PCMMProperties;
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15 import org.umu.cops.stack.COPSException;
16 import org.umu.cops.stack.COPSHeader;
17 import org.umu.cops.stack.COPSMsg;
18 import org.umu.cops.stack.COPSMsgParser;
21 * this class provides a set of utilities to efficiently read/write data from a
22 * stream, it could parameterized with a reading timeout or -1 for blocking
26 public class PCMMChannel {
28 private Logger logger = LoggerFactory.getLogger(getClass().getName());
29 private ByteBuffer dataBuffer;
30 private Socket socket;
32 public static final int DEFAULT_BYTE_BUFFER_SIZE = 2048;
33 public static final int DEFAULT_READ_TIMEOUT = -1;
35 public PCMMChannel(Socket socket) {
36 this(socket, PCMMProperties.get(PCMMProperties.DEFAULT_TIEMOUT,
37 Integer.class, DEFAULT_READ_TIMEOUT));
40 public PCMMChannel(Socket socket, int timeout) {
42 dataBuffer = ByteBuffer.allocateDirect(DEFAULT_BYTE_BUFFER_SIZE);
43 logger.info("Allocated byte buffer with size = "
44 + DEFAULT_BYTE_BUFFER_SIZE);
45 this.timeout = timeout;
46 logger.info("Set read/write timeout to : " + timeout);
50 public int readData(byte[] dataRead, int nchar) throws IOException {
52 input = getSocket().getInputStream();
54 int startTime = (int) (new Date().getTime());
56 if (timeout == -1 || input.available() != 0) {
57 nread += input.read(dataRead, nread, nchar - nread);
58 startTime = (int) (new Date().getTime());
60 int nowTime = (int) (new Date().getTime());
61 if ((nowTime - startTime) > timeout)
64 } while (nread != nchar);
75 * @throws COPSException
78 public void sendMsg(COPSMsg msg) throws IOException, COPSException {
79 logger.debug("sendMsg({})==>{}", getSocket(), msg);
81 msg.writeData(getSocket());
85 * Method receiveMessage
90 * @throws COPSException
93 public COPSMsg receiveMessage() throws IOException, COPSException {
95 byte[] hBuf = new byte[8];
97 logger.debug("receiveMessage({})", getSocket());
99 nread = readData(hBuf, 8);
102 throw new COPSException("Error reading connection");
106 throw new COPSException("Bad COPS message");
109 COPSHeader hdr = new COPSHeader(hBuf);
110 int dataLen = hdr.getMsgLength() - hdr.getHdrLength();
111 logger.debug("COPS Msg length :[" + dataLen + "]\n");
112 byte[] buf = new byte[dataLen + 1];
115 nread = readData(buf, dataLen);
116 buf[dataLen] = (byte) '\0';
117 logger.debug("Data read length:[" + nread + "]\n");
119 if (nread != dataLen) {
120 throw new COPSException("Bad COPS message");
122 COPSMsgParser prser = new COPSMsgParser();
123 COPSMsg msg = prser.parse(hdr, buf);
128 * @return the dataBuffer
130 public ByteBuffer getDataBuffer() {
136 * the dataBuffer to set
138 public void setDataBuffer(ByteBuffer dataBuffer) {
139 this.dataBuffer = dataBuffer;
145 public Socket getSocket() {
153 public void setSocket(Socket socket) {
154 this.socket = socket;
158 * @return the timeout
160 public int getTimeout() {
168 public void setTimeout(int timeout) {
169 this.timeout = timeout;