1 ////////////////////////////////////////////////////////////////////////////////////

    2 ///

    3 ///  \file tutorial_02.cpp

    4 ///  \brief This file is part of a set of tutorials for learning how to use

    5 ///        JAUS++.  This program demonstrates how to find what components

    6 ///        the Discovery service has found, and how to send/receive

    7 ///        messages.

    8 ///

    9 ///  <br>Author(s): Daniel Barber

   10 ///  <br>Created: 3 July 2010

   11 ///  <br>Copyright (c) 2010

   12 ///  <br>Applied Cognition and Training in Immersive Virtual Environments

   13 ///  <br>(ACTIVE) Laboratory

   14 ///  <br>Institute for Simulation and Training (IST)

   15 ///  <br>University of Central Florida (UCF)

   16 ///  <br>All rights reserved.

   17 ///  <br>Email: dbarber@ist.ucf.edu

   18 ///  <br>Web:  http://active.ist.ucf.edu

   19 ///

   20 ///  Redistribution and use in source and binary forms, with or without

   21 ///  modification, are permitted provided that the following conditions are met:

   22 ///      * Redistributions of source code must retain the above copyright

   23 ///        notice, this list of conditions and the following disclaimer.

   24 ///      * Redistributions in binary form must reproduce the above copyright

   25 ///        notice, this list of conditions and the following disclaimer in the

   26 ///        documentation and/or other materials provided with the distribution.

   27 ///      * Neither the name of the ACTIVE LAB, IST, UCF, nor the

   28 ///        names of its contributors may be used to endorse or promote products

   29 ///        derived from this software without specific prior written permission.

   30 ///

   31 ///  THIS SOFTWARE IS PROVIDED BY THE ACTIVE LAB''AS IS'' AND ANY

   32 ///  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED

   33 ///  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

   34 ///  DISCLAIMED. IN NO EVENT SHALL UCF BE LIABLE FOR ANY

   35 ///  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES

   36 ///  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;

   37 ///  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND

   38 ///  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT

   39 ///  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS

   40 ///  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

   41 ///

   42 ////////////////////////////////////////////////////////////////////////////////////

   43 #include <jaus/core/component.h>

   44 #include <cxutils/keyboard.h>

   45 #include <iostream>

   46 

   47 // NOTE - Run another JAUS program like tutorial_01

   48 // so that this program can talk to it.

   49 

   50 // After going through this example, try and modify

   51 // this program to look for different services and send

   52 // different messages.  For example: Query Time, Report Time from

   53 // the Time service.

   54 

   55 int main(int argc, char* argv[])

   56 {

   57     JAUS::Component component;

   58 

   59     // Setup identification info.  For questions about this,

   60     // see the previous tutorial(s).

   61     JAUS::Discovery* discoveryService = NULL;

   62     discoveryService = component.DiscoveryService();

   63     discoveryService->SetSubsystemIdentification(JAUS::Subsystem::Vehicle,

   64                                                 "Robot");

   65     discoveryService->SetNodeIdentification("Primary Computer");

   66     discoveryService->SetComponentIdentification("Baseline");

   67 

   68     JAUS::Address componentID(1000, 1, 2);

   69     // Initialize!

   70     std::cout << "Initializing component...";

   71     if(component.Initialize(componentID) == false)

   72     {

   73         std::cout << "Failed to initialize component [" << componentID.ToString() << "]\n";

   74         return 0;

   75     }

   76     std::cout << "Success!\n";

   77 

   78     // Now go into your main computer loop until the

   79     // component has been told to shutdown.

   80     JAUS::Time::Stamp displayStatusTimeMs = JAUS::Time::GetUtcTimeMs();

   81     while(true)

   82     {

   83         JAUS::Management* managementService = NULL;

   84         managementService = component.ManagementService();

   85         if(managementService->GetStatus() == JAUS::Management::Status::Shutdown)

   86         {

   87             // Exit program.

   88             break;

   89         }

   90 

   91         if(JAUS::Time::GetUtcTimeMs() - displayStatusTimeMs > 500)

   92         {

   93             // Use the discovery service to get a list of

   94             // discovered subsystems. (see below for how to clear map).

   95             JAUS::Subsystem::Map discoveredSubsystems;

   96             discoveryService->GetSubsystems(discoveredSubsystems);

   97             std::cout << "======================================================\n";

   98 

   99             JAUS::Subsystem::Map::iterator subsystem;

  100             // The map is indexed by the subsystem number.

  101             for(subsystem = discoveredSubsystems.begin();

  102                 subsystem != discoveredSubsystems.end();

  103                 subsystem++)

  104             {

  105                 std::cout << "Subsystem: "

  106                           << subsystem->first

  107                           << " Identification: "

  108                           << subsystem->second->mIdentification

  109                           << std::endl;

  110 

  111                 // Lets see if it has specific service we

  112                 // want to communicate with.  For this tutorial

  113                 // let's check for the Liveness service, which

  114                 // supports the Query Heartbeat Pulse and

  115                 // Report Heartbeat Pulse messages.

  116                 // We can use the subsystem data structure to

  117                 // get a list of components on the subsystem that

  118                 // have the service, so we can send a message to it.

  119                 JAUS::Address::List componentsWithLiveness;

  120                 componentsWithLiveness = subsystem->second->GetComponentsWithService(JAUS::Liveness::Name);

  121                 JAUS::Address::List::iterator c;

  122                 for(c = componentsWithLiveness.begin();

  123                     c != componentsWithLiveness.end();

  124                     c++)

  125                 {

  126                     // First, make sure it is not the

  127                     // component we are using, because we

  128                     // want to talk to a different one.

  129                     if( (*c) != component.GetComponentID())

  130                     {

  131                         // Now that we have the ID of

  132                         // component with the Liveness

  133                         // service, lets send a query message

  134                         // and wait for the response.

  135 

  136                         // Setup the query message to send.

  137                         JAUS::QueryHeartbeatPulse query;

  138                         query.SetDestinationID( (*c) );

  139                         query.SetSourceID(component.GetComponentID());

  140 

  141                         // This is the response message we want.

  142                         JAUS::ReportHeartbeatPulse response;

  143 

  144                         // Send and see if we got a

  145                         // response, but only wait up to 1 second

  146                         // for a response.  Default value for waiting

  147                         // is 100 ms.

  148                         std::cout << "\tSending Query to " << c->ToString() << std::endl;

  149                         if(component.Send(&query, &response, 1000))

  150                         {

  151                             std::cout << "\tReceived Response Message!\n\t";

  152                             response.Print();

  153                         }

  154                     }

  155                 }

  156                 // The above steps can be used to find any component

  157                 // with a specific service you wish to communicate with.

  158             }

  159 

  160             // Make sure you delete the subsystem map when

  161             // you are done with it, otherwise you will have a

  162             // memory leak.

  163             JAUS::Subsystem::DeleteSubsystemMap(discoveredSubsystems);

  164 

  165             displayStatusTimeMs = JAUS::Time::GetUtcTimeMs();

  166         }

  167 

  168         if(CxUtils::GetChar() == 27)

  169         {

  170             break;

  171         }

  172 

  173         CxUtils::SleepMs(1);

  174     }

  175 

  176     // Shutdown your component completely.  Any

  177     // services added or belonging to the component

  178     // will be deleted.

  179     component.Shutdown();

  180 

  181     return 0;

  182 }

  183 

  184 

  185 

  186 /* End of File */

  187