@TCL_STARTUP@

# meet_one.tcl
#
# This is the "email_agent".  It uses agent_meet to establish a direct
# connection with the "file_agent" and exchange status information.  It
# assumes that the "file_agent" is running on the same machine.

# wait for the given agent to come into existence 

proc waitForAgent agent {

	# tell the server to notify us when the agent with the given name
	# comes into existence

    set tag [agent_status exists $agent -event]

	# wait for the notification (which comes in the form of an event)

    set m [mask new]
    mask add $m "[lindex $agent 0] ANY ANY ANY -tag $tag"
    set oldm [mask_swap event $m]
    agent_getevent tag string -blocking
    set m [mask_swap event $oldm]
    mask delete $m 
}

  # register the agent on the current machine

if {[catch agent_begin]} {
  return -code error "ERROR: unable to register the agent on $agent(actual-server)"
}

if {[catch {

    # this is the "email_agent"

  agent_name email_agent

    # wait for the "file_agent" to come into existence

  waitForAgent "$agent(local-ip) file_agent"

    # meet with the "file_agent"

  set sockfd [agent_meet "$agent(local-ip) file_agent"]

    # exchange status information

  tcpip_write $sockfd "{NEW_MESSAGES 0} {IN_BOX_MESSAGES 10}"
  set status [tcpip_read $sockfd -blocking]
  tcpip_close $sockfd

    # display the status information

  puts "This is the email_agent ($agent(local))!"
  puts "The file agent says ... $status\n"

} error_message]} then {

    # make sure that we clean up on error

  agent_end

  return -code error -errorcode $errorCode -errorinfo $errorInfo $error_message

} 

  # clean up
 
agent_end
