Changeset 224 for Evidence/Bridge.cc
- Timestamp:
- 06/17/10 09:00:51 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Evidence/Bridge.cc
r221 r224 7 7 thus serialized by DIM and no Mutex is necessary. 8 8 9 Oliver Grimm, May 2010 9 Remote procedure calls are bridged through their underlying services 10 and commands. 11 12 Oliver Grimm, June 2010 10 13 11 14 \********************************************************************/ … … 21 24 using namespace std; 22 25 26 23 27 // Class declaration 24 28 class Bridge: public DimClient, public EvidenceServer { 25 29 26 30 struct Item { 31 string Name; 32 DimCommand *Command; 27 33 DimStampedInfo *DataItem; 28 34 DimService *Service; … … 34 40 35 41 void infoHandler(); 36 void AddService(string, const char *); 42 void commandHandler(); 43 void AddService(string, char *, int); 37 44 void RemoveService(string); 38 45 … … 42 49 }; 43 50 44 //45 51 // Constructor 46 //47 52 Bridge::Bridge(char *Name, int Port): EvidenceServer(SERVER_NAME) { 48 53 … … 54 59 } 55 60 56 // 61 57 62 // Destructor: Delete all subscriptions and services 58 //59 63 Bridge::~Bridge() { 60 64 61 while (List.size() != 0) RemoveService(List[0]. DataItem->getName());65 while (List.size() != 0) RemoveService(List[0].Name); 62 66 delete ServerList; 63 67 } 64 68 65 // 69 66 70 // Service subscription and repeating 67 //68 71 void Bridge::infoHandler() { 69 72 … … 80 83 RemoveService(string(Token)+"/SERVICE_LIST"); 81 84 } 82 else AddService(string(Token)+"/SERVICE_LIST", "C");85 else AddService(string(Token)+"/SERVICE_LIST", (char *) "C", DimSERVICE); 83 86 84 87 // Skip server IP address and process ID … … 91 94 // If service is SERVICE_LIST, scan and subscribe/unsubscribe to services 92 95 if (strstr(I->getName(), "/SERVICE_LIST") != NULL) { 93 char * Type, *Name = strtok(I->getString(), "+-!|");96 char *Format, *Name = strtok(I->getString(), "+-!|"); 94 97 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 } 99 107 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 } 102 126 } 103 127 } … … 110 134 for (int Service=0; Service<List.size(); Service++) if (I == List[Service].DataItem) { 111 135 112 // Ignores repeating DIS_DNS services113 if (List[Service].Service == NULL) break;114 115 136 // Copy service data 116 137 delete[] List[Service].Data; … … 125 146 } 126 147 127 // 128 // Add service subscription 129 // 130 void Bridge::AddService(string Name, const char *Format) { 148 149 // Command repeating 150 void Bridge::commandHandler() { 151 152 sendCommandNB(getCommand()->getName(), getCommand()->getData(), getCommand()->getSize()); 153 } 154 155 156 // Service subscription 157 void 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; 131 161 132 162 // Check if already subscribed to this service 133 163 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 144 176 List.push_back(New); 145 177 } 146 178 147 179 148 // 149 // Remove service from watch list 150 // 180 // Remove service from watch list (unused pointer are NULL) 151 181 void Bridge::RemoveService(string Name) { 152 182 153 // Find service index154 183 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) { 156 185 delete (*E).DataItem; 157 186 delete (*E).Service; 158 187 delete[] (*E).Data; 188 delete (*E).Command; 189 159 190 List.erase(E); 160 191 } … … 162 193 163 194 164 //165 195 // Main program 166 //167 196 int main(int argc, char *argv[]) { 168 197 198 // Check command line argument 169 199 if (argc == 1) { 170 200 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.