public class Util extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
Util.PacketType
The known packet types used in the protocol, along with the byte values
which identify them, and the names by which we describe them, and the port
on which they are received.
|
| Modifier and Type | Field and Description |
|---|---|
static Map<Integer,Map<Byte,Util.PacketType>> |
PACKET_TYPE_MAP
Allows a known packet type to be looked up given the port number it was received on and the packet type byte.
|
static int |
PACKET_TYPE_OFFSET
The offset into protocol packets which identify the content of the packet.
|
| Modifier and Type | Method and Description |
|---|---|
static long |
addressToLong(InetAddress address)
Converts the bytes that make up an internet address into the corresponding integer value to make
it easier to perform bit-masking operations on them.
|
static Object |
allocateNamedLock(String name)
Obtain an object that can be synchronized against to provide exclusive access to a named resource,
given its unique name.
|
static DatagramPacket |
buildPacket(Util.PacketType type,
ByteBuffer deviceName,
ByteBuffer payload)
Build a standard-format UDP packet for sending to port 50001 or 50002 in the protocol.
|
static long |
bytesToNumber(byte[] buffer,
int start,
int length)
Reconstructs a number that is represented by more than one byte in a network packet in big-endian order.
|
static long |
bytesToNumberLittleEndian(byte[] buffer,
int start,
int length)
Reconstructs a number that is represented by more than one byte in a network packet in little-endian order, for
the very few protocol values that are sent in this quirky way.
|
static void |
freeNamedLock(String name)
Indicate that an object obtained from
allocateNamedLock(String) is no longer needed by the caller, so
it is eligible for garbage collection if no other threads have it allocated. |
static ByteBuffer |
getMagicHeader()
Get the sequence of nine bytes which begins all UDP packets sent in the protocol as a
ByteBuffer. |
static long |
halfFrameToTime(long halfFrame)
Figure out the track time that corresponds to a half-frame number (75 frames per second, so 150 half-frames).
|
static void |
numberToBytes(int number,
byte[] buffer,
int start,
int length)
Writes a number to the specified byte array field, breaking it into its component bytes in big-endian order.
|
static double |
pitchToMultiplier(long pitch)
Convert a pitch value reported by a device to the corresponding multiplier (0.0 to 2.0, where normal, unadjusted
pitch has the multiplier 1.0).
|
static double |
pitchToPercentage(long pitch)
Convert a pitch value reported by a device to the corresponding percentage (-100% to +100%, where normal,
unadjusted pitch has the value 0%).
|
static boolean |
sameNetwork(int prefixLength,
InetAddress address1,
InetAddress address2)
Checks whether two internet addresses are on the same subnet.
|
static int |
timeToHalfFrame(long milliseconds)
Convert a track position (time) into the corresponding half-frame value (75 frames per second, so 150 half-frames).
|
static int |
unsign(byte b)
Converts a signed byte to its unsigned int equivalent in the range 0-255.
|
static Util.PacketType |
validateHeader(DatagramPacket packet,
int port)
Check to see whether a packet starts with the standard header bytes, followed by a known byte identifying it.
|
static void |
writeFully(ByteBuffer buffer,
WritableByteChannel channel)
Writes the entire remaining contents of the buffer to the channel.
|
public static final int PACKET_TYPE_OFFSET
public static final Map<Integer,Map<Byte,Util.PacketType>> PACKET_TYPE_MAP
public static ByteBuffer getMagicHeader()
ByteBuffer.
Each call returns a new instance, so you don't need to worry about messing with buffer positions.ByteBuffer containing the header with which all protocol packets begin.public static DatagramPacket buildPacket(Util.PacketType type, ByteBuffer deviceName, ByteBuffer payload)
type - the type of packet to create.deviceName - the 0x14 (twenty) bytes of the device name to send in the packet.payload - the remaining bytes which come after the device name.public static Util.PacketType validateHeader(DatagramPacket packet, int port)
packet - a packet that has just been receivedport - the port on which the packet has been receivednull if the packet was not recognizedpublic static int unsign(byte b)
b - a byte value to be considered an unsigned integerpublic static long bytesToNumber(byte[] buffer,
int start,
int length)
buffer - the byte array containing the packet datastart - the index of the first byte containing a numeric valuelength - the number of bytes making up the valuepublic static long bytesToNumberLittleEndian(byte[] buffer,
int start,
int length)
buffer - the byte array containing the packet datastart - the index of the first byte containing a numeric valuelength - the number of bytes making up the valuepublic static void numberToBytes(int number,
byte[] buffer,
int start,
int length)
number - the number to be written to the arraybuffer - the buffer to which the number should be writtenstart - where the high-order byte should be writtenlength - how many bytes of the number should be writtenpublic static long addressToLong(InetAddress address)
address - an address whose integer equivalent is desiredpublic static boolean sameNetwork(int prefixLength,
InetAddress address1,
InetAddress address2)
prefixLength - the number of bits within an address that identify the networkaddress1 - the first address to be comparedaddress2 - the second address to be comparedpublic static double pitchToPercentage(long pitch)
pitch - the reported device pitchpublic static double pitchToMultiplier(long pitch)
pitch - the reported device pitchpublic static void writeFully(ByteBuffer buffer, WritableByteChannel channel) throws IOException
buffer - the data to be writtenchannel - the channel to which we want to write dataIOException - if there is a problem writing to the channelpublic static long halfFrameToTime(long halfFrame)
halfFrame - the half-frame that we are interested in knowing the time forpublic static int timeToHalfFrame(long milliseconds)
milliseconds - how long a track has been playing forpublic static Object allocateNamedLock(String name)
CrateDigger
to protect against race conditions where one thread creates the file and another thinks it has already been
downloaded and tries to parse the partial file.
Once the exclusive lock is no longer needed, freeNamedLock(String) should be called with the same
name so the lock can be garbage collected if no other threads are now using it.name - uniquely identifies some resource to which exclusive access is neededsynchronized block to guarantee exclusive access to the resourcepublic static void freeNamedLock(String name)
allocateNamedLock(String) is no longer needed by the caller, so
it is eligible for garbage collection if no other threads have it allocated.name - uniquely identifies some resource to which exclusive access was previously neededCopyright © 2016–2019 Deep Symmetry, LLC. All rights reserved.