04a4aff51c70be4196fc7be3d0de60f78b0029ae
[packetcable.git] / protocol_plugins.packetcable / src / main / java / org / umu / cops / stack / COPSMsgParser.java
1 /*\r
2  * Copyright (c) 2003 University of Murcia.  All rights reserved.\r
3  * --------------------------------------------------------------\r
4  * For more information, please see <http://www.umu.euro6ix.org/>.\r
5  */\r
6 \r
7 package org.umu.cops.stack;\r
8 \r
9 /**\r
10  * COPS Message Parser\r
11  *\r
12  * @version COPSMsgParser.java, v 1.00 2003\r
13  *\r
14  */\r
15 \r
16 // import org.umu.cops.common.COPSDebug;\r
17 \r
18 public class COPSMsgParser {\r
19     ///\r
20     public COPSMsgParser() {\r
21 \r
22     }\r
23 \r
24     /** Parses the given COPS data and returns a COPSMsg object\r
25      *     with COPS object filed in.The COPSMsg object is allocated in the\r
26      *     call and it is the responsibility of the caller to free the memory\r
27      *\r
28      * @param    data                a  byte[]\r
29      *\r
30      * @return   a COPSMsg\r
31      *\r
32      * @throws   COPSException\r
33      *\r
34      */\r
35     public COPSMsg parse(byte[] data) throws COPSException {\r
36         COPSHeader hdr = new COPSHeader(data);\r
37 \r
38         byte[] buf = new byte[data.length - 8];\r
39         System.arraycopy(data,8,buf,0,data.length - 8);\r
40 \r
41         return (parse(hdr, buf));\r
42     }\r
43 \r
44     /**\r
45      * Parse the message with given header , the data is pointing\r
46      * to the data following the header\r
47      *\r
48      * @param    hdr                 a  COPSHeader\r
49      * @param    data                a  byte[]\r
50      *\r
51      * @return   a COPSMsg\r
52      *\r
53      * @throws   COPSException\r
54      *\r
55      */\r
56     public COPSMsg parse(COPSHeader hdr, byte[] data) throws COPSException {\r
57         COPSMsg copsMsg = null;\r
58         short cCode = hdr.getOpCode();\r
59         switch (cCode) {\r
60         case COPSHeader.COPS_OP_REQ: {\r
61             // COPSDebug.out(getClass().getName(), "Creating REQ msg");\r
62             copsMsg = new COPSReqMsg();\r
63             copsMsg.parse(hdr, data);\r
64         }\r
65         break;\r
66         case COPSHeader.COPS_OP_DEC: {\r
67             // COPSDebug.out(getClass().getName(), "Creating DEC msg");\r
68             copsMsg = new COPSDecisionMsg();\r
69             copsMsg.parse(hdr, data);\r
70         }\r
71         break;\r
72         case COPSHeader.COPS_OP_RPT: {\r
73             // COPSDebug.out(getClass().getName(), "Creating RPT msg");\r
74             copsMsg = new COPSReportMsg();\r
75             copsMsg.parse(hdr, data);\r
76         }\r
77         break;\r
78         case COPSHeader.COPS_OP_DRQ: {\r
79             // COPSDebug.out(getClass().getName(), "Creating DRQ msg");\r
80             copsMsg = new COPSDeleteMsg();\r
81             copsMsg.parse(hdr, data);\r
82         }\r
83         break;\r
84         case COPSHeader.COPS_OP_OPN: {\r
85             // COPSDebug.out(getClass().getName(), "Creating Client-Open msg");\r
86             copsMsg = new COPSClientOpenMsg();\r
87             copsMsg.parse(hdr, data);\r
88         }\r
89         break;\r
90         case COPSHeader.COPS_OP_CAT: {\r
91             // COPSDebug.out(getClass().getName(), "Creating Client-Accept msg");\r
92             copsMsg = new COPSClientAcceptMsg();\r
93             copsMsg.parse(hdr, data);\r
94         }\r
95         break;\r
96         case COPSHeader.COPS_OP_CC: {\r
97             // COPSDebug.out(getClass().getName(), "Creating Client-Close msg");\r
98             copsMsg = new COPSClientCloseMsg();\r
99             copsMsg.parse(hdr, data);\r
100         }\r
101         break;\r
102         case COPSHeader.COPS_OP_KA: {\r
103             // COPSDebug.out(getClass().getName(), "Creating KA msg");\r
104             copsMsg = new COPSKAMsg();\r
105             copsMsg.parse(hdr, data);\r
106         }\r
107         break;\r
108         case COPSHeader.COPS_OP_SSQ: {\r
109             // COPSDebug.out(getClass().getName(), "Creating Sync-State Request msg");\r
110             copsMsg = new COPSSyncStateMsg();\r
111             copsMsg.parse(hdr, data);\r
112         }\r
113         break;\r
114         case COPSHeader.COPS_OP_SSC: {\r
115             // COPSDebug.out(getClass().getName(), "Creating Sync-State Complete msg");\r
116             copsMsg = new COPSSyncStateMsg();\r
117             copsMsg.parse(hdr, data);\r
118         }\r
119         break;\r
120         default:\r
121             // COPSDebug.out(getClass().getName(), "Unknown message type");\r
122             break;\r
123         }\r
124 \r
125 \r
126         // if(copsMsg != null)\r
127         //     try { copsMsg.dump(COPSDebug.out); } catch (Exception e) {};\r
128 \r
129         return copsMsg;\r
130     }\r
131 }\r
132 \r
133 \r