I am hoping someone can tell me why my task isn't returning data to the
stdoutput when I send commands via writeData.
I am able to create a task and receive initial output when I launch the
task, but when I try to send commands via writeData calls through my
stdinput pipe the task never returns more data.
I've attached the fdb program so you can drop it in your path, change the
launchPath var and try it your own self.

Source:

class ApplicationDelegate

  attr_accessor :runButton, :continueButton, :stepButton, :quitButton
  attr_accessor :debugText

  def awakeFromNib
    @debugText.setString("")
  end

  def applicationDidFinishLaunching(notification)
    #Initialize envionment variable references
    defaultEnvironment = NSProcessInfo.processInfo.environment
    environment =
NSMutableDictionary.alloc.initWithDictionary(@defaultEnvironment)

    #Path to the executatble
    launchPath = "/Applications/Adobe\ Flash\ Builder\
Beta/sdks/3.4.0/bin/fdb"

    #Create a reference to the notification center for listening on
notifications
    @center = NSNotificationCenter.defaultCenter

    #Create Task
    @fdb = NSTask.new

    #Add an observer for the NSTaskDidTerminateNotification

 
@center.addObserver(self,selector:"taskDead:",name:"NSTaskDidTerminateNotification",object:@fdb
)
    @fdb.setLaunchPath(launchPath)

    #Set up the environment
    environment.setObject("YES",forKey:"NSUnbufferedIO")
    @fdb.setEnvironment(environment)

    #Create output pipe
    @outputPipe = NSPipe.new
    @outputHandle = @outputPipe.fileHandleForReading

 
@center.addObserver(self,selector:"taskDataAvailable:",name:"NSFileHandleReadCompletionNotification",object:@outputHandle
)
    @fdb.setStandardOutput(@outputPipe)

    #Create error pipe
    @errorPipe = NSPipe.new
    @errorHandle = @errorPipe.fileHandleForReading

 
@center.addObserver(self,selector:"errorDataAvailable:",name:"NSFileHandleReadCompletionNotification",object:@errorHandle
)
    @fdb.setStandardError(@errorPipe)

    #Create input pipe
    @inputPipe = NSPipe.new
    @inputHandle = @inputPipe.fileHandleForWriting
    @fdb.setStandardInput(@inputPipe)

    #Launch that puppy
    @fdb.launch

    #Set the outputs to read in the background and notify asynchronously
    @outputHandle.readInBackgroundAndNotify
    @errorHandle.readInBackgroundAndNotify

    #Release the memory allocations for discarded objects
    @environment.release
  end

  def taskDataAvailable(notification)
    data =
notification.userInfo.valueForKey(NSFileHandleNotificationDataItem)
    if !data.nil?
      string =
NSString.alloc.initWithData(data,encoding:NSASCIIStringEncoding)
      self.parseDataString(string)
      @outputHandle.readInBackgroundAndNotify
    end
  end

  def errorDataAvailable(notification)
    data =
notification.userInfo.valueForKey(NSFileHandleNotificationDataItem)
    if !data.nil?
      string = "ERROR: "
      string +=
NSString.alloc.initWithData(data,encoding:NSASCIIStringEncoding)
      self.parseDataString(string)
      @errorHandle.readInBackgroundAndNotify
    end
  end

  def parseDataString(dataString)
    @debugText.textStorage.mutableString.appendString(dataString)
    @debugText.setTextColor(NSColor.whiteColor)
  end

  def run(sender)
    puts "RUN"

 @inputHandle.writeData("r\n",dataUsingEncoding:NSString.defaultCStringEncoding)
  end

  def contiune(sender)
    puts "CONTINUE"

 @inputHandle.writeData("c\n",dataUsingEncoding:NSString.defaultCStringEncoding)
  end

  def step(sender)

 @inputHandle.writeData("s\n",dataUsingEncoding:NSString.defaultCStringEncoding)
  end

  def exit(sender)

 
@inputHandle.writeData("quit\n",dataUsingEncoding:NSString.defaultCStringEncoding)
    self.killTask
  end

  def applicationShouldTerminateAfterLastWindowClosed(application)
    return true
  end

  def killTask
    if @fdb.isRunning
      @fdb.terminate
    end
  end

  def taskDead(notification)
    exitCode = notification.object.terminationStatus
    if exitCode != 0
      puts "Task exited with code " + exitCode.to_s + " probably not a good
thing."
    end
    @center.removeObserver(self)
    puts "Task died dude."
  end

end

Attachment: fdb
Description: Binary data

_______________________________________________
MacRuby-devel mailing list
MacRuby-devel@lists.macosforge.org
http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

Reply via email to