Actor.cpp

Go to the documentation of this file.
00001 /* ****************************************************************** **
00002 **    OpenSees - Open System for Earthquake Engineering Simulation    **
00003 **          Pacific Earthquake Engineering Research Center            **
00004 **                                                                    **
00005 **                                                                    **
00006 ** (C) Copyright 1999, The Regents of the University of California    **
00007 ** All Rights Reserved.                                               **
00008 **                                                                    **
00009 ** Commercial use of this program without express permission of the   **
00010 ** University of California, Berkeley, is strictly prohibited.  See   **
00011 ** file 'COPYRIGHT'  in main directory for information on usage and   **
00012 ** redistribution,  and for a DISCLAIMER OF ALL WARRANTIES.           **
00013 **                                                                    **
00014 ** Developed by:                                                      **
00015 **   Frank McKenna (fmckenna@ce.berkeley.edu)                         **
00016 **   Gregory L. Fenves (fenves@ce.berkeley.edu)                       **
00017 **   Filip C. Filippou (filippou@ce.berkeley.edu)                     **
00018 **                                                                    **
00019 ** ****************************************************************** */
00020                                                                         
00021 // $Revision: 1.4 $
00022 // $Date: 2005/11/23 18:24:30 $
00023 // $Source: /usr/local/cvs/OpenSees/SRC/actor/actor/Actor.cpp,v $
00024                                                                         
00025                                                                         
00026 // Written: fmk
00027 // Revision: A
00028 //
00029 // Purpose: This file contains the implementation of Actor.
00030 //
00031 // What: "@(#) Actor.C, revA"
00032 
00033 #include <Actor.h>
00034 #include <Channel.h>
00035 #include <ChannelAddress.h>
00036 #include <Message.h>
00037 #include <MovableObject.h>
00038 #include <Matrix.h>
00039 #include <Vector.h>
00040 #include <ID.h>
00041 
00042 // Actor
00043 //      constructor to init the list.
00044 
00045 
00046 Actor::Actor(Channel &theChan,
00047              FEM_ObjectBroker &myBroker,
00048              int numActorMethods)
00049 :theBroker(&myBroker), theChannel(&theChan),
00050  numMethods(0), maxNumMethods(numActorMethods), actorMethods(0), 
00051  theRemoteShadowsAddress(0), commitTag(0)
00052 {
00053   // call setUpActor on the channel and get shadows address
00054   theChannel->setUpConnection();
00055   theRemoteShadowsAddress = theChan.getLastSendersAddress();
00056 
00057   if (numActorMethods != 0)
00058     actorMethods = new ActorMethod *[numActorMethods];
00059   
00060   if (actorMethods == 0)
00061     maxNumMethods = 0;
00062   
00063   for (int i=0; i<numMethods; i++)
00064     actorMethods[i] = 0;
00065 }
00066 
00067 
00068 Actor::~Actor()
00069 {
00070   // delete the array of actorMethods if constructed one
00071   if (actorMethods != 0)
00072     delete actorMethods;
00073 }
00074 
00075 
00076 // void AddMethod(int tag, int (*fp)()):
00077 //      Method to add a function to the list of avaiable actor methods.
00078 //      The function will be identified as tag, it is a function with
00079 //      no args that returns an int.
00080 
00081 int 
00082 Actor::addMethod(int tag, int (*fp)())
00083 {
00084     // check we are not over our limit
00085     if (numMethods >= maxNumMethods)
00086         return -2;
00087 
00088     // check no other with the same tag exists
00089     ActorMethod *methodPtr;
00090     for (int i=0; i<numMethods; i++) {
00091         methodPtr = actorMethods[i];
00092         if (methodPtr->tag == tag)
00093             return -1;
00094     }
00095 
00096     // add the new method
00097     ActorMethod *newMethod = new ActorMethod;
00098     if (newMethod == 0)
00099         return -3;
00100     
00101     newMethod->tag = tag;
00102     newMethod->theMethod = fp;
00103 
00104     actorMethods[numMethods] = newMethod;
00105     numMethods++;
00106     return 0;
00107 }
00108 
00109 
00110 
00111 
00112 
00113 // int GetMethod():
00114 //      Method to return the integer tag of the next method the actor
00115 //      has been asked to invoke.
00116 
00117 int 
00118 Actor::getMethod()
00119 {
00120     int method = -1;
00121     Message msg(&method,1);
00122     this->recvMessage(msg);
00123     return method;
00124 }
00125 
00126 
00127 
00128 
00129 // int ProcMethod(int tag):
00130 //      Method to process the function whose id is tag.
00131 
00132 int 
00133 Actor::processMethod(int tag)
00134 {
00135     ActorMethod *current =0;
00136 
00137     for (int i=0; i<numMethods; i++)
00138         if (actorMethods[i]->tag == tag) {
00139             current = actorMethods[tag];
00140         }
00141 
00142     if (current == 0)
00143         return -1;
00144     
00145     return (*current).theMethod();
00146 }
00147 
00148 
00149 
00150 int
00151 Actor::sendObject(MovableObject &theObject, 
00152                   ChannelAddress *theAddress )
00153 {
00154     if (theAddress == 0)
00155         return theChannel->sendObj(commitTag, theObject,theRemoteShadowsAddress);
00156     else
00157         return theChannel->sendObj(commitTag, theObject,theAddress);    
00158 }
00159 
00160 int
00161 Actor::recvObject(MovableObject &theObject, 
00162                   ChannelAddress *theAddress )
00163 {
00164     if (theAddress == 0)
00165         return theChannel->recvObj(commitTag, theObject,*theBroker,
00166                                    theRemoteShadowsAddress); 
00167     else
00168         return theChannel->recvObj(commitTag, theObject,*theBroker,theAddress); 
00169 }
00170 
00171 
00172 int
00173 Actor::recvMessage(Message &theMessage, ChannelAddress *theAddress )
00174 {
00175     if (theAddress == 0)
00176         return theChannel->recvMsg(0, commitTag, theMessage,theRemoteShadowsAddress);
00177     else
00178         return theChannel->recvMsg(0, commitTag, theMessage,theAddress);        
00179 }
00180 
00181 int
00182 Actor::sendMessage(const Message &theMessage, ChannelAddress *theAddress )
00183 {
00184     if (theAddress == 0)
00185         return theChannel->sendMsg(0, commitTag, theMessage,theRemoteShadowsAddress);
00186     else
00187         return theChannel->sendMsg(0, commitTag, theMessage,theAddress);        
00188 }
00189 
00190 
00191 
00192 int
00193 Actor::sendMatrix(const Matrix &theMatrix, ChannelAddress *theAddress )
00194 {
00195     if (theAddress == 0)    
00196         return theChannel->sendMatrix(0, commitTag, theMatrix,theRemoteShadowsAddress);
00197     else
00198         return theChannel->sendMatrix(0, commitTag, theMatrix,theAddress);      
00199 }
00200 
00201 int
00202 Actor::recvMatrix(Matrix &theMatrix, ChannelAddress *theAddress )
00203 {
00204     if (theAddress == 0)
00205         return theChannel->recvMatrix(0, commitTag, theMatrix,theRemoteShadowsAddress);
00206     else
00207         return theChannel->recvMatrix(0, commitTag, theMatrix,theAddress);      
00208 }
00209 
00210 int
00211 Actor::sendVector(const Vector &theVector, ChannelAddress *theAddress )
00212 {
00213     if (theAddress == 0)
00214         return theChannel->sendVector(0, commitTag, theVector,theRemoteShadowsAddress);
00215     else
00216         return theChannel->sendVector(0, commitTag, theVector,theAddress);      
00217 }
00218 
00219 int
00220 Actor::recvVector(Vector &theVector, ChannelAddress *theAddress )
00221 {
00222     if (theAddress == 0)
00223         return theChannel->recvVector(0, commitTag, theVector,theRemoteShadowsAddress);
00224     else
00225         return theChannel->recvVector(0, commitTag, theVector,theAddress);      
00226 }
00227 
00228 int
00229 Actor::sendID(const ID &theID, ChannelAddress *theAddress )
00230 {
00231     if (theAddress == 0)
00232         return theChannel->sendID(0, commitTag, theID,theRemoteShadowsAddress);
00233     else
00234         return theChannel->sendID(0, commitTag, theID,theAddress);      
00235 }
00236 
00237 int
00238 Actor::recvID(ID &theID, ChannelAddress *theAddress )
00239 {
00240     if (theAddress == 0)
00241         return theChannel->recvID(0, commitTag, theID,theRemoteShadowsAddress);
00242     else
00243         return theChannel->recvID(0, commitTag, theID,theAddress);      
00244 }
00245 
00246 
00247 void
00248 Actor::setCommitTag(int tag)
00249 {
00250   commitTag = tag;
00251 }
00252 
00253 
00254 Channel *
00255 Actor::getChannelPtr(void) const
00256 {
00257     return theChannel;
00258 }
00259 
00260 FEM_ObjectBroker *
00261 Actor::getObjectBrokerPtr(void) const
00262 {
00263     return theBroker;
00264 }
00265 
00266 
00267 
00268 ChannelAddress *
00269 Actor::getShadowsAddressPtr(void) const
00270 {
00271     return theRemoteShadowsAddress;
00272 }
00273 
00274 
00275 // barrier check:
00276 //
00277 int
00278 Actor::barrierCheck(int myResult = 0)
00279 {
00280   int result;
00281   static ID data(1);
00282   data(0) = myResult; 
00283   theChannel->sendID(0, commitTag, data); 
00284   theChannel->recvID(0, commitTag, data);
00285   result = data(0);
00286   return result;
00287 }
00288 

Generated on Mon Oct 23 15:04:56 2006 for OpenSees by doxygen 1.5.0