Ejemplo n.º 1
0
 public void PushMessageBySiteToSiteCommand(PlatformMessage platformMessage)
 {
     lock (SiteToSiteMessages)
     {
         SiteToSiteMessages.Enqueue(platformMessage);
     }
 }
Ejemplo n.º 2
0
        public override async Task Push(PlatformMessage platformMessage)
        {
            try
            {
                if (platformMessage.NextJump != null)
                {
                    logger.Info("Jump tp " + platformMessage.NextJump.Id);
                    HttpWebRequest httpWebRequest = WebRequest.CreateHttp(new Uri($"http://{platformMessage.NextJump.IpAddress}/api/SiteToSiteJump", UriKind.Absolute));
                    httpWebRequest.Headers.Add("x-principal", platformMessage.NextJump.IpAddress);
                    httpWebRequest.Headers.Add("x-principal-id", platformMessage.NextJump.Id);
                    httpWebRequest.Method = "POST";
                    var data   = JsonConvert.SerializeObject(platformMessage);
                    var buffer = Encoding.UTF8.GetBytes(data);
                    var stream = await httpWebRequest.GetRequestStreamAsync();

                    await stream.WriteAsync(buffer, 0, buffer.Length);

                    await stream.FlushAsync();

                    var response = await httpWebRequest.GetResponseAsync();

                    logger.Info("Complete jump " + platformMessage.NextJump.IpAddress);
                }
            }
            catch (Exception ex)
            {
                logger.Error("Jump to " + platformMessage.NextJump.Id, ex);
            }
        }
Ejemplo n.º 3
0
        public MessageEntranceTest()
        {
            this.PlatformMessageInQueue = new PlatformMessage()
            {
                Command = new ScrapyCore.Core.Platform.Commands.Command()
                {
                    CommandCode = ScrapyCore.Core.Platform.Commands.CommandCode.Configure,
                    CommandType = ScrapyCore.Core.Platform.Commands.CommandTransfer.Random
                }
            };
            this.PlatformMessageInMemory = new PlatformMessage()
            {
                Command = new ScrapyCore.Core.Platform.Commands.Command()
                {
                    CommandCode = ScrapyCore.Core.Platform.Commands.CommandCode.HeartBeat,
                    CommandType = ScrapyCore.Core.Platform.Commands.CommandTransfer.Forward
                }
            };

            IMessageQueueHandler <PlatformMessage> messageQueueHandler = Moq.Mock.Of <IMessageQueueHandler <PlatformMessage> >();

            Moq.Mock.Get(messageQueueHandler).Setup(x => x.MessageObject).Returns(PlatformMessageInQueue);
            IMessageQueue messageQueue = Moq.Mock.Of <IMessageQueue>();

            Moq.Mock.Get(messageQueue).Setup(x => x.GetMessage <PlatformMessage>())
            .Returns(Task.FromResult(messageQueueHandler));

            messageEntrance = new MessageEntrance(messageQueue);
        }
        protected override void HeartBeatProcessor()
        {
            // TODO: Refactor to a method or may be class
            PlatformMessage platformMessage = new PlatformMessage()
            {
                Command = new Core.Platform.Commands.Command()
                {
                    CommandCode = Core.Platform.Commands.CommandCode.HeartBeat,
                    CommandType = Core.Platform.Commands.CommandTransfer.Random,
                },
                NextJump = null
            };
            HeartBeatModel heartBeatModel = new HeartBeatModel()
            {
                ChannelId = bootstrap.GetVariableSet("Termination"),
                SentTime  = DateTime.Now,
                Id        = hostedMachine.Id,
                Model     = "Hydralisk",
                External  = hostedMachine
            };

            platformMessage.Routes.Add(new MessageRoute(
                                           new Pricipal()
            {
                Id        = hostedMachine.Id,
                IpAddress = hostedMachine.PrivateIpAddress
            }
                                           ));
            platformMessage.MessageData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(heartBeatModel));
            messageOut.SendQueueMessage(platformMessage).Wait();
        }
Ejemplo n.º 5
0
    protected virtual void HandleMessageFromPlatform(PlatformMessage message)
    {
        PrintMessage(message);

        var messageName = message.Name.ToLowerInvariant();

        switch (messageName)
        {
        case "simple_message":
            message.SetResponse("OK");
            break;

        case "some_message_with_json_response":
            message.SetResponse("{ \"name\" : \"value\" }", "application/json");
            break;

        case "some_message_with_binary_response":
            message.SetResponse(new byte[] { 65, 82, 86, 73 });
            break;

        case "some_message_with_error_response":
            message.SetError("Error description");
            break;

        default:
            return;
        }
    }
        public Task ProcessMessage(PlatformMessage platformMessage)
        {
            var commandCode = platformMessage.Command.CommandCode;

            if (Processors.ContainsKey(commandCode))
            {
                return(Processors[commandCode].ProcessAsync(platformMessage));
            }
            return(Task.CompletedTask);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// The callback from native layer when a native message is received
        /// </summary>
        /// <param name="jsonMessage">The json format of the PlatformMessage converted from the native message by the UnityPlatformBridge</param>
        public void OnNativeMessage(string jsonMessage)
        {
            //Deserialize
            PlatformMessage message = JsonConvert.DeserializeObject <PlatformMessage>(jsonMessage);

            //Dispatch the message
            if (actions.ContainsKey(message.Header))
            {
                actions[message.Header].Invoke(message);
            }
        }
Ejemplo n.º 8
0
        public async Task ProcessAsync(PlatformMessage platformMessage)
        {
            string         hbdInfo        = Encoding.UTF8.GetString(platformMessage.MessageData);
            HeartBeatModel heartBeatModel = JsonConvert.DeserializeObject <HeartBeatModel>(hbdInfo);
            await channelStatus.StoreAsync("instance-" + heartBeatModel.Id, heartBeatModel, new TimeSpan(0, 0, 10));

            await channelStatus.StoreAsync("channel-" + heartBeatModel.ChannelId, new ChannelModel()
            {
                Congestion = (DateTime.Now - heartBeatModel.SentTime).TotalMilliseconds,
                Id         = heartBeatModel.ChannelId
            }, new TimeSpan(0, 0, 10));
        }
 public async Task OutRandom <T>(T obj)
 {
     PlatformMessage platformMessage = new PlatformMessage()
     {
         Command = new Commands.Command()
         {
             CommandCode = Commands.CommandCode.Working,
             CommandType = Commands.CommandTransfer.Random
         },
         MessageData = Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj))
     };
     await MessageQueue.SendQueueMessage(platformMessage);
 }
 public bool PostValue([FromBody] PlatformMessage platformMessage,
                       [FromHeader(Name = "x-principal")] string princpal,
                       [FromHeader(Name = "x-principal-id")] string princpalId)
 {
     if (princpal == platformMessage.NextJump.IpAddress.ToString() && princpalId == platformMessage.NextJump.Id)
     {
         if (princpal == Request.Host.Host)
         {
             ///The siteToSite Command should be processed in this jump.
             platformMessage.Command.CommandType = CommandTransfer.Random;
             messageEntrance.PushMessageBySiteToSiteCommand(platformMessage);
         }
     }
     return(true);
 }
Ejemplo n.º 11
0
        public async Task MessagingProcessor(IMessageProcessor processor)
        {
            var messageHandler = await messageQueueIn.GetMessage <PlatformMessage>();

            MessageRoute messageRoute = new MessageRoute(pricipal);

            this.platformMessage = messageHandler.MessageObject;
            await processor.ProcessAsync(platformMessage);

            messageRoute.Complete();
            await messageHandler.Complete();

            this.platformMessage.Routes.Add(messageRoute);
            await messageQueueQut.SendQueueMessage(this.platformMessage);
        }
Ejemplo n.º 12
0
 public Task <IMessageQueueHandler <PlatformMessage> > FetchMessage()
 {
     lock (SiteToSiteMessages)
     {
         if (this.SiteToSiteMessages.Count > 0)
         {
             PlatformMessage platformMessage = SiteToSiteMessages.Dequeue();
             return(Task.FromResult((IMessageQueueHandler <PlatformMessage>) new PackagedMessageWithHandler()
             {
                 MessageObject = platformMessage
             }));
         }
     }
     return(messageQueueIn.GetMessage <PlatformMessage>());
 }
Ejemplo n.º 13
0
        public override void SendPlatformMessage(PlatformMessage message)
        {
#if UNITY_EDITOR
            //TODO: Mock in editor
            GameManager.Instance.UnityPlatformBridge.OnNativeMessage(JsonConvert.SerializeObject(message));
#elif UNITY_ANDROID
            AndroidJavaObject intentObject = new AndroidJavaObject("android.content.Intent");

            //Set intent
            intentObject.Call <AndroidJavaObject>("setAction", message.Header);
            if (message.Content != null)
            {
                foreach (var msg in message.Content)
                {
                    intentObject.Call <AndroidJavaObject>("putExtra", msg.Key, msg.Value);
                }
            }

            GetCurrentActivity().Call("sendBroadcast", intentObject);
#endif
        }
Ejemplo n.º 14
0
    private void PrintMessage(PlatformMessage message)
    {
        // Message method (GET/POST) and name (e.g., "skip")
        string messageInfo = string.Format("{0} message \"{1}\" received.", message.Method, message.Name);

        // Message parameters (if available)
        if (message.Params.Count > 0)
        {
            messageInfo += " Parameters:";
            for (int i = 0; i < message.Params.Count; ++i)
            {
                messageInfo += string.Format(" {0} = {1};", message.Params.GetKey(i), message.Params.Get(i));
            }
        }
        // Message data (if available)
        string messageData = message.GetDataAsString();

        if (!string.IsNullOrEmpty(messageData))
        {
            messageInfo += string.Format(" Data: {0}", messageData);
        }
        // Print message info
        Debug.Log(messageInfo);
    }
Ejemplo n.º 15
0
 /// <summary>
 /// The listener for the native message, it will dispatch the PlatformMessage to the message queue
 /// </summary>
 /// <param name="message"></param>
 /// <returns></returns>
 public virtual void OnMessage(PlatformMessage message)
 {
     GameManager.SafeQueueMessage(message);
 }
Ejemplo n.º 16
0
 public Task ProcessAsync(PlatformMessage platformMessage)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Send message to native platform
 /// </summary>
 /// <param name="message"></param>
 public abstract void SendPlatformMessage(PlatformMessage message);
Ejemplo n.º 18
0
 public async Task Terminate(PlatformMessage platformMessage)
 {
     var operation = manager.GetRawOperation(platformMessage.Command.CommandType);
     await operation.Push(platformMessage);
 }
Ejemplo n.º 19
0
 public override void SendPlatformMessage(PlatformMessage message)
 {
     //Forward the message as json string
     OnNativeMessage(JsonConvert.SerializeObject(message));
 }
Ejemplo n.º 20
0
 public Task ProcessAsync(PlatformMessage platformMessage)
 {
     return(WorkProcessor.Process(platformMessage.MessageData, exit));
 }
Ejemplo n.º 21
0
 public Task ProcessAsync(PlatformMessage platformMessage)
 {
     systemController.Stop();
     systemController.Terminate();
     return(Task.CompletedTask);
 }
Ejemplo n.º 22
0
 public override Task Push(PlatformMessage platformMessage)
 {
     ///The transfer message should be process in next jump.
     platformMessage.Command.CommandType = Commands.CommandTransfer.Random;
     return(messageQueue.SendQueueMessage(platformMessage));
 }
Ejemplo n.º 23
0
 public override Task Push(PlatformMessage platformMessage)
 {
     return(MessageProcessorManager.ProcessMessage(platformMessage));
 }
Ejemplo n.º 24
0
 public abstract Task ProcessAsync(PlatformMessage platformMessage);
Ejemplo n.º 25
0
 public abstract Task Push(PlatformMessage platformMessage);