def cl_thread_func(list):
    def discovered(error, address, name, list):
        try:
            if error:
                logwrite("error %d" % error)
                cl_loop.stop()
            elif not address:
                cl_loop.stop()
            else:
                list.append((address, name))
                resolver.next()
        except:
            log_exception()

    try:
        tell("(client thread)")

        resolver = BtDeviceDiscoverer()
        resolver.first(discovered, list)

        cl_loop = AoLoop()
        cl_loop.open()
        cl_loop.start()

        cl_loop.close()
        resolver.close()

        tell("client now all done")
    except:
        log_exception()
def cl_thread_func(list):
    def discovered(error, address, name, list):
        try:
            if not error:
                list.append((address, name))
                resolver.next()
            else:
                # KErrEof -25 is what we get when no more devices
                logwrite("error %d" % error)
                cl_loop.stop()
        except:
            log_exception()

    try:
        tell("(client thread)")

        resolver = AoResolver()
        resolver.open()
        resolver.discover(discovered, list)

        cl_loop = AoLoop()
        cl_loop.open()
        cl_loop.start()

        cl_loop.close()
        resolver.close()

        tell("client now all done")
    except:
        log_exception()
def wk_thread_func():
    try:
        tell("(worker thread)")
        loop = AoLoop()
        loop.open()
        imm = AoImmediate()
        imm.open()
        imm.complete(completed, loop)
        loop.start()
        imm.close()
        loop.close()
        tell("all done")
    except:
        log_exception()
def wk_thread_func():
    """
    The ``tell`` calls here break things, it looks like.
    """
    try:
        tell("(worker thread)")
        loop = AoLoop()
        loop.open()
        loop.close()
        tell("all done")
        mn_lock.release()
        finish_logging()
    except:
        log_exception()
Ejemplo n.º 5
0
# setup Exit key handler
old_title = appuifw.app.title
appuifw.app.title = u"TestApp"

# --------------------------------------------------------------------

from aosocketnativenew import AoLoop, AoImmediate


def completed(error, loop):
    loop.stop()


try:
    tell("started")
    loop = AoLoop()
    imm = AoImmediate()
    imm.open()
    imm.complete(completed, loop)
    loop.start()
    imm.close()
    tell("stopped looping")
except:
    log_exception()
    appuifw.note(u"Fatal error.", "error")

# --------------------------------------------------------------------

appuifw.app.title = old_title
    def __loop(self):
        """
        The thread that owns all the sockets runs this loop that
        processes all asynchronous events, as well as decides how to
        handle any requests in the internal queue. To get this thread
        to do something for you (instead of blocking and waiting for
        events), simply queue a new asynchronous request for the
        thread to deal with.
        """
        try:
            if logging_enabled():
                logwrite("internal thread running")
                logwrite("logging enabled")

            # We use this value internally, too, so ensure it's set
            # before we do.
            while not self.thread:
                time.sleep(0.1)

            set_thread_priority(self.thread_pri)

            self.aoloop = AoLoop()
            self.aoloop.open()

            # Note that it is imperative that we _create_ active objects
            # within this thread, as otherwise they will get registered
            # with the active scheduler of some other thread, resulting
            # in stray signals when requests are made. We should also
            # note that some other thread may try to access the ``itc``
            # object already before this thread gets to run, which is
            # why we created the object earlier, but are only now
            # registering it with the active scheduler.
            self.immediate = AoImmediate()
            self.immediate.open()
            self.itc.open()

            # We do not use this in this class, but we assume all
            # subclassers need an instance of this for initializing
            # RSocket instances.
            if logging_enabled():
                logwrite("creating socket server handle")
            self.socket_serv = SymbianSocketServ()

            # Run a new active scheduler loop until someone
            # calls ``close``.
            self.immediate.complete(self.__req_init, None)
            if logging_enabled():
                logwrite("starting ao loop")
            self.aoloop.start()
            if logging_enabled():
                logwrite("ao loop exited")

            # This will cancel any remaining requests.
            self.__process_any_requests()

            # Now release those waiting for requests that we already
            # started processing.
            self.cancel_all_pending_requests()

            # This will ensure that no new socket-related events
            # will be generated, but some might have been generated
            # already, causing callbacks after all the cleanup
            # has already been done. We must make sure not to do
            # anything in such callbacks.
            self.close_all_managed_sockets()

            # Note that those objects that might have thread-specific
            # sessions must be cleaned up by this thread, rather
            # than left for GC to handle. We are doing the cleanup here.
            self.socket_serv.close()
            self.aoloop.close()
            self.immediate.close()
            self.itc.close()
            if logging_enabled():
                logwrite("stopping logging for internal thread")
            thread_finish_logging()

            # This thread should die any moment after this.
            self.dead = True
        except:
            # Does nothing if logging has been stopped already.
            log_exception()
class SymbianAsyncMgr(AbstAsyncMgr):
    """
    This class implements a base class for socket managers that use
    sockets in an asynchronous manner in a Symbian environment.

    Each instance of this class has an internal thread that makes any
    asynchronous requests on the sockets managed by the instance,
    allowing many requests on many sockets to be outstanding at any
    one time. The thread is driven by its own native active scheduler,
    i.e. the thread does nothing but responds to events issued by the
    active scheduler.
    """
    def __init__(self, thread_pri=None):
        AbstAsyncMgr.__init__(self)
        if thread_pri:
            self.thread_pri = thread_pri
        else:
            # EPriorityAbsoluteHigh
            self.thread_pri = 500

    def create_internal_thread(self):
        """
        This method creates and starts the internal thread that owns
        all the sockets, storing a reference to it in the internal
        ``thread`` property.
        """
        # Create the Symbian-specific instance of this.
        self.itc = SymbianItc()

        # Now create and start the thread.
        # Do not forget to store the thread ID.
        if logging_enabled():
            logwrite("starting internal thread")
        self.thread = None
        self.thread = start_thread(target=self.__loop,
                                   name="socket-owner-thread-%d" % hash(self),
                                   args=())

    def __loop(self):
        """
        The thread that owns all the sockets runs this loop that
        processes all asynchronous events, as well as decides how to
        handle any requests in the internal queue. To get this thread
        to do something for you (instead of blocking and waiting for
        events), simply queue a new asynchronous request for the
        thread to deal with.
        """
        try:
            if logging_enabled():
                logwrite("internal thread running")
                logwrite("logging enabled")

            # We use this value internally, too, so ensure it's set
            # before we do.
            while not self.thread:
                time.sleep(0.1)

            set_thread_priority(self.thread_pri)

            self.aoloop = AoLoop()
            self.aoloop.open()

            # Note that it is imperative that we _create_ active objects
            # within this thread, as otherwise they will get registered
            # with the active scheduler of some other thread, resulting
            # in stray signals when requests are made. We should also
            # note that some other thread may try to access the ``itc``
            # object already before this thread gets to run, which is
            # why we created the object earlier, but are only now
            # registering it with the active scheduler.
            self.immediate = AoImmediate()
            self.immediate.open()
            self.itc.open()

            # We do not use this in this class, but we assume all
            # subclassers need an instance of this for initializing
            # RSocket instances.
            if logging_enabled():
                logwrite("creating socket server handle")
            self.socket_serv = SymbianSocketServ()

            # Run a new active scheduler loop until someone
            # calls ``close``.
            self.immediate.complete(self.__req_init, None)
            if logging_enabled():
                logwrite("starting ao loop")
            self.aoloop.start()
            if logging_enabled():
                logwrite("ao loop exited")

            # This will cancel any remaining requests.
            self.__process_any_requests()

            # Now release those waiting for requests that we already
            # started processing.
            self.cancel_all_pending_requests()

            # This will ensure that no new socket-related events
            # will be generated, but some might have been generated
            # already, causing callbacks after all the cleanup
            # has already been done. We must make sure not to do
            # anything in such callbacks.
            self.close_all_managed_sockets()

            # Note that those objects that might have thread-specific
            # sessions must be cleaned up by this thread, rather
            # than left for GC to handle. We are doing the cleanup here.
            self.socket_serv.close()
            self.aoloop.close()
            self.immediate.close()
            self.itc.close()
            if logging_enabled():
                logwrite("stopping logging for internal thread")
            thread_finish_logging()

            # This thread should die any moment after this.
            self.dead = True
        except:
            # Does nothing if logging has been stopped already.
            log_exception()

    def __req_init(self, error, dummy_param):
        """
        Starts accepting requests. No requests, not even a ``close``
        request can be delivered to the internal thread before this
        has been done.
        """
        try:
            #logwrite("enter __req_init")
            self.mutex.acquire()
            try:
                self.itc.request(self.__got_request, None)
                self.condition.signal()
            finally:
                self.mutex.release()
            #logwrite("exit __req_init")
        except:
            log_exception()

    def __process_any_requests(self):
        ## Get any requests.
        #logwrite("check for new requests")
        self.mutex.acquire()
        try:
            reqs = self.drain()
        finally:
            self.mutex.release()

        ## Prosess any requests we got.
        #logwrite("process any new requests")
        for req in reqs:
            #logwrite("processing request")
            self.process_request(req)

    def __got_request(self, error, dummy_param):
        try:
            #logwrite("enter __got_request")

            ## In our case, nobody ever calls ``cancel`` on ``self.itc``,
            ## so the only possible ``error`` value should be 0.
            assert error == 0

            ## Process all the requests in the queue.
            self.__process_any_requests()

            ## Let more requests arrive, unless one of the requests
            ## triggered a shutdown.
            #logwrite("maybe let new requests come")
            self.mutex.acquire()
            try:
                if self.dying:
                    #logwrite("no, no more requests")
                    self.may_signal = False
                    self.condition.signal_all()
                    self.aoloop.stop()
                else:
                    #logwrite("yes, new requests may come")
                    self.itc.request(self.__got_request, None)
                    self.condition.signal()
            finally:
                self.mutex.release()

            #logwrite("exit __got_request")
        except:
            log_exception()
Ejemplo n.º 8
0

def connected(error, sock):
    if error == 0:
        tell("connected")
        sock.sendall("GET / HTTP/1.0\n\n", written, sock)
    else:
        tell("failed to connect")
        loop.stop()


try:
    serv = SymbianSocketServ()
    sock = TcpSymbianSocket()

    loop = AoLoop()
    loop.open()

    tell("making connect request")
    sock.connect(serv, ("pdis.hiit.fi", 80), connected, sock)
    tell("made connect request")
    loop.start()

    sock.close()
    serv.close()
    loop.close()
except:
    log_exception()
    appuifw.note(u"Fatal error.", "error")

# --------------------------------------------------------------------