ALMA Computing Group

acsexmplClientWave.cpp

Go to the documentation of this file.
00001 /******************************************************************************* 00002 * ALMA - Atacama Large Millimiter Array 00003 * (c) European Southern Observatory, 2002 00004 * Copyright by ESO (in the framework of the ALMA collaboration) 00005 * and Cosylab 2002, All rights reserved 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or (at your option) any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00020 * 00021 * "@(#) $Id: acsexmplClientWave.cpp,v 1.102 2007/02/01 05:14:26 cparedes Exp $" 00022 * 00023 * who when what 00024 * -------- -------- ---------------------------------------------- 00025 * david 2002-06-17 fixed client.init(argc,argv) 00026 * gchiozzi 2002-02-13 cleane up 00027 * msekoran 2001/07/13 created 00028 */ 00029 00053 00058 00100 /* @}*/ 00101 /* @}*/ 00102 00103 #include <maciSimpleClient.h> 00104 #include <acsexmplMountC.h> 00105 #include "acsexmplCallbacks.h" 00106 #include <acsThread.h> 00107 #include <math.h> 00108 00109 ACE_RCSID(acsexmpl, acsexmpClientWave, "$Id: acsexmplClientWave.cpp,v 1.102 2007/02/01 05:14:26 cparedes Exp $") 00110 using namespace ACS; 00111 using namespace maci; 00112 00113 //ATTENTION: 00114 //For the time being use global variable. Not clean. 00115 int LENGTH = 10; 00116 int INTERVAL = 1; 00117 int ITERATIONS = 0; 00118 00119 00129 class WorkerThread : public ACS::Thread 00130 { 00131 public: 00132 WorkerThread(const ACE_CString& name, 00133 MOUNT_ACS::Mount * mount, 00134 const ACS::TimeInterval& responseTime=ThreadBase::defaultResponseTime, 00135 const ACS::TimeInterval& sleepTime=ThreadBase::defaultSleepTime) : 00136 ACS::Thread(name, responseTime, sleepTime), 00137 myCallback("objfix") 00138 { 00139 ACS_TRACE("WorkerThread::WorkerThread"); 00140 mount_p = mount; 00141 cbv = myCallback._this(); 00142 } 00143 00144 ~WorkerThread() { 00145 ACS_TRACE("WorkerThread::~WorkerThread"); 00146 } 00147 00148 virtual void onStart() 00149 { 00150 az = 0; el = 0; count = 0; 00151 00152 } 00153 00154 virtual void runLoop() 00155 { 00156 //while there are still values that need to be set... 00157 if (ITERATIONS<=0) 00158 exit(); 00159 00160 ACS::CBDescIn desc; 00161 00162 try 00163 { 00164 00165 00166 //perform a few miscellaneous operations on the azimuth and 00167 //elevation the telescope will move to. 00168 az = sin(count * 0.157) * 200 - 100; 00169 el = sin(count * 0.157) * 90; 00170 00171 //move the telescope asynchronously 00172 ACS_SHORT_LOG((LM_INFO, "%s: Going to (%f,%f)", getName().c_str(),az,el)); 00173 mount_p->objfix(az, el, cbv.in(), desc); 00174 00175 count = count++==10 ? 0 : count; 00176 //make sure we stop moving the telescope at some point. 00177 ITERATIONS--; 00178 } 00179 catch(...) 00180 { 00181 ACS_SHORT_LOG((LM_ERROR,"Error!")); 00182 } 00183 00184 ACE_OS::fflush(stdout); 00185 00186 } 00187 00188 00189 virtual void onStop() 00190 { 00191 ACS_SHORT_LOG((LM_INFO, "%s: OK", getName().c_str())); 00192 } 00193 00194 private: 00195 MOUNT_ACS::Mount * mount_p; 00196 double az, el; 00197 int count; 00198 MyCBvoid myCallback; 00199 ACS::CBvoid_var cbv; 00200 00201 }; 00202 00203 /*******************************************************************************/ 00204 00207 int main(int argc, char *argv[]) 00208 { 00209 00210 //Checks command-line arguments. 00211 if (argc < 4) 00212 { 00213 ACS_SHORT_LOG((LM_INFO, "Usage: %s <component name> <length of time for process to run> <time interval for monitor> <options>", argv[0])); 00214 return -1; 00215 } 00216 else 00217 { 00218 ACS_SHORT_LOG((LM_INFO, "Welcome to %s!", argv[0])); 00219 00220 // Total amount of time the telescope will be operated on. 00221 LENGTH = atoi(argv[2]); 00222 00223 // Requested inteval for moving the telescope. 00224 INTERVAL = atoi(argv[3]); 00225 00226 // Number of times we change the position of the telescope 00227 ITERATIONS = static_cast<int>(static_cast<double>(LENGTH) / static_cast<double>(INTERVAL)); 00228 } 00229 00230 //Creates and initialyses the SimpleClient object 00231 SimpleClient client; 00232 if (client.init(argc,argv) == 0) 00233 { 00234 ACE_DEBUG((LM_DEBUG,"Cannot init client")); 00235 return -1; 00236 } 00237 else 00238 { 00239 //Must log into manager before we can really do anything 00240 client.login(); 00241 } 00242 00243 ACS_SHORT_LOG((LM_INFO, "Will:")); 00244 ACS_SHORT_LOG((LM_INFO, " contact : %s", argv[1])); 00245 ACS_SHORT_LOG((LM_INFO, " work for : %ds", LENGTH)); 00246 ACS_SHORT_LOG((LM_INFO, " with interval: %ds", INTERVAL)); 00247 ACS_SHORT_LOG((LM_INFO, " # cmds sent : %d", ITERATIONS)); 00248 00249 try 00250 { 00251 //Now gets the specific MOUNT we have requested on the command-line 00252 ACS_SHORT_LOG((LM_INFO, "Getting component: %s", argv[1])); 00253 MOUNT_ACS::Mount_var mount = client.getComponent<MOUNT_ACS::Mount>(argv[1], 0, true); 00254 00255 //Prints the descriptor of the requested component 00256 ACS_SHORT_LOG((LM_DEBUG, "Requesting descriptor()... ")); 00257 ACS::CharacteristicComponentDesc_var descriptor = mount->descriptor(); 00258 ACS_SHORT_LOG((LM_DEBUG, "Got descriptor().")); 00259 ACS_SHORT_LOG((LM_INFO,"Descriptor:")); 00260 ACS_SHORT_LOG((LM_INFO,"\tname: %s", descriptor->name.in())); 00261 00262 //create and spawn the thread for commands 00263 MOUNT_ACS::Mount * mount_p = mount.ptr(); 00264 00265 WorkerThread thread_p("actionThread", // name of the thread 00266 mount_p, // the mount parameter 00267 ThreadBase::defaultResponseTime, 00268 INTERVAL); 00269 // by default threads that are not created using a thread manager are creatd suspended so we have to resume them!! 00270 thread_p.resume(); 00271 00272 //Enter main loop and stays there for a fixed amount of time 00273 ACS_SHORT_LOG((LM_INFO, "Going in main loop for %ds...", LENGTH)); 00274 ACE_Time_Value tv(LENGTH); 00275 client.run(tv); 00276 } 00277 catch(maciErrType::CannotGetComponentExImpl &_ex) 00278 { 00279 _ex.log(); 00280 return -1; 00281 } 00282 catch(...) 00283 { 00284 ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__, 00285 "main"); 00286 uex.log(); 00287 return -1; 00288 }//try-catch 00289 00290 //Another try section where we release our component and logout from manager 00291 try 00292 { 00293 ACS_SHORT_LOG((LM_INFO,"Releasing...")); 00294 client.releaseComponent(argv[1]); 00295 client.logout(); 00296 } 00297 catch(maciErrType::CannotReleaseComponentExImpl &_ex) 00298 { 00299 _ex.log(); 00300 return -1; 00301 } 00302 catch(...) 00303 { 00304 ACSErrTypeCommon::UnexpectedExceptionExImpl uex(__FILE__, __LINE__, 00305 "main"); 00306 uex.log(); 00307 return -1; 00308 }//try-catch 00309 00310 00311 //sleep for 3 sec to allow everytihng to cleanup and stabilyse 00312 //so that the tests can be determinitstic. 00313 ACE_OS::sleep(3); 00314 return 0; 00315 } 00319 /*___oOo___*/ 00320 00321 00322 00323