Changeset 229 for Evidence/Bridge.cc


Ignore:
Timestamp:
06/24/10 07:51:15 (14 years ago)
Author:
ogrimm
Message:
Config requests non-blocking if not made from main thread, adapted all servers to GetConfig() returning std::string, workaround for erroneous SERVICE_LIST
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Bridge.cc

    r224 r229  
    2424using namespace std;
    2525
    26 
    2726// Class declaration
    2827class Bridge: public DimClient, public EvidenceServer {
    2928
    3029        struct Item {
    31           string Name;
    3230          DimCommand *Command;
    3331          DimStampedInfo *DataItem;
     
    3533          char *Data;
    3634        };
    37         vector<struct Item> List;
     35        map<string, struct Item> Map;
    3836
    3937        DimInfo *ServerList;
     
    4139    void infoHandler();
    4240    void commandHandler();
     41        public:
    4342        void AddService(string, char *, int);
    4443        void RemoveService(string);
    45    
     44        void BugFix();
     45
    4646  public:
    4747    Bridge(char *, int);
     
    6363Bridge::~Bridge() {
    6464
    65   while (List.size() != 0) RemoveService(List[0].Name); 
     65  while (Map.size() != 0) RemoveService((*Map.begin()).first); 
    6666  delete ServerList;
    6767}
     
    7777
    7878  // If service is DIS_DNS/SERVER_LIST, subscribe to all SERVICE_LIST services
    79   if (strcmp(I->getName(), "DIS_DNS/SERVER_LIST") == 0) {       
     79  if (strcmp(I->getName(), "DIS_DNS/SERVER_LIST") == 0) {
     80    printf("DIS_DNS/SERVER_LIST: '%s'\n", I->getString());
    8081        char *Token = strtok(I->getString(), "+-!@");   
    8182        while (Token != NULL) {
     
    9495  // If service is SERVICE_LIST, scan and subscribe/unsubscribe to services
    9596  if (strstr(I->getName(), "/SERVICE_LIST") != NULL) {
     97
     98        // Bug fix for empty SERVICE_LIST
     99        if (strlen(I->getString()) == 0) {
     100          string Tmp(I->getName());
     101          RemoveService(I->getName());
     102          AddService(Tmp.c_str(), (char *) "C", DimSERVICE);
     103          return;
     104        }
     105
    96106        char *Format, *Name = strtok(I->getString(), "+-!|");
    97107        while (Name != NULL) {
     
    131141  }
    132142
    133   // Identify service and repeat to secondary DNS
    134   for (int Service=0; Service<List.size(); Service++) if (I == List[Service].DataItem) {
    135 
    136     // Copy service data
    137     delete[] List[Service].Data;
    138     List[Service].Data = new char [I->getSize()];
    139     memcpy(List[Service].Data, I->getData(), I->getSize());
    140 
    141         // Set new service properties and update service
    142     List[Service].Service->setQuality(I->getQuality());
    143     List[Service].Service->setTimestamp(I->getTimestamp(), I->getTimestampMillisecs());
    144         List[Service].Service->updateService(List[Service].Data, I->getSize());
    145   }
     143  // Check if service known and repeat to secondary DNS
     144  if (Map.count(I->getName()) == 0) return;
     145
     146  // Copy service data
     147  delete[] Map[I->getName()].Data;
     148  Map[I->getName()].Data = new char [I->getSize()];
     149  memcpy(Map[I->getName()].Data, I->getData(), I->getSize());
     150
     151  // Set new service properties and update service
     152  Map[I->getName()].Service->setQuality(I->getQuality());
     153  Map[I->getName()].Service->setTimestamp(I->getTimestamp(), I->getTimestampMillisecs());       
     154  Map[I->getName()].Service->updateService(Map[I->getName()].Data, I->getSize());
    146155}
    147156
     
    161170
    162171  // Check if already subscribed to this service
    163   for (int i=0; i<List.size(); i++) {
    164         if (Name == List[i].Name) return;
    165   }
     172  if (Map.count(Name) != 0) return;
    166173
    167174  // Create subscription and service to secondary DNS or new command
    168   struct Item New = {Name, NULL, NULL, NULL, NULL};
    169 
     175  Map[Name].Command = NULL;
     176  Map[Name].DataItem = NULL;
     177  Map[Name].Service = NULL;
     178  Map[Name].Data = NULL;
     179 
    170180  if  (Type == DimSERVICE) { 
    171         New.Service = new DimService(Name.c_str(), (char *) Format, New.Data, 0);
    172         New.DataItem = new DimStampedInfo(Name.c_str(), NO_LINK, this);
    173   }
    174   else if (Type == DimCOMMAND) New.Command = new DimCommand(Name.c_str(), Format, this);
    175 
    176   List.push_back(New);
     181        Map[Name].Service = new DimService(Name.c_str(), (char *) Format, Map[Name].Data, 0);
     182        Map[Name].DataItem = new DimStampedInfo(Name.c_str(), NO_LINK, this);
     183  }
     184  else if (Type == DimCOMMAND) Map[Name].Command = new DimCommand(Name.c_str(), Format, this);
    177185}
    178186
     
    181189void Bridge::RemoveService(string Name) {
    182190
    183   vector<struct Item>::iterator E;
    184   for (E=List.begin(); E<List.end(); ++E) if (Name == (*E).Name) {
    185         delete (*E).DataItem;
    186         delete (*E).Service;
    187         delete[] (*E).Data;
    188         delete (*E).Command;
    189 
    190         List.erase(E);
    191   }
     191  // Check if actually subscribed to service 
     192  if (Map.count(Name) == 0) return;
     193
     194  delete Map[Name].DataItem;
     195  delete Map[Name].Service;
     196  delete[] Map[Name].Data;
     197  delete Map[Name].Command;
     198
     199  Map.erase(Name);
    192200}
    193201
     
    206214 
    207215  // Sleep until signal caught
    208   pause();
    209 }
     216  while (!Class.ExitRequest) pause();
     217}
Note: See TracChangeset for help on using the changeset viewer.