#!/opt/SUNWconn/bin/tmnscript

#
#ident  "@(#)manager	1.2 98/04/28 Copyright SMI"
#
# Copyright (c) 04/28/98 by Sun Microsystems, Inc. 
# All Rights Reserved
#


#################################
# Define Procedures & Variables #
#################################

# Define a procedure to get the base directory of a package
#
proc pkgbasedir { pkgname } {
   if {[catch {exec pkgparam $pkgname BASEDIR} result]} {
        puts stderr "pkgparam $pkgname failed: $result"
        return "/opt"
   }
   return $result
}


# Define a procedure to wait and get a message
#
proc receiveAll { session } {
   set match "more"
   while { $match == "more" } {
      mp::wait 10 $session
      set tmp [mp::receive $session]
      puts stdout "Message received: $tmp \n"
      set match [lindex $tmp 6]
   }
}

# Define a procedure to get the local hostname
#
proc getHostName {} {
   # exec uname -n
   if {[catch {exec /bin/uname -n} result]} {
        return "unknown"
   }
   return $result
}


##################################
# Initialization & Configuration #
##################################

puts stdout "\nInitialization & Configuration"
puts stdout "------------------------------------------------\n"

global root_dir
set root_dir .

set basedir [pkgbasedir SUNWtmns1]
source $basedir/SUNWconn/TMNscript/script/tmns_type.tcl

# Disable mapper and parser modes
set mp_mapperModeOn false
set mp_asn1ModeOn false
puts stdout "Mapping and ASN.1 modes disabled \n"

# Set diagnostic modes: trace mode and error mode disabled
set mp_misTraceOn false
set mp_errorModeOn false
puts stdout "Trace and error modes disabled \n"

# Disable support for universal types
mp::negotiate { XMP_DECODING off }
puts stdout "Support for universal types disabled \n"

# Build address of the manager
set manager {{psel dflt} {ssel Prs} {tsel CMIP}}
set host [getHostName]
lappend manager [list host $host]
puts stdout "Address for manager application: $manager \n"

# Set the application context to use
set appcontext "2.9.0.0.2"

# Bind the application
set sessions [mp::bind -acm off $manager]
puts stdout "manager bound\n"


###################
# Manager at Work #
###################

proc showStat {} {
global total lastt

	set tt [clock seconds]
	set thr [expr $total / ($tt - $lastt + 0.1)]
	if {$thr != 0.0} {
		puts "Total per sec: $thr"
		}
	set total 0
	after 5000 showStat
	set lastt [clock seconds]
}

global total lastt
set total 0
set lastt [clock seconds]
after 5000 showStat

while {1} {
catch {mp::doOneEvent}
set cmd "mp::wait 1 $sessions"
if [catch {eval $cmd} active] {
	break
	}
foreach session $active {
	set tmp [mp::receive $session]
	set prim [lindex $tmp 0]
	set id [lindex $tmp 4]

	##puts "RECEIVED: $tmp\n"

	##
	# handle Event Indication
	#
	if {$prim=="mp::eventInd"} {
		incr total 1
		set mode [lindex $tmp 6]
		if {$mode=="conf"} {
			set rsp "mp::eventRsp -ses $session -id $id"
			##puts "SEND RSP: $rsp"
			catch {eval $rsp}
			}
		}

	##
	# handle Association Ind
	#
	if {$prim=="mp::assocInd"} {
		set info " \{\{assoc-Result accept\} \{acse-Args \{\{application-Context $appcontext\}\}\}\}"
		set rsp "mp::assocRsp -ses $session -id $id -info $info"
		puts "SEND RSP: $rsp"
		if {[catch {eval $rsp} sess] == 0} {
			lappend sessions $sess
			}
		}
	
	##
	# handle Abort Indication
	#
	if {$prim=="mp::abortInd"} {
		# remove the old session from the sessions list
		#
		set index [lsearch $sessions $session]
		## JK DEBUG
		puts "old sessions: $sessions"
		set sessions [lreplace $sessions $index $index]
		## JK DEBUG
		puts "new sessions: $sessions"
		}
	
	##
	# handle Association Release Indication
	#
	if {$prim=="mp::releaseInd"} {
		# send the response
		#
		set info "{{reason normal}}"
		set rsp "mp::releaseRsp -ses $session -id $id -info $info"
		puts "Send RELEASE RESPONSE: $rsp\n"
		catch {eval $rsp} error

		# remove the old session from the sessions list
		#
		set index [lsearch $sessions $session]
		## JK DEBUG
		##puts "old sessions: $sessions"
		set sessions [lreplace $sessions $index $index]
		## JK DEBUG
		##puts "new sessions: $sessions"
		}
	}
}
	
##############
# That's all #
##############

mp::unbind $session

