Source code: com/voytechs/jnetstream/codec/PacketImpl.java
1 /*
2 * File: PacketImpl.java
3 * Auth: Mark Bednarczyk
4 * Date: DATE
5 * Id: $Id: PacketImpl.java,v 1.1.1.1 2003/09/22 16:32:13 voytechs Exp $
6 ********************************************
7 Copyright (C) 2003 Mark Bednarczyk
8
9 This program is free software; you can redistribute it and/or
10 modify it under the terms of the GNU General Public License
11 as published by the Free Software Foundation; either version 2
12 of the License, or (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 ********************************************
23 * $Log: PacketImpl.java,v $
24 * Revision 1.1.1.1 2003/09/22 16:32:13 voytechs
25 * Initial import.
26 *
27 */
28 package com.voytechs.jnetstream.codec;
29
30 import com.voytechs.jnetstream.primitive.*;
31 import com.voytechs.jnetstream.primitive.address.*;
32 import com.voytechs.jnetstream.io.*;
33
34 import java.lang.*;
35 import java.util.*;
36
37 /**
38 *
39 */
40 public class PacketImpl
41 implements Packet, MutablePacket {
42
43 /* Internal attributes */
44 private static final boolean debug = false;
45
46 /* Store the headers in 2 structures. 1=array based and second hashtable based
47 * for easy lookup by name.
48 */
49 private ArrayList headerList = new ArrayList();
50 private Hashtable headerHash = new Hashtable();
51
52 private Hashtable perm;
53 private Hashtable temp = new Hashtable();
54
55 /**
56 *
57 * @param
58 * @exception
59 */
60 public PacketImpl(Hashtable permProperties, PacketInputStream in) {
61 this.perm = permProperties;
62
63 setPacketPropertiesFromStream(in);
64 }
65
66 protected void setPacketPropertiesFromStream(PacketInputStream in) {
67
68 LongPrimitive packetLength = new LongPrimitive();
69 packetLength.setValue(in.getPacketLength());
70 setProperty(PACKET_LENGTH, packetLength);
71 }
72
73
74 /**
75 */
76 public void setProperty(String name, Primitive property) {
77 temp.put(name, property);
78 }
79
80
81 /**
82 */
83 public Primitive getProperty(String name) {
84
85 Primitive prim = (Primitive)temp.get(name);
86
87 if(prim != null)
88 return(prim);
89
90 prim = (Primitive)perm.get(name);
91 if(prim != null)
92 return(prim);
93
94 return(null);
95 }
96
97
98 /**
99 * Adds a new header to the packet.
100 */
101 public void addHeader(Header header) {
102 headerList.add(header);
103 headerHash.put(header.getName(), header);
104 }
105
106
107 /**
108 * Returns the named header from the packet.
109 */
110 public Header getHeader(String name) {
111 return((Header)headerHash.get(name));
112 }
113
114 /**
115 * Returns the indexed header from the packet.
116 */
117 public Header getHeader(int index) {
118 return((Header)headerList.get(index));
119 }
120
121 /**
122 * Returns the size of this packet. Size refers to number of headers
123 * decoded. Only headers that this codec understands are decoded. There may
124 * be additional undecoded headers of other protocoal still in the stack.
125 * @return Number of headers in the packet.
126 */
127 public int getHeaderCount() {
128 return(headerList.size());
129 }
130
131 public String toString() {
132
133 OutputBuffer s = new OutputBuffer();
134
135 HeaderImpl header;
136
137 for(int i = 0; i < headerList.size(); i ++) {
138 header = (HeaderImpl)getHeader(i);
139
140 header.toString(s);
141 }
142
143 return(s.toString());
144 }
145
146 /**
147 * Test function for PacketImpl
148 * @param args command line arguments
149 */
150 public static void main(String [] args) {
151 }
152
153 } /* END OF: PacketImpl */