1 ////////////////////////////////////////////////////////////////////////////////////
2 ///
3 /// \file tutorial_02_a.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. This program is a follow on to tutorial_02.cpp
8 ///
9 /// <br>Author(s): Daniel Barber
10 /// <br>Created: 12 February 2011
11 /// <br>Copyright (c) 2011
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 <jaus/core/time/querytime.h>
45 #include <jaus/core/time/reporttime.h>
46 #include <cxutils/keyboard.h>
47 #include <iostream>
48
49 // NOTE - Run another JAUS program like tutorial_01
50 // so that this program can talk to it.
51
52 int main(int argc, char* argv[])
53 {
54 JAUS::Component component;
55
56 // Setup identification info. For questions about this,
57 // see the previous tutorial(s).
58 JAUS::Discovery* discoveryService = NULL;
59 discoveryService = component.DiscoveryService();
60 discoveryService->SetSubsystemIdentification(JAUS::Subsystem::Vehicle,
61 "Robot");
62 discoveryService->SetNodeIdentification("Primary Computer");
63 discoveryService->SetComponentIdentification("Baseline");
64
65 JAUS::Address componentID(1000, 1, 2);
66 // Initialize!
67 std::cout << "Initializing component...";
68 if(component.Initialize(componentID) == false)
69 {
70 std::cout << "Failed to initialize component [" << componentID.ToString() << "]\n";
71 return 0;
72 }
73 std::cout << "Success!\n";
74
75 // Now go into your main computer loop until the
76 // component has been told to shutdown.
77 JAUS::Time::Stamp displayStatusTimeMs = JAUS::Time::GetUtcTimeMs();
78 while(true)
79 {
80 JAUS::Management* managementService = NULL;
81 managementService = component.ManagementService();
82 if(managementService->GetStatus() == JAUS::Management::Status::Shutdown)
83 {
84 // Exit program.
85 break;
86 }
87
88 if(JAUS::Time::GetUtcTimeMs() - displayStatusTimeMs > 500)
89 {
90 // Use the discovery service to get a list of
91 // discovered subsystems. (see below for how to clear map).
92 JAUS::Subsystem::Map discoveredSubsystems;
93 discoveryService->GetSubsystems(discoveredSubsystems);
94 std::cout << "======================================================\n";
95
96 JAUS::Subsystem::Map::iterator subsystem;
97 // The map is indexed by the subsystem number.
98 for(subsystem = discoveredSubsystems.begin();
99 subsystem != discoveredSubsystems.end();
100 subsystem++)
101 {
102 std::cout << "Subsystem: "
103 << subsystem->first
104 << " Identification: "
105 << subsystem->second->mIdentification
106 << std::endl;
107
108 // Lets see if it has specific service we
109 // want to communicate with. For this tutorial
110 // let's check for the Time service, which
111 // supports the Query Time and
112 // Report Time messages.
113 // We can use the subsystem data structure to
114 // get a list of components on the subsystem that
115 // have the service, so we can send a message to it.
116 JAUS::Address::List componentsWithTime;
117 componentsWithTime = subsystem->second->GetComponentsWithService(JAUS::TimeService::Name);
118 JAUS::Address::List::iterator c;
119 for(c = componentsWithTime.begin();
120 c != componentsWithTime.end();
121 c++)
122 {
123 // First, make sure it is not the
124 // component we are using, because we
125 // want to talk to a different one.
126 if( (*c) != component.GetComponentID())
127 {
128 // Now that we have the ID of
129 // component with the Time
130 // service, lets send a query message
131 // and wait for the response.
132
133 // Setup the query message to send.
134 JAUS::QueryTime query;
135 query.SetDestinationID( (*c) );
136 query.SetSourceID(component.GetComponentID());
137 // Request time and date data, by modifying the
138 // presence vector field to request them.
139 query.SetPresenceVector(JAUS::QueryTime::PresenceVector::Time | JAUS::QueryTime::PresenceVector::Date);
140
141 // This is the response message we want.
142 JAUS::ReportTime 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 if(response.IsFieldPresent(JAUS::ReportTime::PresenceVector::Time))
154 {
155 std::cout << "\tTime: " << response.GetTimeStamp().ToString() << std::endl;
156 }
157 if(response.IsFieldPresent(JAUS::ReportTime::PresenceVector::Date))
158 {
159 std::cout << "\tDate: "
160 << response.GetDateStamp().mYear
161 << ", " << response.GetDateStamp().mMonth
162 << ", " << response.GetDateStamp().mDay << std::endl;
163 }
164 }
165 }
166 }
167 // The above steps can be used to find any component
168 // with a specific service you wish to communicate with.
169 }
170
171 // Make sure you delete the subsystem map when
172 // you are done with it, otherwise you will have a
173 // memory leak.
174 JAUS::Subsystem::DeleteSubsystemMap(discoveredSubsystems);
175
176 displayStatusTimeMs = JAUS::Time::GetUtcTimeMs();
177 }
178
179 if(CxUtils::GetChar() == 27)
180 {
181 break;
182 }
183
184 CxUtils::SleepMs(1);
185 }
186
187 // Shutdown your component completely. Any
188 // services added or belonging to the component
189 // will be deleted.
190 component.Shutdown();
191
192 return 0;
193 }
194
195
196
197 /* End of File */
198