Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

DmxALClient.cc

00001 
00018 #include <math.h>  
00019 #include <string>
00020 #include <iostream>
00021 #include <unistd.h>
00022 
00023 #include "ace/Get_Opt.h"
00024 #include "ace/OS.h"
00025 #include "ace/streams.h"
00026 #include "ace/Log_Msg.h"
00027 #include "ace/Reactor.h"
00028 
00029 #include "PDmxSteerableDevice.h"
00030 #include "DmxControlBlock.h"
00031 #include "PDmxLightDevice.h"
00032 #include <RectangularRegion.h>  
00033 #include <Vectors.h>  
00034 #include <algorithm>
00035 
00036 #include "DmxALClient.h"
00037 
00038 ACE_RCSID(dmxlib_src,DmxClient,"$Id: DmxALClient.cc,v 1.3 2002/03/14 13:56:52 abaebler Exp $");
00039 
00040 DmxClient::DmxClient() :
00041     finished_(0),
00042     devnum_(0),
00043     hostname_("zach"),
00044     lampoff_(false),
00045     usval_(2000000),
00046     amplitude_(1.0),
00047     base_(0)
00048 {
00049 }
00050 
00051 void DmxClient::stop() {
00052   finished_=1;
00053 }
00054 
00055 int DmxClient::handle_signal(int signum, siginfo_t*, ucontext_t*) {
00056     std::string s;
00057     switch(signum) {
00058     case SIGINT:
00059         ACE_DEBUG((DMX_DEBUG(Signal handler caught SIGINT.\n)));
00061         // Following is commented out because code is blocking and because 
00062         // underlying client-server application logic relies
00063         // on immediate handling of incoming messages. Waiting for user's response
00064         // causes this logic to fail. Would need a separate thread or a call to handle_input()
00065         // within the disconnect() method in PDmxBaseDevice. (vb)
00067 //        cout << "Are you really sure you want to shut the client down?! yes or no?" << endl;
00068 //        cin >> s;
00069 //        transform(s.begin(),s.end(), s.begin(), tolower);
00070 //        if (s.compare(std::string("yes"))==0) {       
00071 //            stop();
00072 //        }
00073         stop();
00074         break;
00075     case SIGPIPE:
00076         ACE_DEBUG((DMX_DEBUG(Signal handler caught SIGPIPE. Do nothing.\n)));
00077         // do nothing
00078         // handled by DmxSocketStream and callees.
00079         break;
00080     default:
00081         ACE_ERROR((DMX_ERROR(Caught unknown signal %i\n),signum));
00082         break;
00083     }
00084     return 0;
00085 }
00086 
00087 int DmxClient::handle_timeout(const ACE_Time_Value &tv, const void* arg) {
00088     int i;
00089 
00090     for (i=base_;i<devnum_+base_;i++) {
00091        
00092 
00093        dvec_[i]->color(f);
00094        dvec_[i]->gobo(f);
00095        dvec_[i]->prism(f);
00096        dvec_[i]->dimmer(f);
00097        if (f<=100){
00098          f++;
00099 }
00100        else{
00101          f=0;
00102 }
00103         if (dvec_[i]->push()==-1) {
00104             ACE_ERROR_RETURN ((DMX_ERROR(Client could not update device %i!\n),i),-1);
00105         }       
00106         phase_+=delta_;
00107         
00108     }
00109     ACE_Reactor::instance()->schedule_timer(this, (const void*) 0, ACE_Time_Value(0l,usval_));
00110 }
00111 
00112 int DmxClient::init(int argc, char* argv[]) {
00113     if ((ACE_LOG_MSG->open(argv[0]))==-1){
00114         ACE_ERROR ((DMX_ERROR(Cannot open messager logger!\n)));
00115         exit(1);
00116     }
00117 
00119     int mpmask=ACE_LOG_MSG->priority_mask();
00120     ACE_CLR_BITS(mpmask,LM_DEBUG);
00121     ACE_SET_BITS(mpmask,LM_INFO);
00122     ACE_SET_BITS(mpmask,LM_ERROR);
00123 
00124 
00128     ACE_Get_Opt get_opt(argc,argv,"hdn:s:lt:a:b:");
00129     int c;
00130     char temp[100];
00131 
00132     while ((c=get_opt())!=EOF) {
00133         switch (c) {
00134         case 'h':
00135             printf("\nUsage: DmxClient [-h] [-d] [-n int] ... \n  h : usage\n  d : debug messages\n  s hostname : server hostname\n  n int : number of devices to create\n  l : lamp off upon exit\n  t usval : time interval between updates\n  a float : amplitude of sinuosidal excitation\n  b int : uid of first device to be created\n");
00136             exit(0);
00137             break;
00138 
00139         case 'd':
00140             ACE_DEBUG((DMX_INFO(Debug info verbose\n)));
00141             ACE_SET_BITS(mpmask,LM_DEBUG);
00142             break;
00143         case 'n':
00144             if ((devnum_=atoi(get_opt.optarg))<0) {
00145                 ACE_ERROR_RETURN ((DMX_ERROR(Invalid number of devices: %i\n),devnum_),-1);
00146             }
00147             break;
00148         case 's':
00149             strcpy(temp,get_opt.optarg);
00150             hostname_=temp;
00151             break;
00152         case 'l':
00153             lampoff_=true;
00154             break;
00155         case 't':
00156             if ((usval_=atoi(get_opt.optarg))<0) {
00157                 ACE_ERROR_RETURN ((DMX_ERROR(Invalid time constant: %i\n),usval_),-1);
00158             }
00159             break;
00160         case 'a':
00161             if ((amplitude_=atof(get_opt.optarg))<0) {
00162                 ACE_ERROR_RETURN ((DMX_ERROR(Invalid amplitude: %f\n),amplitude_),-1);
00163             }           
00164             break;
00165         case 'b':
00166             if ((base_=atoi(get_opt.optarg))<0) {
00167                 ACE_ERROR_RETURN ((DMX_ERROR(Invalid base uid: %i\n),base_),-1);
00168             } 
00169             break;
00170 
00171         default:
00172             ACE_DEBUG((DMX_INFO(Unknown command-line option -%c\n),c));
00173             break;
00174         }
00175     }
00176 
00177     ACE_DEBUG((DMX_INFO(%i devices to be created.\n),devnum_));
00178     ACE_DEBUG((DMX_INFO(Client expect running dmx server on %s.\n),hostname_.c_str()));           
00179 
00180     ACE_LOG_MSG->priority_mask(mpmask);
00181     ACE_LOG_MSG->priority_mask(mpmask,ACE_Log_Msg::PROCESS);
00182 
00183     const u_short PORT = DMX_SERVER_DEFAULT_PORT ;
00184     
00185     int i;
00186 
00187     dvec_.resize(devnum_);
00188 
00189     PlanarObject *p1;
00190     p1=new RectangularRegion;
00191     p1->setXYPlane(0);
00192     p1->setRegion(Vector2d(2,2),Vector2d(4,4));
00193   
00194     for (i=base_;i<devnum_+base_;i++) {
00195         ACE_DEBUG ((DMX_INFO(Creating Device with uid %i\n),i));
00196         dvec_[i]=new PDmxLightDevice(i);
00197     }
00198 
00200     ACE_INET_Addr addr(PORT, hostname_.c_str());
00201 
00202     for (i=base_;i<devnum_+base_;i++) {
00203         ACE_DEBUG((DMX_INFO(Connecting Device %i\n),i));
00204         if (dvec_[i]->connect(addr)==-1) {
00205             ACE_ERROR_RETURN ((DMX_ERROR(Client could not connect device %i!\n),i),-1);
00206         }
00207     
00208         dvec_[i]->color(0);
00209         dvec_[i]->gobo(0);
00210         dvec_[i]->prism(0);
00211         dvec_[i]->dimmer(0);
00212   //      dvec_[i]->setOrientation(Vector2d(4,4),p1);
00213         if (dvec_[i]->push()==-1) {
00214             ACE_ERROR_RETURN ((DMX_ERROR(Client could not initialize device %i!\n),i),-1);
00215         }
00216     }
00217 
00218     ACE_Reactor* preactor = ACE_Reactor::instance();
00219 
00220     preactor->register_handler(SIGINT,this);
00221     preactor->register_handler(SIGPIPE,this);
00222     preactor->schedule_timer(this, (const void*) 0, ACE_Time_Value(0l,usval_));
00223 
00224     delta_=M_PI/16;
00225     phase_=0.0;
00226 }
00227 
00228 int DmxClient::wrapup() {
00229     int i;
00230     for (i=base_;i<devnum_+base_;i++) {
00231         ACE_DEBUG((DMX_INFO(Disconnecting Device %i\n),i));
00232         if (lampoff_) {
00233             ACE_DEBUG((DMX_INFO(Lamp Off %i\n),i));
00234             dvec_[i]->shutter(false);
00235             dvec_[i]->lamp(false);
00236         }
00237         if (dvec_[i]->disconnect()==-1) {
00238             ACE_ERROR_RETURN ((DMX_ERROR(Client could not disconnect device %i!\n),i),-1);
00239         }
00240     }
00241 }
00242 int main(int argc, char* argv[]) {
00243     DmxClient client;
00244     
00245     client.init(argc,argv);
00246 
00247     while (!client.finished()){   
00248       ACE_Reactor::instance()->handle_events();
00249     }
00250 
00251     client.wrapup();
00252 
00253     return 0;
00254 }
00255 
00256 /*
00257  * $Log: DmxALClient.cc,v $
00258  * Revision 1.3  2002/03/14 13:56:52  abaebler
00259  * these are test c++ clients.
00260  * warning for missing return statement fxd.
00261  *
00262  * Revision 1.2  2002/02/28 18:56:32  abaebler
00263  * gcc3.0 compatible.
00264  *
00265  * Revision 1.1  2001/10/31 18:31:49  abaebler
00266  * Ambientlight standalone client to test Ambient light
00267  *
00268  * Revision 1.12  2001/08/16 17:36:39  expodev
00269  * dimmer set to 10% dimming instead of 90%
00270  *
00271  * Revision 1.11  2001/06/19 18:40:50  vincent
00272  * many changes. see release-0-1-5 in RELEASE
00273  *
00274  * Revision 1.10  2001/05/14 10:28:17  expodev
00275  * Added science et cite changes
00276  *
00277  * Revision 1.9  2001/04/17 22:59:27  abaebler
00278  * geolib works + more. See RELEASE files dmxlib
00279  *
00280  * Revision 1.8  2001/04/04 18:53:12  vincent
00281  * Many changes. See RELEASE file.
00282  *
00283  * Revision 1.7  2001/03/22 13:04:20  vincent
00284  * Added RCSID to cc files. Consolidated log messages. Updated client.
00285  *
00286  * Revision 1.6  2001/03/21 12:39:39  vincent
00287  * New client. New error logging macros. New file id macro. 
00288  * Exceptions are now throw by DmxSocketStream and 
00289  * DmxDevicePool and caught by others.
00290  *
00291  */

Generated on Tue Jul 6 17:54:11 2004 for dmxlib by doxygen1.2.18