Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Issue with $stdin/$stdout w/another program

Status
Not open for further replies.

MuppetMaster

Technical User
Nov 21, 2005
8
US
I am attempting to author a class that interacts with a 3rd party application. The idea is that the 3rd party application receives and event, makes a request to an external Ruby script via STDIN/STDOUT, and then the external Ruby scripts issues commands via STDOUT and receives the response via STDIN.

I am able to receive the initial set of paramters that the application sends to my Ruby script via STDIN as follows:

Code:
  protected
  def getInitialParams()
   @initial_params = Hash.new
   $stdin.each do | line |
     if line.length <= 1
      break
     end
     line = line.split(":")
     line[0] = line[0].rstrip
     line[1] = line[1].to_s.strip
     @initial_params["#{line[0]}"] = line[1]
   end
   @log.debug("initial params: #{@initial_params['agi_request']}")
  end

(Any recommendations on building a Hash based on STDIN welcome.)

But, when I try to write a command:

Code:
  def astCommand(command)
   # Must be 'puts' to standard out as Asterisk requres a newline for commands
   $stdout.puts command
   @log.debug("After command puts")
   result = $stdin.gets
   return result
  end

Nothing appears to be returned to the 3rd party app and it 'hangs' waiting for the STDIN (only returns one line in this case as opposed to multiple at initialization).

If I run my script at the command line and interact with it there, STDIN/STDOUT work fine.

I am able to interact via STDIN/STDOUT with this same third party app via PHP. What am I doing wrong here?
 
I have also just tried:

Code:
puts command

Not using $stdout with the same result.
 
Gave this a try as well:

Also gave this a try:

Code:
   @stdin = IO.open(0, "r")
   @stdout = IO.open(1, "w")

And still no luck...
 
Not too hot on Ruby, but do you have to do anything to flush the output buffer i.e. ensure it's actually written to STDOUT rather than just waiting in the buffer?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Yes, I did flush to be sure as well, but no go. Even without the flush, it works at the commandline...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top