Advertisement
vitareinforce

AMQP On Hololens

Jun 11th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using RabbitMQ;
  7. using RabbitMQ.Client;
  8. using System.Diagnostics;
  9. using RabbitMQ.Client.Events;
  10.  
  11. //download rabbitmq client buat UWP disini https://drive.google.com/file/d/0B8YLfSLDHjT-amVpcnBWaEZ2Yjg/view?usp=sharing
  12.  
  13. namespace HMD_Holo
  14. {
  15.     class AMQPService
  16.     {
  17.         private string hostname = "localhost";
  18.         private int port = 5672;
  19.         private string user = "guest";
  20.         private string pass = "guest";
  21.         private string vhost = "/";
  22.  
  23.         private string routing_key = "kfx_queue";
  24.  
  25.         ConnectionFactory connectionFactory;
  26.         IConnection connection;
  27.         IModel channel;
  28.         private string replyQueueName;
  29.         private QueueingBasicConsumer consumer;
  30.  
  31.         public AMQPService()
  32.         {
  33.             connectionFactory = new ConnectionFactory();
  34.             connectionFactory.HostName = hostname;
  35.             connectionFactory.Port = port;
  36.             connectionFactory.UserName = user;
  37.             connectionFactory.Password = pass;
  38.             connectionFactory.VirtualHost = vhost;
  39.  
  40.             _connect();
  41.  
  42.             if(connection.IsOpen) {
  43.                 _createChannel();
  44.             }
  45.            
  46.         }
  47.  
  48.         ~AMQPService()
  49.         {
  50.             Close();
  51.         }
  52.  
  53.         private async void _connect()
  54.         {
  55.             Debug.WriteLine("Attempt to connect to server : " + hostname);
  56.             connection = connectionFactory.CreateConnection();
  57.             Debug.WriteLine("Connection Status : " + connection.IsOpen);
  58.         }
  59.        
  60.         private async void _createChannel()
  61.         {
  62.             Debug.WriteLine("Create Channel");
  63.             channel = connection.CreateModel();
  64.             Debug.WriteLine("Setup Basic QOS");
  65.             channel.BasicQos(0, 1, false);
  66.  
  67.             if(channel.IsOpen)
  68.             {
  69.                 replyQueueName = channel.QueueDeclare().QueueName;
  70.                 Debug.WriteLine("Declare Queue : " + replyQueueName);
  71.                 consumer = new QueueingBasicConsumer(channel);
  72.                 Debug.WriteLine("Consume Queue");
  73.                 channel.BasicConsume(queue: replyQueueName,
  74.                                      noAck: true,
  75.                                      consumer: consumer);
  76.                 Debug.WriteLine("Queue Consumed");
  77.             }
  78.         }
  79.  
  80.         //ini gimana caranya biar bisa asynchronous??
  81.         public string Call(string message)
  82.         {
  83.             var corrId = Guid.NewGuid().ToString();
  84.             Debug.WriteLine("Corr ID : " + corrId.ToString());
  85.             var props = channel.CreateBasicProperties();
  86.             Debug.WriteLine("Props : " + props.ToString());
  87.             props.ReplyTo = replyQueueName;
  88.             Debug.WriteLine("Reply To : " + replyQueueName);
  89.             props.CorrelationId = corrId;
  90.             Debug.WriteLine("Corr ID : " + corrId.ToString());
  91.  
  92.             var messageBytes = Encoding.UTF8.GetBytes(message);
  93.             Debug.WriteLine("Get Message");
  94.             channel.BasicPublish(exchange: "",
  95.                                  routingKey: routing_key,
  96.                                  basicProperties: props,
  97.                                  body: messageBytes);
  98.             Debug.WriteLine("Publish Message");
  99.  
  100.             while (true)
  101.             {
  102.                 var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
  103.                 if (ea.BasicProperties.CorrelationId == corrId)
  104.                 {
  105.                     Debug.WriteLine("Message Content : " + Encoding.UTF8.GetString(ea.Body));
  106.                     return Encoding.UTF8.GetString(ea.Body);
  107.                 }
  108.             }
  109.         }
  110.  
  111.         public async void Close()
  112.         {
  113.             connection.Close();
  114.         }
  115.     }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement