eap.freeciv
Class BinaryField

java.lang.Object
  extended byeap.freeciv.Field
      extended byeap.freeciv.BinaryField

public class BinaryField
extends Field


Constructor Summary
BinaryField()
           
 
Method Summary
 void addIndex(int index)
          Alter the way this field references its data, so as to append an index.
 int getSize(Packet packet)
          Returns the size of this field in bytes, given the values stored in a paket.
 boolean isSet(Packet packet)
          Returns true if the value(s) for this field are set in the given packet
 void read(Packet packet, PacketSource source)
          Read the field from the given source and write its contents to the given packet.
 void removeIndex()
          Reverse the effects of the last call to addIndex(int)
 void setName(String name)
           
 void setSize(String size)
           
 void write(Packet packet, OutputStream out)
          Write this field to the given stream, taking values from the given packet.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

BinaryField

public BinaryField()
Method Detail

setName

public void setName(String name)

addIndex

public void addIndex(int index)
Description copied from class: Field
Alter the way this field references its data, so as to append an index. Exactly how this is done depends on the subclass. The index can later be removed with Field.removeIndex(). Multiple calls to this method are cumulative - i.e. a field can have multiple indices.

This is used by arrays. The typical procedure is to iterate over array elements, set the index in each component field, perform some operation, and then remove the index. The nice thing about this way of doing things is that an array can contain any type of field, including other arrays. The drawback is that you have to be very careful that the indices always get removed after use. Otherwise the structure could get into an invalid state. A related problem is that it violates the rule that packet structures are reusable, since two separate threads could be trying to add indices to the same field at the same time. So far this hasn't been a problem, since the indices stay on the field for a very short time. Also, in practice one thread handles client to server trafic and the other handles server to client trafic, and these generally use different packet types. Initially I set the index in the packet, but I ripped that out because it set the array for all fields. But to be thread safe I would have to go back to something like that, but specific to one keyword.

Specified by:
addIndex in class Field
See Also:
ArrayField

removeIndex

public void removeIndex()
Description copied from class: Field
Reverse the effects of the last call to Field.addIndex(int)

Specified by:
removeIndex in class Field

isSet

public boolean isSet(Packet packet)
Description copied from class: Field
Returns true if the value(s) for this field are set in the given packet

Specified by:
isSet in class Field

setSize

public void setSize(String size)

read

public void read(Packet packet,
                 PacketSource source)
          throws IOException
Description copied from class: Field
Read the field from the given source and write its contents to the given packet. Remember that a packet is just a storage repository and a structure (composed of fields) describes how the packet is serialized.

Specified by:
read in class Field
Throws:
IOException

write

public void write(Packet packet,
                  OutputStream out)
           throws IOException
Description copied from class: Field
Write this field to the given stream, taking values from the given packet.

Specified by:
write in class Field
Throws:
IOException

getSize

public int getSize(Packet packet)
Description copied from class: Field
Returns the size of this field in bytes, given the values stored in a paket. Many fields have a fixed size, but some do not and need to reference the packet data.

Specified by:
getSize in class Field