org.javagroups.util
Class Util

java.lang.Object
  |
  +--org.javagroups.util.Util

public class Util
extends java.lang.Object

Collection of various utility routines that can not be assigned to other classes.


Field Summary
static java.lang.String DIAG_GROUP
           
static int MAX_PORT
           
 
Constructor Summary
Util()
           
 
Method Summary
static java.lang.String activeThreads()
           
static java.lang.String array2String(boolean[] array)
           
static java.lang.String array2String(int[] array)
           
static java.lang.String array2String(long[] array)
           
static boolean checkForLinux()
           
static boolean checkForSolaris()
           
static boolean checkForWindows()
           
static void crash()
           
static java.net.DatagramSocket createDatagramSocket(java.net.InetAddress addr, int port)
          Creates a DatagramSocket bound to addr.
static java.net.ServerSocket createServerSocket(int start_port)
          Finds first available port starting at start_port and returns server socket
static byte[] defragmentBuffer(byte[][] fragments)
          Concatenates smaller fragments into entire buffers.
static java.util.Vector determineLeftMembers(java.util.Vector old_mbrs, java.util.Vector new_mbrs)
          Returns all members that left between 2 views.
static void doubleWrite(byte[] buf, java.io.OutputStream out)
          Makes sure that we detect when a peer connection is in the closed state (not closed while we send data, but before we send data).
static void dumpStack(boolean exit)
           
static boolean fileExists(java.lang.String fname)
           
static byte[][] fragmentBuffer(byte[] buf, int frag_size)
          Fragments a byte buffer into smaller fragments of (max.) frag_size.
static java.lang.String getHostname()
           
static void main(java.lang.String[] args)
           
static java.lang.Object objectFromByteBuffer(byte[] buffer)
          Creates an object from a byte buffer
static byte[] objectToByteBuffer(java.lang.Object obj)
          Serializes an object into a byte buffer.
static long[] parseCommaDelimitedLongs(java.lang.String s)
          E.g.
static java.util.Vector pickSubset(java.util.Vector members, double subset_percentage)
          Selects a random subset of members according to subset_percentage and returns them.
static java.lang.String print(java.lang.Throwable t)
           
static java.lang.String printEvent(Event evt)
           
static void printFragments(byte[][] frags)
           
static java.lang.String printMembers(java.util.Vector v)
           
static java.lang.String printMessage(Message msg)
          Tries to read an object from the message's buffer and prints it
static java.lang.String printMethodCall(Message msg)
          Tries to read a MethodCall object from the message's buffer and prints it.
static java.lang.String printStackTrace()
          Use with caution: lots of overhead
static java.lang.String printStackTrace(java.lang.Throwable t)
          Use with caution: lots of overhead
static void printThreads()
           
static long random(long range)
          Returns a random value in the range [1 - range]
static void removeFile(java.lang.String fname)
           
static boolean sameHost(Address one, Address two)
          Checks whether 2 Addresses are on the same host
static java.lang.String shortName(java.lang.String hostname)
           
static long sizeOf(java.lang.Object inst)
           
static long sizeOf(java.lang.String classname)
           
static void sleep(long timeout)
          Sleep for timeout msecs.
static void sleep(long msecs, boolean busy_sleep)
          On most UNIX systems, the minimum sleep time is 10-20ms.
static void sleepRandom(long timeout)
          Sleeps between 1 and timeout milliseconds, chosen randomly.
static boolean tossWeightedCoin(double probability)
          Tosses a coin weighted with probability and returns true or false.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_PORT

public static final int MAX_PORT
See Also:
Constant Field Values

DIAG_GROUP

public static final java.lang.String DIAG_GROUP
See Also:
Constant Field Values
Constructor Detail

Util

public Util()
Method Detail

objectFromByteBuffer

public static java.lang.Object objectFromByteBuffer(byte[] buffer)
                                             throws java.lang.Exception
Creates an object from a byte buffer

java.lang.Exception

objectToByteBuffer

public static byte[] objectToByteBuffer(java.lang.Object obj)
                                 throws java.lang.Exception
Serializes an object into a byte buffer. The object has to implement interface Serializable or Externalizable

java.lang.Exception

sleep

public static void sleep(long timeout)
Sleep for timeout msecs. Returns when timeout has elapsed or thread was interrupted


sleep

public static void sleep(long msecs,
                         boolean busy_sleep)
On most UNIX systems, the minimum sleep time is 10-20ms. Even if we specify sleep(1), the thread will sleep for at least 10-20ms. On Windows, sleep() seems to be implemented as a busy sleep, that is the thread never relinquishes control and therefore the sleep(x) is exactly x ms long.


random

public static long random(long range)
Returns a random value in the range [1 - range]


sleepRandom

public static void sleepRandom(long timeout)
Sleeps between 1 and timeout milliseconds, chosen randomly. Timeout must be > 1


tossWeightedCoin

public static boolean tossWeightedCoin(double probability)
Tosses a coin weighted with probability and returns true or false. Example: if probability=0.8, chances are that in 80% of all cases, true will be returned and false in 20%.


getHostname

public static java.lang.String getHostname()

dumpStack

public static void dumpStack(boolean exit)

printStackTrace

public static java.lang.String printStackTrace(java.lang.Throwable t)
Use with caution: lots of overhead


printStackTrace

public static java.lang.String printStackTrace()
Use with caution: lots of overhead


print

public static java.lang.String print(java.lang.Throwable t)

crash

public static void crash()

printEvent

public static java.lang.String printEvent(Event evt)

printMessage

public static java.lang.String printMessage(Message msg)
Tries to read an object from the message's buffer and prints it


printMethodCall

public static java.lang.String printMethodCall(Message msg)
Tries to read a MethodCall object from the message's buffer and prints it. Returns empty string if object is not a method call


printThreads

public static void printThreads()

activeThreads

public static java.lang.String activeThreads()

fragmentBuffer

public static byte[][] fragmentBuffer(byte[] buf,
                                      int frag_size)
Fragments a byte buffer into smaller fragments of (max.) frag_size. Example: a byte buffer of 1024 bytes and a frag_size of 248 gives 4 fragments of 248 bytes each and 1 fragment of 32 bytes.

Returns:
An array of byte buffers (byte[]).

defragmentBuffer

public static byte[] defragmentBuffer(byte[][] fragments)
Concatenates smaller fragments into entire buffers.

Parameters:
fragments - An array of byte buffers (byte[])
Returns:
A byte buffer

printFragments

public static void printFragments(byte[][] frags)

array2String

public static java.lang.String array2String(long[] array)

array2String

public static java.lang.String array2String(int[] array)

array2String

public static java.lang.String array2String(boolean[] array)

pickSubset

public static java.util.Vector pickSubset(java.util.Vector members,
                                          double subset_percentage)
Selects a random subset of members according to subset_percentage and returns them. Picks no member twice from the same membership. If the percentage is smaller than 1 -> picks 1 member.


determineLeftMembers

public static java.util.Vector determineLeftMembers(java.util.Vector old_mbrs,
                                                    java.util.Vector new_mbrs)
Returns all members that left between 2 views. All members that are element of old_mbrs but not element of new_mbrs are returned.


printMembers

public static java.lang.String printMembers(java.util.Vector v)

doubleWrite

public static void doubleWrite(byte[] buf,
                               java.io.OutputStream out)
                        throws java.lang.Exception
Makes sure that we detect when a peer connection is in the closed state (not closed while we send data, but before we send data). 2 writes ensure that, if the peer closed the connection, the first write will send the peer from FIN to RST state, and the second will cause a signal (IOException).

java.lang.Exception

sizeOf

public static long sizeOf(java.lang.String classname)

sizeOf

public static long sizeOf(java.lang.Object inst)

sameHost

public static boolean sameHost(Address one,
                               Address two)
Checks whether 2 Addresses are on the same host


removeFile

public static void removeFile(java.lang.String fname)

fileExists

public static boolean fileExists(java.lang.String fname)

parseCommaDelimitedLongs

public static long[] parseCommaDelimitedLongs(java.lang.String s)
E.g. 2000,4000,8000


shortName

public static java.lang.String shortName(java.lang.String hostname)

createServerSocket

public static java.net.ServerSocket createServerSocket(int start_port)
Finds first available port starting at start_port and returns server socket


createDatagramSocket

public static java.net.DatagramSocket createDatagramSocket(java.net.InetAddress addr,
                                                           int port)
                                                    throws java.lang.Exception
Creates a DatagramSocket bound to addr. If addr is null, socket won't be bound. If address is already in use, start_port will be incremented until a socket can be created.

Parameters:
addr - The InetAddress to which the socket should be bound. If null, the socket will not be bound.
port - The port which the socket should use. If 0, a random port will be used. If > 0, but port is already in use, it will be incremented until an unused port is found, or until MAX_PORT is reached.
java.lang.Exception

checkForLinux

public static boolean checkForLinux()

checkForSolaris

public static boolean checkForSolaris()

checkForWindows

public static boolean checkForWindows()

main

public static void main(java.lang.String[] args)


Copyright ? 2001,2002 www.javagroups.com . All Rights Reserved.