#!/usr/contrib/bin/agent

 

include control/do

include control/common

include econ/transaction

include econ/usebank

include MIME

 

#===========================================================

# Procedure: vendor {name filename}

# Arguments: name: the name you want to give this agent (ie, vendor1)

# filename: the name of the file where this vendor's catalog is

# Returns: Nothing

# Purpose: Defines a vendor's actions and reactions to situations

# ===========================================================

proc vendor {soundWarehouse name catalogData cash maker lifetime} {

global agent wallet errorCode errorInfo

# initialize the vendor's wallet

init_bank $cash

 

#load shared object needed to interface sql with tcl

load sql.so

 

# wall-time limit

 

require wall $lifetime

 

# register the vendor's name and send the "I'm alive" \

message to the spawner

 

agent_name $name

agent_event $maker $name [list $wallet $catalogData]

 

set NewCatalogData {}

 

foreach cat $catalogData {

set thumbfile [lindex $cat 2]

set temp [mime_encode $soundWarehouse/$thumbfile]

set cat [lreplace $cat 2 2 $temp]

lappend NewCatalogData $cat

}

 

set catalogData $NewCatalogData

 

do {

{ "STOP" } {

agent_send $maker 0 DONE

agent_end

exit 0

}

 

{ "catalog_request %s %s" returnAddress query_string } {

if {$query_string != ""} {

 

global config

 

set host $config(databaseName)

set userid $config(useridName)

set password $config(userPassword)

 

set conn [sql connect localhost $userid $password]

sql selectdb $conn debbie

 

set query_command "select picture, price, thumbnail from $name "

append query_command $query_string

 

set temp_catalog {}

 

sql query $conn $query_command

while {[set row [sql fetchrow $conn]] != ""} {

set row [string trim $row]

lappend temp_catalog [list [lindex $row 0] [lindex $row 1] [lindex $row 2]]

}

 

set QueryCatalogData {}

 

foreach cat $temp_catalog {

set thumbfile [lindex $cat 2]

set temp [mime_encode $soundWarehouse/$thumbfile]

set cat [lreplace $cat 2 2 $temp]

lappend QueryCatalogData $cat

}

 

sql endquery $conn

sql disconnect $conn

 

agent_send $returnAddress 0 [list $name $QueryCatalogData]

 

} else {

agent_send $returnAddress 0 [list $name $catalogData]

}

}

{ "arbiter_request %s %s %s %s %s" who arbamt to deal thread} {

 

log "Arbiter is Requesting $name regarding $deal"

set requestedProduct [lindex $deal 0]

log "The requested product is: $requestedProduct"

set myOffer [lookup $requestedProduct $catalogData]

log "$name offers the following deal: $myOffer"

 

# FIX -- We need to catch any errors so that we can tell the

# arbiter to give the puchasers's money back. Right now the

# purchaser loses his money because the arbiter is never informed.

 

if { $myOffer == "notfound" } {

 

return -code error "vendor \"$name\" does not sell product \"$requestedProduct\"

 

} elseif { $myOffer == $deal } {

 

set mimeString [mime_encode $soundWarehouse/$requestedProduct]

sell $who $sender $arbamt $mimeString $thread

 

} else {

 

return -code error "unknown error in vendor $name"

}

}

}

}

 

#===========================================================

# Procedure: lookup {name vendorData}

# Arguments: (1) name: The name of the product we are looking up

# (2) vendorData: The file we are looking in

# Returns: {productName, productPrice}

# Purpose: Lookup is a utility function used by a vendor to search for a

# product(currently a sound) in a vendors 'warehouse'.

# Note: if the file is not in the warehouse, it returns 'nofile'

# ===========================================================

proc lookup {name catalogData} {

 

foreach entry $catalogData {

 

set productName [lindex $entry 0]

set productPrice [lindex $entry 1]

 

if {[string compare $name $productName] == 0} {

return [list $productName $productPrice]

}

}

 

return "notfound"

}

 

 

#################################################################

# getCatalog

#################################################################

# Overview: getCatalog is a utility function called by the vendor startup

# script. It opens the vendors product catalog and packages it as list to be

# returned to inquiring agents.

 

proc getCatalog file {

 

set code [

catch {

set fd [open $file r]

} errorMessage

]

 

if {$code} {

return -code error -errocode NO_FILE "file \"$vendorCatalogPath/$vendorFile\" could not be opened: $errorMessage"

}

 

while {[gets $fd line] != "-1"} {

 

set line [string trim $line]

 

if {$line == ""} {

continue

}

#added line 2

lappend catalog [list [lindex $line 0] [lindex $line 1] [lindex $line 2]]

}

 

close $fd

return $catalog

}