require 'drb'

## A simple Distributed Ruby (Drb) GLAST Server
## This small script can launch a Gaudi application that uses HepRepCorba to
## setup a Corba server for HepRep events; the Corba server is specified in the
## produced hepeventserver.ior file. This script will serve such CORBA ior via
## DRB. This can be useful for people to launch Gleam remotely and connect to it
## directly from FRED

##
## You need to CHANGE these two lines:
##
## The jobOptions file to be used
jobOptions = "/nfs/slac/g/svac/common/builds/Fred/jopOptions/Read_Root_files/jobOptions.txt"
## The working dir where the ior file will be produced
workingDir = "<your_Workdir-Can-be-any-dir>"

##
## To use a different EngineeringModel Release/GlastRelease,
## change the following three lines (look at what's already
## there and modify it accordingly to get your new release):
##

## Some setup for the Gaudi application

## A personal setup, useful for setting things like CMTPATHS, if needed
personalSetup = "/nfs/slac/g/svac/common/builds/Fred/FredGlastLinks/GlastRelease.sh"
## This is the cmt produced setup file for the package
packageSetup = "/nfs/slac/g/svac/common/builds/Fred/FredGlastLinks/setup.sh"
## This is the executable to be launched
packageExe = "/nfs/slac/g/svac/common/builds/Fred/FredGlastLinks/LatIntegration.exe"

## You can use a different port number, but it's usually not necessary!
## Port number:
port = "9000"

##
## No need to ever change anything below this!
##

### FROM NOW ONLY RUBY CODE, YOU CAN IGNORE IT

#######################################
######### From here on, you can skip :)
#######################################

## This is the class we will distribute with DRB .. just gives you an Ior and
## you can shutdown the server from here
class IorServer

attr_accessor :personalSetup, :packageSetup, :packageExe, :jobOptions,
:workingDir

def initialize()
@personalSetup = ""
@packageSetup = ""
@packageExe = ""
@jobOptions = ""
@workingDir = ""

@jobs = Hash.new
@iorFile = []
@busy = false
@actualId = 0
end

def launchJob
@actualId = @actualId + 1
## Here we launch the Gaudi application; platform specific code here
if RUBY_PLATFORM =~ /mswin32/ or RUBY_PLATFORM =~ /i386-mingw32/
if @personalSetup != ""
persCmd = "#{File.basename(@personalSetup)} &&"
end
IO.popen("start cmd /c \"#{persCmd} cd #{File.dirname(@packageSetup)} && #{File.basename(@packageSetup)}&& cd #{@workingDir} && #{@packageExe} #{@jobOptions}\"")
else
persCmd = ""
if @personalSetup != ""
persCmd = "source #{@personalSetup};"
end
IO.popen("xterm -e bash -c \"#{persCmd} source #{@packageSetup} ; cd #{@workingDir} ; #{@packageExe} #{@jobOptions}\"")
end

## And here we wait for the ior file to be created, than start the DRB server
## and serve the file
ready = false
p "Waiting for the ior file to be produced"
time = Time::now
while (!ready)
print "#"
sleep 1
iorFile = @workingDir+File::SEPARATOR+"hepeventserver.ior"
if (FileTest.exist?(iorFile))
f = File.new(iorFile,"r")
if f.mtime > time
ready = true
end
end
end

print "Ok, ready to serve the ior file for job #{@actualId}\n"
s = ""
p iorFile
file = File.new(iorFile, "r")
file.each{|x|
s << x
}

@jobs[@actualId] = s.delete("\n")
end

def getIor
return @jobs[@actualId]
end

## XXX The shutdown seems to rise an error on the client (FRED in our case);
## for now I've solved by simply intercept the exception, but probably must be
## dealt properly.
def shutDown
# DRb.stop_service
end

end

## Create and distribute the server
#server = IorServer.new(workingDir+File::SEPARATOR+"hepeventserver.ior")
server = IorServer.new
server.personalSetup = personalSetup
server.packageSetup = packageSetup
server.packageExe = packageExe
server.jobOptions = jobOptions
server.workingDir = workingDir

DRb.start_service("druby://:"+port, server)
DRb.thread.join

 

Last updated by: Chuck Patterson 03/16/2005