Sie sind auf Seite 1von 8

Sebelumnya,

Untuk tugas besar EL5206 teman teman dapat menggunakan server RabbitMQ dengan konfigurasi
berikut

Hostname : 167.205.7.226
Username : ARmachine
Password : 12345
Virtual Host : /ARX

Untuk nama exchange, queue dan routing key harap tambahkan prefix berikut pada nama exchange,
queue dan routing keynya agar tidak bertabrakan dengan kelompok lain.

Prefix : TMDG2017-[NomorKelompok]-[BebasDiisiNamaApaSaja]

Contoh yg dipakai di code dibawah : TMDG2017-5-Ifadin

Untuk Publisher (console app c#):


Instal RabbitMQ.Client melalui NuGet Package Manager, lalu kopas:
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Publisher_cmd
{
class RMQ
{
public ConnectionFactory connectionFactory;
public IConnection connection;
public IModel channel;
public void InitRMQConnection(string host = "167.205.7.226", int port = 5672,
string user = "ARmachine",
string pass = "12345", string vhost = "/ARX")
{
connectionFactory = new ConnectionFactory();
connectionFactory.HostName = host;
connectionFactory.Port = port;
connectionFactory.UserName = user;
connectionFactory.Password = pass;
connectionFactory.VirtualHost = vhost;
}
public void CreateRMQConnection()
{
connection = connectionFactory.CreateConnection();
Console.WriteLine("Koneksi " + (connection.IsOpen ? "Berhasil!" : "Gagal!"));
}
public void CreateRMQChannel(string queue_name, string routingKey = "TMDG2017-5-
Ifadin", string exchange_name =
"TMDG2017-5-Ifadin")
{
if (connection.IsOpen)
{
channel = connection.CreateModel();
Console.WriteLine("Channel " + (channel.IsOpen ? "Berhasil!" :
"Gagal!"));
}
if (channel.IsOpen)
{
channel.QueueDeclare(queue: queue_name,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
Console.WriteLine("Queue telah dideklarasikan..");
}
}
public void SendMessage(string tujuan, string msg = "send")
{
byte[] responseBytes = Encoding.UTF8.GetBytes(msg);// konversi pesan dalam
bentuk string menjadi byte
channel.BasicPublish(exchange: "",
routingKey: tujuan,
basicProperties: null,
body: responseBytes);
Console.WriteLine("Pesan: '" + msg + "' telah dikirim.");
}
public void Disconnect()
{
channel.Close();
channel = null;
Console.WriteLine("Channel ditutup!");
if (connection.IsOpen)
{
connection.Close();
}
Console.WriteLine("Koneksi diputus!");
connection.Dispose();
connection = null;
}
}

class Program
{
static void Main(string[] args)
{
RMQ rmq = new RMQ();
Console.WriteLine("Tekan tombol apapun untuk inisialisasi RMQ parameters.");
Console.ReadKey();
rmq.InitRMQConnection(); // inisialisasi parameter (secara default) untuk
koneksi ke server RMQ
Console.WriteLine("Tekan tombol apapun untuk membuka koneksi ke RMQ.");
Console.ReadKey();
rmq.CreateRMQConnection(); // memulai koneksi dengan RMQ
Console.Write("Masukkan nama queue channel untuk mengirim pesan melalui
RMQ.\n>> ");
string queue_name = Console.ReadLine();
rmq.CreateRMQChannel(queue_name);
Console.Write("Masukkan pesan yang akan dikirim atau 'exit' to close.\n>>");
Console.Write(">> tujuan: ");
string tujuan = Console.ReadLine();
Console.Write(">> pesan: ");
string inputMsg = Console.ReadLine();
while (inputMsg != "exit")
{
rmq.SendMessage(tujuan, inputMsg);
Console.Write(">> tujuan: ");
tujuan = Console.ReadLine();
Console.Write(">> pesan: ");
inputMsg = Console.ReadLine();
}
rmq.Disconnect();
}
}
}

Untuk Consumer (Hololens)


Contoh menggunakan aplikasi hololens bulan mengorbit pada bumi (default template hololens
urhosharp)

Instal RabbitMQ.Client melalui NuGet Package Manager

Add class dengan nama RMQ.cs pada project hololens, kopas :


class RMQ
{
private string datas = "";

public ConnectionFactory connectionFactory;


public IConnection connection;
public IModel channel;
public void InitRMQConnection(string host = "167.205.7.226", int port = 5672,
string user = "ARmachine",
string pass = "12345", string vhost= "/ARX")
{
connectionFactory = new ConnectionFactory();
connectionFactory.HostName = host;
connectionFactory. Port = port;
connectionFactory.UserName = user;
connectionFactory.Password = pass;
connectionFactory.VirtualHost = vhost;
}
public void CreateRMQConnection()
{
connection = connectionFactory.CreateConnection();
Debug.WriteLine("Koneksi " + (connection.IsOpen ? "Berhasil! " : "Gagal! "));
}
public void CreateRMQChannel(string queue_name, string routingKey = "TMDG2017-5-
Ifadin", string exchange_name = "TMDG2017-5-Ifadin")
{
if (connection.IsOpen)
{
channel = connection.CreateModel();
Debug.WriteLine("Channel " + (channel.IsOpen ? "Berhasil! " : "Gagal!
"));
}
if(channel.IsOpen){
channel.QueueDeclare(queue: queue_name,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
Debug.WriteLine("Queue telah dideklarasikan. . ");
}
}
public void SendMessage(string tujuan, string msg = "send")
{
byte[] responseBytes = Encoding.UTF8.GetBytes(msg); // konversi pesan dalam
bentuk stringmenj adi byte
channel.BasicPublish(exchange: "",
routingKey: tujuan,
basicProperties: null,
body: responseBytes);
Debug.WriteLine("Pesan: ' " + msg + "' telah dikirim. ");
}
public void WaitingMessage(string queue_name)
{
using (channel = connection.CreateModel())
{
channel.QueueDeclare(queue: queue_name,
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
//Debug.WriteLine(" [x] Pesan diterima: {0} ", message);
datas = message;
};
channel.BasicConsume(queue: queue_name, noAck: true, consumer: consumer);
//Debug.WriteLine(" memutus koneksi. ");
//Disconnect();
}
}
public string getData()
{
return datas;
}
public void Disconnect()
{
channel.Close();
channel = null;
Debug.WriteLine("Channel ditutup! ");
if (connection.IsOpen)
{
connection.Close();
}
Debug.WriteLine("Koneksi diputus! ");
connection.Dispose();
connection = null;
}
}
Lalu pada Program.cs tambahkan code between ///////RMQ-HOLOLENS start :) and ///////RMQ-
HOLOLENS end :)

public class HelloWorldApplication : StereoApplication


{
Node earthNode;

///////RMQ-HOLOLENS start :)
RMQ rabbitmq;
///////RMQ-HOLOLENS end :)

public HelloWorldApplication(ApplicationOptions opts) : base(opts) { }

protected override async void Start()


{
// Create a basic scene, see StereoApplication
base.Start();

///////RMQ-HOLOLENS start :)
rabbitmq = new RMQ();
rabbitmq.InitRMQConnection();
rabbitmq.CreateRMQConnection();
///////RMQ-HOLOLENS end :)

// Enable input
EnableGestureManipulation = true;
EnableGestureTapped = true;

// Create a node for the Earth


earthNode = Scene.CreateChild();
earthNode.Position = new Vector3(0, 0, 1.5f); //1.5m away
earthNode.SetScale(0.3f); //D=30cm

// Scene has a lot of pre-configured components, such as Cameras (eyes),


Lights, etc.
DirectionalLight.Brightness = 1f;
DirectionalLight.Node.SetDirection(new Vector3(-1, 0, 0.5f));

//Sphere is just a StaticModel component with Sphere.mdl as a Model.


var earth = earthNode.CreateComponent<Sphere>();
earth.Material = Material.FromImage("Textures/Earth.jpg");

var moonNode = earthNode.CreateChild();


moonNode.SetScale(0.27f); //27% of the Earth's size
moonNode.Position = new Vector3(1.2f, 0, 0);

// Same as Sphere component:


var moon = moonNode.CreateComponent<StaticModel>();
moon.Model = CoreAssets.Models.Sphere;

moon.Material = Material.FromImage("Textures/Moon.jpg");

// Run a few actions to spin the Earth, the Moon and the clouds.
earthNode.RunActions(new RepeatForever(new RotateBy(duration: 1f,
deltaAngleX: 0, deltaAngleY: -4, deltaAngleZ: 0)));
await TextToSpeech("Hello world from UrhoSharp!");

}
///////RMQ-HOLOLENS start :)
protected override void OnUpdate(float timeStep)
{
//konsep
//listen
rabbitmq.WaitingMessage("TMDG2017-5-Ifadin");
//terima data
var data = rabbitmq.getData();
Debug.WriteLine("Isi:" + data);
//gerakin bidak ??
//kosongkan data
data = "";
Debug.WriteLine("Isi:" + data);
//selesai
}
///////RMQ-HOLOLENS end :)

// For HL optical stabilization (optional)


public override Vector3 FocusWorldPoint => earthNode.WorldPosition;

//Handle input:

Vector3 earthPosBeforeManipulations;
public override void OnGestureManipulationStarted() =>
earthPosBeforeManipulations = earthNode.Position;
public override void OnGestureManipulationUpdated(Vector3 relativeHandPosition)
=>
earthNode.Position = relativeHandPosition + earthPosBeforeManipulations;

public override void OnGestureTapped() { }


public override void OnGestureDoubleTapped() { }
}
Cara testing, jalankan publisher dan hololensnya
Pada publisher cmd :

isikan nama queue channel sesuai prefix, contoh : TMDG2017-5-Ifadin

Tujuan: TMDG2017-5-Ifadin

Pesan: terserah

Pada output debug hololens, perhatikan jika pesan dari publisher sudah masuk

Karena getData nya di fungsi OnUpdate, jadi di output debug nya muncul berkali2. Edit2 sesuai
kebutuhan kalian ya

Das könnte Ihnen auch gefallen