Пример #1
0
        public override WebServiceResponse Unmarshall(XmlUnmarshallerContext context)
        {
            XmlTextReader     reader   = new XmlTextReader(context.ResponseStream);
            ListTopicResponse response = new ListTopicResponse();

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:
                    switch (reader.LocalName)
                    {
                    case MNSConstants.XML_ELEMENT_TOPIC_URL:
                        reader.Read();
                        response.TopicUrls.Add(reader.Value);
                        break;

                    case MNSConstants.XML_ELEMENT_NEXT_MARKER:
                        reader.Read();
                        response.NextMarker = reader.Value;
                        break;
                    }
                    break;
                }
            }
            reader.Close();
            return(response);
        }
Пример #2
0
        public void start()
        {
            string username;
            string password;

            int     choix  = 0;
            Boolean logged = false;


            Console.WriteLine("Connection established");
            while (true)
            {
                TcpClient comm = new TcpClient(hostname, port);
                do
                {
                    do
                    {
                        Console.WriteLine("1- Login\n2-Register ");
                        choix = int.Parse(Console.ReadLine());
                    } while (choix != 1 && choix != 2);

                    switch (choix)
                    {
                    case 1:
                        Console.WriteLine("\n\n-----------LOGIN---------");
                        Console.WriteLine("Username pls : ");
                        username = Console.ReadLine();
                        Console.WriteLine("Password pls : ");
                        password = Console.ReadLine();


                        Net.sendMsg(comm.GetStream(), new LoginRequest(username, password));

                        LoginResponse loginResponse = (LoginResponse)Net.rcvMsg(comm.GetStream());
                        logged = loginResponse.IsLogged;
                        id     = loginResponse.Id;

                        if (!logged)
                        {
                            Console.WriteLine("Wrong credentials !!");
                        }
                        else
                        {
                            Console.WriteLine("you've been logged");
                            status           = Status.Logged;
                            this.currentUser = loginResponse.User;
                        }
                        break;

                    case 2:

                        Console.WriteLine("\n\n-----------Register---------");
                        Console.WriteLine("Username pls : ");
                        username = Console.ReadLine();

                        bool isSame = false;

                        do
                        {
                            Console.WriteLine("Password pls : ");
                            password = Console.ReadLine();
                            Console.WriteLine("Repeat Password pls : ");
                            string repatPassword = Console.ReadLine();

                            if (password != repatPassword)
                            {
                                Console.WriteLine("Please enter the same password !!!");
                            }
                            else
                            {
                                isSame = true;
                            }
                        } while (!isSame);
                        Net.sendMsg(comm.GetStream(), new RegisterRequest(username, password));
                        RegisterResponse response = (RegisterResponse)Net.rcvMsg(comm.GetStream());

                        if (response.HasError)
                        {
                            Console.WriteLine(response.Error);
                        }
                        else
                        {
                            Console.WriteLine("you have been registered as " + response.Username);
                        }

                        break;
                    }
                } while (status == Status.Disconnected);

                do
                {
                    Console.WriteLine("1- List topics \n2- create topic\n3- join a topic\n4- send private message\n");
                    Console.Write("--> ");
                    try
                    {
                        choix = int.Parse(Console.ReadLine());
                    }
                    catch
                    {
                        choix = 0;
                    }


                    switch (choix)
                    {
                    case 1:
                        Net.sendMsg(comm.GetStream(), new ListTopicRequest());
                        ListTopicResponse response = (ListTopicResponse)Net.rcvMsg(comm.GetStream());
                        response.display();
                        Console.WriteLine("\n");
                        break;

                    case 2:
                        Console.WriteLine("Please write down the topic title : ");
                        Console.Write("--> ");
                        string title = Console.ReadLine();
                        Console.WriteLine("Please write down a description for the topic : ");
                        string content = Console.ReadLine();



                        Net.sendMsg(comm.GetStream(), new CreateRequest(id, title, content, DateTime.Now));

                        CreateResponse createResponse = (CreateResponse)Net.rcvMsg(comm.GetStream());

                        if (createResponse.HasError)
                        {
                            Console.WriteLine(createResponse.Error);
                        }
                        else
                        {
                            Console.WriteLine("Your topic have been created, you can check it out by listing the topics");
                        }
                        break;

                    case 3:
                        Console.WriteLine("Please choose the number of the topic from the list : ");
                        Console.Write("--> ");
                        int topicNumber;
                        try
                        {
                            topicNumber = int.Parse(Console.ReadLine());
                        }
                        catch
                        {
                            topicNumber = 0;
                        }

                        Net.sendMsg(comm.GetStream(), new JoinTopicRequest(topicNumber));

                        JoinTopicResponse joinResponse = (JoinTopicResponse)Net.rcvMsg(comm.GetStream());

                        if (joinResponse.HasError)
                        {
                            Console.WriteLine(joinResponse.Error);
                        }
                        else
                        {
                            joinResponse.display();

                            bool continuer = true;

                            do
                            {
                                Console.WriteLine("send a message ? (y/n)");
                                ConsoleKeyInfo input = Console.ReadKey(true);

                                if (input.Key == ConsoleKey.N)
                                {
                                    continuer = false;
                                }
                                else
                                {
                                    Console.WriteLine("Please, write your message : ");
                                    Console.Write("-->  ");
                                    string message = Console.ReadLine();

                                    TopicMessage topicMsg = new TopicMessage(DateTime.Now, currentUser.Username, message, topicNumber);

                                    Net.sendMsg(comm.GetStream(), new SendTopicMsgRequest(topicMsg));
                                    SendTopicMsgResponse msgResponse = (SendTopicMsgResponse)Net.rcvMsg(comm.GetStream());

                                    if (!msgResponse.HasError)
                                    {
                                        joinResponse.Topic.addMessage(topicMsg);

                                        joinResponse.Topic.displayMessages();
                                    }
                                    else
                                    {
                                        Console.WriteLine("Error : " + msgResponse.Error);
                                    }
                                }
                            } while (continuer);
                        }
                        break;

                    case 4:
                        Console.WriteLine("Who do you want to send the message to :");
                        Console.Write("-->  ");
                        String receiver = Console.ReadLine();

                        if (receiver == currentUser.Username)
                        {
                            Console.WriteLine("\n\nYOU CAN'T SEND A MESSAGE TO YOURSELF !!\n\n");
                        }
                        else
                        {
                            Net.sendMsg(comm.GetStream(), new FindUserRequest(currentUser.Id, receiver));
                            FindUserResponse findResponse = (FindUserResponse)Net.rcvMsg(comm.GetStream());


                            if (!findResponse.HasError)
                            {
                                findResponse.displayChat();

                                bool continuer = true;

                                do
                                {
                                    Console.WriteLine("send a message ? (y/n)");
                                    ConsoleKeyInfo input = Console.ReadKey(true);

                                    if (input.Key == ConsoleKey.N)
                                    {
                                        continuer = false;
                                    }
                                    else
                                    {
                                        Console.WriteLine("Please, write your message : ");
                                        Console.Write("-->  ");
                                        string message = Console.ReadLine();

                                        PrivateMessage privateMsg = new PrivateMessage(DateTime.Now, currentUser.Id, findResponse.Receiver.Id, message);

                                        Net.sendMsg(comm.GetStream(), new SendPrivateRequest(privateMsg));
                                        SendPrivateResponse msgResponse = (SendPrivateResponse)Net.rcvMsg(comm.GetStream());

                                        if (!msgResponse.HasError)
                                        {
                                            findResponse.Messages.Add(privateMsg);

                                            findResponse.displayChat();
                                        }
                                        else
                                        {
                                            Console.WriteLine("Error : " + msgResponse.Error);
                                        }
                                    }
                                } while (continuer);
                            }
                            else
                            {
                                Console.WriteLine("Error : " + findResponse.Error);
                            }
                        }
                        break;

                    default:
                        Console.WriteLine("make a wise choice mate");
                        break;
                    }
                } while (status == Status.Logged);

                //Net.sendMsg(comm.GetStream(), new Expr(op1, op2, op));
                //Console.WriteLine("Result = " + (Result)Net.rcvMsg(comm.GetStream()));
            }
        }
Пример #3
0
        static void Main(string[] args)
        {
            var config = Newtonsoft.Json.JsonConvert.DeserializeObject <ConfigModel>(File.ReadAllText(@"E:\MNS.json"));

            _accessKeyId     = config.AccessKeyId;
            _secretAccessKey = config.AccessKey;
            _endpoint        = config.EndPoint;

            #region Topic Releated Test Cases

            IMNS client = new MNSClient(_accessKeyId, _secretAccessKey, _endpoint);

            /* 1.1. Create queue */
            var createTopicRequest = new CreateTopicRequest
            {
                TopicName = _topicName
            };

            Topic topic = null;
            try
            {
                client.DeleteTopic(_topicName);
                topic = client.CreateTopic(createTopicRequest);
                Console.WriteLine("Create topic successfully, topic name: {0}", topic.TopicName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Create topic failed, exception info: " + ex.Message);
                return;
            }

            topic = client.GetNativeTopic(_topicName);
            try
            {
                ListTopicResponse res = client.ListTopic(null, null, 10);
                Console.WriteLine("List topic successfully, topic name: {0}", _topicName);
                foreach (String topicUrl in res.TopicUrls)
                {
                    Console.WriteLine(topicUrl);
                }
                if (res.NextMarker != null)
                {
                    Console.WriteLine("NextMarker: " + res.NextMarker);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete topic failed, exception info: " + ex.Message);
            }

            try
            {
                GetTopicAttributesResponse res = topic.GetAttributes();
                Console.WriteLine("GetTopicAttributes, topic name: {0}", _topicName);
                Console.WriteLine(res.Attributes.CreateTime);
                Console.WriteLine(res.Attributes.LastModifyTime);
                Console.WriteLine(res.Attributes.MaximumMessageSize);
                Console.WriteLine(res.Attributes.MessageRetentionPeriod);
                Console.WriteLine(res.Attributes.LoggingEnabled);
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetTopicAttributes failed, exception info: " + ex.Message);
            }

            try
            {
                TopicAttributes attributes = new TopicAttributes()
                {
                    MaximumMessageSize = 2048
                };
                topic.SetAttributes(attributes);
                Console.WriteLine("SetTopicAttributes succeed, topic name: {0}", _topicName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("SetTopicAttributes failed, exception info: " + ex.Message + ex.GetType().Name);
            }

            try
            {
                SubscribeResponse res = topic.Subscribe(_subscriptionName, "http://XXXX");
                Console.WriteLine("Subscribe, subscriptionUrl: {0}", res.SubscriptionUrl);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
            }

            try
            {
                GetSubscriptionAttributeResponse res = topic.GetSubscriptionAttribute(_subscriptionName);
                Console.WriteLine("GetSubscriptionAttributeResponse, subs name: {0}", _subscriptionName);
                Console.WriteLine(res.Attributes.CreateTime);
                Console.WriteLine(res.Attributes.LastModifyTime);
                Console.WriteLine(res.Attributes.TopicName);
                Console.WriteLine(res.Attributes.TopicOwner);
                Console.WriteLine(res.Attributes.EndPoint);
                Console.WriteLine(res.Attributes.Strategy);
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetSubscriptionAttribute failed, exception info: " + ex.Message);
            }

            try
            {
                SubscriptionAttributes attributes = new SubscriptionAttributes()
                {
                    Strategy = SubscriptionAttributes.NotifyStrategy.EXPONENTIAL_DECAY_RETRY
                };
                topic.SetSubscriptionAttribute(_subscriptionName, attributes);
                Console.WriteLine("SetSubscriptionAttribute succeed, topic name: {0}", _topicName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("SetSubscriptionAttribute failed, exception info: " + ex.Message + ex.GetType().Name);
            }

            try
            {
                GetSubscriptionAttributeResponse res = topic.GetSubscriptionAttribute(_subscriptionName);
                Console.WriteLine("GetSubscriptionAttributeResponse, subs name: {0}", _subscriptionName);
                Console.WriteLine(res.Attributes.CreateTime);
                Console.WriteLine(res.Attributes.LastModifyTime);
                Console.WriteLine(res.Attributes.TopicName);
                Console.WriteLine(res.Attributes.TopicOwner);
                Console.WriteLine(res.Attributes.EndPoint);
                Console.WriteLine(res.Attributes.Strategy);
                Console.WriteLine(res.Attributes.ContentFormat);
            }
            catch (Exception ex)
            {
                Console.WriteLine("GetSubscriptionAttribute failed, exception info: " + ex.Message);
            }

            try
            {
                ListSubscriptionResponse res = topic.ListSubscription("");
                Console.WriteLine("ListSubscription successfully, topic name: {0}", _topicName);
                foreach (String subscriptionUrl in res.SubscriptionUrls)
                {
                    Console.WriteLine(subscriptionUrl);
                }
                if (res.NextMarker != null)
                {
                    Console.WriteLine("NextMarker: " + res.NextMarker);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("ListSubscription failed, exception info: " + ex.Message);
            }

            try
            {
                var response = topic.PublishMessage("message here </asdas\">");
                Console.WriteLine("PublishMessage succeed! " + response.MessageId);
            }
            catch (Exception ex)
            {
                Console.WriteLine("PublishMessage failed, exception info: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            try
            {
                topic.Unsubscribe(_subscriptionName);
                Console.WriteLine("Unsubscribe succeed!");
            }
            catch (Exception ex)
            {
                Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
                Console.WriteLine(ex.StackTrace);
            }

            //// PUBLISH MESSAGE for SMS
            //try
            //{
            //    var res = topic.Subscribe(_subscriptionName + "batchsms", topic.GenerateBatchSmsEndpoint());
            //    Console.WriteLine(res.SubscriptionUrl);

            //    PublishMessageRequest request = new PublishMessageRequest();
            //    MessageAttributes messageAttributes = new MessageAttributes();
            //    BatchSmsAttributes batchSmsAttributes = new BatchSmsAttributes();
            //    batchSmsAttributes.FreeSignName = "陈舟锋";
            //    batchSmsAttributes.TemplateCode = "SMS_15535414";
            //    Dictionary<string, string> param = new Dictionary<string, string>();
            //    param.Add("name", "CSharpBatch");
            //    batchSmsAttributes.AddReceiver("13735576932", param);

            //    messageAttributes.BatchSmsAttributes = batchSmsAttributes;
            //    request.MessageAttributes = messageAttributes;
            //    request.MessageBody = "</asdas\">";
            //    PublishMessageResponse resp = topic.PublishMessage(request);

            //    Console.WriteLine(resp.MessageId);

            //    // check sms
            //    System.Threading.Thread.Sleep(3000);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
            //}

            //// PUBLISH MESSAGE for SMS
            //try
            //{
            //    var res = topic.Subscribe(_subscriptionName + "singlesms", topic.GenerateSmsEndpoint());
            //    Console.WriteLine(res.SubscriptionUrl);

            //    PublishMessageRequest request = new PublishMessageRequest();
            //    MessageAttributes messageAttributes = new MessageAttributes();
            //    SmsAttributes smsAttributes = new SmsAttributes();
            //    smsAttributes.FreeSignName = "陈舟锋";
            //    smsAttributes.TemplateCode = "SMS_15535414";
            //    Dictionary<string, string> param = new Dictionary<string, string>();
            //    param.Add("name", "CSharpSingle");
            //    smsAttributes.Receiver = "13735576932";
            //    smsAttributes.SmsParams = param;

            //    messageAttributes.SmsAttributes = smsAttributes;
            //    request.MessageAttributes = messageAttributes;
            //    request.MessageBody = "</asdas\">";
            //    PublishMessageResponse resp = topic.PublishMessage(request);

            //    Console.WriteLine(resp.MessageId);

            //    // check sms
            //    System.Threading.Thread.Sleep(3000);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
            //}

            //// PUBLISH MESSAGE TO QUEUE AND MAIL
            //string queueName = "TestQueueNameHere";
            //try
            //{
            //    var queue = client.CreateQueue(queueName);

            //    var res = topic.Subscribe(_subscriptionName, topic.GenerateMailEndpoint("*****@*****.**"));
            //    // res = topic.Subscribe(_subscriptionName + "2", topic.GenerateQueueEndpoint(queueName));
            //    res = topic.Subscribe(new SubscribeRequest(_subscriptionName + "2", topic.GenerateQueueEndpoint(queueName), "TAG", SubscriptionAttributes.NotifyStrategy.BACKOFF_RETRY, SubscriptionAttributes.NotifyContentFormat.JSON));

            //    PublishMessageRequest request = new PublishMessageRequest();
            //    MessageAttributes messageAttributes = new MessageAttributes();
            //    MailAttributes mailAttributes = new MailAttributes();
            //    mailAttributes.AccountName = "*****@*****.**";
            //    mailAttributes.Subject = "TestMail C#";
            //    mailAttributes.IsHtml = false;
            //    mailAttributes.ReplyToAddress = false;
            //    mailAttributes.AddressType = 0;
            //    messageAttributes.MailAttributes = mailAttributes;
            //    request.MessageAttributes = messageAttributes;
            //    request.MessageTag = "TAG";
            //    request.MessageBody = "message here2222 </asdas\">";
            //    topic.PublishMessage(request);

            //    var resp = queue.ReceiveMessage(30);
            //    Console.WriteLine(resp.Message.Body);

            //    // check mailbox
            //    System.Threading.Thread.Sleep(3000);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Subscribe failed, exception info: " + ex.Message);
            //}

            //try
            //{
            //    client.DeleteQueue(queueName);
            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine("Delete queue failed, exception info: " + ex.Message);
            //}

            try
            {
                client.DeleteTopic(_topicName);
                Console.WriteLine("Delete topic successfully, topic name: {0}", _topicName);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Delete topic failed, exception info: " + ex.Message);
            }
            #endregion

            Console.ReadKey();
        }