#!../../src/xotclsh
# $Id: s.Counter2.xotcl 1.7 01/03/23 22:00:32+01:00 neumann@somewhere.wu-wien.ac.at $

package require xotcl::awo::htmlPlace
package require xotcl::awo::webDocument

HtmlPlace ::receiver -port 8091 -allowExit exit

### Define a specialization of web agent called counter
Class Counter -superclass WebAgent -parameter {{count 1}}
Counter instproc init args {         ;### Constructor
  next                               ;### Call superclass constructor
  [self] persistent count            ;### make count variable persistent
  [[self] place] exportObjs [self]   ;### export the counter object
  [self] exportProcs increment       ;### export counter's  increment method
}
Counter instproc increment {} {      ;### increment method
  ::incr [self]::count
}
### Create two counter instances c1/c2
Counter c1
Counter c2


### Define a HTML WebFacde
Class WebCounter -superclass WebFacadeHtml 
WebCounter instproc increment {} {  ;### A method that decorates increment with HTML appearence
  [self] instvar webagent           ;### import var that stores shielded web agent
  $webagent [self proc]             ;### call the actual method on the web agent
  [self] default                    ;### update HTMl page
}
WebCounter instproc default {} {  ;### Method for updating HTML page
  [self] instvar webagent         ;### importiere var that stores shielded web agent
  $webagent instvar count         ;### importiere  count var
  #
  # and create web-page with counter content
  # 
  [self] simplePage [self] "Html-Facade for $webagent" \
      "The value in $webagent is $count.<p>
 	<A HREF='$webagent+invokeFacade+increment'>Increment Counter</a><p>"
}
#
# create two web-facade instances; one for each counter
#
WebCounter web-c1
WebCounter web-c2

#
# attach webfacades to counters and export the same procs 
# as the web agent exports (== increment)
#
foreach instance [Counter info instances] {
  set webfacade web-[string trimleft $instance :]
  $instance attachWebFacade $webfacade
  $webfacade exportProcsOfWebAgent
}

### Start event loop and handle connections
receiver startEventLoop 
