@TCL_STARTUP@

# meet_two.tcl
#
# This is the "file_agent".  It uses agent_meet to establish a direct
# connection with the "email_agent" and exchange status information.  It
# assumes that the "email_agent" has the same controlling server.

# 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 "file_agent"

  agent_name file_agent

    # wait for the email agent to come into existence

  waitForAgent "$agent(local-ip) email_agent"

    # meet with the "email_agent"

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

    # exchange status information

  tcpip_write $sockfd "{FREE_SPACE 65536 KB}"
  set status [tcpip_read $sockfd -blocking]
  tcpip_close $sockfd

    # display the status information

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

} error_message]} then {

    # did an error occur?  

  agent_end

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

}

  # clean up

agent_end
