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

    2 ///

    3 ///  \file tutorial_01.cpp

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

    5 ///        JAUS++.  This program demonstrates how to create and initialize

    6 ///        a JAUS component with the Core Service Set.

    7 ///

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

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

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

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

   12 ///  <br>(ACTIVE) Laboratory

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

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

   15 ///  <br>All rights reserved.

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

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

   18 ///

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

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

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

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

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

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

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

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

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

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

   29 ///

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

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

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

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

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

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

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

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

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

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

   40 ///

   41 ////////////////////////////////////////////////////////////////////////////////////

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

   43 #include <cxutils/keyboard.h>

   44 #include <iostream>

   45 

   46 

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

   48 {

   49     // Create a component.  By default a component

   50     // has all the services of the Core Service set:

   51     // - Transport (JUDP)

   52     // - Control

   53     // - Discovery

   54     // - Events

   55     // - Liveness

   56     // - Time

   57     // - Management

   58     JAUS::Component component;

   59 

   60     // The Transport service is used to send

   61     // and receive messages to other JAUS components.  All

   62     // other services use the Transport service.  The

   63     // default transport type for JAUS++ is UDP communication

   64     // using the JUDP class.

   65 

   66     // The Discovery service is used to find

   67     // other JAUS components and services on the

   68     // network using the Transport service.  In JAUS++

   69     // Discovery will automatically find these components,

   70     // make connections to them, and keep track of what

   71     // services they have.

   72 

   73     //  The first thing we must do for a component is

   74     //  configure its identification.  This is done by

   75     //  using the Discovery Service.  Get a pointer

   76     //  to the service:

   77     JAUS::Discovery* discoveryService = NULL;

   78     discoveryService = (JAUS::Discovery*)component.GetService(JAUS::Discovery::Name);

   79     //  Alternative method:

   80     //  discoveryService = component.DiscoveryService();

   81 

   82     // Set the type of subsystem the component is for.  Subsystem

   83     // types available are currently Vehicle, or OCU.  The string

   84     // name "Robot" represents the type or category of platform.

   85     // You must set the subsystem identification before you will be

   86     // able to initialize your component.

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

   88                                                 "Robot");

   89     // You can also set identification information for the component

   90     // and node that it is on.

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

   92     discoveryService->SetComponentIdentification("Baseline");

   93 

   94     // Now that we have setup our identification information we

   95     // can initialize our component.  First, create the

   96     // component ID.

   97     JAUS::Address componentID(1000, 1, 1);

   98     // Initialize!

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

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

  101     {

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

  103         return 0;

  104     }

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

  106 

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

  108     // component has been told to shutdown.

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

  110     while(true)

  111     {

  112         // Let's check the "state" of our component. This

  113         // is done using the Management service. 

  114         // A component can be in the following states:

  115         // - Initialized

  116         // - Ready

  117         // - Standby

  118         // - Shutdown

  119         // - Failure

  120         // - Emergency

  121         JAUS::Management* managementService = NULL;

  122         managementService = (JAUS::Management*)component.GetService(JAUS::Management::Name);

  123         // Alternative method:

  124         //managementService = component.ManagementService();

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

  126         {

  127             // Exit program.

  128             break;

  129         }

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

  131         {

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

  133             // Print status of the service.

  134             managementService->PrintStatus(); std::cout << std::endl;

  135 

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

  137         }

  138 

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

  140         {

  141             break;

  142         }

  143 

  144         CxUtils::SleepMs(1);

  145     }

  146 

  147     // Shutdown your component completely.  Any

  148     // services added or belonging to the component

  149     // will be deleted.

  150     component.Shutdown();

  151 

  152     return 0;

  153 }

  154 

  155 

  156 

  157 /* End of File */

  158