Changeset 224 for Evidence/Bridge.cc


Ignore:
Timestamp:
06/17/10 09:00:51 (14 years ago)
Author:
ogrimm
Message:
Message severity encoded now using standard DIM structure, other updates
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Evidence/Bridge.cc

    r221 r224  
    77  thus serialized by DIM and no Mutex is necessary.
    88
    9   Oliver Grimm, May 2010
     9  Remote procedure calls are bridged through their underlying services
     10  and commands.
     11 
     12  Oliver Grimm, June 2010
    1013
    1114\********************************************************************/
     
    2124using namespace std;
    2225
     26
    2327// Class declaration
    2428class Bridge: public DimClient, public EvidenceServer {
    2529
    2630        struct Item {
     31          string Name;
     32          DimCommand *Command;
    2733          DimStampedInfo *DataItem;
    2834          DimService *Service;
     
    3440       
    3541    void infoHandler();
    36         void AddService(string, const char *);
     42    void commandHandler();
     43        void AddService(string, char *, int);
    3744        void RemoveService(string);
    3845   
     
    4249};
    4350
    44 //
    4551// Constructor
    46 //
    4752Bridge::Bridge(char *Name, int Port): EvidenceServer(SERVER_NAME) {
    4853
     
    5459}
    5560
    56 //
     61
    5762// Destructor: Delete all subscriptions and services
    58 //
    5963Bridge::~Bridge() {
    6064
    61   while (List.size() != 0) RemoveService(List[0].DataItem->getName()); 
     65  while (List.size() != 0) RemoveService(List[0].Name); 
    6266  delete ServerList;
    6367}
    6468
    65 //
     69
    6670// Service subscription and repeating
    67 //
    6871void Bridge::infoHandler() {
    6972
     
    8083                RemoveService(string(Token)+"/SERVICE_LIST");
    8184          }
    82           else AddService(string(Token)+"/SERVICE_LIST", "C");
     85          else AddService(string(Token)+"/SERVICE_LIST", (char *) "C", DimSERVICE);
    8386
    8487          // Skip server IP address and process ID
     
    9194  // If service is SERVICE_LIST, scan and subscribe/unsubscribe to services
    9295  if (strstr(I->getName(), "/SERVICE_LIST") != NULL) {
    93         char *Type, *Name = strtok(I->getString(), "+-!|");
     96        char *Format, *Name = strtok(I->getString(), "+-!|");
    9497        while (Name != NULL) {
    95           // Only consider DIM services (not commands and RPCs)
    96       if (((Type = strtok(NULL, "\n")) != NULL) &&
    97                   (strstr(Type, "|CMD") == NULL) && (strstr(Type, "|RPC") == NULL)) {
    98             if (*I->getString() == '-' || *I->getString() == '!') RemoveService(Name);
     98      if ((Format = strtok(NULL, "\n")) != NULL) {
     99            // Check if service added or removed/unavailable
     100            if (*I->getString() == '-' || *I->getString() == '!') {
     101                  if (strstr(Format, "|RPC") != NULL) {
     102                        RemoveService(string(Name)+"/RpcIn");
     103                        RemoveService(string(Name)+"/RpcOut");
     104                  }
     105                  else RemoveService(Name);
     106                }
    99107                else {
    100                   Type[strlen(Type)-1] = '\0'; // Isolate service format
    101                   AddService(Name, Type);
     108                  // Determine type of service
     109                  if (strstr(Format, "|CMD") != NULL) {
     110                        *(strstr(Format, "|CMD")) = '\0';
     111                        AddService(Name, Format, DimCOMMAND);
     112                  }
     113                  else if (strstr(Format, "|RPC") != NULL) {
     114                        *(strstr(Format, "|RPC")) = '\0';
     115                    if (strchr(Format, ',') != NULL) {
     116                          *strchr(Format, ',') = '\0';
     117                          AddService(string(Name)+"/RpcIn", Format, DimCOMMAND);
     118                          AddService(string(Name)+"/RpcOut", Format+strlen(Format)+1, DimSERVICE);
     119                        }
     120                       
     121                  }
     122                  else {
     123                        Format[strlen(Format)-1] = '\0';
     124                        AddService(Name, Format, DimSERVICE);
     125                  }
    102126                }
    103127          }
     
    110134  for (int Service=0; Service<List.size(); Service++) if (I == List[Service].DataItem) {
    111135
    112         // Ignores repeating DIS_DNS services
    113         if (List[Service].Service == NULL) break;
    114 
    115136    // Copy service data
    116137    delete[] List[Service].Data;
     
    125146}
    126147
    127 //
    128 // Add service subscription
    129 //
    130 void Bridge::AddService(string Name, const char *Format) {
     148
     149// Command repeating
     150void Bridge::commandHandler() {
     151 
     152  sendCommandNB(getCommand()->getName(), getCommand()->getData(), getCommand()->getSize());
     153}
     154
     155
     156// Service subscription
     157void Bridge::AddService(string Name, char *Format, int Type) {
     158
     159  // Do not forward  DIS_DNS and History services
     160  if ((Name.find("DIS_DNS/") != string::npos) || (Name.find("History/") != string::npos)) return;
    131161
    132162  // Check if already subscribed to this service
    133163  for (int i=0; i<List.size(); i++) {
    134         if (Name == List[i].DataItem->getName()) return;
    135   }
    136 
    137   // Create subscription and new service to secondary DNS (do not forward DIS_DNS services)
    138   struct Item New;
    139  
    140   New.Data = NULL;
    141   if (Name.find("DIS_DNS/") != string::npos) New.Service = NULL;
    142   else New.Service = new DimService(Name.c_str(), (char *) Format, New.Data, 0);
    143   New.DataItem = new DimStampedInfo(Name.c_str(), NO_LINK, this);
     164        if (Name == List[i].Name) return;
     165  }
     166
     167  // Create subscription and service to secondary DNS or new command
     168  struct Item New = {Name, NULL, NULL, NULL, NULL};
     169
     170  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
    144176  List.push_back(New);
    145177}
    146178
    147179
    148 //
    149 // Remove service from watch list
    150 //
     180// Remove service from watch list (unused pointer are NULL)
    151181void Bridge::RemoveService(string Name) {
    152182
    153   // Find service index
    154183  vector<struct Item>::iterator E;
    155   for (E=List.begin(); E<List.end(); ++E) if (Name == (*E).DataItem->getName()) {
     184  for (E=List.begin(); E<List.end(); ++E) if (Name == (*E).Name) {
    156185        delete (*E).DataItem;
    157186        delete (*E).Service;
    158187        delete[] (*E).Data;
     188        delete (*E).Command;
     189
    159190        List.erase(E);
    160191  }
     
    162193
    163194
    164 //         
    165195// Main program
    166 //
    167196int main(int argc, char *argv[]) {
    168197
     198  // Check command line argument
    169199  if (argc == 1) {
    170200        printf("Usage: %s <address of primary DNS> [port] (default port %d)\n", argv[0], DEFAULT_PORT);
Note: See TracChangeset for help on using the changeset viewer.