private static void CheckKVSLoad(BrokerService brokerService) { List<String> servers = brokerService.GetKVSList(); KVSClient kvs; if (servers.Count == 0) { Console.WriteLine("There aren't any KVS servers register in Broker."); return; } int i = 0; foreach(String address in servers) { kvs = new KVSClient("KVSService", address); try { Console.WriteLine("Server {0} has {1} pairs.", i, kvs.GetTotalPairsStored()); }catch(EndpointNotFoundException) { Console.WriteLine("Problem with the KVS: {0}. Removing it from server list.", address); brokerService.DeregisterServer(address); } ++i; } }
//IBroker public void StorePair(Pair p) { //getting message context with the client name MessageProperties msgProp = OperationContext.Current.IncomingMessageProperties; ContextMessageProperty ctxProperty = msgProp[ContextMessageProperty.Name] as ContextMessageProperty; string clientName = ""; if (ctxProperty.Context.ContainsKey("clientName")) { clientName = ctxProperty.Context["clientName"]; } if(clientName == "") throw new FaultException("ERROR - Client Name not set."); if(m_servers.Count <= 0) throw new FaultException("ERROR - System is down."); ClientKey ck = new ClientKey(clientName, p.Key); if (m_serverPairs.ContainsKey(ck)) throw new FaultException("ERROR - Duplicated key: " + p.Key); String address = GetKVS(); KVSClient kvs = new KVSClient("KVSService", address); try { kvs.StorePair(ck, p.Value); }catch(EndpointNotFoundException) { DeregisterServer(address); StorePair(p); } m_serverPairs.TryAdd(ck, address);//add the key and the server to the list if (!m_servers.ContainsKey(clientName)) m_users.Add(clientName); //Console.WriteLine("Pair Stored in server: " + m_currId); }
//IBrokerKVS public void RegisterServer(String address) { KVSClient kvs = new KVSClient("KVSService", address); m_servers.TryAdd(address, kvs); }
public String ReadValue(String key) { //getting message context with the client name MessageProperties msgProp = OperationContext.Current.IncomingMessageProperties; ContextMessageProperty ctxProperty = msgProp[ContextMessageProperty.Name] as ContextMessageProperty; string clientName = ""; if (ctxProperty.Context.ContainsKey("clientName")) { clientName = ctxProperty.Context["clientName"]; } if (clientName == "") throw new FaultException("ERROR - Client Name not set."); ClientKey ck = new ClientKey(clientName, key); if (!m_serverPairs.ContainsKey(ck)) throw new FaultException("ERROR - Key doesn't exists: " + key); String ret = ""; String address = GetKVS(ck); KVSClient kvs = new KVSClient("KVSService", address); try { ret += kvs.ReadValue(ck); }catch(EndpointNotFoundException) { String temp; m_serverPairs.TryRemove(ck, out temp); throw new FaultException("ERROR - Problem with key: " + key + " | key removed from system"); } return ret; }