1 package org.jacorb.orb.giop; 2 3 22 23 import org.omg.CORBA.BAD_PARAM ; 24 import org.omg.GIOP.MsgType_1_1; 25 import org.omg.IOP.ServiceContext ; 26 27 32 public class Messages 33 { 34 37 static final int MSG_HEADER_SIZE = 12; 38 39 54 static final ServiceContext [] service_context = new ServiceContext [0]; 55 56 67 private static int skipServiceContext(byte[] buf, 68 int offset, 69 int length, 70 boolean little_endian ) 71 { 72 73 int pos = offset; 74 75 for( int i = 0; i < length; i++) 76 { 77 pos += 4; 79 80 pos = skipSequence( buf, pos, 1, little_endian ); 82 83 int diff = pos % 4; 86 if( diff != 0 ) 87 { 88 pos += ( 4 - diff ); 89 } 90 } 91 92 return pos; 93 } 94 95 106 private static final int skipSequence( byte[] buf, 107 int offset, 108 int element_size, 109 boolean little_endian ) 110 { 111 112 int length = readULong( buf, offset, little_endian ); 113 114 return offset + 4 + length * element_size; 115 } 116 117 118 119 public static int getRequestId( byte[] buf ) 120 { 121 int msg_type = getMsgType( buf ); 122 int giop_minor = getGIOPMinor( buf ); 123 boolean little_endian = isLittleEndian( buf ); 124 125 int request_id = -1; 126 127 if( giop_minor == 2 ) 128 { 129 131 if( msg_type == MsgType_1_1._Request || 132 msg_type == MsgType_1_1._LocateRequest || 133 msg_type == MsgType_1_1._Reply || 134 msg_type == MsgType_1_1._LocateReply || 135 msg_type == MsgType_1_1._CancelRequest || 136 msg_type == MsgType_1_1._Fragment ) 137 { 138 request_id = readULong( buf, MSG_HEADER_SIZE, little_endian ); 141 } 142 else 143 { 144 throw new BAD_PARAM 145 ("Messages of type " + msg_type + " don't have request ids"); 146 } 147 } 148 else if( giop_minor == 0 || giop_minor == 1 ) 149 { 150 if( msg_type == MsgType_1_1._Request || 151 msg_type == MsgType_1_1._Reply ) 152 { 153 155 int service_ctx_length = readULong( buf, 157 MSG_HEADER_SIZE, 158 little_endian ); 159 160 if( service_ctx_length == 0 ) 161 { 162 165 request_id = readULong( buf, 166 MSG_HEADER_SIZE + 4, 167 little_endian ); 168 } 169 else 170 { 171 int pos = skipServiceContext( buf, 173 MSG_HEADER_SIZE + 4, service_ctx_length, 175 little_endian ); 176 177 request_id = readULong( buf, pos, little_endian ); 179 } 180 } 181 else if( msg_type == MsgType_1_1._LocateRequest || 182 msg_type == MsgType_1_1._LocateReply ) 183 { 184 request_id = readULong( buf, MSG_HEADER_SIZE, little_endian ); 186 } 187 else 188 { 189 throw new BAD_PARAM 190 ("Messages of type " + msg_type + " don't have request ids"); 191 } 192 } 193 194 return request_id; 195 } 196 197 public static final int getMsgSize( byte[] buf ) 198 { 199 return readULong( buf, 8, isLittleEndian( buf ) ); 200 } 201 202 public static final int readULong( byte[] buf, 203 int pos, 204 boolean little_endian ) 205 { 206 if( little_endian ) 207 { 208 return (( (buf[pos+3] & 0xff) << 24) + 209 ( (buf[pos+2] & 0xff) << 16) + 210 ( (buf[pos+1] & 0xff) << 8) + 211 ( (buf[pos] & 0xff) << 0)); 212 } 213 else { 215 return (( (buf[pos] & 0xff) << 24) + 216 ( (buf[pos+1] & 0xff) << 16) + 217 ( (buf[pos+2] & 0xff) << 8) + 218 ( (buf[pos+3] & 0xff) << 0)); 219 } 220 } 221 222 public static final boolean isLittleEndian( byte[] buf ) 223 { 224 return (0x01 & buf[6]) != 0; 226 } 227 228 public static final boolean moreFragmentsFollow( byte[] buf ) 229 { 230 return (0x02 & buf[6]) != 0; 232 } 233 234 public static final int getMsgType( byte[] buf ) 235 { 236 return buf[7]; 237 } 238 239 public static final int getGIOPMajor( byte[] buf ) 240 { 241 return buf[4]; 242 } 243 244 public static final int getGIOPMinor( byte[] buf ) 245 { 246 return buf[5]; 247 } 248 249 public static final boolean responseExpected( byte flags ) 250 { 251 return flags == (byte)0x01 || flags == (byte)0x03; 252 } 253 254 public static final byte responseFlags( boolean response_expected ) 255 { 256 return (byte) (response_expected ? 0x03 : 0x00); 257 } 258 259 } 260 | Popular Tags |