Esempio n. 1
0
 protected override void OnReceive(object message)
 {
     if (message is CreateOmnetNode create)
     {
         var actorRef = create.Node;
         var id       = OmnetSimulation.Instance().CreateNodeAndId(create.NodeType);
         _idToRef = _idToRef.Add(id, actorRef);
         _refToId = _refToId.Add(actorRef, id);
     }
     else if (message is Subscribe subscribe)
     {
         var topic = subscribe.Topic;
         if (!_subscribers.ContainsKey(topic))
         {
             _subscribers = _subscribers.Add(topic, ImmutableList <IActorRef> .Empty);
         }
         _subscribers = _subscribers.SetItem(topic, _subscribers[topic].Add(subscribe.Subscriber));
     }
     else if (message is Broadcast broadcast)
     {
         // There seems to be no way to use publish here... we need to implement pubsub manually.
         var sender      = broadcast.Sender;
         var broadcasted = broadcast.Message;
         var topic       = broadcasted.GetType();
         var msgId       = _nextMessageId++;
         if (_subscribers.ContainsKey(topic))
         {
             _subscribers[topic].ForEach(sub =>
             {
                 if (_refToId.TryGetValue(sub, out var subId))
                 {
                     var task = new Task(() =>
                     {
                         sub.Tell(broadcasted, sender);
                     });
                     _waitingForOmnetToDelvierMessage.TryAdd(Tuple.Create(msgId, subId), task);
                 }
             });
         }
         else
         {
             Context.System.Log.Warning("No subscribers for topic " + topic.FullName);
         }
         BroadcastInOmnet(sender, broadcasted, msgId);
     }
     else if (message is Send send)
     {
         SendInOmnet(send.Sender, send.Receiver, send.Message)
         .ContinueWith(result => { send.Receiver.Tell(send.Message, send.Sender); });
     }
 }
Esempio n. 2
0
 public void BroadcastInOmnet(IActorRef sender, object message, int msgId)
 {
     _log.Info("Broadcasting {0} from {1} ", message.GetType(), sender?.Path.Name);
     if (_refToId.TryGetValue(sender, out var senderId))
     {
         _log.Info($"Broadcasting {msgId} to omnet.");
         var numberOfBytes = 10;
         OmnetSimulation.Instance().Send(senderId, OmnetSimulation.BROADCAST_ADDR, numberOfBytes, msgId);
     }
     else
     {
         throw new Exception("Key for sender not found");
     }
 }
        private void InternalSchedule(TimeSpan?initialDelay, TimeSpan delay, ICanTell receiver, object message, Action action,
                                      IActorRef sender, ICancelable cancelable, int deliveryCount = 0)
        {
            // TODO: This probably needs adjustment for repetative events?
            OmnetSimulation.Instance().SetGlobalTimerMillisecounds(delay.Milliseconds + initialDelay?.Milliseconds ?? 0);

            var scheduledTime = TimeNow.Add(initialDelay ?? delay).UtcTicks;

            if (!_scheduledWork.TryGetValue(scheduledTime, out var tickItems))
            {
                tickItems = new ConcurrentQueue <ScheduledItem>();
                _scheduledWork.TryAdd(scheduledTime, tickItems);
            }
            var type = message == null ? ScheduledItem.ScheduledItemType.Action : ScheduledItem.ScheduledItemType.Message;

            tickItems.Enqueue(new ScheduledItem(initialDelay ?? delay, delay, type, message, action,
                                                initialDelay.HasValue || deliveryCount > 0, receiver, sender, cancelable));
        }
Esempio n. 4
0
        public Task BroadcastInOmnet(IActorRef sender, object message)
        {
            Console.WriteLine("Broadcasting {0} from {1} ", message.GetType(), sender?.Path.Name);
            var task = new Task(() => { });

            if (_refToId.TryGetValue(sender, out var senderId))
            {
                Console.WriteLine("Broadcasting to omnet.");
                var msgId = _nextMessageId++;
                _waitingForOmnetToDelvierMessage.TryAdd(msgId, task);
                var numberOfBytes = 10;
                OmnetSimulation.Instance().Send(senderId, 0x000000, numberOfBytes, msgId);
            }
            else
            {
                throw new Exception("Key for sender not found");
            }
            return(task);
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="receiver"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public Task SendInOmnet(IActorRef sender, IActorRef receiver, object message)
        {
            Console.WriteLine("Sending {0} from {1} to {2}", message.GetType(), sender?.Path.Name, receiver?.Path.Name);
            var task = new Task(() => { });

            if (
                _refToId.TryGetValue(sender, out var senderId)
                &&
                _refToId.TryGetValue(receiver, out var receiverId))
            {
                Console.WriteLine("Sending message to omnet.");
                var msgId = _nextMessageId++;
                // Add to the dictionary first, since omnet might invoce the callback immediately.
                _waitingForOmnetToDelvierMessage.TryAdd(msgId, task);
                var numberOfBytes = 10;
                OmnetSimulation.Instance().Send(senderId, receiverId, numberOfBytes, msgId);
            }
            else
            {
                throw new Exception("Key for sender or receiver not found");
            }
            return(task);
        }
Esempio n. 6
0
 protected override void OnReceive(object message)
 {
     if (message is CreateOmnetNode create)
     {
         var actorRef = create.Node;
         var id       = OmnetSimulation.Instance().CreateNodeAndId();
         _idToRef = _idToRef.Add(id, actorRef);
         _refToId = _refToId.Add(actorRef, id);
     }
     else if (message is Subscribe subscribe)
     {
         if (!_subscribers.ContainsKey(subscribe.Topic))
         {
             _subscribers = _subscribers.Add(subscribe.Topic, ImmutableList <IActorRef> .Empty);
         }
         _subscribers.SetItem(subscribe.Topic, _subscribers[subscribe.Topic].Add(subscribe.Subscriber));
     }
     else if (message is Broadcast broadcast)
     {
         // There seems to be no way to use publish here... we need to implement pubsub manually.
         if (_subscribers.ContainsKey(broadcast.Message.GetType()))
         {
             var subs = _subscribers[broadcast.Message.GetType()];
             BroadcastInOmnet(broadcast.Sender, broadcast.Message).ContinueWith(
                 result =>
             {
                 subs.ForEach(sub => sub.Tell(broadcast.Message, broadcast.Sender));
             });
         }
     }
     else if (message is Send send)
     {
         SendInOmnet(send.Sender, send.Receiver, send.Message)
         .ContinueWith(result => { send.Receiver.Tell(send.Message, send.Sender); });
     }
 }