Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using RabbitMQ.Client;
- using RabbitMQ.Client.Events;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace HMD_Holo
- {
- class RabbitMQServices
- {
- private string data = "";
- /* KFX GPS Tracker Params */
- private string user = "ARmachine";
- private string pass = "12345";
- private string vhost = "/ARX";
- private string exchange = "kfx_exchanges";
- private string queue = "kfx.plane";
- private string routingkey = "kfx.plane";
- private string hostname = "localhost";
- private int port = 5672;
- private ConnectionFactory connectionFactory;
- private IConnection connection;
- private IModel channel;
- private EventingBasicConsumer consumer;
- public RabbitMQServices(string queue_name = null, string routing_key = null)
- {
- Debug.WriteLine("Begin");
- connectionFactory = new ConnectionFactory();
- connectionFactory.HostName = hostname;
- connectionFactory.Port = port;
- connectionFactory.UserName = user;
- connectionFactory.Password = pass;
- connectionFactory.VirtualHost = vhost;
- if(queue_name != null)
- {
- queue = queue_name;
- }
- if(routing_key != null)
- {
- routingkey = routing_key;
- }
- connect();
- }
- ~RabbitMQServices()
- {
- disconnect();
- }
- public async void connect()
- {
- connection = connectionFactory.CreateConnection();
- Debug.WriteLine("Connection : " + connection.IsOpen);
- }
- public bool connectionStatus()
- {
- return connection.IsOpen;
- }
- public async void createChannel()
- {
- if (connection.IsOpen)
- {
- channel = connection.CreateModel();
- Debug.WriteLine("Channel : " + channel.IsOpen);
- }
- if (channel.IsOpen)
- {
- Debug.WriteLine("Declare Exchange");
- channel.ExchangeDeclare(exchange: exchange,
- type: "topic",
- durable: true);
- Debug.WriteLine("Declare Queue");
- channel.QueueDeclare(queue: queue,
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null);
- Debug.WriteLine("Bind Queue");
- channel.QueueBind(queue: queue,
- exchange: exchange,
- routingKey: routingkey);
- Debug.WriteLine("Declare Consuming Queue Process");
- consumer = new EventingBasicConsumer(channel);
- //debugging here
- Debug.WriteLine("Begin Reveiving data");
- consumer.Received += (model, ea) =>
- {
- Debug.WriteLine("Retriving......");
- var body = ea.Body;
- data = Encoding.UTF8.GetString(body);
- Debug.WriteLine("Data : " + data);
- };
- channel.BasicConsume(queue: queue,
- noAck: true,
- consumer: consumer);
- }
- }
- public string getData()
- {
- /*
- using (channel = connection.CreateModel())
- {
- Debug.WriteLine("Declare Exchange");
- channel.ExchangeDeclare(exchange: exchange,
- type: "topic",
- durable: true);
- Debug.WriteLine("Declare Queue");
- channel.QueueDeclare(queue: queue,
- durable: false,
- exclusive: false,
- autoDelete: false,
- arguments: null);
- Debug.WriteLine("Bind Queue");
- channel.QueueBind(queue: queue,
- exchange: exchange,
- routingKey: routingkey);
- Debug.WriteLine("Declare Consuming Queue Process");
- consumer = new EventingBasicConsumer(channel);
- //debugging here
- Debug.WriteLine("Begin Reveiving data");
- consumer.Received += (model, ea) =>
- {
- Debug.WriteLine("Retriving......");
- var body = ea.Body;
- data = Encoding.UTF8.GetString(body);
- Debug.WriteLine("Data : " + data);
- };
- channel.BasicConsume(queue: queue,
- noAck: true,
- consumer: consumer);
- }
- */
- return data;
- }
- public async void disconnect()
- {
- channel = null;
- if (connection.IsOpen)
- {
- Debug.WriteLine("Disconnect");
- connection.Close();
- }
- Debug.WriteLine("Dispose Connection");
- connection.Dispose();
- connection = null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement