From bab890b80daf75b00a44e34bbdfad42ccf8629d8 Mon Sep 17 00:00:00 2001 From: chatty Date: Tue, 28 Nov 2000 17:07:45 +0000 Subject: *** empty log message *** --- comm/global.h | 28 ------------- comm/test.cc | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ comm/testbus.cc | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+), 28 deletions(-) delete mode 100644 comm/global.h create mode 100644 comm/test.cc create mode 100644 comm/testbus.cc (limited to 'comm') diff --git a/comm/global.h b/comm/global.h deleted file mode 100644 index 71244dc..0000000 --- a/comm/global.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * The Unix Channel - * - * by Michel Beaudouin-Lafon - * - * Copyright 1990-1993 - * Laboratoire de Recherche en Informatique (LRI) - * - * General definitions - * - * $Id$ - * $CurLog$ - */ - -#ifndef UchGlobal_H_ -#define UchGlobal_H_ - -#ifndef NIL -#define NIL 0 -#define Nil(type) ((type) NIL) -#endif /* NIL */ - -typedef void *pointer; - -#include "ccu/bool.h" -#include "ccu/word.h" - -#endif /* UchGlobal_H_ */ diff --git a/comm/test.cc b/comm/test.cc new file mode 100644 index 0000000..a6dfabf --- /dev/null +++ b/comm/test.cc @@ -0,0 +1,122 @@ +#include "uch.h" +#include +#include +#include + +#define TESTMSG + +#ifdef TESTMSG +class MyRemoteAgent : public UchRemoteAgent { +public: + MyRemoteAgent (UchAgent* a, UchChannel* ch) : UchRemoteAgent (a, ch) {} + ~MyRemoteAgent () {} +bool NewMessage (UchMsgBuffer&, bool); +}; + +class MyAgent : public UchAgent { +public: + MyAgent (UchAddress* a) : UchAgent (a) {} + ~MyAgent () {} +UchRemoteAgent* HandleNew (UchChannel* ch) { return new MyRemoteAgent (this, ch); } +}; + +class MyMsg : public UchMessage { + CcuString S1; + CcuString S2; + +public: + MyMsg (const char* s1, const char* s2) : UchMessage (), S1 (s1), S2 (s2) {} + MyMsg () : UchMessage (), S1 ("S1"), S2 ("S2") {} + ~MyMsg () {} + void ReadFrom (UchMsgBuffer&, lword); + void WriteTo (UchMsgBuffer&); + void Dump () { printf ("[%s] [%s]\n", (const char*) S1, (const char*) S2); } +}; + +bool +MyRemoteAgent :: NewMessage (UchMsgBuffer& b, bool) +{ + MyMsg m; + b.Get (m); + m.Dump (); + return true; +} + +void +MyMsg :: WriteTo (UchMsgBuffer& b) +{ + b << S1 << S2; +} + +void +MyMsg :: ReadFrom (UchMsgBuffer& b, lword len) +{ + b >> S1 >> S2; +} + +#endif + +UchAgent* A; +UchPortServer* PS; +const char* BUF; + +int +fMatch (const char* match, lword host, sword port, const char*) +{ + printf ("0x%lx:%d -> %s\n", host, port, match); + UchInetAddress* addr = new UchInetAddress (host == UchInetAddress::LoopBack () + ? LOOPBACK : host, port); + UchRemoteAgent* ra = A->Contact (addr); + if (ra) { + printf ("joined\n"); +#ifdef TESTMSG + MyMsg m ("hello", "world"); + ra->Send (m); +#endif + } else { + printf ("connection failed\n"); + delete addr; + } + return 1; +} + +void +fQuit (int, int) +{ + printf ("quit\n"); + PS->Remove (BUF, *(UchInetAddress*) A->BoundTo ()); + exit (0); +} + +void +fTick (Millisecond t) +{ + printf ("%d\n", t); +} + +main (int argc, char** argv) +{ + const char* id = argc > 1 ? argv [1] : ""; + UchInetAddress addr (ANYADDR); +#ifdef TESTMSG + MyAgent a (&addr); +#else + UchAgent a (&addr); +#endif + if (a.Setup ()) { + A = &a; + UchPortServer ps ("portserv"); + PS = &ps; + ps.Match ("portserv:chatty", fMatch); + char buf [64]; + strcpy (buf, "%s:%u:test"); + strcat (buf, id); + BUF = buf; + printf ("registering as %s\n", buf); + ps.Register (buf, *(UchInetAddress*) a.BoundTo ()); + UchSignalHandler h (*a.GetMultiplexer (), SigHup, fQuit); +// UchTimeOut to (*a.GetMultiplexer (), 1000, fTick); + a.Run (); + ps.Remove (buf, *(UchInetAddress*) a.BoundTo ()); + } +} 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 +#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; + const char* m = be->Matches; + cout << "Event '" << be->Matches << "' 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 () +{ + 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 (); +} -- cgit v1.1