예제 #1
0
        public void OnReceivedNextRequest(Datagram requestData)
        {
            switch (requestData.DataType)
            {
            case DataType.Message:
            case DataType.ChangeBike:
            case DataType.EmergencyBreak:
                string id = requestData.Data.RecipientId;
                if (id == "All")                                         // NEEDS TO BE TESTED // HIER KAN IETS FOUT GAAN
                {
                    foreach (PatientTask ct in clientsFromDoctor)
                    {
                        GetClient(ct.UserData.Id).SendToClient(requestData);
                    }
                }
                else
                {
                    GetClient(id).SendToClient(requestData);
                }                                                    //HIER EINDIGT HET
                break;

            case DataType.AddClient:
                Console.WriteLine(requestData.Data);
                {
                    var clientData = requestData.Data;
                    WriteNewClientData(clientData);
                    Datagram response = new Datagram();
                    response.DataType = DataType.NewClientData;
                    response.Data     = new JsonNewClientData {
                        ClientData = serverData.Clients.Last()
                    };
                    SendToDoctor(response);
                }

                break;

            case DataType.StartSession:
            {
                string      clientId = requestData.Data.Id;
                PatientTask task     = GetClient(clientId);
                if (clientsFromDoctor.Contains(task))
                {
                    Datagram response = new Datagram();
                    response.DataType = DataType.StartSession;
                    response.Data     = new JsonResponse {
                        Error = "200", Message = "SessionStart"
                    };
                    task.SendToClient(response);
                    SendToDoctor(response);
                }
            }
            break;

            case DataType.EndSession:
            {
                string      clientId = requestData.Data.Id;
                PatientTask task     = GetClient(clientId);
                Datagram    response = new Datagram();
                response.DataType = DataType.EndSession;
                response.Data     = new JsonResponse {
                    Error = "200", Message = "SessionEnd"
                };
                task.SendToClient(response);
                SendToDoctor(response);
            }
            break;

            case DataType.RequestAllClientData:

                SendToDoctor(new Datagram()
                {
                    DataType = DataType.AllClientData,
                    Data     = new JsonAllClientData()
                    {
                        ClientDatas = serverData.Clients
                    }
                });
                break;

            case DataType.RequestNewClientSnapshots:
                JsonRequestNewClientData d = TcpHelper.ToConcreteType <JsonRequestNewClientData>(requestData.Data);

                var snapshots = GetAllNewSnapshots(d.CurrentClientDatas);

                SendToDoctor(new Datagram()
                {
                    DataType = DataType.NewClientSnapshots,
                    Data     = new JsonNewClientSnapshot()
                    {
                        NewSnapShots = snapshots
                    }
                });

                break;

            case DataType.LogOut:
                foreach (PatientTask ct in clientsFromDoctor)
                {
                    ct.SendToClient(new Datagram()
                    {
                        DataType = DataType.LogOut,
                        Data     = new JsonLogOut()
                        {
                        }
                    });
                }
                currentTasks.RemoveAll((x) => x.Client == client);
                foreach (PatientTask ct in clientsFromDoctor)
                {
                    ct.Client.Close();
                }
                client.Close();
                break;
            }
            OnHandleNextRequest();
        }
예제 #2
0
        public void OnAuthorizeConnection(Datagram received)
        {
            TcpTask currentTask = null;

            if (received.DataType == DataType.Login)
            {
                var loginRequest = TcpHelper.ToConcreteType <JsonLogin>(received.Data);
                if (loginRequest.IsDoctorProgram)
                {
                    DoctorData currentUser = FindDoctorWithPassword(loginRequest.Id, loginRequest.Password);
                    if (currentUser != null)
                    {
                        DoctorTask doctorTask = new DoctorTask(client, serverData, currentUser, currentTasks);
                        currentTask = doctorTask;
                        lock (OnlineListSyncRoot)
                        {
                            currentTasks.Add(doctorTask);
                        }
                    }
                }
                else
                {
                    //moet voor client lists in server data want we voegen data toe maar lezen ook.
                    serverData.ClientListLock.EnterReadLock();
                    PatientData currentUser = FindClientWithPassword(loginRequest.Id, loginRequest.Password);
                    serverData.ClientListLock.ExitReadLock();
                    if (currentUser != null)
                    {
                        // ZORGT VOOR DEADLOCK!!!
                        PatientTask clientTask = new PatientTask(client, serverData, currentUser, currentTasks);
                        currentTask = clientTask;
                        lock (OnlineListSyncRoot)
                        {
                            currentTasks.Add(clientTask);
                        }
                    }
                }
            }

            //TcpHelper writerHelper = new TcpHelper(client);
            //dynamic response = new ExpandoObject();
            //response.DataType = DataType.Login;
            //response.Data = new ExpandoObject();

            TcpHelper writerHelper = new TcpHelper(client);
            var       response     = new Datagram();

            response.DataType = DataType.Login;
            if (currentTask != null)
            {
                response.Data = new JsonResponse
                {
                    Error   = "200",
                    Message = "LoginOK"
                };
                writerHelper.WriteJsonData(response);
                currentTask.Run();
            }
            else
            {
                response.Data = new JsonResponse
                {
                    Error   = "500",
                    Message = "LoginWrong"
                };
                writerHelper.WriteJsonData(response);
                //lets try that again
                TcpHelper helper = new TcpHelper(client);
                helper.ReadJsonData(OnAuthorizeConnection);
            }
        }