Exemplo n.º 1
0
        public void RecieveActorMessageFromServiceBusTopic()
        {
            string subscription = "firstsubscription";
            string TopicName    = "test-" + "RecieveActorMessageFromServiceBusTopic".ToLower();
            var    Topic        = GetServiceBusTopicSender(TopicName);

            try
            {
                CreateTopicAndSubscriptions(TopicName, new string[] { subscription });

                string message      = "HelloWorld";
                var    actorMessage = new PingPong.Ping(message);
                SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(actorMessage);
                IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();
                serializer.OnMessageRecieved(sMsg);
                byte[] msgBytes = (byte[])sMsg.ProcessingResult;
                Topic.SendMessageAsync(new ServiceBusMessage(msgBytes));

                long azureOperatorID = Telegraph.Instance.Register(
                    new ServiceBusTopicSubscriptionOperator <IActorMessage>(LocalConcurrencyType.DedicatedThreadCount, Connections.ServiceBusConnectionString, TopicName, subscription, false),
                    (PingPong.Ping foo) => { Assert.IsTrue(message.Equals((string)foo.Message, StringComparison.InvariantCulture)); });
            }
            finally
            {
                Telegraph.Instance.UnRegisterAll();
                DeleteServiceBusTopic(TopicName);
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// Emit the event specified and send the data
 /// </summary>
 /// <param name="eventName">Event to emit</param>
 /// <param name="o">Object to be sended</param>
 /// <returns>If the event was emitted</returns>
 public bool Emit(string eventName, object o)
 {
     try
     {
         Message msg                 = SerializeMessage.Serialize(o);
         byte[]  serializedEvent     = new byte[1024 + 255];
         byte[]  serializedEventName = new byte[255];
         byte[]  encodedEventName    = Encoding.ASCII.GetBytes(eventName);
         int     j = 0;
         for (int i = 0; i < encodedEventName.Length; i++)
         {
             serializedEvent[i] = encodedEventName[i];
         }
         for (int i = 255; i < (255 + msg.Data.Length); i++)
         {
             serializedEvent[i] = msg.Data[j];
             j++;
         }
         this.connection.Send(serializedEvent);
         return(true);
     }
     catch (SocketException e)
     {
         return(false);
     }
 }
Exemplo n.º 3
0
        private static void TestValTypeArray <T>(IActorMessageSerializationActor serialization, IActorMessageDeserializationActor deserialization, T[] val) where T : IEquatable <T>
        {
            ValueArrayTypeMessage <T> intType = new ValueArrayTypeMessage <T>(val);
            var    typeVal = (T[])intType.Message;
            string id      = intType.Id;

            SerializeMessage <IActorMessage> sMsg = new SerializeMessage <IActorMessage>(intType);

            if (!serialization.OnMessageRecieved(sMsg))
            {
                Assert.Fail(string.Format("Seriaization of array {0} Failed", val.GetType().Name));
            }

            DeserializeMessage <IActorMessage> dMsg = new DeserializeMessage <IActorMessage>(sMsg.ProcessingResult as byte[]);

            if (!deserialization.OnMessageRecieved(dMsg))
            {
                Assert.Fail(string.Format("DeSeriaization of array {0} Failed", val.GetType().Name));
            }

            T[] message = (T[])(dMsg.ProcessingResult as ValueTypeMessage <T>).Message;

            for (int i = 0; i < message.Length; ++i)
            {
                Assert.IsTrue(message[i].Equals(typeVal[i]));
            }
            Assert.IsTrue(message.Length != 0);
            Assert.IsTrue((dMsg.ProcessingResult as IActorMessageIdentifier).Id.Equals(id));
        }
Exemplo n.º 4
0
        public void RecieveActorMessageFromServiceBusQueue()
        {
            string queueName = "test-" + "RecieveActorMessageFromServiceBusQueue".ToLower();

            try
            {
                this.StartProcessing(queueName);
                var    sender       = GetServiceBusQueueSender(queueName);
                string message      = "HelloWorld";
                var    actorMessage = new PingPong.Ping(message);
                SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(actorMessage);
                IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();
                serializer.OnMessageRecieved(sMsg);
                byte[] msgBytes = (byte[])sMsg.ProcessingResult;
                sender.SendMessageAsync(new ServiceBusMessage(msgBytes));

                long azureOperatorID = Telegraph.Instance.Register(
                    new ServiceBusQueueSubscriptionOperator <IActorMessage>(LocalConcurrencyType.DedicatedThreadCount, Connections.ServiceBusConnectionString, queueName, false, 2),
                    (PingPong.Ping foo) => { Assert.IsTrue(message.Equals((string)foo.Message, StringComparison.InvariantCulture)); });
            }
            finally
            {
                Telegraph.Instance.UnRegisterAll();
                DeleteServiceBusQueue(queueName);
            }
        }
Exemplo n.º 5
0
        public void RecieveActorMessageFromEventHub()
        {
            string eventHubName  = "test-" + "RecieveActorMessageFromEventHub".ToLower();
            string consumerGroup = eventHubName + Guid.NewGuid().ToString().Substring(0, 6);

            CreateEventHub(eventHubName, consumerGroup);
            var client = new EventHubProducerClient(Connections.EventHubConnectionString, eventHubName);

            try
            {
                string message      = "HelloWorld";
                var    actorMessage = new PingPong.Ping(message);
                SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(actorMessage);
                IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();
                serializer.OnMessageRecieved(sMsg);
                byte[] msgBytes = (byte[])sMsg.ProcessingResult;

                using (var eventBatch = client.CreateBatchAsync().Result)
                {
                    eventBatch.TryAdd(new EventData(msgBytes));
                    client.SendAsync(eventBatch).Wait();
                }

                long azureOperatorID = Telegraph.Instance.Register(
                    new EventHubSubscriptionOperator <IActorMessage>(Connections.EventHubConnectionString, eventHubName, false),
                    (PingPong.Ping foo) => { Assert.IsTrue(message.Equals((string)foo.Message, StringComparison.InvariantCulture)); });
            }
            finally
            {
                Telegraph.Instance.UnRegisterAll();
                DeleteEventHub(eventHubName);
            }
        }
Exemplo n.º 6
0
        public static void ComplexMessageSerializationDeserialization()
        {
            Telegraph.Instance.Register(new LocalQueueOperator(new LocalSwitchboard(LocalConcurrencyType.ActorsOnThreadPool))); // performs a reset.

            IActorMessageDeserializationActor deserialization = new IActorMessageDeserializationActor();

            Telegraph.Instance.Register <SerializeMessage <IActorMessage>, IActorMessageSerializationActor>(() => new IActorMessageSerializationActor());
            Telegraph.Instance.Register <DeserializeMessage <IActorMessage>, IActorMessageDeserializationActor>(() => deserialization);
            deserialization.Register <CustomSerializableMessage>((object msg) => (CustomSerializableMessage)msg);

            CustomSerializableMessage        originalMsg   = new CustomSerializableMessage(100, "Foo");
            SerializeMessage <IActorMessage> serializeRqst = new SerializeMessage <IActorMessage>(originalMsg);

            var serializeTask = Telegraph.Instance.Ask(serializeRqst);

            serializeTask.Wait();

            //NOTE you could also do the following to avoid having to register above
            //var serializeTask = (new MessageSerializationActor()).Ask(serializeRqst);
            //serializeTask.Wait();

            byte[] serializedBytes = (serializeTask.Result.ProcessingResult as byte[]);

            DeserializeMessage <IActorMessage> msgToDeSerialize = new DeserializeMessage <IActorMessage>(serializedBytes);
            var task = Telegraph.Instance.Ask(msgToDeSerialize);

            task.Wait();

            CustomSerializableMessage output = (task.Result as CustomSerializableMessage);

            Console.WriteLine("Serialized " + output.MyProperty.ToString());
            System.Diagnostics.Debug.Assert(100 == output.MyProperty);
        }
Exemplo n.º 7
0
        public static void BasicMessageSerializationDeserialization()
        {
            Telegraph.Instance.Register(new LocalQueueOperator(new LocalSwitchboard(LocalConcurrencyType.ActorsOnThreadPool))); // performs a reset.

            Telegraph.Instance.Register <string>(message => Console.WriteLine("BasicMessageSerializationDeserialization: " + message));
            Telegraph.Instance.Register <SerializeMessage <IActorMessage>, IActorMessageSerializationActor>(() => new IActorMessageSerializationActor());
            Telegraph.Instance.Register <DeserializeMessage <IActorMessage>, IActorMessageDeserializationActor>(() => new IActorMessageDeserializationActor());

            var processingTask = Telegraph.Instance.Ask("foo");

            processingTask.Wait();

            SerializeMessage <IActorMessage> serializeRqst = new SerializeMessage <IActorMessage>(processingTask.Result);

            var serializeTask = Telegraph.Instance.Ask(serializeRqst);

            serializeTask.Wait();

            //NOTE you could also do the following to avoid having to register above
            //var serializeTask = (new MessageSerializationActor()).Ask(serializeRqst);
            //serializeTask.Wait();

            byte[] serializedBytes = (serializeTask.Result.ProcessingResult as byte[]);

            DeserializeMessage <IActorMessage> msgToSerialize2 = new DeserializeMessage <IActorMessage>(serializedBytes);
            var task = Telegraph.Instance.Ask(msgToSerialize2);

            task.Wait();

            string output = (task.Result.Message as string);

            System.Diagnostics.Debug.Assert(task.Result.GetType() == typeof(string));

            Console.WriteLine("Serialized and De-serialized " + output);
        }
Exemplo n.º 8
0
        /// <summary>
        /// Listen for a server request
        /// </summary>
        public void Listen()
        {
            Message msg = new Message();
            object  objReceived;
            string  strEvent = "";

            byte[] eventName;
            byte[] serializedEvent;
            bool   connected = true;
            int    objectIndex;

            try
            {
                while (connected)
                {
                    serializedEvent = new byte[1024 + 255];
                    eventName       = new byte[255];
                    msg.Data        = new byte[1024];
                    objectIndex     = 0;

                    this.connection.Receive(serializedEvent, 0, 1024 + 255, SocketFlags.None);
                    //Getting the event name buffer
                    for (int i = 0; i < 255; i++)
                    {
                        eventName[i] = serializedEvent[i];
                    }
                    strEvent = Utils.Buffer2String(eventName);

                    //Getting the object buffer
                    for (int i = 255; i < (1024 + 255); i++)
                    {
                        msg.Data[objectIndex] = serializedEvent[i];
                        objectIndex++;
                    }
                    objReceived = SerializeMessage.Deserialize(msg);
                    this.executeEvent(strEvent, objReceived);
                }
            }
            catch (SocketException e)
            {
                this.executeEvent("disconnect", e.Message);
            }
        }
Exemplo n.º 9
0
        public void RecieveActorMessageFromDirectoryQueue()
        {
            string queueName = "test-" + "RecieveActorMessageFromStorageQueue".ToLower();

            using (var queue = GetDirectoryQueue(queueName))
            {
                queue.CreateIfNotExists();
                try
                {
                    string message      = "HelloWorld";
                    var    actorMessage = new PingPong.Ping(message);
                    SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(actorMessage);
                    IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();
                    serializer.OnMessageRecieved(sMsg);
                    byte[]           msgBytes = (byte[])sMsg.ProcessingResult;
                    ManualResetEvent received = new ManualResetEvent(false);

                    IActorMessageDeserializationActor deserilaizationActor = new IActorMessageDeserializationActor();
                    Telegraph.Instance.Register <DeserializeMessage <IActorMessage> >(deserilaizationActor);
                    deserilaizationActor.Register((object msg) => (IActorMessage)msg);
                    deserilaizationActor.Register((object msg) => (PingPong.Ping)msg);

                    long azureOperatorID = Telegraph.Instance.Register(
                        new DirectoryQueueSubscriptionOperator <IActorMessage>(LocalConcurrencyType.DedicatedThreadCount, queueRootPath, queueName, false, 2),
                        (PingPong.Ping foo) =>
                    {
                        Assert.IsTrue(message.Equals((string)foo.Message, StringComparison.InvariantCulture));
                        received.Set();
                    });

                    queue.AddMessage(new DirectoryQueueMessage(msgBytes));

                    Assert.IsTrue(received.WaitOne(TimeSpan.FromSeconds(13), true), "We did not receive the message");
                }
                finally
                {
                    Telegraph.Instance.UnRegisterAll();
                    GetDirectoryQueue(queueName).Delete();
                    GetDirectoryQueue(queueName + "-deadletter").Delete();
                }
            }
        }
Exemplo n.º 10
0
        public void RecieveActorMessageFromStorageQueuePostRegister()
        {
            string queueName = "test-" + "RecieveActorMessageFromStorageQueuePostRegister".ToLower();
            var    queue     = GetStorageQueue(queueName);

            queue.CreateIfNotExists();
            try
            {
                string message      = "HelloWorld";
                var    actorMessage = new PingPong.Ping(message);
                SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(actorMessage);
                IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();
                serializer.OnMessageRecieved(sMsg);
                byte[]           msgBytes       = (byte[])sMsg.ProcessingResult;
                string           base64MsgBytes = Convert.ToBase64String(msgBytes);
                ManualResetEvent received       = new ManualResetEvent(false);

                Telegraph.Instance.Register <DeserializeMessage <IActorMessage>, IActorMessageDeserializationActor>(() => new IActorMessageDeserializationActor());

                long azureOperatorID = Telegraph.Instance.Register(
                    new StorageQueueSubscriptionOperator <IActorMessage>(LocalConcurrencyType.DedicatedThreadCount, Connections.StorageConnectionString, queueName, false, 2),
                    (PingPong.Ping foo) =>
                {
                    Assert.IsTrue(message.Equals((string)foo.Message, StringComparison.InvariantCulture));
                    received.Set();
                });

                queue.SendMessage(new BinaryData(base64MsgBytes));

                Assert.IsTrue(received.WaitOne(TimeSpan.FromSeconds(3), true), "We did not receive the message");
            }
            finally
            {
                Telegraph.Instance.UnRegisterAll();
                GetStorageQueue(queueName).DeleteAsync();
                GetStorageQueue(queueName + "-deadletter").DeleteAsync();
            }
        }
Exemplo n.º 11
0
        public HybridConnectionListener CreateHybridListener(string hybridConnectionName, PingPong.Pong response)
        {
            RelayConnectionStringBuilder connectionStringBuilder = new RelayConnectionStringBuilder(Connections.HybridRelayConnectionString)
            {
                EntityPath = hybridConnectionName
            };
            //var tokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(connectionStringBuilder.SharedAccessKeyName, connectionStringBuilder.SharedAccessKey);
            //var uri = new Uri(string.Format("https://{0}/{1}", connectionStringBuilder.Endpoint.Host, hybridConnectionName));
            var listener = new HybridConnectionListener(connectionStringBuilder.ToString());

            SerializeMessage <IActorMessage> sMsg       = new SerializeMessage <IActorMessage>(response);
            IActorMessageSerializationActor  serializer = new IActorMessageSerializationActor();

            serializer.OnMessageRecieved(sMsg);
            byte[] responseBytes = (byte[])sMsg.ProcessingResult;

            // Subscribe to the status events.
            listener.Connecting += (o, e) => { Console.WriteLine("Connecting"); };
            listener.Offline    += (o, e) => { Console.WriteLine("Offline"); };
            listener.Online     += (o, e) => { Console.WriteLine("Online"); };

            // Provide an HTTP request handler
            listener.RequestHandler = (context) =>
            {
                // Do something with context.Request.Url, HttpMethod, Headers, InputStream...
                context.Response.StatusCode        = HttpStatusCode.OK;
                context.Response.StatusDescription = "OK";
                context.Response.OutputStream.Write(responseBytes, 0, responseBytes.Length);

                // The context MUST be closed here
                context.Response.Close();
            };

            listener.OpenAsync().Wait();
            Console.WriteLine("Server listening");
            return(listener);
        }
Exemplo n.º 12
0
        public byte[] SerializeSerializeMessageReturn()
        {
            Telegraph.Instance.UnRegisterAll();

            Telegraph.Instance.Register(new LocalQueueOperator(new LocalSwitchboard(LocalConcurrencyType.ActorsOnThreadPool))); // performs a reset.
            IActorMessageSerializationActor serializer     = new IActorMessageSerializationActor();
            SimpleMessage <string>          msgToSerialize = new SimpleMessage <string>();

            msgToSerialize.Message = "Foo";

            SerializeMessage <IActorMessage> serializeMessage = new SerializeMessage <IActorMessage>(msgToSerialize);

            msgToSerialize.ProcessingResult = null;

            Telegraph.Instance.Register <SerializeMessage <IActorMessage>, IActorMessageSerializationActor>(() => new IActorMessageSerializationActor());

            var task = Telegraph.Instance.Ask(serializeMessage);

            task.Wait();

            byte[] serializedBytes2 = (task.Result.ProcessingResult as byte[]);

            return(serializedBytes2);
        }