00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <MachineBroker.h>
00029 #include <OPS_Globals.h>
00030 #include <ID.h>
00031 #include <Channel.h>
00032
00033 #include <Actor.h>
00034 #include <FEM_ObjectBroker.h>
00035
00036 #include <OPS_Globals.h>
00037
00038 MachineBroker::MachineBroker(FEM_ObjectBroker *theBroker)
00039 :theObjectBroker(theBroker), actorChannels(0), numActorChannels(0), numActiveChannels(0), activeChannels(0)
00040 {
00041
00042 }
00043
00044 MachineBroker::~MachineBroker()
00045 {
00046 if (actorChannels != 0) {
00047 delete [] actorChannels;
00048 delete activeChannels;
00049 }
00050 }
00051
00052
00053 int
00054 MachineBroker::shutdown(void)
00055 {
00056
00057 for (int i=0; i<numActorChannels; i++) {
00058 ID idData(1);
00059 idData(0) = 0;
00060 Channel *theChannel = actorChannels[i];
00061 if (theChannel->sendID(0, 0, idData) < 0) {
00062 opserr << "MachineBroker::shutdown(void) - failed to send ID\n";
00063 }
00064
00065 if (theChannel->recvID(0, 0, idData) < 0) {
00066 opserr << "MachineBroker::shutdown(void) - failed to recv ID\n";
00067 }
00068
00069 this->freeProcess(theChannel);
00070 }
00071
00072 if (actorChannels != 0) {
00073 delete [] actorChannels;
00074 delete activeChannels;
00075 actorChannels = 0;
00076 activeChannels = 0;
00077 numActorChannels = 0;
00078 numActiveChannels = 0;
00079 }
00080
00081 return 0;
00082 }
00083
00084
00085 int
00086 MachineBroker::runActors(void)
00087 {
00088 Channel *theChannel = this->getMyChannel();
00089 ID idData(1);
00090 int done = 0;
00091
00092
00093 while (done == 0) {
00094
00095 if (theChannel->recvID(0, 0, idData) < 0) {
00096 opserr << "MachineBroker::runActors(void) - failed to recv ID\n";
00097 }
00098
00099 int actorType = idData(0);
00100
00101
00102 if (idData(0) == 0) {
00103 done = 1;
00104
00105 if (theChannel->sendID(0, 0, idData) < 0) {
00106 opserr << "MachineBroker::run(void) - failed to send ID\n";
00107 }
00108
00109 return 0;
00110
00111 } else {
00112
00113
00114 Actor *theActor = theObjectBroker->getNewActor(actorType, theChannel);
00115 if (theActor == 0) {
00116 opserr << "MachineBroker::run(void) - invalid actor type\n";
00117 idData(0) = 1;
00118 } else
00119 idData(0) = 0;
00120
00121
00122 if (theChannel->sendID(0, 0, idData) < 0) {
00123 opserr << "MachineBroker::run(void) - failed to send ID\n";
00124 }
00125
00126
00127 if (theActor->run() != 0) {
00128 opserr << "MachineBroker::run(void) - actor failed while running\n";
00129 }
00130
00131
00132 delete theActor;
00133 }
00134 done = 0;
00135 }
00136
00137 return 0;
00138 }
00139
00140
00141 Channel *
00142 MachineBroker::startActor(int actorType, int compDemand)
00143 {
00144 if (compDemand != 0)
00145 opserr << "MachineBroker::startActor() - does not take computational demand variable into account\n";
00146
00147 Channel *theChannel = 0;
00148
00149
00150 if (numActiveChannels < numActorChannels) {
00151
00152 for (int i=0; i<numActorChannels; i++) {
00153 if ((*activeChannels)(i) == 0) {
00154 theChannel = actorChannels[i];
00155 i=numActorChannels;
00156 numActiveChannels++;
00157 (*activeChannels)(i) = 1;
00158 }
00159 }
00160 }
00161
00162
00163 if (theChannel == 0) {
00164
00165 theChannel = this->getRemoteProcess();
00166
00167 if (theChannel == 0) {
00168 opserr << "MachineBroker::startActor() - no available channel available\n";
00169 return 0;
00170 }
00171
00172 Channel **nextChannels = new Channel *[numActorChannels +1];
00173 ID *nextChannelID = new ID(numActorChannels+1);
00174 for (int i=0; i<numActorChannels; i++) {
00175 nextChannels[i] = actorChannels[i];
00176 (*nextChannelID)(i) = (*activeChannels)(i);
00177 }
00178
00179 nextChannels[numActorChannels] = theChannel;
00180 (*nextChannelID)(numActorChannels) = 0;
00181
00182
00183 if (actorChannels != 0) {
00184 delete [] actorChannels;
00185 delete activeChannels;
00186 }
00187
00188
00189 actorChannels = nextChannels;
00190 activeChannels = nextChannelID;
00191 numActorChannels++;
00192 numActiveChannels++;
00193 }
00194
00195
00196 ID idData(1);
00197 idData(0) = actorType;
00198 if (theChannel->sendID(0, 0, idData) != 0) {
00199 opserr << "MachineBroker::startActor() - failed to send actorType\n";
00200 this->freeProcess(theChannel);
00201 return 0;
00202 }
00203
00204 if (theChannel->recvID(0, 0, idData) != 0) {
00205 opserr << "MachineBroker::startActor() - remote process failure\n";
00206 return 0;
00207 }
00208 if (idData(0) != 0) {
00209 opserr << "MachineBroker::startActor() - could not start actorType: " << actorType << endln;
00210 this->freeProcess(theChannel);
00211 return 0;
00212 }
00213
00214 return theChannel;
00215
00216 }
00217
00218 int
00219 MachineBroker::finishedWithActor(Channel *theChannel)
00220 {
00221
00222 for (int i=0; i<numActorChannels; i++) {
00223 if (theChannel == actorChannels[i]) {
00224 numActiveChannels--;
00225 (*activeChannels)(i) = 0;
00226 return 0;
00227 }
00228 }
00229
00230
00231 return -1;
00232 }