Exemple #1
0
        private void ListenToRequests()
        {
            var buffer = new byte[1];
            var stream = new List <byte>();

            while (_client.Connected)
            {
                var count = _client.Client.Receive(buffer);
                if (count <= 0)
                {
                    continue;
                }
                if (buffer[0] == 0)
                {
                    var total         = stream.ToArray();
                    var messageString = Encoding.UTF8.GetString(total);
                    var msg           = StompMessage.FromString(messageString);
                    stream.Clear();
                    if (OnReceived != null)
                    {
                        OnReceived(this, msg);
                    }
                }
                else
                {
                    stream.Add(buffer[0]);
                }
            }
        }
Exemple #2
0
        private void HandleNotification(object sender, StompMessage msg)
        {
            try
            {
                if (!msg.Type.Equals(ServerActions.Notification))
                {
                    return;
                }

                // TODO: handle notification here
                var type     = msg.Headers["type"];
                var subforum = msg.Headers["subforum"];
                DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable <Message>));
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(msg.Content)))
                {
                    IEnumerable <Message> messages = (IEnumerable <Message>)serializer.ReadObject(stream);


                    Notification notification = new Notification {
                        SubForum = subforum, Type = NotifictionType.RefreshAll, Messages = messages
                    };
                    if (OnNotification != null)
                    {
                        OnNotification(this, notification);
                    }
                }
            }
            catch (Exception ex)
            {
            }
        }
Exemple #3
0
 protected void FillError(StompMessage msg)
 {
     if (msg.Headers.ContainsKey("error"))
     {
         Error = msg.Headers["error"];
     }
 }
Exemple #4
0
        public void Send(StompMessage message)
        {
            var buffer = Encoding.UTF8.GetBytes(message.ToString());

            _client.Client.Send(buffer);
            var zero = new byte[] { 0 };

            _client.Client.Send(zero);
        }
Exemple #5
0
        private void HandleLoginResponse(object server, StompMessage msg)
        {
            if (msg.Type != ServerActions.LoginMember)
            {
                return;
            }

            _client.OnReceived -= HandleLoginResponse;
            _waitEvent.Set();
        }
Exemple #6
0
 private void HandleRemoveSubForumResponse(object sender, StompMessage e)
 {
     if (e.Type != ServerActions.RemoveSubForum)
     {
         return;
     }
     FillError(e);
     _client.OnReceived -= HandleRemoveSubForumResponse;
     _waitEvent.Set();
 }
Exemple #7
0
 private void HandleEditMessageResponse(object sender, StompMessage msg)
 {
     if (msg.Type != ServerActions.EditMessage)
     {
         return;
     }
     FillError(msg);
     _client.OnReceived -= HandleEditMessageResponse;
     _waitEvent.Set();
 }
Exemple #8
0
 private void HandleRemoveFriendRequest(object sender, StompMessage msg)
 {
     if (msg.Type != ServerActions.RemoveFriend)
     {
         return;
     }
     FillError(msg);
     _client.OnReceived -= HandleRemoveFriendRequest;
     _waitEvent.Set();
 }
Exemple #9
0
 private void HandleIsSystemInitializedResponse(object server, StompMessage msg)
 {
     if (msg.Type != ServerActions.IsSystemInitialized)
     {
         return;
     }
     FillError(msg);
     _client.OnReceived -= HandleIsSystemInitializedResponse;
     _initialized        = bool.Parse(msg.Content);
     _waitEvent.Set();
 }
Exemple #10
0
 private void HandleLoginGuestRequest(object sender, StompMessage e)
 {
     if (e.Type != ServerActions.LoginGuest)
     {
         return;
     }
     FillError(e);
     _client.OnReceived -= HandleLoginGuestRequest;
     _sid = e.Content;
     _waitEvent.Set();
 }
Exemple #11
0
 private void HandleGetSubForumsResponse(object sender, StompMessage msg)
 {
     if (msg.Type != ServerActions.GetSubForums)
     {
         return;
     }
     FillError(msg);
     _subforums          = msg.Content.Trim().Split('\n');
     _client.OnReceived -= HandleGetSubForumsResponse;
     _waitEvent.Set();
 }
Exemple #12
0
        private void HandleLoginGuestResponse(object sender, StompMessage msg)
        {
            if (msg.Type != ServerActions.LoginGuest)
            {
                return;
            }

            FillError(msg);
            _sessionId          = msg.Headers["sid"];
            _client.OnReceived -= HandleLoginGuestResponse;
            _waitEvent.Set();
        }
Exemple #13
0
        public void LogIn(string username, string password)
        {
            var msg = new StompMessage {
                Type = ServerActions.LoginMember
            };

            msg.Headers.Add("username", username);
            msg.Headers.Add("password", password);
            _client.OnReceived += HandleLoginResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);
        }
Exemple #14
0
        public string[] GetForums()
        {
            var msg = new StompMessage {
                Type = ServerActions.GetForums
            };

            _client.OnReceived += HandleGetForumsResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);
            ValidateError();
            return(_forums);
        }
Exemple #15
0
        public void LogoutMember(string sid)
        {
            var msg = new StompMessage {
                Type = ServerActions.LogoutMember
            };

            msg.Headers.Add("sid", sid);

            _client.OnReceived += HandleLogoutMemberRequest;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #16
0
        public void InitSystem(string admin, string adminpass)
        {
            var msg = new StompMessage {
                Type = ServerActions.InitSystem
            };

            msg.Headers.Add("username", admin);
            msg.Headers.Add("password", adminpass);
            _client.OnReceived += HandleInitSystemResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #17
0
        public void RemoveFriend(string sid, string friend)
        {
            var msg = new StompMessage {
                Type = ServerActions.RemoveFriend
            };

            msg.Headers.Add("sid", sid);
            msg.Headers.Add("friend", friend);

            _client.OnReceived += HandleRemoveFriendRequest;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #18
0
        public void RemoveForum(string adminSession, string forumName)
        {
            var msg = new StompMessage {
                Type = ServerActions.RemoveForum
            };

            msg.Headers.Add("forum", forumName);
            msg.Headers.Add("adminSessionId", adminSession);

            _client.OnReceived += HandleRemoveForumResponse;

            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #19
0
        public string LoginGuest(string forumName)
        {
            var msg = new StompMessage {
                Type = ServerActions.LoginGuest
            };

            msg.Headers.Add("forum", forumName);

            _sid = string.Empty;
            _client.OnReceived += HandleLoginGuestRequest;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
            return(_sid);
        }
Exemple #20
0
        public IEnumerable <Message> GetThreads(string sid, string subforum)
        {
            var msg = new StompMessage {
                Type = ServerActions.GetThreads
            };

            msg.Headers.Add("sid", sid);
            msg.Headers.Add("subforum", subforum);

            _client.OnReceived += HandleGetThreadsResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
            return(_messages);
        }
Exemple #21
0
        public string[] GetSubForums(string sid)
        {
            var msg = new StompMessage {
                Type = ServerActions.GetSubForums
            };

            msg.Headers.Add("sid", sid);

            _subforums          = null;
            _client.OnReceived += HandleGetSubForumsResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
            return(_subforums);
        }
Exemple #22
0
        public void RemoveMessage(string sid, string subforum, string msgid)
        {
            var msg = new StompMessage {
                Type = ServerActions.RemoveMessage
            };

            msg.Headers.Add("sid", sid);
            msg.Headers.Add("subforum", subforum);
            msg.Headers.Add("msgid", msgid);

            _client.OnReceived += HandleRemoveMessageResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #23
0
        public void Register(string sid, string username, string password)
        {
            var msg = new StompMessage {
                Type = ServerActions.Register
            };

            msg.Headers.Add("sid", sid);
            msg.Headers.Add("username", username);
            msg.Headers.Add("password", password);

            _client.OnReceived += HandleRegisterRequest;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #24
0
        public string LogInAdmin(string username, string password)
        {
            var msg = new StompMessage {
                Type = ServerActions.LoginAdmin
            };

            msg.Headers.Add("username", username);
            msg.Headers.Add("password", password);
            _client.OnReceived += HandleLoginAdminResponse;
            _sid = string.Empty;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
            return(_sid);
        }
Exemple #25
0
        public void AddModerator(string sid, string subforum, string moderator)
        {
            var msg = new StompMessage()
            {
                Type = ServerActions.AddModerator
            };

            msg.Headers.Add("sid", sid);
            msg.Headers.Add("subforum", subforum);
            msg.Headers.Add("moderator", moderator);

            _client.OnReceived += HandleAddModeratorResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #26
0
        public void AddNewThread(string sid, string subforum, string title, string body)
        {
            var msg = new StompMessage {
                Type = ServerActions.AddNewThread
            };

            msg.Headers.Add("sid", sid);
            msg.Headers.Add("subforum", subforum);
            msg.Headers.Add("title", title);
            msg.Content = body;

            _client.OnReceived += HandleAddNewThreadResponse;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #27
0
        public void CreateForum(string adminSession, string forumName, string manager, string password)
        {
            var msg = new StompMessage {
                Type = ServerActions.CreateForum
            };

            msg.Headers.Add("forum", forumName);
            msg.Headers.Add("adminSessionId", adminSession);
            msg.Headers.Add("manager", manager);
            msg.Headers.Add("password", password);
            _client.OnReceived += HandleCreateForumResponse;

            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);

            ValidateError();
        }
Exemple #28
0
        public bool IsSystemInitialized()
        {
            var msg = new StompMessage {
                Type = ServerActions.IsSystemInitialized
            };

            _client.OnReceived += HandleIsSystemInitializedResponse;
            _initialized        = false;
            _client.Send(msg);
            _waitEvent.WaitOne(TIMEOUT);
            try
            {
                ValidateError();
                return(_initialized);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Exemple #29
0
        private void HandleGetThreadsResponse(object sender, StompMessage msg)
        {
            if (msg.Type != ServerActions.GetThreads)
            {
                return;
            }
            FillError(msg);
            _client.OnReceived -= HandleGetThreadsResponse;

            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(IEnumerable <Message>));

            using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(msg.Content)))
            {
                IEnumerable <Message> messages = (IEnumerable <Message>)serializer.ReadObject(stream);

                //JsonValue value = JsonObject.Parse(msg.Content);
                _messages = messages;

                _waitEvent.Set();
            }
        }
Exemple #30
0
        private void HandleLoginAdminResponse(object sender, StompMessage msg)
        {
            if (msg.Type != ServerActions.LoginAdmin)
            {
                return;
            }
            ValidateError();

            try
            {
                _sid = msg.Headers["sid"];
            }
            catch (Exception ex)
            {
            }
            finally
            {
                _client.OnReceived -= HandleLoginAdminResponse;
                _waitEvent.Set();
            }
        }