public void PushMessageBySiteToSiteCommand(PlatformMessage platformMessage) { lock (SiteToSiteMessages) { SiteToSiteMessages.Enqueue(platformMessage); } }
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); } }
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(); }
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); }
/// <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); } }
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); }
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); }
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>()); }
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 }
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); }
/// <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); }
public Task ProcessAsync(PlatformMessage platformMessage) { throw new NotImplementedException(); }
/// <summary> /// Send message to native platform /// </summary> /// <param name="message"></param> public abstract void SendPlatformMessage(PlatformMessage message);
public async Task Terminate(PlatformMessage platformMessage) { var operation = manager.GetRawOperation(platformMessage.Command.CommandType); await operation.Push(platformMessage); }
public override void SendPlatformMessage(PlatformMessage message) { //Forward the message as json string OnNativeMessage(JsonConvert.SerializeObject(message)); }
public Task ProcessAsync(PlatformMessage platformMessage) { return(WorkProcessor.Process(platformMessage.MessageData, exit)); }
public Task ProcessAsync(PlatformMessage platformMessage) { systemController.Stop(); systemController.Terminate(); return(Task.CompletedTask); }
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)); }
public override Task Push(PlatformMessage platformMessage) { return(MessageProcessorManager.ProcessMessage(platformMessage)); }
public abstract Task ProcessAsync(PlatformMessage platformMessage);
public abstract Task Push(PlatformMessage platformMessage);