public RabbitMQHandler() { th = TrafficHandler.getInstance(); //remoteTransaction = new RemoteTransaction(10.1, "PKW"); //Send(remoteTransaction, "group3"); Receive(); }
public TrafficHandler(ITrafficHandler eventHandler) { this.eventHandler = eventHandler; reliableMonitor = new ReliableMonitor(this); packetReceivedCallbacks = new Dictionary <Packet.Reliability, Action <Packet> >(); packetReceivedCallbacks[Packet.Reliability.None] = packet => { }; packetReceivedCallbacks[Packet.Reliability.Ack] = OnAckReceived; packetReceivedCallbacks[Packet.Reliability.Reliable] = OnReliableReceived; packetReceivedCallbacks[Packet.Reliability.Unreliable] = OnUnreliableReceived; latestReceived = new BoundedList <Packet>(1024 * 8); pendingOut = new BlockingQueue <Packet>(); }
public void Send(RemoteTransaction transaction, String group) { th = TrafficHandler.getInstance(); var factory = new ConnectionFactory(); factory.Uri = "amqp://*****:*****@rabbit.binna.eu/"; //Insert your own user and password using (var connection = factory.CreateConnection()) using (var channel = connection.CreateModel()) { var transactionString = JsonConvert.SerializeObject(transaction); //Awesome function var properties = channel.CreateBasicProperties(); properties.Persistent = true; channel.BasicPublish(exchange: "car.production", //Choose between bank.development and bank.production depending of the queue (e.g. 70 is production, 71 is development) routingKey: group, //This relates to the queue name of the receiver bank basicProperties: properties, //Set the properties to persistent, otherwise the messages will get lost if the server restarts body: GetBytes(transactionString)); } }
private void OnBeforeResponse(Session session, IListenerFilter filter, ITrafficHandler handler) { _logger.Verbose("{ThisMethod} for {Method} {Url}", nameof(OnBeforeResponse), session.RequestMethod, session.url); if (filter.Verbs.Any() && !filter.Verbs.Any(verbFilter => session.RequestMethod.Equals(verbFilter))) { return; } if (filter.ApplyFilters) { if (ShouldFilterHostnameAndRoute(filter)) { if (MatchesHostnameAndRouteFilter(session, filter)) { handler.HandleResponse(session); } } else if (ShouldFilterOnlyOnHostname(filter)) { if (ResponseHostnameMatchesAnyFilterHostname(session, filter)) { handler.HandleResponse(session); } } else if (ShouldFilterOnlyOnRoute(filter)) { if (ResponseRouteMatchesAnyFilterRoute(session, filter)) { handler.HandleResponse(session); } } } else { handler.HandleResponse(session); } }
public void StartListening(IListenerConnection listenerConnection, IListenerFilter listenerFilter, ITrafficHandler trafficHandler) { _logger.Debug("Starting to listen using {Listener} and filters {@Verbs} {@Hostnames} {@Routes}", listenerConnection.GetType().Name, listenerFilter.Verbs, listenerFilter.Hostnames, listenerFilter.Routes); listenerConnection.Connect(); listenerConnection.OnBeforeResponse += session => OnBeforeResponse(session, listenerFilter, trafficHandler); }
public void Receive() { th = TrafficHandler.getInstance(); var factory = new ConnectionFactory(); factory.Uri = "amqp://*****:*****@rabbit.binna.eu/"; using (var connection = factory.CreateConnection()) { using (var channel = connection.CreateModel()) { channel.QueueDeclare(queue: "group3", durable: true, //Always use durable: true, because the queue on the server is configured this way. Otherwise you'll not be able to connect exclusive: false, autoDelete: false, arguments: null); var consumer = new EventingBasicConsumer(channel); ITrafficHandler th = TrafficHandler.getInstance(); int counter = 0; consumer.Received += (model, ea) => { var body = GetString(ea.Body); var transaction = JsonConvert.DeserializeObject <RemoteTransaction>(body); channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false); //Important. When the message get's not acknowledged, it gets sent again Console.WriteLine("[x] Received:"); PrintTransaction(transaction); if (th != null) { int speed = (int)transaction.Speed / 20; if (speed > 5) { speed = 5; } if (transaction.CarType == "PKW") { th.addCarToEntryPoint(null, (int)TrafficObject.Fahrzeugtyp.Car, speed, counter * 5); } if (transaction.CarType == "LKW") { th.addCarToEntryPoint(null, (int)TrafficObject.Fahrzeugtyp.Truck, speed, counter * 5); } counter++; } }; channel.BasicConsume(queue: "group3", noAck: false, //If noAck: false the command channel.BasicAck (see above) has to be implemented. Don't set it true, or the message will not get resubmitted, if the bank was offline consumer: consumer); if (th == null) { //Thread.Sleep(3000); Console.WriteLine(" Press [enter] to exit receive."); Console.ReadLine(); } Thread.Sleep(100); } } }