MicoleAgent.cpp

Go to the documentation of this file.
00001 /*
00002  * This file is part of Micole Architecture
00003  *
00004  * Copyright (C) 2007 Micole partners
00005  *
00006  * Micole Architecture is free software: you can redistribute it 
00007  * and/or modify it under the terms of the GNU Lesser General 
00008  * Public License as published by the Free Software Foundation, 
00009  * either version 3 of the License, or (at your option) any 
00010  * later version.
00011  *
00012  * Micole Architecture is distributed in the hope that it will be 
00013  * useful, * but WITHOUT ANY WARRANTY; without even the implied 
00014  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
00015  * PURPOSE.  See the GNU Lesser General Public License for more 
00016  * details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with Micole Architecture.  If not, see
00020  * <http://www.gnu.org/licenses/>.
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                 //bus->start(domain);
00038         }
00039 
00040         bindMessage("^Quit$", BUS_CALLBACK_OF(MicoleAgent, handleQuitMessage ));
00041         /*bindMessage("^Ping$", BUS_CALLBACK_OF(MicoleAgent, handlePingMessage ));
00042         
00043         bindMessage("^Start$", BUS_CALLBACK_OF(MicoleAgent, handleStartMessage));
00044         bindMessage("^Print$", BUS_CALLBACK_OF(MicoleAgent, handlePrintIDMessage));
00045         bindMessage("^RegisterReply:(.*)", BUS_CALLBACK_OF(MicoleAgent, handleRegisterReply));
00046         bindMessage("^Suspend$", BUS_CALLBACK_OF(MicoleAgent, handleSuspendMessage ));*/
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         //sendDirectMessage("bus",1,message);
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 //Ivy::BindMsg(...) returns new regexp_id every time it's called. So, we
00087 //need to update the bindings after calling BindMsg().
00088 int MicoleAgent::bindMessage(const char *regexp, MicoleCallback * cb)
00089 {
00090         MicoleBinding b(regexp, bus, cb);
00091 
00092         bindings.push_back(b);
00093         //return b.getId();
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         //@TODO: handle the unbinds...
00116         for (vector<MicoleBinding>::iterator i = bindings.begin(); i != bindings.end(); i++)
00117                 unBindMessage(*i);*/
00118 
00119         //bus->UnbindMsg(resumeId);
00120         //bus->UnbindMsg(printId);
00121         //bus->UnbindMsg(startId);
00122         //bus->UnbindMsg(regReplyId); //@TODO cause crash, unknown bind
00123         //bus->UnbindMsg(pingId);
00124         //bus->UnbindMsg(quitId);
00125         stopAgent();
00126         state = UNKNOWN;
00127 }
00128 
00129 
00130 void MicoleAgent::handlePingMessage(MicoleBus *app, int argc, const char **argv)
00131 {
00132         //cout << getAgentName() << " received msg: PING! " << endl;
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         /*cout<<"Suspending MicoleAgent"<<endl;
00149         for (unsigned i = 0 ; i < bindings.size() ; ++i)
00150                 unBindMessage(bindings[i]);
00151 
00152         resumeId = bus->BindMsg("^Resume$",BUS_CALLBACK_OF(MicoleAgent, handleResumeMessage ));
00153         */
00154         //bindings.erase(bindings.begin(),bindings.end());
00155 }
00156 
00157 
00158 void MicoleAgent::resumeAgent()
00159 {
00160         /*
00161                 -re-bind the agen's messages
00162                 -unbind resume handler
00163         *//*
00164         for (unsigned i = 0 ; i < bindings.size() ; ++i)
00165                 bindMessage( bindings[i]);
00166 
00167         bus->UnbindMsg(resumeId);*/
00168 }
00169 
00170 void MicoleAgent::stopAgent()
00171 {
00172         //bus->stop(); crashes the program
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 void SuspendMsgListener::OnDirectMessage(MicoleBus *app, int id, const char *arg){
00195         
00196         if(id == 1)
00197         {
00198                 cout<<"Trying to suspend MicoleAgent..."<<id<<endl;
00199                 master->suspendAgent();
00200         }
00201         
00202 }
00203 
00204 SuspendMsgListener::SuspendMsgListener(MicoleAgent *m){
00205 
00206         master = m;
00207 }
00208 */
00209 

Generated on Tue Oct 16 17:10:42 2007 for Micole by  doxygen 1.4.7