00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "stdafx.h"
00024 #include "MicoleAgent.h"
00025 #include "MicoleStringStream.h"
00026
00027 using namespace AgentModuleConstants;
00028
00029 MicoleBus * MicoleAgent::bus = 0;
00030
00031 MicoleAgent::MicoleAgent(const string &name, const string &type, const char *domain)
00032 :agentName(name), agentType(type), state(ACTIVE)
00033 {
00034 cout<< "MicoleAgent constructor 1: " << name <<endl;
00035 if (!bus){
00036 bus = new MicoleBus(true);
00037
00038 }
00039
00040 bindMessage("^Quit$", BUS_CALLBACK_OF(MicoleAgent, handleQuitMessage ));
00041
00042
00043
00044
00045
00046
00047
00048 }
00049
00050 MicoleAgent::~MicoleAgent()
00051 {
00052 if(bus)
00053 {
00054 cout << "Stopping bus..." << endl;
00055 bus->stop();
00056 bus = 0;
00057 }
00058 if(bindings.size() >0)
00059 bindings.erase(bindings.begin(), bindings.end());
00060 }
00061
00062 string MicoleAgent::getAgentName()
00063 {
00064 return agentName;
00065 }
00066
00067 string MicoleAgent::getAgentType()
00068 {
00069 return agentType;
00070 }
00071
00072 int MicoleAgent::sendMessage(const char *message)
00073 {
00074
00075 bus->sendMsg(message);
00076 return 0;
00077 }
00078
00079 int MicoleAgent::sendMessage(const string message)
00080 {
00081
00082 bus->sendMsg(message.c_str());
00083 return 0;
00084 }
00085
00086
00087
00088 int MicoleAgent::bindMessage(const char *regexp, MicoleCallback * cb)
00089 {
00090 MicoleBinding b(regexp, bus, cb);
00091
00092 bindings.push_back(b);
00093
00094 return 0;
00095 }
00096
00097
00098
00099 void MicoleAgent::handleSuspendMessage( MicoleBus *app, int argc, const char **argv)
00100 {
00101 suspendAgent();
00102 state = SUSPENDED;
00103 }
00104
00105 void MicoleAgent::handleResumeMessage(MicoleBus *app, int argc, const char **argv)
00106 {
00107 resumeAgent();
00108 state = ACTIVE;
00109 }
00110
00111 void MicoleAgent::handleQuitMessage( MicoleBus *app, int argc, const char **argv)
00112 {
00113 cout<<"MicoleAgent "<<getAgentName()<<" stopping..."<<endl;
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125 stopAgent();
00126 state = UNKNOWN;
00127 }
00128
00129
00130 void MicoleAgent::handlePingMessage(MicoleBus *app, int argc, const char **argv)
00131 {
00132
00133 MicoleStringStream s;
00134 s << agentName << ": " << state;
00135
00136 }
00137
00138 void MicoleAgent::handleRegisterReply( MicoleBus *app, int argc, const char **argv)
00139 {
00140 const char* arg = (argc < 1) ? "" : argv[0];
00141 cout << "Register Reply: " << arg << endl;
00142 identifier = arg;
00143 }
00144
00145
00146 void MicoleAgent::suspendAgent()
00147 {
00148
00149
00150
00151
00152
00153
00154
00155 }
00156
00157
00158 void MicoleAgent::resumeAgent()
00159 {
00160
00161
00162
00163
00164
00165
00166
00167
00168 }
00169
00170 void MicoleAgent::stopAgent()
00171 {
00172
00173 }
00174
00175 void MicoleAgent::handleStartMessage( MicoleBus *app, int argc, const char **argv)
00176 {
00177 obtainIdentifier();
00178 }
00179
00180 void MicoleAgent::handlePrintIDMessage( MicoleBus *app, int argc, const char **argv)
00181 {
00182 cout<<"Name: "<<agentName<<"\t"<< "ID: "<<identifier<<endl;
00183 }
00184
00185 void MicoleAgent::obtainIdentifier()
00186 {
00187 if(this->agentType != "Registry")
00188 sendMessage("Register:"+agentName);
00189 else
00190 identifier = "1";
00191 }
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209