41.1.3 Transmitting user data over UDP

Currently there is no support in class Agent to transmit user data. There are two ways to transmit a serialized ADU through transport agents. First, for UDP agent (and all agents derived from there), we can derive from class UDP and add a new method send(int nbytes, char *userdata) to pass user data from Application to Agent. To pass data from an Agent to an Application is somewhat trickier: each agent has a pointer to its attached application, we dynamically cast this pointer to an AppConnector and then call AppConnector::process_data().

As an example, we illustrate how class HttpInvalAgent is implemented. It is based on UDP, and is intended to deliver web cache invalidation messages (ns/webcache/inval-agent.h). It is defined as:

        class HttpInvalAgent : public Agent {
        public: 
                HttpInvalAgent();

                virtual void recv(Packet *, Handler *);
                virtual void send(int realsize, AppData* data);

        protected:
                int off_inv_;
        };

Here recv(Packet*, Handler*) overridden to extract user data, and a new send(int, AppData*) is provided to include user data in packetes. An application (HttpApp) is attached to an HttpInvalAgent using Agent::attachApp() (a dynamic cast is needed). In send(), the following code is used to write user data from AppData to the user data area in a packet:

        Packet *pkt = allocpkt(data-\>size());
        hdr_inval *ih = (hdr_inval *)pkt-\>access(off_inv_);
        ih-\>size() = data-\>size();
        char *p = (char *)pkt-\>accessdata();
        data-\>pack(p);

In recv(), the following code is used to read user data from packet and to deliver to the attached application:

        hdr_inval *ih = (hdr_inval *)pkt-\>access(off_inv_);
        ((HttpApp*)app_)-\>process_data(ih-\>size(), (char *)pkt-\>accessdata());
        Packet::free(pkt);

Tom Henderson 2011-11-05