#!../../src/xotclsh
#!/usr/local/bin/xotclsh
# $Id: s.AgentClient.xotcl 1.7 01/03/23 22:00:32+01:00 neumann@somewhere.wu-wien.ac.at $
#if {![info exists ACTIWEB_HOME]} {source ~/actiweb/paths.xotcl}

package require xotcl::package
package verbose 1

package require xotcl::mos::agent
package require xotcl::mos::httpPlace

Place p
set receiverHost localhost:8087

#
# if we migrate an object, we cannot send the result to [self]
# in this example we just use a small result printer object to
# print the result to the stderr stream
#
Object resultPrinter
resultPrinter proc printResult {o m code result} {
  if {$code == "OK"} {
    puts stderr "[self] -- Result -- $m:\n$result"
  } else {
    puts stderr "[self] -- Error -- $m: \n$result"
  }
}

Class CloneableClass -superclass {Agent Class}

CloneableClass AgentExample -superclass Agent -parameter {
  {invokeAgent hallo.txt}
  {invokeMethod content}
}
AgentExample proc test-calls {} {
  #showObj [self]

  set host [::set ::receiverHost]
  [self] clone $host "" [list [self] class-cloned]
}
AgentExample proc class-cloned {args} {
  [self] a1
  [self] a2
  [self] a3
  [self] a4
  a1 synchronousInvokeCall
  a3 synchronousMigrateCall
  a4 synchronousCloneCall

  #a1 asynchronousInvokeCall
  #a1 asynchronousCloneCall
  #a2 asynchronousMigrateCall
  exit
}
AgentExample instproc computeXXX args {
  return XXX--[[self] set var1]--XXXX
}
AgentExample instproc init args {
  next
  [self] set var1 xyz
  [self] persistent var1
}
AgentExample instproc synchronousInvokeCall args {

  set host [::set ::receiverHost]
  [self] instvar invokeAgent invokeMethod
  puts stderr "*******************[self]: [self proc]"
  puts stderr "Result: [[self] syncInvoke $host $invokeAgent $invokeMethod]"
}
AgentExample instproc synchronousMigrateCall args {
  set host [::set ::receiverHost]
  puts stderr "*******************[self]: [self proc]"
  set result [[self] syncMigrate $host computeXXX]
  puts stderr "Result: '$result'"
}
AgentExample instproc synchronousCloneCall args {
  set host [::set ::receiverHost]
  puts stderr "*******************[self]: [self proc]"
  set result [[self] syncClone $host computeXXX]
  puts stderr "Result: '$result'"
}
AgentExample instproc asynchronousInvokeCall args {
  set host [::set ::receiverHost]
  [self] instvar invokeAgent invokeMethod
  puts stderr "*******************[self]: [self proc]"
  [self] invoke [list resultPrinter printResult [self] INVOKE] $host $invokeAgent $invokeMethod
}
AgentExample instproc asynchronousCloneCall args {
  set host [::set ::receiverHost]
  puts stderr "*******************[self]: [self proc]"
  [self] clone $host computeXXX [list resultPrinter printResult [self] CLONE]
}
AgentExample instproc asynchronousMigrateCall args {
  set host [::set ::receiverHost]
  puts stderr "*******************[self]: [self proc]"
  [self] migrate $host computeXXX [list resultPrinter printResult [self] MIGRATE]
}


#AgentExample instproc callSet args {
#  set host localhost:8087
#  set agent welcome
#
#  puts stderr INVOKE*******************************
#  set r [[self] invoke $host welcome set+a+1]
#  puts stderr result=$r
#}

AgentExample instproc destroy args {
  next
}

Agent registryTest

registryTest proc test args {
  set host localhost:8087
  [self] createRequest "[self] testOnPlaceName $host" http://$host/
}

registryTest proc testOnPlaceName {host placeName} {
  set place [Place getInstance]
  set subject "http://[$place host]:[$place port]/[self]"

  RDFdb [self]::db
  [self]::db add "http://nestroy.wi-inf.uni-essen.de/schema/service#name" \
    $subject registryTest
  [self]::db add "http://nestroy.wi-inf.uni-essen.de/schema/xotcl#class" \
    $subject [[self] info class]
  #
  # place info als bag machen ...
  #
  [self]::db add "http://nestroy.wi-inf.uni-essen.de/schema/agent#place" \
    $subject $place

  set rdfText [${place}::rdfCreator createFromTriples [[self]::db getTriples]] 

  [self]::db destroy

  [self] invoke $host ${placeName}::registry register $rdfText

  set r [[self] invoke $host ${placeName}::registry query registryTest]
  puts "************************Query: $r\n"
  set r [[self] invoke $host ${placeName}::registry queryProperty http://nestroy.wi-inf.uni-essen.de/schema/agent\#place ::p]
  puts "************************QueryProperty: $r\n"
}


p exportedObjs client 
#p startEventLoop "registryTest test"
#p startEventLoop "client callWelcome; registryTest test"
p startEventLoop "AgentExample test-calls"

