summaryrefslogtreecommitdiff
path: root/comm/testbus.cc
diff options
context:
space:
mode:
authorchatty2000-11-28 17:07:45 +0000
committerchatty2000-11-28 17:07:45 +0000
commitbab890b80daf75b00a44e34bbdfad42ccf8629d8 (patch)
treeae27912ae0240e359efc291a0bcf24b45371d798 /comm/testbus.cc
parentff600558b5554303394584cc5608190ad0423dbd (diff)
downloadivy-league-bab890b80daf75b00a44e34bbdfad42ccf8629d8.zip
ivy-league-bab890b80daf75b00a44e34bbdfad42ccf8629d8.tar.gz
ivy-league-bab890b80daf75b00a44e34bbdfad42ccf8629d8.tar.bz2
ivy-league-bab890b80daf75b00a44e34bbdfad42ccf8629d8.tar.xz
*** empty log message ***
Diffstat (limited to 'comm/testbus.cc')
-rw-r--r--comm/testbus.cc125
1 files changed, 125 insertions, 0 deletions
diff --git a/comm/testbus.cc b/comm/testbus.cc
new file mode 100644
index 0000000..757688b
--- /dev/null
+++ b/comm/testbus.cc
@@ -0,0 +1,125 @@
+
+#include "Multiplexer.h"
+#include "TimeOut.h"
+#include "BusAccess.h"
+#include "dnn/Reaction.h"
+#include <stdlib.h>
+#include <stdio.h>
+#include <ostream.h>
+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<UchBusEvent*> (&ev);
+ if (!be)
+ return;
+ const char* m = be->Matches;
+ cout << "Event '" << be->Matches << "' matches '" << be->Regexp << "'\n";
+ cout << "\t(" << be->NbMatches << " matches:";
+ CcuListIterOf<CcuString> 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<UchAgentEvent*> (&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<UchAgentEvent*> (&ev);
+ if (!ae)
+ return;
+ UchBusAgent* a = ae->GetAgent ();
+ cout << "BYE " << a->GetName () << "!\n";
+
+}
+
+
+
+main ()
+{
+ fprintf (stderr, "00\n");
+ const char* bus = getenv ("IVYBUS");
+ int bus_number = bus ? atoi (bus) : 2010;
+
+ fprintf (stderr, "0\n");
+ UchOpen ();
+ fprintf (stderr, "1\n");
+ UchBusAccess a ("Uch test", bus_number); A = &a;
+
+ fprintf (stderr, "2\n");
+ /* periodically send messages on the bus (see foo for timeout handling) */
+ UchTimeOut t (1000, foo);
+
+ fprintf (stderr, "3\n");
+ /* subscribe to a few types of events (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, "^(glidepoint) (.*)");
+ a.Subscribe (c1, "TRAFFIC START");
+ a.Subscribe (c1, "(.*TRAFFIC FILE NAME.*)");
+ a.Subscribe (c1, "^AVION:(.*) RADAR (.*)");
+
+ /* react to new agents calling (see agentready for event handling) */
+ DnnCallback c3 (agentready);
+ c3.SubscribeTo (a.NewAgents);
+ fprintf (stderr, "4\n");
+
+ /* reaction to agents leaving (see agentready for subscription,
+ and agentbye for event handling) */
+ DnnCallback c4 (agentbye); C = &c4;
+
+ UchLoop ();
+}