<chapter>
<title>DRAFT: WAP box</title>

	<para>This chapter describes the internal workings of the WAP
	box: how state machines are implemented, other major code modules,
	threads, communication with WML content servers, how WML and
	WMLScript are converted to a binary format, timeouts with HTTP
	requests and other external communications, how to deal with a
	broken bearer box connection, how the box is started automatically.
	</para>
	
	<para>This chapter will be written by Lars and Aarno.</para>

<para><literallayout>
Each transaction and session is represented by a
data structure that implements (together with the static state
machine engine code) the state machine for that layer. Each new
WDP packet is handled by a new thread in a WAP box. That thread
finds the correct WTP state machine or creates a new one. It then
feeds the WDP packet as an event to the WTP state machine. If
that state machine needs to deal with a session, it calls the WSP
engine code to find or create a WSP state machine, and feeds the
suitable events to that state machine. This is all handled by the
same thread that was originally created to handle the WDP packet.
And similarly, if the WSP state machine needs to give events
to a WTP state machine, it is handled by the same thread. Once
all the events that are spawned by the WDP packet are handled,
the thread dies.

It is possible that bursts of WDP packets arrive faster
than they can be processed. In that case, normal locking of data
structures ensures that only one packet at a time is handled,
and the rest are queued. In fact, if the newly created thread for
a new WDP packet notices it can't process the packet, because the
corresponding WTP state machine is still locked by the previous
packet, it will put the event in a WTP state machine specific
queue and terminate. The thread that has the lock will, after
it has processed its packet (or other event), look at the state
machine's pending event queue and process the packets there. This
way, there need not be unnecessary threads. This is important
when there is lots of traffic due to lots of users: it is important
that the thread table of the operating system is not filled.
	


types of events (WTP):
WTP PDU's: different event for each PDU type
WSP service primitives
timer signals

types of events (WSP):
WTP service primitives
pseudo-events generated by the WSP/WAP box implementation
http request events?

data structures.
WTPEvent
WTPMachine
WSPSessionEvent
WSPSessionMachine
WSPMethodEvent
WSPMethodMachine

algorithm (WTP engine):
read message from bearer box
convert it into a WTPEvent
find or create the appropriate WTPMachine
feed the WTPEvent to the WTPMachine
the WTPMachine handles the event according to the state tables in the WAP spec
if necessary, the WTPMachine sends events to WSP engine

algorithm (WSP engine):
get WSPEvent from WTP engine
find or create the appropriate WSPSessionMachine or WSPMethodMachine
feed the WSPEvent to the state machine
the state machine handles the event according to state tables in WAP spec

coding strategy for WTP and WSP engines:
like for messages
</literallayout></para>

</chapter>

