Re: Adding custom inputs to NSRunLoop
Re: Adding custom inputs to NSRunLoop
- Subject: Re: Adding custom inputs to NSRunLoop
- From: Chris Kane <email@hidden>
- Date: Wed, 26 Sep 2001 12:28:39 -0700
Your model describes one type of input source. All input sources
usually have some sort of blocking model, or periods where there is
nothing to do, and then something happens, and there is something to do
for a short while. In this first kind, it's up to you to implement the
blocking (which usually requires another thread in the background) or
use a pre-canned input source that does this for you. Another type of
input source, where the run loop does the blocking for the
implementation, is available when the input source can cast its
"signaling" and input collection activities in terms of Mach ports, and
there are pre-canned input sources like this as well.
But that's some of the theory. In practice, the usual way a custom
input source is done (pre-CoreFoundation particularly) is to use an
NSPort as a "proxy" into the run loop for your input source. You can
subclass NSPort or NSMachPort and implement things "directly", but that
requires more sophistication, and use of Mach API. By "proxy" I mean as
an input intermediary. But even if you're using an NSPort as a proxy,
it is reasonable to make your custom input source a subclass of NSPort,
for type purposes (to be a bit more abstract, and have the instances
usable where NSPort * are used).
In that usage, your custom class instances contain an instance of NSPort
as one of their fields/ivars, and they make themselves the delegate of
that NSPort. You can just make a generic port using +[NSPort port] and
retain that. The instance then puts the NSPort in the NSRunLoop (in
response to -scheduleInRunLoop:forMode: if you subclassed NSPort,
otherwise your custom class needs a method for clients to tell it "put
yourself in this run loop now"). When that NSPort receives a message,
the delegate's -handleMachMessage: or -handlePortMessage: (depending on
which it implements) will get called, and that is the point at which the
input source "does the work".
Now, where do these messages to that NSPort come from? The input source
object sends them to its NSPort. Remember that to do its own
blocking/waiting, the input source needs to be doing that in a
background thread. Sending the message, possibly with information about
the input, to the NSPort is the way to get that from the background
thread to all the run loops with which the source has been registered.
One of them will pick it up and the delegate will get called.
That's the general outline, at least.
Chris Kane
Cocoa Frameworks, Apple
On Wednesday, September 26, 2001, at 03:58 AM, Malte Tancred wrote:
Yes, but how do I implement a port? I can't find
anything in the API that allows your own port subclass
to report it's status to the run loop nor get a slice
of the loop.
Perhaps it's my picture of what a run loop is that's
scewed, but as I understand, each cycle of the run
loop involves checking the states of it's input sources.
If they're 'ready' they're 'fired'; an NSTimer invokes its
action and an NSPort sends its delegate is sent the
appropriate -handleMachMessage: or -handlePortMessage:
message.
My perception of the run loop is that it to some
degree resembles the concept of the select() system
call, with goodies added.
So, if I want to implement a subclass to NSPort, is there
a message sent from the run loop telling my port to
handle the input?
Cheerio,
Malte
Ondra:
Malte,
Malte Tancred (MT) wrote at Wed, 26 Sep 2001 11:25:02 +0200:
MT> How do I add a custom input source to NSRunLoop? NSRunLoop
MT> only supports NSPorts and NSTimers, as far as I can see.
Perhaps my imagination is flawed, but somewhat I can't imagine another
possible source of events? If it's internal, it's a timer; if it's
external,
it's a port?
---
Ondra Cada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc
--
Malte Tancred
Computer programmer, Oops AB, Sweden
mailto:email@hidden
http://www.oops.se/
_______________________________________________
cocoa-dev mailing list
email@hidden
http://www.lists.apple.com/mailman/listinfo/cocoa-dev