Repository navigation
Signal and foreign function tutorial
In addition to allowing non-Waldo code to call into Waldo, a Waldo programmer may also want to allow Waldo code to call non-Waldo code. Generally, this can happen for two reasons:
* The Waldo language is missing a useful feature
* To get the non-Waldo code to perform some action
You should use calls to the Functions for the first of these and signals for the second of these. The next two sections talks about each.
Foreign functions are functions that are written in non-Waldo code (ie, Python) that are then passed into Waldo and executed in Waldo. Below is an example of a foreign function written in Python:
def print_msg(endpoint, to_print):
print (to_print)
A Waldo method that uses this foreign function is below:
Public Function (Function(in:Text; returns: Nothing) print_func)
{
print_func('hello, world!');
}
If you're confused by the Function syntax, see the types tutorial.
Note that regardless of how many other arguments the foreign function takes, it always takes the endpoint object calling it as its first argument.
Foreign functions copy all data out of Waldo: any changes they make to data will not affect the data inside Waldo and, generally, should not have side effects because Waldo will not backout changes made in foreign functions. A good use of foreign functions is to use them as ways to extend the language with operations that are not permitted, but should eventually be. For instance, the Waldo math library makes use of foreign functions to provide operations, such as mod and rand.
Frequently, after a transaction completes, you may want to trigger some additional work on an endpoint. You can do this using signal. signal takes in a Function object and any arguments to pass to that function object. It makes a deep copy of the arguments at the time that signal is called and inserts both the function to call and copied arguments onto a queue. Whenever non-Waldo code calls service_signal on an endpoint, Waldo removes the first Function from the head of the queue with its arguments and executes it.
As a more complete example, consider a basic two-person chat application. (Note: all that Symmetric does here is to automatically generate a copy of ChatterA's logic for ChatterB. See the tips and tricks page for a longer description of Symmetric.)
ChatApp
Symmetric ChatterA, ChatterB;
Sequences {
SeqSendMsg: ChatterA.send_msg -> ChatterB.receive_msg;
}
Sequence SeqSendMsg (Text msg)
{
ChatterA.send_msg{}
ChatterB.receive_msg
{
signal(display_msg_func,msg);
}
}
ChatterA
{
Function (in: Text; returns: Nothing) display_msg_func;
onCreate(Function (in: Text; returns: Nothing) init_display_msg_func)
{
display_msg_func = init_display_msg_func;
}
Public Function send_msg_to_other_side(Text msg)
{
SeqSendMsg(msg);
}
}
A corresponding Python file below instruments it so that ChatterA listens for and displays input from ChatterB by continuously polling with service_signal.
import time
import sys
sys.path.append("C:/Users/perceptual/Waldo")
from lib import Waldo
from emitted import ChatterA, ChatterB
HOSTNAME = '127.0.0.1'
PORT = 6922
def display_msg(endpoint,msg):
# note: a more sophisticated app might write to a gui instead.
print ('MSG: ' + msg)
def run_chatter_a():
# runs in accept mode
Waldo.tcp_accept(
ChatterA, HOSTNAME, PORT, display_msg,
connected_callback = listen_for_other_side)
while True:
pass
def run_chatter_b():
chatter_b = Waldo.tcp_connect(
ChatterB, HOSTNAME, PORT, display_msg)
listen_for_user_input(chatter_b)
def listen_for_other_side(endpoint_obj):
'''
Continuously poll to see if there's a message from other side to display.
'''
while(True):
endpoint_obj.service_signal()
time.sleep(.5)
def listen_for_user_input(endpoint_obj):
while True:
msg_to_send = raw_input('Message to partner: ')
endpoint_obj.send_msg_to_other_side(msg_to_send)
if __name__ == '__main__':
'''
Passing in argument 'a' starts ChatterA listening.
Executing another process passing in 'b' starts ChatterB taking input from user.
'''
if (sys.argv[1] == 'a'):
run_chatter_a()
else:
run_chatter_b()