예제 #1
0
        public static Packet RetrievePacket(int packetSize, ref List<byte> buffer)
        {  
            Packet p = null;
            JObject json = RetrieveJson(packetSize, ref buffer);

            if (json == null)
                return null;

            //Console.WriteLine("Got " + json["CMD"].ToString().ToUpper() + " packet.");

            switch (json["CMD"].ToString().ToUpper())
            {
                case LoginResponsePacket.LoginRcmd:
                    p = new LoginResponsePacket(json);
                    break;
                case ChatPacket.DefCmd:
                    p = new ChatPacket(json);
                    break;
                case SubscribePacket.DefCmd:
                    p = SubscribePacket.GetSubscribePacket(json);
                    break;
                case DataFromClientPacket<Object>.DefCmd:
                    //only measurements are supported.
                    p = new DataFromClientPacket<Measurement>(json);
                    break;
                case PullResponsePacket<Object>.DefCmd:
                    p = HandlePullResponsePacket(json);
                    break;
                case PushPacket<Object>.DefCmd:
                    p = HandlePushPacket(json);
                    break;
                case NotifyPacket.Cmd:
                    p = new NotifyPacket(json);
                    break;
                default:
                    Console.WriteLine("Unsupported packet type: {0}", json["CMD"].ToString());
                    break;
            }
                                

            return p;
        }
예제 #2
0
        public void HandleSubscribePacket(JObject json)
        {
            var notifier = Notifier.Instance;
            var packet = SubscribePacket.GetSubscribePacket(json);

            if (packet == null)
            {
                Send(new ResponsePacket(Statuscode.Status.SyntaxError, "resp-subscr"));
                return;
            }

            var specialist = Authentication.GetUserByAuthToken(packet.AuthToken) as Specialist;
            var client = Authentication.GetUser(packet.Client) as Client;

            var success = packet.Subscribe ? notifier.Subscribe(specialist, client) : notifier.Unsubscribe(specialist, client);

            
            if (packet.Subscribe)
            {
                ChatPacket serverToClientMessage = new ChatPacket(String.Format("{0} has connected to your training and is monitoring your progress.", specialist.Fullname),"","");
                ChatPacket serverToSpecMessage = new ChatPacket(String.Format("You have sucessfully connected to {0}", client.Fullname), "", "");    
                Authentication.GetStream(client).Send(serverToClientMessage);
                Authentication.GetStream(specialist).Send(serverToSpecMessage);
            }
            else
            {
                ChatPacket serverToClientMessage = new ChatPacket(String.Format("{0} has deemed you unworthy of his time and has disconnected.", specialist.Fullname), "", "");
                ChatPacket serverToSpecMessage = new ChatPacket(String.Format("You have sucessfully abandoned {0}'s training.", client.Username), "", "");
                Authentication.GetStream(client).Send(serverToClientMessage);
                Authentication.GetStream(specialist).Send(serverToSpecMessage);
            }

            Send(new ResponsePacket(Statuscode.Status.Ok, "resp-subscr"));

        }
예제 #3
0
        private void _sendButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(_textBox.Text))
                return;
            else
            {

                String message = _textBox.Text;

                AddNewMessage(Settings.GetInstance().CurrentUser.Fullname, message);

                string destination;
                if (isSpecialist)
                    destination = client.Username;
                else
                    destination = "";

                JObject json = new ChatPacket(message, destination, Settings.GetInstance().authToken, broadcastCheckbox.Checked).ToJsonObject();
                TCPController.Send(json.ToString());
                _textBox.Text = "";
            }
        }
예제 #4
0
        public void HandleChatPacket(JObject json)
        {
            var p = new ChatPacket(json);
            //var destinationClientHandler = Authentication.GetStream(p.UsernameDestination);


            var newPacket = new ChatPacket(p.Message, currentUser.Username, "SERVER");
            if (currentUser.IsSpecialist || currentUser.IsAdministrator)
            {
                if (p.IsBroadcast)
                {
                    var broadcast = new ChatPacket(p.Message, p.UsernameDestination, "", true);
                    Authentication.GetAllUsers().ForEach(x => x.Send(broadcast));
                }
                else
                {

                    var spec = currentUser as Specialist;
                    var clients = Notifier.Instance.GetListeners(spec);
                    //get all other specialist subscribed to this client, excluding the sending specialist
                    var specialists =
                        Notifier.Instance.GetListeners((Client) _database.getUser(p.UsernameDestination))
                            .Where(x => x.Username != currentUser.Username);

                    var allReceivers = clients.Concat<User>(specialists).Distinct(new User.UserComparer());
                    allReceivers.ToList().ForEach(x => Authentication.GetStream(x).Send(newPacket));
                }


            }
            else
            {
                var clie = currentUser as Client;
                var specialists = Notifier.Instance.GetListeners(clie).ToList();

                specialists.ForEach(x => Authentication.GetStream(x.Username).Send(newPacket));
            }

        }