1 /* 2 * Copyright (c) 1998-2002 The Jgroup Team. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * 17 */ 18 19 package jgroup.relacs.types; 20 21 import java.io.IOException; 22 import java.io.ObjectInput; 23 import java.io.ObjectOutput; 24 25 /** 26 * The <code>MessageId</code> class wraps a value of the relacs daemon 27 * type "message id" in an object. A message id uniquely identifies 28 * multicast messages. 29 * 30 * @author Alberto Montresor 31 * @since Jgroup 1.2 32 */ 33 public final class MessageId 34 { 35 36 //////////////////////////////////////////////////////////////////////////////////////////// 37 // Constants 38 //////////////////////////////////////////////////////////////////////////////////////////// 39 40 /** Size of this object in bytes */ 41 public static final int SIZE = 4; 42 43 44 //////////////////////////////////////////////////////////////////////////////////////////// 45 // Fields 46 //////////////////////////////////////////////////////////////////////////////////////////// 47 48 /** Wrapped value */ 49 private int value; 50 51 52 //////////////////////////////////////////////////////////////////////////////////////////// 53 // Constructor 54 //////////////////////////////////////////////////////////////////////////////////////////// 55 56 /** 57 * Creates a <CODE>MessageId</CODE> object containing the specified 58 * value. 59 */ 60 public MessageId(int value) 61 { 62 this.value = value; 63 } 64 65 66 //////////////////////////////////////////////////////////////////////////////////////////// 67 // Accessor method 68 //////////////////////////////////////////////////////////////////////////////////////////// 69 70 /** 71 * Returned the wrapped value. 72 */ 73 public int getValue() 74 { 75 return value; 76 } 77 78 //////////////////////////////////////////////////////////////////////////////////////////// 79 // Marshaling / unmarshaling methods 80 //////////////////////////////////////////////////////////////////////////////////////////// 81 82 /** 83 * Marshals the specified message id to an outgoing message. 84 */ 85 public static void marshal(ObjectOutput msg, int value) 86 throws IOException 87 { 88 msg.writeInt(value); 89 } 90 91 /** 92 * Unamrshals a message id from an incoming message. 93 */ 94 public static int unmarshal(ObjectInput msg) 95 throws IOException 96 { 97 return msg.readInt(); 98 } 99 100 /** 101 * Unamrshals a message id from an incoming byte array. 102 */ 103 public static int unmarshal(byte[] buf, int offset) 104 { 105 return ((buf[offset++] << 24) & 0xFF000000) | ((buf[offset++] << 16) & 0x00FF0000) | 106 ((buf[offset++] << 8) & 0x0000FF00) | (buf[offset] & 0x000000FF); 107 } 108 109 } // END MessageId