Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1.  public string StartPublishingInactiveVehicles(int timePeriodInSeconds, string address)
  2.         {
  3.             string random = RandomString(16);
  4.             string queueName = "OBU1_InactiveVehicles_" + random;
  5.             Task.Run(() => publishInactiveVehiclesAsync(queueName, timePeriodInSeconds, address));
  6.             return queueName;
  7.         }
  8.  
  9.         private async Task publishInactiveVehiclesAsync(string queueName, int timePeriodInSeconds, string address)
  10.         {
  11.             if (string.IsNullOrEmpty(address))
  12.                 address = "http://127.0.0.1:8080/TRAAS_WS"; //default address
  13.  
  14.             TraasReference.ServiceImplClient client = new TraasReference.ServiceImplClient();
  15.  
  16.             client.Endpoint.Address = new EndpointAddress(address);
  17.  
  18.             ConnectionFactory factory = new ConnectionFactory();
  19.             IProtocol protocol = Protocols.AMQP_0_9_1;
  20.             factory.VirtualHost = "/";
  21.             factory.UserName = "soa";
  22.             factory.Password = "soasoa";
  23.             factory.HostName = "164.8.251.96";
  24.             factory.Port = 5672;
  25.             factory.Protocol = protocol;
  26.  
  27.             try
  28.             {
  29.                 using (IConnection conn = factory.CreateConnection())
  30.                 {
  31.                     using (IModel ch = conn.CreateModel())
  32.                     {
  33.                         ch.QueueDeclare(queue: queueName,
  34.                                      durable: false,
  35.                                      exclusive: false,
  36.                                      autoDelete: false,
  37.                                      arguments: null);
  38.  
  39.                         Stopwatch stopwatch = new Stopwatch();
  40.                         stopwatch.Start();
  41.                         Dictionary<string, bool> seznamVozil = new Dictionary<string, bool>();
  42.  
  43.                         client.Open();
  44.                         while (stopwatch.Elapsed.TotalSeconds < timePeriodInSeconds)
  45.                         {
  46.  
  47.                             string[] activeVehicles = client.Vehicle_getIDList();
  48.                             foreach (string s in activeVehicles)
  49.                             {
  50.                                 if (seznamVozil.ContainsKey(s))
  51.                                     seznamVozil[s] = true;
  52.                                 else
  53.                                     seznamVozil.Add(s, true);
  54.                             }
  55.  
  56.                             List<string> inactiveVehicles = new List<string>();
  57.                             foreach (KeyValuePair<string, bool> entry in seznamVozil)
  58.                             {
  59.                                 if (!entry.Value)
  60.                                     inactiveVehicles.Add(entry.Key);
  61.                             }
  62.  
  63.                             string msg = "";
  64.  
  65.                             foreach (string s in inactiveVehicles)
  66.                             {
  67.                                 msg += s + " ";
  68.                                 seznamVozil.Remove(s);
  69.                             }
  70.  
  71.                             if (msg != "")
  72.                             {
  73.                                 var body = Encoding.UTF8.GetBytes(msg);
  74.  
  75.                                 ch.BasicPublish(exchange: "",
  76.                                                      routingKey: queueName,
  77.                                                      basicProperties: null,
  78.                                                      body: body);
  79.                             }
  80.  
  81.                             foreach (var key in seznamVozil.Keys.ToList())
  82.                             {
  83.                                 seznamVozil[key] = false;
  84.                             }
  85.  
  86.                             System.Threading.Thread.Sleep(5000);
  87.                             //client.Close();
  88.                         }
  89.  
  90.                         client.Close();
  91.                     }
  92.                 }
  93.  
  94.             }
  95.             catch (FaultException e)
  96.             {
  97.                 client.Abort();
  98.  
  99.             }
  100.             catch (Exception e)
  101.             {
  102.                 client.Abort();
  103.             }
  104.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement