From bc1e27b492c0388cf709a2aa635c3360c1b0ba68 Mon Sep 17 00:00:00 2001 From: sc Date: Fri, 22 Dec 2000 10:21:03 +0000 Subject: Incorporated further developments --- comm/testirda.cc | 173 +++++++++++++++++++++++++++++-------------------------- 1 file 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 Peers (16); +IvlListOf 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 (&ev); - if (!be) + /* find accessible devices */ + IvlListOf 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 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 (&ev); - if (!ae) - return; + PrevNbPending = NbPending; + IvlDictionnaryOf copy = Peers; + + /* add new addresses to Peers and create corresponding agents */ + IvlListIterOf 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 hi = *(IvlHashTableOf*)© + while (++hi) { + IvlHashCellOf* 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 hj = *(IvlHashTableOf*)&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 (&ev); - if (!ae) + IvlBusEvent* be = dynamic_cast (&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 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 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); -- cgit v1.1