00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "NodeManager.h"
00012
00014 Define_Module(NodeManager);
00015
00019 NodeManager::NodeManager()
00020 {
00021 }
00022
00026 NodeManager::~NodeManager()
00027 {
00028 }
00029
00033 cModule*
00034 NodeManager::isRegistered(const host_id *hostId)
00035 {
00036 cModule *host = NULL;
00037 cModuleType *moduleType = cModuleType::find("VirtualHost");
00038 int numHosts = par("numRegisteredHosts");
00039 ev << "isRegistered(): id = " << std::string((char *) hostId) << "\n";
00040 for (SubmoduleIterator submod(getParentModule()); !submod.end(); submod++)
00041 {
00042 host = submod();
00043 if (moduleType == host->getModuleType())
00044 {
00045 std::string id = host->par("hostId");
00046 if ((std::string((char *) hostId).compare(id)) == 0)
00047 {
00048 ev << " host = " << host << "\n";
00049 return host;
00050 }
00051 }
00052 }
00053 ev << "host = NULL ==> unknown host!\n";
00054 return NULL;
00055 }
00056
00060 void
00061 NodeManager::addNode(registrationMsg *regMsg)
00062 {
00063 int port = 0;
00064 char ipAddr[40];
00065 int registeredHosts = par("numRegisteredHosts");
00066
00067
00068 host_id *hostId = (host_id *) &((regMsg->header).id);
00069 registrationInfo *regInfo = &(regMsg->info);
00070 std::string hostName = (char *) &(regInfo->name);
00071 if (regInfo->addr.ss_family == AF_INET)
00072 {
00073 inet_ntop(AF_INET,
00074 &((struct sockaddr_in *) ®Info->addr)->sin_addr,
00075 ipAddr, 40);
00076 port = ntohs(((struct sockaddr_in *) ®Info->addr)->sin_port);
00077 }
00078 if (regInfo->addr.ss_family == AF_INET6)
00079 {
00080 inet_ntop(AF_INET,
00081 &((struct sockaddr_in6 *) ®Info->addr)->sin6_addr,
00082 ipAddr, 40);
00083 port = ntohs(((struct sockaddr_in6 *) ®Info->addr)->sin6_port);
00084 }
00085
00086 ev << getFullPath() << ": registering host " << hostName;
00087 ev << " (id: " << std::string((char *) hostId) << ")";
00088 ev << ", " << ipAddr << ":" << port;
00089 ev << ", Vifs: " << regInfo->numVif << "\n";
00090
00091
00092 cModuleType *moduleType = cModuleType::find("VirtualHost");
00093 cModule *virtualHost = moduleType->create("host", getParentModule(), registeredHosts + 1, registeredHosts);
00094
00095
00096 int numVif = regInfo->numVif;
00097 if (numVif > MAX_NUM_VIF)
00098 {
00099 numVif = MAX_NUM_VIF;
00100 ev << getFullPath() << ".addNode(): MAX_NUM_VIF = " << MAX_NUM_VIF << "; ";
00101 ev << "only registering " << MAX_NUM_VIF << " of " << numVif << "interfaces\n";
00102 }
00103
00104
00105 virtualHost->par("hostId") = std::string((char *) hostId);
00106 virtualHost->par("hostName") = hostName;
00107 virtualHost->par("ipAddress") = ipAddr;
00108 virtualHost->par("port") = port;
00109 virtualHost->par("numVif") = numVif;
00110 virtualHost->finalizeParameters();
00111 virtualHost->buildInside();
00112
00113 for (int i = 0; i < numVif; i++)
00114 {
00115 MACAddress *macAddr = new MACAddress();
00116 macAddr->setAddressBytes((unsigned char *) &(regInfo->vifData[i].vifMac));
00117
00118 cModule *wlan = check_and_cast<cModule *>(virtualHost->getSubmodule("ath", i));
00119 Ieee80211Mac *mac = check_and_cast<Ieee80211Mac *>(wlan->getSubmodule("mac"));
00120 mac->par("address") = macAddr->str();
00121 }
00122
00123
00124 virtualHost->callInitialize();
00125
00126
00127 MsgHandler *handler = check_and_cast<MsgHandler *>(virtualHost->getSubmodule("msgHandler"));
00128 handler->initializeVifParameters(regMsg->info.vifData);
00129
00130 virtualHost->scheduleStart(simTime());
00131
00132
00133 par("numRegisteredHosts") = registeredHosts + 1;
00134 handleParameterChange("numRegisteredHosts");
00135
00136
00137 unsigned int msgId = regInfo->msgId;
00138 handler->sendAck(msgId);
00139 }
00140
00141 void
00142 NodeManager::removeNode(cModule *host)
00143 {
00144 std::string hostName = host->par("hostName");
00145 std::string hostId = host->par("hostId");
00146 ev << getFullPath() << ": removing host " << hostName;
00147 ev << " (id: " << hostId << ")\n";
00148 host->deleteModule();
00149
00150
00151 int registeredHosts = par("numRegisteredHosts");
00152 par("numRegisteredHosts") = registeredHosts - 1;
00153 handleParameterChange("numRegisteredHosts");
00154 }
00155
00156 void
00157 NodeManager::handleParameterChange(const char *parname)
00158 {
00159
00160 }
00161
00162