Collection of various utility routines that can not be assigned to other classes.
activeThreads
public static String activeThreads()
array2String
public static String array2String(boolean[] array)
array2String
public static String array2String(int[] array)
array2String
public static String array2String(long[] array)
checkForLinux
public static boolean checkForLinux()
checkForSolaris
public static boolean checkForSolaris()
checkForWindows
public static boolean checkForWindows()
computeFragOffsets
public static java.util.List computeFragOffsets(byte[] buf,
int frag_size)
computeFragOffsets
public static java.util.List computeFragOffsets(int offset,
int length,
int frag_size)
Given a buffer and a fragmentation size, compute a list of fragmentation offset/length pairs, and
return them in a list. Example:
Buffer is 10 bytes, frag_size is 4 bytes. Return value will be ({0,4}, {4,4}, {8,2}).
This is a total of 3 fragments: the first fragment starts at 0, and has a length of 4 bytes, the second fragment
starts at offset 4 and has a length of 4 bytes, and the last fragment starts at offset 8 and has a length
of 2 bytes.
- List. A List of offset/length pairs
crash
public static void crash()
createDatagramSocket
public static DatagramSocket createDatagramSocket(InetAddress addr,
int port)
throws 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.
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.
createServerSocket
public static ServerSocket createServerSocket(int start_port)
Finds first available port starting at start_port and returns server socket
defragmentBuffer
public static byte[] defragmentBuffer(fragments[][] )
Concatenates smaller fragments into entire buffers.
determineLeftMembers
public static Vector determineLeftMembers(Vector old_mbrs,
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.
doubleWrite
public static void doubleWrite(byte[] buf,
OutputStream out)
throws 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).
dumpStack
public static void dumpStack(boolean exit)
fileExists
public static boolean fileExists(String fname)
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.
- An array of byte buffers (
byte[]
).
getHostname
public static String getHostname()
getStackTrace
public static String getStackTrace(Throwable t)
main
public static void main(args[] )
memStats
public static String memStats(boolean gc)
objectFromByteBuffer
public static Object objectFromByteBuffer(byte[] buffer)
throws Exception
Creates an object from a byte buffer
objectToByteBuffer
public static byte[] objectToByteBuffer(Object obj)
throws Exception
Serializes an object into a byte buffer.
The object has to implement interface Serializable or Externalizable
parseCommaDelimitedLongs
public static long[] parseCommaDelimitedLongs(String s)
E.g. 2000,4000,8000
parseCommaDelimitedStrings
public static java.util.List parseCommaDelimitedStrings(String l)
e.g. "bela,jeannette,michelle" --> List{"bela", "jeannette", "michelle"}
pickSubset
public static Vector pickSubset(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.
print
public static String print(Throwable t)
printEvent
public static String printEvent(Event evt)
printFragments
public static void printFragments(frags[][] )
printMembers
public static String printMembers(Vector v)
printMessage
public static String printMessage(Message msg)
Tries to read an object from the message's buffer and prints it
printMethodCall
public static 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
printStackTrace
public static String printStackTrace()
Use with caution: lots of overhead
printStackTrace
public static String printStackTrace(Throwable t)
Use with caution: lots of overhead
printThreads
public static void printThreads()
prompt
public static void prompt(String s)
random
public static long random(long range)
Returns a random value in the range [1 - range]
removeFile
public static void removeFile(String fname)
sameHost
public static boolean sameHost(Address one,
Address two)
Checks whether 2 Addresses are on the same host
shortName
public static String shortName(String hostname)
sizeOf
public static long sizeOf(Object inst)
sizeOf
public static long sizeOf(String classname)
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.
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%.