[MacRuby-devel] NSTask writeData confusion

Kristofer Joseph kristoferjoseph at gmail.com
Wed Sep 9 01:10:24 PDT 2009


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090909/20d9c6c7/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fdb
Type: application/octet-stream
Size: 1212 bytes
Desc: not available
URL: <http://lists.macosforge.org/pipermail/macruby-devel/attachments/20090909/20d9c6c7/attachment-0001.obj>


More information about the MacRuby-devel mailing list