ó ó’‡Nc@sadZddgZddlZddlmZddlmZddlmZm Z ddlm Z dd l m Z dd l mZmZmZmZmZmZmZmZmZmZmZdd lmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$d Z%d Z&d d>Z'de fd„ƒYZ(defd„ƒYZ)e*e*d„Z+dS(sŃ A reactor for integrating with U{CFRunLoop}, the CoreFoundation main loop used by MacOS X. This is useful for integrating Twisted with U{PyObjC} applications. tinstallt CFReactori˙˙˙˙N(t implements(t IReactorFDSet(tPosixReactorBaset_Waker(t _NO_FILEDESC(tlog( tCFRunLoopAddSourcetCFRunLoopRemoveSourcetCFRunLoopGetMaint CFRunLoopRunt CFRunLoopStoptCFRunLoopTimerCreatetCFRunLoopAddTimertCFRunLoopTimerInvalidatetkCFAllocatorDefaulttkCFRunLoopCommonModestCFAbsoluteTimeGetCurrent( tCFSocketCreateWithNativetCFSocketSetSocketFlagstCFSocketEnableCallBackstCFSocketCreateRunLoopSourcetCFSocketDisableCallBackstCFSocketInvalidatetkCFSocketWriteCallBacktkCFSocketReadCallBacktkCFSocketConnectCallBackt*kCFSocketAutomaticallyReenableReadCallBackt+kCFSocketAutomaticallyReenableWriteCallBackiiit _WakerPluscBseZdZd„ZRS(sˇ The normal Twisted waker will simply wake up the main loop, which causes an iteration to run, which in turn causes L{PosixReactorBase.runUntilCurrent} to get invoked. L{CFReactor} has a slightly different model of iteration, though: rather than have each iteration process the thread queue, then timed calls, then file descriptors, each callback is run as it is dispatched by the CFRunLoop observer which triggered it. So this waker needs to not only unblock the loop, but also make sure the work gets done; so, it reschedules the invocation of C{runUntilCurrent} to be immediate (0 seconds from now) even if there is no timed call work to do. cCs#tj|ƒ}|jjtƒ|S(sw Wake up the loop and force C{runUntilCurrent} to run immediately in the next timed iteration. (RtdoReadtreactort_scheduleSimulatetTrue(tselftresult((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR@s(t__name__t __module__t__doc__R(((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR/scBsíeZdZeeƒddd„Zd„Zd„Zd„Z d„Z d„Z d„Z d„Z d „Zd „Zd „Zd „Zd „Zd„ZeZd„ZdZed„Zd„Zd„Zd„Zd„Zdd„ZRS(s The CoreFoundation reactor. You probably want to use this via the L{install} API. @ivar _fdmap: a dictionary, mapping an integer (a file descriptor) to a 4-tuple of: - source: a C{CFRunLoopSource}; the source associated with this socket. - socket: a C{CFSocket} wrapping the file descriptor. - descriptor: an L{IReadDescriptor} and/or L{IWriteDescriptor} provider. - read-write: a 2-C{list} of booleans: respectively, whether this descriptor is currently registered for reading or registered for writing. @ivar _idmap: a dictionary, mapping the id() of an L{IReadDescriptor} or L{IWriteDescriptor} to a C{fd} in L{_fdmap}. Implemented in this manner so that we don't have to rely (even more) on the hashability of L{IReadDescriptor} providers, and we know that they won't be collected since these are kept in sync with C{_fdmap}. Necessary because the .fileno() of a file descriptor may change at will, so we need to be able to look up what its file descriptor I{used} to be, so that we can look it up in C{_fdmap} @ivar _cfrunloop: the L{CFRunLoop} pyobjc object wrapped by this reactor. @ivar _inCFLoop: Is L{CFRunLoopRun} currently running? @type _inCFLoop: C{bool} @ivar _currentSimulator: if a CFTimer is currently scheduled with the CF run loop to run Twisted callLater calls, this is a reference to it. Otherwise, it is C{None} cCsbi|_i|_|dkr't}n||_|dkrHtƒ}n||_tj|ƒdS(N( t_fdmapt_idmaptNoneR t_runnerR t _cfrunloopRt__init__(R#trunLooptrunner((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR-ss        cCsB|js>t|ƒ|_|jj|jƒ|j|jƒndS(s™ Override C{installWaker} in order to use L{_WakerPlus}; otherwise this should be exactly the same as the parent implementation. N(twakerRt_internalReaderstaddt addReader(R#((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt installWaker€s cCs|\}}||jkr2t|j|tƒdSd}t} |j|\} } } } yx| jƒdkrut}nY|tk} | rŹ| t rÎt j | | j ƒ}qÎn"| t rÎt j | | jƒ}nWn!tjƒd}t jƒnX|r|j| || ƒndS(s‰ The socket callback issued by CFRunLoop. This will issue C{doRead} or C{doWrite} calls to the L{IReadDescriptor} and L{IWriteDescriptor} registered with the file descriptor that we are being notified of. @param cfSocket: The L{CFSocket} which has got some activity. @param callbackType: The type of activity that we are being notified of. Either L{kCFSocketReadCallBack} or L{kCFSocketWriteCallBack}. @param ignoredAddress: Unused, because this is not used for either of the callback types we register for. @param ignoredData: Unused, because this is not used for either of the callback types we register for. @param context: The data associated with this callback by L{CFSocketCreateWithNative} (in L{CFReactor._watchFD}). A 2-tuple of C{(int, CFRunLoopSource)}. Ni˙˙˙˙i(R(R R,RR*tFalsetfilenoRRt_READRtcallWithLoggerRt_WRITEtdoWritetsystexc_infoterrt_disconnectSelectable(R#tcfSockett callbackTypetignoredAddresst ignoredDatatcontexttfdt smugglesrctwhytisReadtsrctskttreadWriteDescriptortrw((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt_socketCallback‹s0     c Cs4|dkrtdƒ‚n||jkrF|j|\}}}}nĘg}|j|ƒtt|ttBtB|j|ƒ}t |t t Bt Bƒt t|dƒ}|j|ƒt|j|tƒt|ttBtBƒttg}||jt|ƒ<||||f|j|/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt_watchFDËs6    cCsitt6tt6|S(s- Convert a C{kCFSocket...} constant to an index into the read/write state list (C{_READ} or C{_WRITE}) (the 4th element of the value of C{self._fdmap}). @param flag: C{kCFSocketReadCallBack} or C{kCFSocketWriteCallBack} @return: C{_READ} or C{_WRITE} (R7RR9R(R#RS((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyRQs cCsÎt|ƒ|jkrdS|dkr;|jt|ƒ}n|}|j|\}}}}t||ƒt||j|ƒ<|t rĘ|t rĘ|jt|ƒ=|j|=t|j |t ƒt |ƒndS(s Unregister a file descriptor with the L{CFRunLoop}, or modify its state so that it's listening for only one notification (read or write) as opposed to both; used to implement C{removeReader} and C{removeWriter}. @param fd: a file descriptor @type fd: C{int} @param descr: an L{IReadDescriptor} or L{IWriteDescriptor} @param flag: L{kCFSocketWriteCallBack} L{kCFSocketReadCallBack} Ni˙˙˙˙( RPR)R(RR5RQR7R9R R,RR(R#RDRRRStrealfdRHRTRK((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt _unwatchFDs   cCs|j|jƒ|tƒdS(s7 Implement L{IReactorFDSet.addReader}. N(RWR6R(R#treader((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR30scCs|j|jƒ|tƒdS(s7 Implement L{IReactorFDSet.addWriter}. N(RWR6R(R#twriter((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt addWriter7scCs|j|jƒ|tƒdS(s: Implement L{IReactorFDSet.removeReader}. N(RYR6R(R#RZ((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt removeReader>scCs|j|jƒ|tƒdS(s: Implement L{IReactorFDSet.removeWriter}. N(RYR6R(R#R[((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt removeWriterEscCs|tg|jjƒD]\}}}}|^qƒ}|t|jƒ8}x(|D] }|j|ƒ|j|ƒqNWt|ƒS(s7 Implement L{IReactorFDSet.removeAll}. (tsetR(tvaluesR1R]R^tlist(R#RHRTRRRKtallDesctdesc((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt removeAllLs 4  cCs6g|jjƒD]"\}}}}|tr|^qS(s8 Implement L{IReactorFDSet.getReaders}. (R(R`R7(R#RHRTRRRK((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt getReadersXs"cCs6g|jjƒD]"\}}}}|tr|^qS(s8 Implement L{IReactorFDSet.getWriters}. (R(R`R9(R#RHRTRRRK((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt getWriters`s"cCs tj||ƒ}|jƒ|S(sw Override L{PosixReactorBase}'s implementation of L{IDelayedCall.reset} so that it will immediately reschedule. Normally C{_moveCallLaterSooner} depends on the fact that C{runUntilCurrent} is always run before the mainloop goes back to sleep, so this forces it to immediately recompute how long the loop needs to stay asleep. (Rt_moveCallLaterSoonerR!(R#ttpleR$((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyRghs cCs(t|_z|jƒWdt|_XdS(sŠ Run the runner (L{CFRunLoopRun} or something that calls it), which runs the run loop until C{crash()} is called. N(R"t _inCFLoopR+R5(R#((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pytmainLoopws csވjdk r(tˆjƒdˆ_nˆjƒ}|rCd}n|dk rŚtƒ|}‡fd†}tt|ddd|dƒ}ˆ_tˆj|t ƒndS(sU Schedule a call to C{self.runUntilCurrent}. This will cancel the currently scheduled call if it is already scheduled. @param force: Even if there are no timed calls, make sure that C{runUntilCurrent} runs immediately (in a 0-seconds-from-now {CFRunLoopTimer}). This is necessary for calls which need to trigger behavior of C{runUntilCurrent} other than running timed calls, such as draining the thread call queue or calling C{crash()} when the appropriate flags are set. @type force: C{bool} gcs!dˆ_ˆjƒˆjƒdS(N(R*t_currentSimulatortrunUntilCurrentR!(tcftimertextra(R#(s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pytsimulate›s  iN( RkR*RttimeoutRR RRR,R(R#tforceRptfireDateRotc((R#s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR!…s      cOs)tj|||||Ž}|jƒ|S(s6 Implement L{IReactorTime.callLater}. (Rt callLaterR!(R#t_secondst_ftargstkwt delayedCall((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyRtŚs cCstj|ƒ|jtƒdS(s1 Implement L{IReactorCore.stop}. N(RtstopR!R"(R#((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyRząs cCsL|j}tj|ƒ|jr,|jƒn|rH|jd|jƒndS(s1 Implement L{IReactorCore.crash} iN(t_startedRtcrashRit_stopNowRt(R#t wasStarted((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR|šs     cCst|jƒdS(sJ Immediately stop the CFRunLoop (which must be running!). N(R R,(R#((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyR}ĆsicCs!|j||jƒ|jƒdS(sż Emulate the behavior of C{iterate()} for things that want to call it, by letting the loop run for a little while and then scheduling a timed call to exit it. N(RtR}Rj(R#tdelay((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pytiterateÍsN(R%R&R'RRR*R-R4RLRWRQRYR3R\R]R^RdReRfRgR5RiRjRkR!RtRzR|R}R€(((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyRKs2$  @ 8           !  cCs3td|d|ƒ}ddlm}||ƒ|S(s6 Configure the twisted mainloop to be run inside CFRunLoop. @param runLoop: the run loop to use. @param runner: the function to call in order to actually invoke the main loop. This will default to L{CFRunLoopRun} if not specified. However, this is not an appropriate choice for GUI applications, as you need to run NSApplicationMain (or something like it). For example, to run the Twisted mainloop in a PyObjC application, your C{main.py} should look something like this:: from PyObjCTools import AppHelper from twisted.internet.cfreactor import install install(runner=AppHelper.runEventLoop) # initialize your application reactor.run() @return: The installed reactor. @rtype: L{CFReactor} R.R/i˙˙˙˙(tinstallReactor(Rttwisted.internet.mainR(R.R/R R((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyRŘs (,R't__all__R;tzope.interfaceRttwisted.internet.interfacesRttwisted.internet.posixbaseRRRttwisted.pythonRtCoreFoundationRR R R R R RRRRRt CFNetworkRRRRRRRRRRRR7R9RORRR*R(((s>/usr/lib/python2.7/dist-packages/twisted/internet/cfreactor.pyt s"  LL ˙Ž