[e-lang] [Fwd: Re: Proposal: E / Erlang integration]

Mark S. Miller <>
Fri Jun 9 06:08:40 CEST 2006


# David's code reformatted so it parses correctly. No substantive changes.
# David, what is B for? It is never used?


pragma.disable("explicit-result-guard")
pragma.enable("easy-return")

# Like the Erlang example, we don't handle errors.

# The hardware, which can be controlled asynchronously.
def lim {
     to startTone(tone :String) { "..." }
     to stopTone() { "..." }
     to startRinging() { "..." }
}

def makePOTS() {
     # Declare the states, to avoid forward references...
     def waiting
     def idle
     def getting_first_digit
     def ringing_B_side
     def calling_B
     def ringing_A_side
     def speech

     # The peer we are connecting to.
     var B := null

     # The number being dialed.
     var number :String := ""

     # Together with the match clause in the definition of pots below,
     # this is just a way to make a  process that has different behaviours
     # over time look like a single object.
     var state := idle
     def pots

     # Now define the states...

     # waiting is a dummy state used while we are waiting for the hardware.
     # (In a real example there would have to be a way of getting out of this
     # state if the hardware fails, but the Erlang code doesn't deal with that
     # either.)
     bind waiting {}

     bind idle {
         to offhook() {
             state := waiting
             when (lim <- startTone("dial")) -> {
                 state := getting_first_digit
             }
         }
         to digit() { # ignore
         }
         to requestConnection(peer) {
             # This is a literal interpretation of the Erlang code, but it is
             # not what should be done in E for security reasons -- the peer
             # should only get a facet, not full access to the pots object.
             peer <- accept(pots)
             state := waiting
             B := peer
             when (lim <- startRinging()) -> {
                 state := ringing_B_side
             }
         }
         # we don't need to match unexpected messages here, but we could.
     }

     bind getting_first_digit {
         to onhook() {
             state := waiting
             when (lim <- stopTone()) -> {
                 state := idle
             }
         }
         to digit(d) {
             state := waiting
             when (lim <- stopTone()) -> {
                 # the Erlang code seemed to be missing some context, but
                 # this is probably about right...
                 number := analyse(number, d, validSequences())
                 state := getting_number
             }
         }
         to requestConnection(peer) {
             # same security comment as above
             peer <- reject(pots)
         }
     }

     bind calling_B {
         to onhook() {
             state := idle
         }
         to digit(d) { # ignore
         }
         to accept(peer) {
             state := waiting
             B := peer
             when (lim <- startTone("ring")) -> {
                 state := ringing_A_side
             }
         }
         to reject(peer) {
             state := waiting
             peer := null
             when (lim <- startTone("busy")) -> {
                 state := wait_on_hook
             }
         }
         to requestConnection(peer) {
             # same security comment as above
             peer <- reject(pots)
         }
     }

     #...  more states, also not shown in Erlang code
     bind pots {
         match [verb, args] { E.call(state, verb, args) }
     }
     return pots
}



More information about the erlang-questions mailing list