org.vishia.msgDispatch
Class MsgDispatcher

java.lang.Object
  extended by org.vishia.msgDispatch.MsgDispatcher
All Implemented Interfaces:
LogMessage

public class MsgDispatcher
extends java.lang.Object
implements LogMessage


Nested Class Summary
 class MsgDispatcher.DispatcherThread
          Class to organize a dispachter thread.
static class MsgDispatcher.Entry
          Stores all data of a message if the message is queued here.
private static class MsgDispatcher.Output
          This class contains all infomations for a output.
private static class MsgDispatcher.TestCnt
          This class contains some test-counts for debugging.
 
Field Summary
private  int actNrofListIdents
          List of idents, its current length.
(package private)  ConcurrentLinkedQueue<MsgDispatcher.Entry> freeOrders
          List of entries for messages to use.
private  int[] listBitDst
          List of destination bits for the idents.
private  int[] listIdents
          List of idents, a array with lengthListIdents elements.
(package private)  ConcurrentLinkedQueue<MsgDispatcher.Entry> listOrders
          List of messages to process in the dispatcher thread.
static int mAdd
          Used for argument mode from setOutputRange(int, int, int, int, int) to add an output.
private  int maxDst
           
static int mConsole
          Mask for dispatch the message to console directly in the calling thread.
static int mConsoleQueued
          queued Console output, it is a fix part of the Message dispatcher.
static int mDispatchBits
          Only this bits are used to indicate the destination via some Bits
static int mDispatchInCallingThread
          If this bit is set in the bitmask for dispatching, the dispatching should only be done in the calling thread.
static int mDispatchInDispatcherThread
          If this bit is set in the bitmask for dispatching, the dispatching should be done in the dispatcher Thread.
private  int mDstMixedOutputs
          Calculated mask of bits which are able to mix.
private  int mDstOneOutput
          Calculated mask of bits which are one index.
static int mRemove
          Used for argument mode from setOutputRange(int, int, int, int, int) to remove an output.
static int mSet
          Used for argument mode from setOutputRange(int, int, int, int, int) to set an output.
private  int nrofMixedOutputs
          Number of Bits in mDispatchWithBits, it is the number of destinations dispached via bit mask.
 LogMessage outputConsole
          A console output is standard everytime..
private  MsgDispatcher.Output[] outputs
          up to 30 destinations for output.
(package private)  MsgDispatcher.TestCnt testCnt
           
 
Constructor Summary
MsgDispatcher(int maxDispatchEntries, int maxQueue, int maxOutputs, int nrofMixedOutputs)
          Initializes the instance.
 
Method Summary
 void close()
          close and flush forces the dispatching of the messages in the queue.
private  int completeDispatchInThreadBits(int dstBits)
          Completes a destination bit mask with the information, whether any destinations are used in the calling thread or in the dispatcher thread.
private  int dispatchMsg(int dstBits, boolean bDispatchInDispatcherThread, int identNumber, OS_TimeStamp creationTime, java.lang.String text, Va_list args)
          Dispatches a message.
 int dispatchQueuedMsg()
          Dispatches all messages, which are stored in the queue.
 void flush()
          flush forces the dispatching of the messages in the queue.
 ConcurrentLinkedQueue<MsgDispatcher.Entry> getSharedFreeEntries()
          Gets the internal free entries for sharing with an other log output, at example LogMessageFile.
private  int insertIdent(int idx, int fromIdent, int toIdent)
          inserts an ident range after given position: 1 means any stored ident number, 7 means a number behind, i,j means the inserted idents.
 boolean isOnline()
          Checks whether the message output is available.
 boolean reportOutput(FileWriter file)
          Writes the msg dispatching outputs in file.
private  int searchDispatchBits(int ident)
           
 boolean sendMsg(int identNumber, java.lang.String text, java.lang.Object... args)
          Sends a message.
 boolean sendMsgTime(int identNumber, OS_TimeStamp creationTime, java.lang.String text, java.lang.Object... args)
          Sends a message.
 boolean sendMsgVaList(int identNumber, OS_TimeStamp creationTime, java.lang.String text, Va_list args)
          Sends a message.
 void setDefaults(java.lang.String fileOut)
           
 java.lang.String setOutputFromString(java.lang.String ctrl, java.lang.StringBuffer errorBuffer)
          Sets the output from a String content.
 int setOutputRange(int fromIdent, int toIdent, int dst, int mode, int level)
          Sets the output dispatch bits for the given message number range.
 void setOutputRoutine(int dstIdx, java.lang.String name, boolean bQueued, LogMessage dst)
          Sets a destination interface to a index for dispatching.
(package private)  void stop()
          It's a debug helper.
 void tickAndFlushOrClose()
          Outputs the queued messages calling LogMessage.flush() for all queued outputs.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

mDispatchInDispatcherThread

public static final int mDispatchInDispatcherThread
If this bit is set in the bitmask for dispatching, the dispatching should be done in the dispatcher Thread. In the calling thread the message is stored in a queue.

See Also:
Constant Field Values

mDispatchInCallingThread

public static final int mDispatchInCallingThread
If this bit is set in the bitmask for dispatching, the dispatching should only be done in the calling thread. It is possible that also the bit mDispatchInDispatcherThread is set, if there is more as one destination.

See Also:
Constant Field Values

mDispatchBits

public static final int mDispatchBits
Only this bits are used to indicate the destination via some Bits

See Also:
Constant Field Values

nrofMixedOutputs

private final int nrofMixedOutputs
Number of Bits in mDispatchWithBits, it is the number of destinations dispached via bit mask.


mDstMixedOutputs

private final int mDstMixedOutputs
Calculated mask of bits which are able to mix.


mDstOneOutput

private final int mDstOneOutput
Calculated mask of bits which are one index.


mConsole

public static final int mConsole
Mask for dispatch the message to console directly in the calling thread. It is like system.out.println(...) respectively printf in C. The console output is a fix part of the Message dispatcher.

See Also:
Constant Field Values

mConsoleQueued

public static final int mConsoleQueued
queued Console output, it is a fix part of the Message dispatcher.

See Also:
Constant Field Values

mAdd

public static final int mAdd
Used for argument mode from setOutputRange(int, int, int, int, int) to add an output. The other set outputs aren't change.

See Also:
Constant Field Values

mSet

public static final int mSet
Used for argument mode from setOutputRange(int, int, int, int, int) to set an output. Outputs before are removed.

See Also:
Constant Field Values

mRemove

public static final int mRemove
Used for argument mode from setOutputRange(int, int, int, int, int) to remove an output. All other outputs aren't change.

See Also:
Constant Field Values

testCnt

final MsgDispatcher.TestCnt testCnt

listOrders

final ConcurrentLinkedQueue<MsgDispatcher.Entry> listOrders
List of messages to process in the dispatcher thread.


freeOrders

final ConcurrentLinkedQueue<MsgDispatcher.Entry> freeOrders
List of entries for messages to use.


actNrofListIdents

private int actNrofListIdents
List of idents, its current length.


listIdents

private int[] listIdents
List of idents, a array with lengthListIdents elements.


listBitDst

private int[] listBitDst
List of destination bits for the idents.


outputs

private MsgDispatcher.Output[] outputs
up to 30 destinations for output.


outputConsole

public final LogMessage outputConsole
A console output is standard everytime..


maxDst

private int maxDst
Constructor Detail

MsgDispatcher

public MsgDispatcher(int maxDispatchEntries,
                     int maxQueue,
                     int maxOutputs,
                     int nrofMixedOutputs)
Initializes the instance.

Parameters:
maxDispatchEntries - The max number of message-ident-number ranges which are dispatched to different outputs. It is a static limited data amount. If you have about 1000 different message-ident-numbers, 1000 is okay. If you have some more different message-ident-numbers, but with determined ranges, which are always dispatched to the same destinations, that number of ranges is okay. In other words, it is the maximal number of lines in a message dispatched file, if all message ranges are set new.
maxQueue - The static limited maximal size of the queue to store messages from user threads to dispatch in the dispatcher thread. If you call the dispatching in dispatcher thread cyclicly in 100-ms-steps, and you have worst case no more as 200 messages in this time, 200 is okay.
maxOutputs - The static limited maximal number of outputs.
nrofMixedOutputs -
Method Detail

getSharedFreeEntries

public final ConcurrentLinkedQueue<MsgDispatcher.Entry> getSharedFreeEntries()
Gets the internal free entries for sharing with an other log output, at example LogMessageFile.


setDefaults

public final void setDefaults(java.lang.String fileOut)

searchDispatchBits

private final int searchDispatchBits(int ident)

insertIdent

private final int insertIdent(int idx,
                              int fromIdent,
                              int toIdent)
inserts an ident range after given position:
 1 means any stored ident number, 7 means a number behind,
 i,j means the inserted idents. The idx selects the 1.-ident.
 before: ....1.7......
 after:  ....1.i.j.7..
If fromIdent == toIdent, only one position is inserted:
 before: ....1.7....
 after:  ....1.j.7..

Parameters:
idx - Index, after them the ident is inserted.
fromIdent - first value of the ident.
toIdent - second value of the ident.
mask - bit to or

setOutputRoutine

public final void setOutputRoutine(int dstIdx,
                                   java.lang.String name,
                                   boolean bQueued,
                                   LogMessage dst)
Sets a destination interface to a index for dispatching.

Parameters:
dstIdx - The index 0..28 or greater, which is associated to the destination.
bQueued - true than the message is queued if this destination is used.
outputs - The destination for message or log output.

setOutputRange

public final int setOutputRange(int fromIdent,
                                int toIdent,
                                int dst,
                                int mode,
                                int level)
Sets the output dispatch bits for the given message number range.

Parameters:
fromIdent - The first ident number for which the dispatch bit is valid.
toIdent - The last ident number for which the dispatch bit is valid.
dst - Bit or index for the destination for the given output range. An or-value #mDispatchWithIndex can be added. If this Bit is set, the rest of bits are the index of the output, greater as nrofMixedOutputs. In this case mode should be mSet.
If the bit #mDispatchWithIndex isn't set, the bits 0..23 describe one or more outputs.
If the bit combination of dst isn't valid, an invalidArgumentException is thrown.
mode - Ones of mAdd, mSet, mRemove to presribe, what to do with dst.
level -
Returns:

setOutputFromString

public final java.lang.String setOutputFromString(java.lang.String ctrl,
                                                  java.lang.StringBuffer errorBuffer)
Sets the output from a String content. Syntax-Example:
 123..512:0x27  //It is a bit mask, all messages from 123 to inclusive 521 are set to channels 0,1,2 and 5  
 1001:+CON.      //Message 1001 is sent additional to CON
 

Parameters:
ctrl - The control String.
errorBuffer - A Buffer to assign an error String: This method doesn't allocate any memory, therefore no String concatenation is done. Instead an error String is assembled in the buffer using append. This argument may be null, than only a short hint of error is returned as constant string or as substring from ctrl.
Returns:
null if all ok, otherwise an error hint. The errorBuffer contains an particularly text.

reportOutput

public final boolean reportOutput(FileWriter file)
Writes the msg dispatching outputs in file. The form is the same which are used for setOutputFromString(String, StringBuffer).

Parameters:
file - The file should be opened already. Hint: A simple writer isn't used because Java2C don't able to translate it yet. (Version 0.84)
Returns:
true if no error.

completeDispatchInThreadBits

private final int completeDispatchInThreadBits(int dstBits)
Completes a destination bit mask with the information, whether any destinations are used in the calling thread or in the dispatcher thread.

Parameters:
dstBits - The input mask
Returns:
returns the same bits as in input mask, but with Bits mDispatchInDispatcherThread and/or mDispatchInCallingThread

dispatchMsg

private final int dispatchMsg(int dstBits,
                              boolean bDispatchInDispatcherThread,
                              int identNumber,
                              OS_TimeStamp creationTime,
                              java.lang.String text,
                              Va_list args)
Dispatches a message. This routine is called either in the calling thread of the message or in the dispatcher thread.

Parameters:
dstBits - Destination identificator. If the bit mDispatchInDispatcherThread is set, the dispatching should be done only for a destination if the destination is valid for the dispatcher thread. Elsewhere if the bit is 0, the dispatching should be done only for a destination if the destination is valid for the calling thread.
bDispatchInDispatcherThread - true if this method is called in dispatcher thread, false if called in calling thread. This param is compared with MsgDispatcher.Output.dstInDispatcherThread, only if it is equal with them, the message is outputted.
identNumber - identification of the message.
creationTime -
text - The identifier text @pjava2c=zeroTermString.
args - @pjava2c=nonPersistent.
Returns:
0 if all destinations are processed, elsewhere dstBits with bits of non-processed dst.

sendMsg

public final boolean sendMsg(int identNumber,
                             java.lang.String text,
                             java.lang.Object... args)
Sends a message. See interface.

Specified by:
sendMsg in interface LogMessage
Parameters:
identNumber -
text - The text representation of the message, format string, see java.lang.String.format(..).
args - see interface
Returns:
TODO

sendMsgTime

public final boolean sendMsgTime(int identNumber,
                                 OS_TimeStamp creationTime,
                                 java.lang.String text,
                                 java.lang.Object... args)
Sends a message. See interface.

Specified by:
sendMsgTime in interface LogMessage
Parameters:
identNumber -
text - The text representation of the message, format string, see java.lang.String.format(..).
args - see interface
creationTime - absolute time stamp. @Java2C=perValue.
Returns:
TODO

sendMsgVaList

public final boolean sendMsgVaList(int identNumber,
                                   OS_TimeStamp creationTime,
                                   java.lang.String text,
                                   Va_list args)
Sends a message. See interface.

Specified by:
sendMsgVaList in interface LogMessage
Parameters:
identNumber -
creationTime -
text - The identifier text @pjava2c=zeroTermString.
typeArgs - Type chars, ZCBSIJFD for boolean, char, byte, short, int, long, float double.
args - see interface
Returns:
true than okay. It is possible, that a destination for dispatching is not available yet. Than the routine returns false. That is for special outputs of message dispatcher. Normally the user shouldn't realize false here and react anywise. If a message isn't able to transport, it is not visible in the creating thread. It is possible that a message is lost anywhere in transportation way. In Generally, to secure a complex systems functionality, any timeouts, repeats and backup strategies are necessary in the supervise software above sending a single message.

dispatchQueuedMsg

public final int dispatchQueuedMsg()
Dispatches all messages, which are stored in the queue. This routine should be called in any user thread respectively it is called in TODOx1.

Returns:
number of messages which are found to dispatch, for statistic use.

stop

final void stop()
It's a debug helper. The method is empty, but it is a mark to set a breakpoint.


close

public void close()
close and flush forces the dispatching of the messages in the queue.

Specified by:
close in interface LogMessage
See Also:
org.vishia.util.LogMessage#close()

flush

public void flush()
flush forces the dispatching of the messages in the queue.

Specified by:
flush in interface LogMessage
See Also:
org.vishia.util.LogMessage#close()

isOnline

public boolean isOnline()
Description copied from interface: LogMessage
Checks whether the message output is available.

Specified by:
isOnline in interface LogMessage

tickAndFlushOrClose

public final void tickAndFlushOrClose()
Outputs the queued messages calling LogMessage.flush() for all queued outputs. This method can be called in the dispatcher thread cyclically, as opposite to the Thread.start(), where this routine is called too.