public void TestBRPop() { string reply = "*2\r\n$4\r\ntest\r\n$5\r\ntest1\r\n"; using (var mock = new FakeRedisSocket(reply, reply)) using (var redis = new RedisClient(mock, new DnsEndPoint("fakehost", 9999))) { Assert.AreEqual("test1", redis.BRPop(60, "test")); Assert.AreEqual("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); Assert.AreEqual("test1", redis.BRPop(TimeSpan.FromMinutes(1), "test")); Assert.AreEqual("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); } }
public void TestBRPop() { string reply = "*2\r\n$4\r\ntest\r\n$5\r\ntest1\r\n"; using (var mock = new MockConnector("localhost", 9999, reply, reply)) using (var redis = new RedisClient(mock)) { Assert.Equal("test1", (string)redis.BRPop(60, "test").Value); Assert.Equal("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); Assert.Equal("test1", (string)redis.BRPop(TimeSpan.FromMinutes(1), "test").Value); Assert.Equal("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); } }
public void TestBRPop() { string reply = "*2\r\n$4\r\ntest\r\n$5\r\ntest1\r\n"; using (var mock = new FakeRedisSocket(reply, reply)) using (var redis = new RedisClient(mock, new DnsEndPoint("fakehost", 9999))) { Assert.Equal("test1", redis.BRPop(60, "test")); Assert.Equal("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); Assert.Equal("test1", redis.BRPop(TimeSpan.FromMinutes(1), "test")); Assert.Equal("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); } }
public void TestBRPop() { string reply = "*2\r\n$4\r\ntest\r\n$5\r\ntest1\r\n"; using (var mock = new MockConnector("MockHost", 9999, reply, reply)) using (var redis = new RedisClient(mock)) { Assert.AreEqual("test1", redis.BRPop(60, "test")); Assert.AreEqual("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); Assert.AreEqual("test1", redis.BRPop(TimeSpan.FromMinutes(1), "test")); Assert.AreEqual("*3\r\n$5\r\nBRPOP\r\n$4\r\ntest\r\n$2\r\n60\r\n", mock.GetMessage()); } }
static async Task Main(string[] args) { // configure Redis var redis = new RedisClient("127.0.0.1"); var config = GetConfig(); var dns = config[0]; //dns pubblico statico ec2 var port = config[1]; string token = ""; string id = "{ " + "\"id\": " + config[2] + "}"; while (true) { //SAMPLE DI CONNESSIONE PER INVIO DATI POST if (CheckForConnection(dns)) { if (token == "") { token = GetTokenfromAPI(id); Console.WriteLine(token); } //autenticazione e connessione macchina var httpWebRequest = (HttpWebRequest)WebRequest.Create("http://" + dns + "/api"); httpWebRequest.PreAuthenticate = true; httpWebRequest.Headers.Add("Authorization", "Bearer " + token); httpWebRequest.Accept = "application/json"; httpWebRequest.ContentType = "application/json"; httpWebRequest.Method = "POST"; // send value to remote API var data = redis.BRPop(0, "sensors_data"); //flusso var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()); streamWriter.Write(data); Console.WriteLine(data); streamWriter.Flush(); streamWriter.Close(); var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) { var result = streamReader.ReadToEnd(); Console.WriteLine(result); } } System.Threading.Thread.Sleep(500);//500*4 = 1 sec Tutti i bus } }
/// <summary> /// 出队操作 /// </summary> /// <param name="key">队例名称</param> /// <returns></returns> public string RPop(string key) { using (RedisClient client = GetClient()) { byte[][] Val = client.BRPop(key, 0); if (Val != null) { string value = Encoding.UTF8.GetString(Val[1]); return(value); } return(null); } }
static void Main(string[] args) { // configure Redis var redis = new RedisClient("127.0.0.1"); while (true) { i = 0; // read from Redis queue string json = redis.BRPop(30, "sensors_data"); try { using (WebClient webClient = new WebClient()) { webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; string response = webClient.UploadString(url, json); if (response != "201") { i = 1; } Console.WriteLine(response); } } catch (Exception err) { Console.Write(err.Message); i = 1; } Console.WriteLine(); if (i == 1) { redis.LPush("sensors_data", json); } } }
/// <summary> /// 出队操作 /// </summary> /// <param name="key">队例名称</param> /// <returns></returns> public string RPop(string key) { using (RedisClient client = GetClient()) { try { byte[][] Val = client.BRPop(key, 0); if (Val != null) { string value = Encoding.UTF8.GetString(Val[1]); return(value); } return(null); } catch { return(null); } finally { client.Dispose(); } } }
static void Main(string[] args) { while (true) { try { //Configura Redis var redis = new RedisClient("127.0.0.1"); //Sending Loop while (true) { //Read, execute and write external data string command = External.ReadValues(); //string command = "serverip 192.168.1.26"; External.Execute(command); External.WriteValues(); //Read from Redis queue List <string> redislist = redis.LRange("sensors_data", 0, redis.LLen("sensors_data") + 1).ToList(); if (wait == true) { Thread.Sleep(5000); } else { string json = redis.BRPop(30, "sensors_data"); // send value to remote API try { using (WebClient webClient = new WebClient()) { //Ottiene token autenticazione if (autentication == true) { using (WebClient wc = new WebClient()) { string credentials = "{\"id\": \"" + Properties.Settings.Default.TokenId + "\", \"password\": \"" + Properties.Settings.Default.TokenPass + "\"}"; wc.Headers[HttpRequestHeader.ContentType] = "application/json"; token = webClient.UploadString(urlToken, credentials); } webClient.Headers[HttpRequestHeader.ContentType] = "application/json"; webClient.Headers[HttpRequestHeader.Authorization] = "Bearer " + token; webClient.UseDefaultCredentials = true; webClient.Credentials = new NetworkCredential(Properties.Settings.Default.NetUser, Properties.Settings.Default.NetPass); } string response = webClient.UploadString(url, json); } Console.Write("si "); Console.WriteLine(); } catch (Exception err) { Console.Write("no "); Console.WriteLine(" : " + err.Message); redis.RPush("sensors_data", json); } } } } catch (Exception ex) { Console.WriteLine(ex.Message); } } }