summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsc2000-12-22 10:21:03 +0000
committersc2000-12-22 10:21:03 +0000
commitbc1e27b492c0388cf709a2aa635c3360c1b0ba68 (patch)
treef68b5f67e5ecd6eeeeff60e5bbf8a5aaff208f71
parentebaf7de91d66eddf1e9db532c3e03ad83aa4b91d (diff)
downloadivy-league-bc1e27b492c0388cf709a2aa635c3360c1b0ba68.zip
ivy-league-bc1e27b492c0388cf709a2aa635c3360c1b0ba68.tar.gz
ivy-league-bc1e27b492c0388cf709a2aa635c3360c1b0ba68.tar.bz2
ivy-league-bc1e27b492c0388cf709a2aa635c3360c1b0ba68.tar.xz
Incorporated further developments
-rw-r--r--comm/testirda.cc173
1 files changed, 90 insertions, 83 deletions
diff --git a/comm/testirda.cc b/comm/testirda.cc
index ffd13ba..100c268 100644
--- a/comm/testirda.cc
+++ b/comm/testirda.cc
@@ -15,6 +15,11 @@ instead of IvlReactions and objects. But this is only a test...
*/
IvlBusAccess* A;
IvlCallback* C;
+IvlObexStream* I;
+IvlDictionnaryOf<IvlObexAgent> Peers (16);
+IvlListOf<IvlObexObject> Pending;
+int NbPending = 0;
+int PrevNbPending = -1;
/*
This is an example of how to emit messages on the bus.
@@ -24,66 +29,87 @@ 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 (IvlEvent& ev)
-{
- IvlBusEvent* be = dynamic_cast<IvlBusEvent*> (&ev);
- if (!be)
+ /* find accessible devices */
+ IvlListOf<IvlIrdaAddress> la;
+ if (IvlIrdaAddress::DiscoverPeers (I->GetFd (), la) == 0) {
+
+ /* if none available, delete all peers */
+ IvlObexAgent* ag;
+ bool found = false;
+ while (ag = Peers.RemoveOne ()) {
+ ag.Trash ();
+ found = true;
+ }
+ if (found)
+ cerr << "no more devices available\n";
+
+ if (found || NbPending != PrevNbPending)
+ cerr << NbPending << " pending message"
+ << (NbPending == 1 ? "\n" : "s\n");
+
+ PrevNbPending = NbPending;
return;
+ }
- cout << "Event matches '" << be->Regexp << "'\n";
- cout << "\t(" << be->NbMatches << " matches: ";
- IvlListIterOf<IvlString> 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
-bus_agent_ready (IvlEvent& ev)
-{
- IvlBusAgentEvent* ae = dynamic_cast<IvlBusAgentEvent*> (&ev);
- if (!ae)
- return;
+ PrevNbPending = NbPending;
+ IvlDictionnaryOf<IvlObexAgent> copy = Peers;
+
+ /* add new addresses to Peers and create corresponding agents */
+ IvlListIterOf<IvlIrdaAddress> li = la;
+ while (++li) {
+ char buf[1024];
+ (*li)->StrRepr (buf);
+ if (copy.Remove (buf) == 0) {
+ Peers[buf] = new IvlObexAgent (*li, I);
+ cerr << "located " << buf << "\n";
+ }
+ }
- IvlBusAgent* a = ae->GetAgent ();
- cout << "Hello " << a->GetName () << "!\n";
+ /* remove obsolete addresses from Peers and delete corresponding agents */
+ IvlHashCellIterOf<IvlObexAgent> hi = *(IvlHashTableOf<IvlObexAgent>*)&copy;
+ while (++hi) {
+ IvlHashCellOf<IvlObexAgent>* c = *hi;
+ cerr << "deleting obsolete " << (const char*) c->GetKey () << "\n";
+ Peers.Remove (c->GetKey ());
+ IvlObexAgent* ag = c->GetInfo ();
+ delete ag;
+ }
+
+ /* send objects to peers */
+ IvlObexObject* p;
+ while (p = Pending.RemoveFirst ()) {
+ --NbPending;
+ cerr << "sending object\n";
+ IvlHashIterOf<IvlObexAgent> hj = *(IvlHashTableOf<IvlObexAgent>*)&Peers;
+ while (++hj)
+ (*hj)->SendObject (*p);
+ delete p;
+ }
- 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
-bus_agent_bye (IvlEvent& ev)
+obex_send (IvlEvent& ev)
{
- IvlBusAgentEvent* ae = dynamic_cast<IvlBusAgentEvent*> (&ev);
- if (!ae)
+ IvlBusEvent* be = dynamic_cast<IvlBusEvent*> (&ev);
+ if (!be)
+ return;
+ if (be->NbMatches < 3) {
+ cerr << "incomplete OBEX Ivy message\n";
return;
- IvlBusAgent* a = ae->GetAgent ();
- cout << "BYE " << a->GetName () << "!\n";
+ }
+ IvlListIterOf<IvlString> li = be->MatchList;
+ IvlObexObject* o = new IvlObexObject;
+ o->SetClass (atoi (**++li));
+ o->SetName (**++li);
+ o->SetBody (**++li);
+ Pending.Append (o);
+ ++NbPending;
}
+
void
irda_agent_ready (IvlEvent& ev)
{
@@ -99,7 +125,7 @@ irda_agent_ready (IvlEvent& ev)
#define MEMO_PAD_ID 0x6d656d6f /* "memo" *.txt */
-main ()
+main (int argc, const char** argv)
{
/* initialize communication library */
IvlOpen ();
@@ -107,48 +133,29 @@ main ()
/* create bus access */
IvlBusAccess a ("Ivl test"); A = &a;
+ /*** IRDA test ***/
+
/* periodically send messages on the bus (see foo for timeout handling) */
IvlTimeOut t (1000, foo);
+ if (argc > 2)
+ t.Stop ();
- /* subscribe to a few event types (see print_event for event handling) */
- /* Note that you can use any subclass of IvlBaseReaction instead
- of IvlCallback */
- IvlCallback 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 bus_agent_ready for event handling) */
- IvlCallback c3 (bus_agent_ready);
- c3.SubscribeTo (a.NewAgents);
-
- /* reaction to agents leaving (see bus_agent_ready for subscription,
- and bus_agent_bye for event handling) */
- IvlCallback c4 (bus_agent_bye); C = &c4;
-
-
-
- /*** IRDA test ***/
/* create IRDA OBEX access */
- IvlObexStream irl (new IvlIrdaAddress);
+ IvlObexStream irl (new IvlIrdaAddress); I = &irl;
- /* find accessible devices */
- IvlListOf<IvlIrdaAddress> la;
- IvlIrdaAddress::DiscoverPeers (irl.GetFd (), la);
- /* test connection to first available device */
- IvlIrdaAddress* ira = la.First ();
- if (ira) {
- IvlObexAgent* ioa = new IvlObexAgent (ira, &irl);
- IvlObexObject o;
- o.SetClass (MEMO_PAD_ID);
- o.SetName ("Essai");
- o.SetBody ("Ceci est un mémo\nenvoyé depuis Linux vers\nle Palm\n");
- ioa->SendObject (o);
- cerr << "ok\n";
+ if (argc <= 1) {
+ IvlObexObject* o = new IvlObexObject;
+ o->SetClass (MEMO_PAD_ID);
+ o->SetName ("Essai1");
+ o->SetBody ("Ceci est un mémo\nenvoyé depuis Linux vers\nle Palm\n");
+ Pending.Append (o);
+ ++NbPending;
}
+ /* test connection to first available device */
+ IvlCallback c2 (obex_send);
+ a.Subscribe (c2, "^OBEX class=(.*) name=(.*) body=(.*)");
IvlCallback c5 (irda_agent_ready);
c5.SubscribeTo (irl.NewAgents);