Пример #1
0
        override protected int _execute(Communicate request)
        {
            // Perform task:


            return(Command.OK);
        }
        public void connectionHandling()
        {
            //Console.WriteLine("Tworzę wątek dla połączenia!");
            NetworkStream stream = client.GetStream();
            int           max    = 65565;

            Byte[] bytes  = new Byte[max];
            Byte[] respon = new Byte[max];
            int    i;

            RequestResolver resolver = new RequestResolver(client);

            while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
            {
                Communicate communicate = new Communicate();
                try
                {
                    if (communicate.fromByteArray(bytes))
                    {
                        Communicate response = resolver.handle(communicate);

                        // Inform server:
                        Console.WriteLine(response.sessID + ": " + response.message != null ? response.message.content : "");

                        // Try to send response from server:
                        respon = response.toByteArray();
                        stream.Write(respon, 0, respon.Length);
                        stream.Flush();
                    }
                } catch (Exception e)
                {
                }
            }
        }
Пример #3
0
 public Communicate(Communicate ob)
 {
     _header   = ob.header;
     _receiver = ob.receiver;
     _sender   = ob.sender;
     _sessID   = ob.sessID;
     _user     = ob.user;
     _message  = ob.message;
 }
Пример #4
0
        override protected int _execute(Communicate request)
        {
            using (var db = new UserContext())
            {
                User user = request.user;
                if (user != null)
                {
                    string username = user.username;
                    string password = user.password;

                    var items = (from b in db.Users
                                 orderby b.username
                                 where b.username == username
                                 where b.password == password
                                 select b);

                    int count = items.Count();

                    // If there is such user:
                    if (count == 1)
                    {
                        // Set session variable:
                        Session session = Session.getInstance();
                        session.set(request.sessID, "user", "1");

                        session.set(request.sessID, "username", items.FirstOrDefault().username);

                        if (request.sender != null)
                        {
                            session.set(request.sessID, "ip", request.sender.ToString());
                        }

                        // Setting response:
                        _response.header  = "authorized";
                        _response.message = new Message {
                            content = "Corretly logged user"
                        };
                        return(Command.OK);
                    }
                    else
                    {
                        _response.header  = "error";
                        _response.message = new Message {
                            content = "There is no such a user. Your username or password is wrong"
                        };
                        return(Command.ERROR);
                    }
                }
            }
            // Communicate error occurrance:
            _response.header  = "error";
            _response.message = new Message {
                content = "A proplem occurred while perform authorization"
            };
            return(Command.ERROR);
        }
        override protected int _execute(Communicate request)
        {
            // Perform default task:
            _response.header  = "error";
            _response.message = new Message {
                content = "Unknown server request"
            };

            return(Command.OK);
        }
Пример #6
0
        override protected int _execute(Communicate request)
        {
            // TODO: send message to user
            Session session = Session.getInstance();
            User    sender  = new User {
                username = session.get(request.sessID, "username")
            };

            User receiver = request.user;

            if (receiver != null)
            {
                string sid = session.getSessionIdByUsername(receiver.username);

                if (sid != null)
                {
                    ClientsInfo clients = ClientsInfo.getInstance();
                    TcpClient   client  = clients.GetClient(sid);

                    if (client != null)
                    {
                        NetworkStream stream = client.GetStream();
                        int           max    = 65565;
                        Byte[]        bytes  = new Byte[max];
                        int           i;

                        Communicate user_message_response = new Communicate();
                        user_message_response.header          = "received";
                        user_message_response.sessID          = sid;
                        user_message_response.user            = sender;
                        user_message_response.message.content = request.message != null ? request.message.content : "";

                        try
                        {
                            // Try to send response from server:
                            bytes = user_message_response.toByteArray();
                            stream.Write(bytes, 0, bytes.Length);
                            stream.Flush();

                            _response.header          = "sent";
                            _response.message.content = "Message has been sent";

                            return(Command.OK);
                        }
                        catch (Exception e) {}
                    }
                }
            }

            _response.header          = "error";
            _response.message.content = "Unknown receiver user";

            return(Command.ERROR);
        }
Пример #7
0
        override protected int _execute(Communicate request)
        {
            using (var db = new UserContext())
            {
                // Create and save a new User:
                User user = request.user;
                if (user != null)
                {
                    var name = user.username;
                    var pass = user.password;
                    var a    = user.age;

                    var u = new User {
                        username = name, password = pass, age = a
                    };

                    // Count users with given username:
                    int count = __count_user_with_username(u.username);

                    // If there are no user with given username:
                    if (count == 0)
                    {
                        // Add new user:
                        db.Users.Add(u);
                        if (db.SaveChanges() > 0)
                        {
                            // Set appropriate header and message:
                            _response.header  = "registered";
                            _response.message = new Message {
                                content = "Correctly registered new user"
                            };
                            return(Command.OK);
                        }
                    }
                    // If there is such a user:
                    else
                    {
                        _response.header  = "error";
                        _response.message = new Message {
                            content = "User with given username already exists"
                        };
                        return(Command.ERROR);
                    }
                }
            }
            // Communicate error occurrance:
            _response.header  = "error";
            _response.message = new Message {
                content = "A proplem occurred while register new user"
            };
            return(Command.ERROR);
        }
        protected bool _is_user_authorized(Communicate c)
        {
            Session session = Session.getInstance();
            int     status  = session.Status(c.sessID);

            //if(status == Session.SESSION_ACTIVE)
            //{
            if (session.get(c.sessID, "user") == "1")
            {
                return(true);
            }
            //}
            return(false);
        }
Пример #9
0
        protected void _init(Communicate c)
        {
            Session     session = Session.getInstance();
            ClientsInfo clients = ClientsInfo.getInstance();

            if (session.Status(c.sessID) == Session.SESSION_NONE)
            {
                c.sessID = session.Start(c.sessID);
                clients.AddClient(c.sessID, _client);
            }
            else
            {
                c.sessID = session.Start(c.sessID);
            }
        }
Пример #10
0
        override protected int _execute(Communicate request)
        {
            // Ending session:
            Session session = Session.getInstance();

            session.Destroy(request.sessID);

            // Setting response:
            _response.header  = "closed";
            _response.message = new Message {
                content = "Correctly log out"
            };

            return(Command.OK);
        }
        public Command getCommand(Communicate communicate)
        {
            if (_is_user_authorized(communicate))
            {
                switch (communicate.header)
                {
                case "getuserlist":
                    return(new GetUserList());

                    break;

                case "sending":
                    return(new Sending());

                    break;

                case "close":
                    return(new Close());

                    break;
                }
            }
            else
            {
                switch (communicate.header)
                {
                case "authorize":
                    return(new Authorize());

                    break;

                case "register":
                    return(new Registration());

                    break;
                }
            }

            return(new DefaultCommand());
        }
Пример #12
0
        public Communicate handle(Communicate communicate)
        {
            // Perform initialization:
            _init(communicate);

            try
            {
                // Find appropriate command:
                Command command = _controller.getCommand(communicate);

                // Execute found command:
                command.execute(communicate);

                // Get response:
                Communicate response = command.Response;
                response.sessID = communicate.sessID;

                // Set sender and receiver:
                //response.sender = ...
                response.receiver = communicate.sender;

                return(response);
            }
            catch (Exception e) { }

            // Create error communicate (response):
            Communicate error = new Communicate(communicate);

            error.header = "error";
            Message m = new Message();

            m.content     = "Server has encountered a problem. Try to send request again.";
            error.message = new Message();

            return(error); // error communicate
        }
Пример #13
0
 protected abstract int _execute(Communicate request);
Пример #14
0
 public void execute(Communicate request)
 {
     _status = _execute(request);
 }
Пример #15
0
 public Command()
 {
     _response = new Communicate();
 }