#include "Multiplexer.h" #include "TimeOut.h" #include "BusAccess.h" #include "dnn/Reaction.h" #include #include #include class DnnEvent; /* Global variables. This is because we use DnnCallbacks and functions instead of DnnReactions and objects. But this is only a test... */ UchBusAccess* A; DnnCallback* C; /* This is an example of how to emit messages on the bus. Here, this function is called periodically so as to demonstrate the behaviour of the bus. */ void foo (Millisecond t) { static int i; static const char* cmds [] = {"free", "click", "lock"}; A->Emit ("tick %d", t); if (i %10 == 0) A->Emit ("knob %s", cmds[(i/10)%3]); ++i; } /* This is an example of how to handle messages coming from the bus. Here, this function is associated to a callback, which in turn is associated to a regexp (see in main). */ void print_event (DnnEvent& ev) { UchBusEvent* be = dynamic_cast (&ev); if (!be) return; cout << "Event matches '" << be->Regexp << "'\n"; cout << "\t(" << be->NbMatches << " matches: "; CcuListIterOf li = be->MatchList; while (++li) cout << "'" << **li << "' "; cout << ")\n"; } /* This is an example of how to handle a new agent connecting to the bus. Most applications are not interested in such events... */ void agentready (DnnEvent& ev) { UchAgentEvent* ae = dynamic_cast (&ev); if (!ae) return; UchBusAgent* a = ae->GetAgent (); cout << "Hello " << a->GetName () << "!\n"; C->SubscribeTo (a->Bye); } /* This is an example of how to handle an agent leaving the bus. Most applications are not interested in such events... */ void agentbye (DnnEvent& ev) { UchAgentEvent* ae = dynamic_cast (&ev); if (!ae) return; UchBusAgent* a = ae->GetAgent (); cout << "BYE " << a->GetName () << "!\n"; } main () { /* initialize communication library */ UchOpen (); /* create bus access */ UchBusAccess a ("Uch test"); A = &a; /* periodically send messages on the bus (see foo for timeout handling) */ UchTimeOut t (1000, foo); /* subscribe to a few event types (see print_event for event handling) */ /* Note that you can use every subclass of DnnBaseReaction instead of DnnCallback */ DnnCallback c1 (print_event); a.Subscribe (c1, "^(CLOCK) (.*)"); a.Subscribe (c1, "TRAFFIC Start"); a.Subscribe (c1, "(.*Tick.*)"); a.Subscribe (c1, "^AIRCRAFT:(.*) Moved (.*)"); /* react to new agents calling (see agentready for event handling) */ DnnCallback c3 (agentready); c3.SubscribeTo (a.NewAgents); /* reaction to agents leaving (see agentready for subscription, and agentbye for event handling) */ DnnCallback c4 (agentbye); C = &c4; UchLoop (); }