Package net.sf.freecol.common.networking
Class Message
- java.lang.Object
-
- net.sf.freecol.common.networking.Message
-
- Direct Known Subclasses:
TrivialMessage
public abstract class Message extends java.lang.ObjectBase abstract class for all messages.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMessage.MessagePriority
-
Field Summary
Fields Modifier and Type Field Description private static java.util.Map<java.lang.String,java.lang.reflect.Constructor<? extends Message>>buildersA map of message name to message constructors, built on the fly as new messages are encountered and suitable constructors found.protected static java.util.logging.Loggerloggerstatic java.util.Comparator<Message>messagePriorityComparatorComparator comparing by message priority.private static java.lang.Class[]readClassesClasses used by Message.read()
-
Constructor Summary
Constructors Modifier Constructor Description protectedMessage()Deliberately trivial constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract voidaiHandler(FreeColServer freeColServer, AIPlayer aiPlayer)AI-side handler for this message.protected abstract <T extends FreeColObject>
voidappendChild(T fco)Append a new child.protected abstract <T extends FreeColObject>
voidappendChildren(java.util.Collection<T> fcos)Append a multiple new children.booleancanMerge()Does this message consist only of mergeable attributes?protected voidclientGeneric(FreeColClient freeColClient)Create a runnable to execute generic message actions.abstract voidclientHandler(FreeColClient freeColClient)Client-side handler for this message.abstract booleancurrentPlayerMessage()Should this message only be sent to a server by the current player?protected voidexpected(java.lang.String wanted, java.lang.String got)Complain about finding the wrong XML element.protected java.util.List<java.lang.String>getArrayAttributes()Get the array attributes of this message.protected java.lang.BooleangetBooleanAttribute(java.lang.String key, java.lang.Boolean defaultValue)Get a boolean attribute value.protected <T extends FreeColObject>
TgetChild(int index, java.lang.Class<T> returnClass)Get a child object.protected abstract intgetChildCount()Get the number of child objects.protected abstract java.util.List<FreeColObject>getChildren()Get the child objects of this message.protected <T extends FreeColObject>
java.util.List<T>getChildren(java.lang.Class<T> returnClass)Get the child objects.protected <T extends java.lang.Enum<T>>
TgetEnumAttribute(java.lang.String key, java.lang.Class<T> returnClass, T defaultValue)Gets an enum attribute value.protected java.lang.IntegergetIntegerAttribute(java.lang.String key, int defaultValue)Get an integer attribute value.abstract Message.MessagePrioritygetPriority()Get the priority of this type of message.intgetPriorityLevel()Get the priority level of this type of message.protected abstract java.lang.StringgetStringAttribute(java.lang.String key)Get a string attribute value.protected abstract java.util.Map<java.lang.String,java.lang.String>getStringAttributeMap()Get a map of all the attributes in this message.abstract java.lang.StringgetType()Get the message tag.protected abstract booleanhasAttribute(java.lang.String key)Checks if an attribute is present in this message.protected InGameControllerigc(FreeColClient freeColClient)protected InGameControllerigc(FreeColServer freeColServer)protected voidinvokeAndWait(FreeColClient freeColClient, java.lang.Runnable runnable)protected voidinvokeLater(FreeColClient freeColClient, java.lang.Runnable runnable)protected booleanisEmpty()Is this message vacuous?booleanisType(java.lang.String type)Checks if this message is of a given type.booleanmerge(Message message)Merge another message into this message if possible.protected PreGameControllerpgc(FreeColClient freeColClient)protected PreGameControllerpgc(FreeColServer freeColServer)protected static voidpretty(java.lang.StringBuilder sb, java.lang.String type, java.util.Map<java.lang.String,java.lang.String> attributeMap, java.util.List<FreeColObject> children)Pretty print the components of a message to a string builder.static Messageread(Game game, FreeColXMLReader xr)Read a new message from a stream.abstract ChangeSetserverHandler(FreeColServer freeColServer, ServerPlayer serverPlayer)Server-side handler for this message.protected voidsetArrayAttributes(java.lang.String[] attributes)Set an array of attributes.protected voidsetArrayAttributes(java.util.List<java.lang.String> attributes)Set a list of attributes as an array.protected voidsetBooleanAttribute(java.lang.String key, java.lang.Boolean value)Sets an attribute in this message with a boolean value.protected abstract voidsetChildren(java.util.List<? extends FreeColObject> fcos)Set the list of objects attached to this message.protected voidsetEnumAttribute(java.lang.String key, java.lang.Enum<?> value)Sets an attribute in this message with an enum value.protected voidsetIntegerAttribute(java.lang.String key, int value)Sets an attribute in this message with an integer value.protected abstract voidsetStringAttribute(java.lang.String key, java.lang.String value)Sets an attribute in this message.protected voidsetStringAttributeMap(java.util.Map<java.lang.String,java.lang.String> attributeMap)Set all the attributes in a map.protected voidsetStringAttributes(java.lang.String[] attributes)Set all the attributes from an array.protected voidsetStringAttributes(java.util.List<java.lang.String> attributes)Set all the attributes from a list.protected abstract voidsetType(java.lang.String type)Set the message tag.java.lang.StringtoString()voidtoXML(FreeColXMLWriter xw)Write this message as XML.protected voidwriteAttributes(FreeColXMLWriter xw)Write any attributes of this message.protected voidwriteChildren(FreeColXMLWriter xw)Write any children of this message.
-
-
-
Field Detail
-
logger
protected static final java.util.logging.Logger logger
-
builders
private static final java.util.Map<java.lang.String,java.lang.reflect.Constructor<? extends Message>> builders
A map of message name to message constructors, built on the fly as new messages are encountered and suitable constructors found.
-
readClasses
private static final java.lang.Class[] readClasses
Classes used by Message.read()
-
messagePriorityComparator
public static final java.util.Comparator<Message> messagePriorityComparator
Comparator comparing by message priority.
-
-
Method Detail
-
getType
public abstract java.lang.String getType()
Get the message tag.- Returns:
- The message tag.
-
setType
protected abstract void setType(java.lang.String type)
Set the message tag.- Parameters:
type- The new message tag.
-
hasAttribute
protected abstract boolean hasAttribute(java.lang.String key)
Checks if an attribute is present in this message.- Parameters:
key- The attribute to look for.- Returns:
- True if the attribute is present.
-
getStringAttribute
protected abstract java.lang.String getStringAttribute(java.lang.String key)
Get a string attribute value.- Parameters:
key- The attribute to look for.- Returns:
- The string value found, or null if the attribute was absent.
-
setStringAttribute
protected abstract void setStringAttribute(java.lang.String key, java.lang.String value)Sets an attribute in this message.- Parameters:
key- The attribute to set.value- The new value of the attribute.
-
getStringAttributeMap
protected abstract java.util.Map<java.lang.String,java.lang.String> getStringAttributeMap()
Get a map of all the attributes in this message.- Returns:
- A
Mapof the attributes.
-
getChildCount
protected abstract int getChildCount()
Get the number of child objects.- Returns:
- The child count.
-
getChildren
protected abstract java.util.List<FreeColObject> getChildren()
Get the child objects of this message.- Returns:
- A list of child
FreeColObjects.
-
setChildren
protected abstract void setChildren(java.util.List<? extends FreeColObject> fcos)
Set the list of objects attached to this message.- Parameters:
fcos- The new list of attachedFreeColObjects.
-
appendChild
protected abstract <T extends FreeColObject> void appendChild(T fco)
Append a new child.- Type Parameters:
T- The child type.- Parameters:
fco- The new child object.
-
appendChildren
protected abstract <T extends FreeColObject> void appendChildren(java.util.Collection<T> fcos)
Append a multiple new children.- Type Parameters:
T- The child type.- Parameters:
fcos- The new child objects.
-
currentPlayerMessage
public abstract boolean currentPlayerMessage()
Should this message only be sent to a server by the current player?- Returns:
- True if this is a current-player-only message.
-
getPriority
public abstract Message.MessagePriority getPriority()
Get the priority of this type of message.- Returns:
- The message priority.
-
canMerge
public boolean canMerge()
Does this message consist only of mergeable attributes?- Returns:
- True if this message is trivially mergeable.
-
aiHandler
public abstract void aiHandler(FreeColServer freeColServer, AIPlayer aiPlayer) throws FreeColException
AI-side handler for this message. AI handlers always return null. FIXME: One day the FreeColServer should devolve to AIMain.- Parameters:
freeColServer- TheFreeColServerhandling the request.aiPlayer- TheAIPlayerthe message was sent to.- Throws:
FreeColException- if there is a problem handling the message.
-
clientHandler
public abstract void clientHandler(FreeColClient freeColClient) throws FreeColException
Client-side handler for this message. Client handlers always return null.- Parameters:
freeColClient- TheFreeColClientto handle this message.- Throws:
FreeColException- if there is a problem building the message.
-
serverHandler
public abstract ChangeSet serverHandler(FreeColServer freeColServer, ServerPlayer serverPlayer)
Server-side handler for this message.- Parameters:
freeColServer- TheFreeColServerhandling the request.serverPlayer- TheServerPlayerthat sent the request.- Returns:
- A
ChangeSetdefining the response.
-
isType
public boolean isType(java.lang.String type)
Checks if this message is of a given type.- Parameters:
type- The type you wish to test against.- Returns:
- True if the type of this message equals the given type.
-
getBooleanAttribute
protected java.lang.Boolean getBooleanAttribute(java.lang.String key, java.lang.Boolean defaultValue)Get a boolean attribute value.- Parameters:
key- The attribute to look for.defaultValue- The fallback result.- Returns:
- The boolean value, or the default value if no boolean is found.
-
getIntegerAttribute
protected java.lang.Integer getIntegerAttribute(java.lang.String key, int defaultValue)Get an integer attribute value.- Parameters:
key- The attribute to look for.defaultValue- The fallback result.- Returns:
- The integer value, or the default value if no integer is found.
-
getEnumAttribute
protected <T extends java.lang.Enum<T>> T getEnumAttribute(java.lang.String key, java.lang.Class<T> returnClass, T defaultValue)Gets an enum attribute value.- Type Parameters:
T- The expected enum type.- Parameters:
key- The attribute name.returnClass- The class of the return value.defaultValue- The default value.- Returns:
- The enum attribute value, or the default value if none found.
-
setBooleanAttribute
protected void setBooleanAttribute(java.lang.String key, java.lang.Boolean value)Sets an attribute in this message with a boolean value.- Parameters:
key- The attribute to set.value- The value of the attribute.
-
setEnumAttribute
protected void setEnumAttribute(java.lang.String key, java.lang.Enum<?> value)Sets an attribute in this message with an enum value.- Parameters:
key- The attribute to set.value- The value of the attribute.
-
setIntegerAttribute
protected void setIntegerAttribute(java.lang.String key, int value)Sets an attribute in this message with an integer value.- Parameters:
key- The attribute to set.value- The value of the attribute.
-
setStringAttributeMap
protected void setStringAttributeMap(java.util.Map<java.lang.String,java.lang.String> attributeMap)
Set all the attributes in a map.- Parameters:
attributeMap- The map of key,value pairs to set.
-
setStringAttributes
protected void setStringAttributes(java.util.List<java.lang.String> attributes)
Set all the attributes from a list.- Parameters:
attributes- A list of alternating key,value pairs.
-
setStringAttributes
protected void setStringAttributes(java.lang.String[] attributes)
Set all the attributes from an array.- Parameters:
attributes- An array of alternating key,value pairs.
-
getArrayAttributes
protected java.util.List<java.lang.String> getArrayAttributes()
Get the array attributes of this message.- Returns:
- A list of the array attributes found.
-
setArrayAttributes
protected void setArrayAttributes(java.util.List<java.lang.String> attributes)
Set a list of attributes as an array.- Parameters:
attributes- A list of attribute values.
-
setArrayAttributes
protected void setArrayAttributes(java.lang.String[] attributes)
Set an array of attributes.- Parameters:
attributes- The array of attributes.
-
getChild
protected <T extends FreeColObject> T getChild(int index, java.lang.Class<T> returnClass)
Get a child object.- Type Parameters:
T- The actual class ofFreeColObjectto get.- Parameters:
index- The index of the child to get.returnClass- The expected class of child.- Returns:
- The child object found, or null if the index is invalid or return class incorrect.
-
getChildren
protected <T extends FreeColObject> java.util.List<T> getChildren(java.lang.Class<T> returnClass)
Get the child objects.- Type Parameters:
T- The actual class ofFreeColObjectto get.- Parameters:
returnClass- The expected class of children.- Returns:
- The children with the expected class.
-
isEmpty
protected boolean isEmpty()
Is this message vacuous?- Returns:
- True if there are no attributes or children present.
-
getPriorityLevel
public final int getPriorityLevel()
Get the priority level of this type of message.- Returns:
- The message priority level.
-
merge
public boolean merge(Message message)
Merge another message into this message if possible.- Parameters:
message- TheMessageto merge.- Returns:
- True if the other message was merged.
-
writeAttributes
protected void writeAttributes(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write any attributes of this message.- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem writing the stream.
-
writeChildren
protected void writeChildren(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write any children of this message.- Parameters:
xw- TheFreeColXMLWriterto write to.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem writing the stream.
-
toXML
public void toXML(FreeColXMLWriter xw) throws javax.xml.stream.XMLStreamException
Write this message as XML.- Parameters:
xw- TheFreeColXMLWriterto write with.- Throws:
javax.xml.stream.XMLStreamException- if there is a problem writing the stream.
-
igc
protected InGameController igc(FreeColClient freeColClient)
-
igc
protected InGameController igc(FreeColServer freeColServer)
-
invokeAndWait
protected void invokeAndWait(FreeColClient freeColClient, java.lang.Runnable runnable)
-
invokeLater
protected void invokeLater(FreeColClient freeColClient, java.lang.Runnable runnable)
-
pgc
protected PreGameController pgc(FreeColClient freeColClient)
-
pgc
protected PreGameController pgc(FreeColServer freeColServer)
-
clientGeneric
protected void clientGeneric(FreeColClient freeColClient)
Create a runnable to execute generic message actions.- Parameters:
freeColClient- The client.
-
expected
protected void expected(java.lang.String wanted, java.lang.String got) throws javax.xml.stream.XMLStreamExceptionComplain about finding the wrong XML element.- Parameters:
wanted- The tag we wanted.got- The tag we got.- Throws:
javax.xml.stream.XMLStreamException- is always thrown.
-
read
public static Message read(Game game, FreeColXMLReader xr) throws FreeColException
Read a new message from a stream.- Parameters:
game- TheGamewithin which to construct the message.xr- AFreeColXMLReaderto read from.- Returns:
- The new
Message. - Throws:
FreeColException- if there is problem reading the message.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
pretty
protected static void pretty(java.lang.StringBuilder sb, java.lang.String type, java.util.Map<java.lang.String,java.lang.String> attributeMap, java.util.List<FreeColObject> children)Pretty print the components of a message to a string builder.- Parameters:
sb- TheStringBuilderto print to.type- The type of the message.attributeMap- A map of key,value attribute pairs.children- The attached childFreeColObjects.
-
-