6f4f7b4c4188737e5bc96a5fc81a0dbdb1e73a59
[packetcable.git] / packetcable-driver / src / main / java / org / umu / cops / stack / COPSMsg.java
1 /*
2  * Copyright (c) 2003 University of Murcia.  All rights reserved.
3  * --------------------------------------------------------------
4  * For more information, please see <http://www.umu.euro6ix.org/>.
5  */
6
7 package org.umu.cops.stack;
8
9 import java.io.IOException;
10 import java.io.OutputStream;
11 import java.net.Socket;
12
13 /**
14  * Represents messages coming from and going to a COPS device such as a CMTS
15  */
16 abstract public class COPSMsg {
17
18     /**
19      * The COPS header that is associated with all COPS messages
20      */
21     private final COPSHeader _hdr;
22
23     /**
24      * Base constructor
25      * @param hdr - the header
26      */
27     public COPSMsg(final COPSHeader hdr) {
28         if (hdr == null) throw new IllegalArgumentException("Header must not be null");
29         this._hdr = hdr;
30     }
31     /**
32      * Returns the message header object
33      * @return   a COPSHeader
34      */
35     public COPSHeader getHeader() {
36         return _hdr;
37     }
38
39     /**
40      * Method writeData. Implementers should be calling super.writeData() for the header prior to writing out the rest.
41      * @param    socket                  a  Socket
42      * @throws   IOException
43      */
44     public final void writeData(final Socket socket) throws IOException {
45         // checkSanity();
46         _hdr.writeData(socket, _hdr.getHdrLength() + getDataLength());
47         writeBody(socket);
48     }
49
50     /**
51      * Returns the number of bytes to be contained within the payload excluding the header
52      * @return - a positive value including the header size
53      */
54     protected abstract int getDataLength();
55
56     /**
57      * Writes out the body data over a socket
58      * @param socket - the socket to which to write
59      */
60     protected abstract void writeBody(Socket socket) throws IOException;
61
62     /**
63      * Write an object textual description in the output stream
64      * @param    os                  an OutputStream
65      * @throws   IOException
66      */
67     final public void dump(final OutputStream os) throws IOException {
68         _hdr.dump(os);
69         dumpBody(os);
70     }
71
72     /**
73      * Creates a string representation of this object and sends it to an output stream
74      * @param os - the output stream
75      * @throws IOException
76      */
77     protected abstract void dumpBody(final OutputStream os) throws IOException;
78
79     @Override
80     public boolean equals(final Object o) {
81         if (this == o) {
82             return true;
83         }
84         if (!(o instanceof COPSMsg)) {
85             return false;
86         }
87
88         final COPSMsg copsMsg = (COPSMsg) o;
89
90         return _hdr.equals(copsMsg._hdr);
91
92     }
93
94     @Override
95     public int hashCode() {
96         return _hdr.hashCode();
97     }
98 }