net.sf.freecol.common.networking
Class DiplomacyMessage

java.lang.Object
  extended by net.sf.freecol.common.networking.Message
      extended by net.sf.freecol.common.networking.DiplomacyMessage

public class DiplomacyMessage
extends Message

The message sent when executing a diplomatic trade.


Nested Class Summary
static class DiplomacyMessage.TradeStatus
          A type for the agreement status.
 
Field Summary
private  DiplomaticTrade agreement
          The trade to make.
(package private) static Turn agreementTurn
           
private  java.lang.String directionString
          The direction the trader is looking.
(package private) static java.util.List<DiplomacyMessage> savedAgreements
          A place to remember what agreements are in progress but not complete.
private  DiplomacyMessage.TradeStatus status
          The agreement status.
private  java.lang.String unitId
          The id of the object doing the trading.
 
Fields inherited from class net.sf.freecol.common.networking.Message
document, logger
 
Constructor Summary
DiplomacyMessage(Game game, org.w3c.dom.Element element)
          Create a new DiplomacyMessage from a supplied element.
DiplomacyMessage(Unit unit, Map.Direction direction, DiplomaticTrade agreement)
          Create a new DiplomacyMessage with the supplied unit, direction and agreement.
 
Method Summary
private  org.w3c.dom.Element createUpdateFor(Player player, java.util.List<FreeColGameObject> objects)
          Create an "update" message for a given player, from the objects that have been transferred in a successful diplomatic agreement.
private  void flushAgreements(Turn turn)
           
 DiplomaticTrade getAgreement()
          Get the agreement (a DiplomaticTrade) in this message.
 java.lang.String getOtherNationName(Player player)
          Get the name of this message's other nation as a string.
 Settlement getSettlement()
          Get the Settlement at which a diplomatic exchange happens.
 Unit getUnit()
          Get the Unit which began this diplomatic exchange.
static java.lang.String getXMLElementTagName()
          The tag name of the root element representing this object.
 org.w3c.dom.Element handle(FreeColServer server, Connection connection)
          Handle a "diplomacy"-message.
 boolean isAccept()
          Does this message indicate agreement to the trade?
 boolean isReject()
          Does this message indicate rejection of the trade?
private  boolean isSameTransaction(DiplomacyMessage message)
          Do DiplomacyMessages match enough to have come from the same transaction? The checking of all fields is probably overkill, but provides some insulation from misbehaving clients.
private  boolean isValidAcceptance(DiplomacyMessage message)
          Is an incoming DiplomacyMessage a valid acceptance of this message? To be valid, the response should be equivalent to the proposal, but with "accept" set.
private  boolean isValidCounterProposal(DiplomacyMessage message)
          Is an incoming DiplomacyMessage a valid counter-proposal? To be valid, the response should be equivalent to the proposal, but with different items to trade (alas, omitted), and be a trade proposal.
private  DiplomacyMessage loadAgreement(DiplomacyMessage message, Turn turn)
           
private  void saveAgreement(DiplomacyMessage message, Turn turn)
           
private  void sendUpdates(ServerPlayer player, ServerPlayer enemy, java.util.List<FreeColGameObject> objects)
          Perform an agreed trade, create updates for original player and the enemy, containing their view of the objects transferred, and send them.
 void setAccept()
          Mark a trade as accepted.
 void setAgreement(DiplomaticTrade agreement)
          Set the agreement (a DiplomaticTrade) in this message.
 void setReject()
          Mark a trade as rejected.
 org.w3c.dom.Element toXMLElement()
          Convert this DiplomacyMessage to XML.
 
Methods inherited from class net.sf.freecol.common.networking.Message
clientError, createError, createError, createNewDocument, createNewRootElement, getAttribute, getChildElement, getDocument, getFreeColProtocolVersion, getType, hasAttribute, insertAsRoot, isType, setAttribute, setAttribute, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

savedAgreements

static java.util.List<DiplomacyMessage> savedAgreements
A place to remember what agreements are in progress but not complete.


agreementTurn

static Turn agreementTurn

unitId

private java.lang.String unitId
The id of the object doing the trading.


directionString

private java.lang.String directionString
The direction the trader is looking.


agreement

private DiplomaticTrade agreement
The trade to make.


status

private DiplomacyMessage.TradeStatus status
The agreement status.

Constructor Detail

DiplomacyMessage

public DiplomacyMessage(Unit unit,
                        Map.Direction direction,
                        DiplomaticTrade agreement)
Create a new DiplomacyMessage with the supplied unit, direction and agreement.

Parameters:
unit - The Unit that is spying.
direction - The Direction the unit is looking.
agreement - The DiplomaticTrade to make.

DiplomacyMessage

public DiplomacyMessage(Game game,
                        org.w3c.dom.Element element)
Create a new DiplomacyMessage from a supplied element.

Parameters:
game - The Game this message belongs to.
element - The Element to use to create the message.
Method Detail

getUnit

public Unit getUnit()
Get the Unit which began this diplomatic exchange. This is a helper routine to be called in-client as it blindly trusts its field.

Returns:
The unit, or null if none.

getSettlement

public Settlement getSettlement()
Get the Settlement at which a diplomatic exchange happens. This is a helper routine to be called in-client as it blindly trusts all fields.

Returns:
The settlement, or null if none.

getOtherNationName

public java.lang.String getOtherNationName(Player player)
Get the name of this message's other nation as a string.

Parameters:
player - This nation's player.
Returns:
The name of the other nation.

getAgreement

public DiplomaticTrade getAgreement()
Get the agreement (a DiplomaticTrade) in this message.

Returns:
The agreement in this message.

setAgreement

public void setAgreement(DiplomaticTrade agreement)
Set the agreement (a DiplomaticTrade) in this message.


isAccept

public boolean isAccept()
Does this message indicate agreement to the trade?

Returns:
True if the agreement has been agreed to.

setAccept

public void setAccept()
Mark a trade as accepted.


isReject

public boolean isReject()
Does this message indicate rejection of the trade?

Returns:
True if the agreement has been rejected.

setReject

public void setReject()
Mark a trade as rejected.


flushAgreements

private void flushAgreements(Turn turn)

saveAgreement

private void saveAgreement(DiplomacyMessage message,
                           Turn turn)

loadAgreement

private DiplomacyMessage loadAgreement(DiplomacyMessage message,
                                       Turn turn)

isSameTransaction

private boolean isSameTransaction(DiplomacyMessage message)
Do DiplomacyMessages match enough to have come from the same transaction? The checking of all fields is probably overkill, but provides some insulation from misbehaving clients. Note that a simple equality test of the agreement items will fail as while they may well be equivalent the IDs will have changed in the course of the round trip to the client which will trick List.equals unless we improve mutual comparison of TradeItems.

Parameters:
message - A DiplomacyMessage to check.
Returns:
True if the message is from the same transaction.

isValidAcceptance

private boolean isValidAcceptance(DiplomacyMessage message)
Is an incoming DiplomacyMessage a valid acceptance of this message? To be valid, the response should be equivalent to the proposal, but with "accept" set.

Parameters:
message - A DiplomacyMessage to examine.
Returns:
True if the message is a valid acceptance.

isValidCounterProposal

private boolean isValidCounterProposal(DiplomacyMessage message)
Is an incoming DiplomacyMessage a valid counter-proposal? To be valid, the response should be equivalent to the proposal, but with different items to trade (alas, omitted), and be a trade proposal.

Parameters:
message - A DiplomacyMessage to examine.
Returns:
True if the message is a valid counter-proposal.

createUpdateFor

private org.w3c.dom.Element createUpdateFor(Player player,
                                            java.util.List<FreeColGameObject> objects)
Create an "update" message for a given player, from the objects that have been transferred in a successful diplomatic agreement.

Parameters:
player - The Player which will receive the update.
objects - The List of FreeColGameObjects transferred.
Returns:
An Element containing a suitable update message, or null if none is required.

sendUpdates

private void sendUpdates(ServerPlayer player,
                         ServerPlayer enemy,
                         java.util.List<FreeColGameObject> objects)
Perform an agreed trade, create updates for original player and the enemy, containing their view of the objects transferred, and send them.

Parameters:
player - The original Player that started this trade.
enemy - The other Player.
objects - A list of objects that need updating.

handle

public org.w3c.dom.Element handle(FreeColServer server,
                                  Connection connection)
Handle a "diplomacy"-message.

Parameters:
server - The FreeColServer that handles the message.
connection - The Connection the message is from.
Returns:
An Element describing the trade with either "accept" or "reject" status, null on trade failure, or an error Element on outright error.

toXMLElement

public org.w3c.dom.Element toXMLElement()
Convert this DiplomacyMessage to XML.

Overrides:
toXMLElement in class Message
Returns:
The XML representation of this message.

getXMLElementTagName

public static java.lang.String getXMLElementTagName()
The tag name of the root element representing this object.

Returns:
"diplomacy".