×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    26

    Understanding Observers

    Hi,

    I am confused if observers are asynchronous or not. Bellow is code snippet to explain what I mean:

    CSocketReader is Active object which reads data from the socket.
    ...
    CSocketReader::ReadData() {
    ...
    SetActive();
    }
    CSocketReader::RunL() {
    ...
    iObserver.NotifyChanges(iBuffer);
    iBuffer.Zero();
    }
    ...
    CEngine has instance of CSocketReader and starts socket reading
    ...

    CEngine::Start() {
    iSocketReader->ReadData();
    }

    // Callback function which is called when
    // CSocketReader reads data from the socket
    CEngine::NotifyChanges(TDes& aBuffer){
    ...
    ProcessData(aBuffer);
    iSocketReader->ReadData(); // read data from the socket again
    }

    So, my question is: when iBuffer.Zero() is executed? Before calling ProcessData(aBuffer) or after?

    I hope you understand what I mean.

    Thank you

  2. #2
    Super Contributor
    Join Date
    Mar 2008
    Posts
    1,481

    Re: Understanding Observers

    Hi,
    Here is an example. Normally observer is a class derived from a mixin class. The observer is passed to active object as a base class. If you store a reference of observer then you can inform to observer that some thing has happened.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    26

    Re: Understanding Observers

    Here is more code what I am trying to accomplish.

    SocketReader.h

    class MStatusSocketReaderChangedNotifier
    {
    public:
    virtual void SocketReaderStatusChanged(TInt aStatus,TDes8& aMessage) = 0;
    };

    class CSocketReader : public CActive
    {
    public:
    static CSocketReader* NewL(MStatusSocketReaderChangedNotifier& aStatusNotify);
    static CSocketReader* NewLC(MStatusSocketReaderChangedNotifier& aStatusNotify);
    ~CSocketReader();
    void Read(RSocket aSocket);

    protected: // from CActive
    void DoCancel();
    void RunL();
    TInt RunError(TInt aError);

    private:
    CSocketReader(MStatusSocketReaderChangedNotifier& aStatusNotify);
    void ConstructL();
    }

    SocketReader.cpp

    CSocketReader* CSocketReader::NewL(MStatusSocketReaderChangedNotifier& aStatusNotify)
    {
    CSocketReader* self = CSocketReader::NewLC(aStatusNotify);
    CleanupStack::Pop(self);
    return self;
    }


    CSocketReader* CSocketReader::NewLC(MStatusSocketReaderChangedNotifier& aStatusNotify)
    {
    CSocketReader* self = new (ELeave) CSocketReader(aStatusNotify);
    CleanupStack::PushL(self);
    self->ConstructL();
    return self;
    }


    CSocketReader::CSocketReader(MStatusSocketReaderChangedNotifier& aStatusNotify)
    : CActive(EPriorityStandard),
    iNotify(aStatusNotify)
    {
    }

    CSocketReader::~CSocketReader()
    {
    Cancel();
    }
    void CSocketReader::ConstructL()
    {
    CActiveScheduler::Add(this);
    }
    void CSocketReader::Read(RSocket aSocket)
    {
    iBuffer.Zero();
    __ASSERT_ALWAYS(!IsActive(), User::Panic(KPanicTcpipEngineRead, ETcpipBadState));
    aSocket.RecvOneOrMore(iBuffer, 0, iStatus, iLength);
    iSocketStatus = EReading;
    SetActive();
    }
    void CSocketReader::RunL()
    {
    iData.Append(iBuffer);
    iNotify.SocketReaderStatusChanged(5,iData);
    iData.Zero();
    }
    ...

    CEngine.cpp

    void CEngine::SocketReaderStatusChanged(TInt aStatus,TDes8& aMessage) {
    switch(aStatus) {
    case 5:
    // parsing received packet
    CheckServerPacket(aMessage);
    iSocketReader->Read(iTcpEngine->GetSocket());
    break;
    };
    }

    So, as I said, I have active object which is reading data from the socket and main engine which parses received packet when it arrives. I am using observer to notify engine when this happen. The idea is that after packet is processed, engine calls SocketReader Read function and active object is started again.
    My concern is about iData which I am passing to engine. I am not sure if it will be deleted before parsing packet or not. Is observer call to SocketReaderStatusChanged function is just a signal after which next line of code is executed (in my case iData.Zero()) or it waits for SocketReaderStatusChanged to complete?

    Thank you again.

  4. #4
    Registered User
    Join Date
    Apr 2005
    Location
    Barcelona
    Posts
    1,678

    Re: Understanding Observers

    Quote Originally Posted by kljajo View Post
    Hi,

    I am confused if observers are asynchronous or not. Bellow is code snippet to explain what I mean:
    It will all depend on how you implement them.

    CSocketReader is Active object which reads data from the socket.
    ...
    CSocketReader::ReadData() {
    ...
    SetActive();
    }
    CSocketReader::RunL() {
    ...
    iObserver.NotifyChanges(iBuffer);
    iBuffer.Zero();
    }
    ...
    CEngine has instance of CSocketReader and starts socket reading
    ...

    CEngine::Start() {
    iSocketReader->ReadData();
    }

    // Callback function which is called when
    // CSocketReader reads data from the socket
    CEngine::NotifyChanges(TDes& aBuffer){
    ...
    ProcessData(aBuffer);
    iSocketReader->ReadData(); // read data from the socket again
    }

    So, my question is: when iBuffer.Zero() is executed? Before calling ProcessData(aBuffer) or after?
    When your RunL() is called, the observer will be notified, so in this case ProcessData() will be called, and then a new async request will be made. Then after that, iBuffer will be zeroed.

    So, assuming that ProcessData() is done with the buffer, you'll be ok to make a new async request using that buffer. Problem comes between the async request and zeroing the buffer. Imagine that just in between (right after your async request is made, and right before the buffer is zeroed out), the scheduler (I'm referring to the preemptive thread scheduler) took control and passed it to the socket server, then you'd have a non-empty buffer being used. Of course, this would work some times, depending on when the scheduler is getting control (depending on the thread's quantum).

    So in this particular case, I'd empty the buffer in ReadData(), just to make sure it is initialized correctly before each async call.

  5. #5
    Super Contributor
    Join Date
    Nov 2004
    Location
    Wiltshire, UK
    Posts
    3,644

    Re: Understanding Observers

    The Active Object framework is asynchronous, it does this through the use state. In the AO case the TRequestStatus embedded in each CActive object.

    Observers on the other hand are just interfaces and do not hold state so are implicitly synchronous, though they can be called from anywhere.

    when iBuffer.Zero() is executed? Before calling ProcessData(aBuffer) or after?
    RunL will be called when the iStatus is changed (asynchronous) the Active Scheduler synchronously calls Runl which synchronously calls notifychanges. When notify changes returns the buffer will be zero'd

    If ProcessData is going to use aBuffer somewhere else or later on (eg as part of a queue in another active object) them the data will need to be copied from aBuffer during the synchronous running of the ProcessData function.
    Download Symbian OS now! [url]http://developer.symbian.org[/url]

Similar Threads

  1. understanding series 30,40 vs. 60
    By hp3 in forum Symbian
    Replies: 9
    Last Post: 2008-11-06, 01:47
  2. Can anybody clarify my understanding of the bluetooth connections?
    By art_1979 in forum Bluetooth Technology
    Replies: 2
    Last Post: 2008-04-07, 15:23
  3. Understanding of the complicated signing process
    By whitewinds in forum Symbian Signed Support, Application Packaging and Distribution and Security
    Replies: 9
    Last Post: 2007-08-22, 07:10
  4. Help - Understanding Free Text Messaging Services...
    By quantass in forum General Messaging
    Replies: 0
    Last Post: 2003-12-25, 03:15
  5. understanding setCurrent()
    By RalfHupfer in forum Mobile Java General
    Replies: 0
    Last Post: 2003-08-13, 15:08

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×