\section{Telephone Exchange}
\label{phone}

\begin{figure}
\begin{center}

\illustrationx{exchange}{5.0in}

\end{center}
\caption{\label{phone-generic}Generic structure of a telephone
switching exchange.}
\end{figure}

The simple telephone exchange allows any subscriber to request that a speech
connection be set up to any other subscriber.
As shown in Figure~\ref{phone-generic} the exchange has three kinds
of components: phone line units (PLUs), the switch
unit (SWU), and the common control unit (CCU). For simplicity, we
assume that each phone line unit handles just one subscriber. The
state of a PLU notes whether the phone's handset is
{\tt off\_hook} or {\tt on\_hook}, and also makes the phone sound signals:
{\tt ring}, {\tt busy\_tone}, {\tt ring\_tone}.
The switch unit provides circuits for connecting any pair of subscribers. The
common control unit receives request and status messages from the
other units and issues control messages to them.
Here and in the appendix we present an executable specification
of the telephone exchange, written as an {\tt Exchange} class in ObjectSisal.
Figure~\ref{phone-object}
shows the relations among the objects of an instance of the {\tt Exchange}
class. The components interact via several event streams conveyed
between pairs of objects. The elements of these streams have type definitions in
ObjectSisal as given in Figure~\ref{phone-types}; the names are chosen from the
viewpoint of the common control unit.

\begin{figure}
\begin{center}

\illustrationx{event-flow}{4.2in}

\end{center}
\caption{\label{phone-object}Inter-object streams in the
   local telephone exchange.}
\end{figure}

\begin{figure}\small\center
\begin{verbatim}
  % Subscriber phone numbers

  type Number = integer;

  % External input events to the PLUs

  type LineExtEvent =
     union [ off_hook, on_hook: null; digits: integer ];

  % Status Signals from the PLUs

  type PhoneState = union [
     idle, dialing, connect, busy_tone,
     ring_tone, ring, speech: null];

  % Input events for CCU from the PLUs

  type LineInEvent = union [
     seize: record [called: Number];
     clear, answer: null ];

  % Input events for CCU from the SWU

  type SwitchInEvent = union [
     success: record[ calling, called: Number ];
     failure: record[ calling: Number ] ];

  % Output events for PLUs from the CCU

  type LineOutEvent =
     union [ sound_ring_tone, start_ring, sound_busy_tone, stop_ring: null ];

  % Output events for the SWU from the CCU

  type SwitchOutEvent = union [
     connect: record[ calling, called: Number ];
     disconnect: record[ line: Number ] ];
\end{verbatim}
\caption{\label{phone-types}Global event types for the telephone exchange.}
\end{figure}


\begin{figure}\small\center
\begin{verbatim}
class Exchange is

  line_cnt: integer;
  line_units: array of PLU;
  switch_unit: SWU;
  control_unit: CCU;

  function Create (n: integer returns Exchange )
    let
      swu := SWU.Create (n);
      ccu := CCU.Create (n);
      plus := for line in [1..n] do
        returns array of PLU.Create ()
        end for;
    in
      object Exchange [line_cnt: n;
        line_units: plus; switch_unit: swu; control_unit: ccu ]
    end let
  end function Create;

  function Run (
      exchange: Exchange;
      ext_events: array of stream of LineExtEvent;
      returns array of stream of PhoneState )
  end function Run
end class Exchange
\end{verbatim}
\caption{\label{exchange-class} The class {\tt Exchange} specifying a
   simple telephone exchange.}
\end{figure}

\begin{figure}\small\center
\begin{verbatim}
  function Run (
      exchange: Exchange;
      ext_events: array of stream of LineExtEvent;
      returns array of stream of PhoneState )
    let
      plu_status_streams, plu_in_streams :=
        for line in [1..line_cnt] do
           line_unit := line_units[line];
           status_stream, in_stream := line_unit.Process (
              ext_events [line],
              stream  plu_out_streams [line] );
        returns
          array of status_stream,
          array of in_stream
        end for;
      merged_plu_inputs := for line in [1..line_cnt] do
        returns merge of TagStream (line, plu_in_streams [line])
        end for;
      swu_in_stream := switch_unit.Process (future_stream swu_out_stream);
      in_stream := merge ( merged_plu_inputs, swu_in_stream);
      plu_out_streams, swu_out_stream := control_unit.Process (in_stream);
    in plu_status_streams
    end let
  end function Run

\end{verbatim}
\caption{\label{run-operation} The {\tt Run} operation of the
   {\tt Exchange} class.}
\end{figure}

Figures~\ref{exchange-class} and~\ref{run-operation} present
the {\tt Exchange} class which
amounts to a formal (but nevertheless executable) specification of
the interconnections shown in Figure~\ref{phone-object}. It is the top
level of the telephone exchange implementation. Its {\tt Create}
operation builds an {\tt Exchange} object containing a SWU, a CCU,
and the specified number of PLUs. In the {\tt Run} operation,
the streams of events from the PLUs and the SWU are merged
into a single input stream for the {\tt Process} function of the {\tt CCU}
object. Because the interconnection of units is cyclic, the
{\tt future\_stream} keyword is used to declare references
to stream values yet to be defined.

\begin{figure}
\begin{center}

\illustrationx{line-state}{4.6in}

\end{center}
\caption{\label{phone-dynamic}Dynamic model (state diagram) for
   the phone line unit.}
\end{figure}

The phone line unit has a significant dynamic model represented by
the state diagram in Figure~\ref{phone-dynamic}. The states are used
for noting the progress of making a connection on behalf of a calling subscriber,
acknowledging the off-hook response of the called subscriber, and clearing
the connection on completion of the call (on-hook condition of either
subscriber). The ObjectSisal implementation of the PLU given in the
appendix merges the streams of external and command events, and uses
a tail-recursive state-transition function {\tt ProcessStep} exactly
as discussed in Section~\ref{principles}.

The CCU responds to signals from the PLUs by asking the SWU to make
and clear paths in the switching network of the exchange, and the
SWU maintains the connections and informs the CCU of its success or failure
to create a path. The details of the ObjectSisal specifications
of these units are given in the appendix.
