Exemplo n.º 1
0
        private static void ConnectCallback(IAsyncResult ar)
        {
            // Signal the main thread to continue.
            _connectSignal.Set();
            if (_stopSignal)
            {
                return;
            }

            TcpListener listener = (TcpListener)ar.AsyncState;
            TcpClient   client   = listener.EndAcceptTcpClient(ar);
            //client.SendTimeout = 5000;
            //client.ReceiveTimeout = 5000;
            string remoteIP = client.Client.RemoteEndPoint.ToString().Split(':')[0];

            try
            {
                Utils.Log("Get report submitted from " + remoteIP);

                using (var socketStream = client.GetStream())
                {
                    ReportInfo report = ReportSendReceiver.ReceiveReport(socketStream);
                    report.CallingIP = remoteIP;

                    HandleReport(report, socketStream);
                }

                client.Close();
            }
            catch (Exception ex)
            {
                Utils.Log("Exception when handle connection from {0}, error: {1}", remoteIP, ex.Message);
                Utils.Log(ex.StackTrace);
            }
        }
Exemplo n.º 2
0
        private static void ThreadFunc(object ctx)
        {
            while (true)
            {
                try
                {
                    if (_stopSignal)
                    {
                        break;
                    }

                    //1. get new pdf file
                    string pdfFile = QueryPdfFile();
                    if (string.IsNullOrEmpty(pdfFile))
                    {
                        Thread.Sleep(2000);
                        continue;
                    }

                    //2. start handle report
                    ReportInfo report = new ReportInfo();
                    report.PdfReport = File.ReadAllBytes(pdfFile);
                    report.Status    = ReportStatus.SubmitInitial;

                    Utils.Log("Start to handle new report, id:{0}, path:{1}", report.Id, pdfFile);

                    bool reportDone = false;
                    do
                    {
                        TcpClient client = new TcpClient();
                        //client.SendTimeout = 5000;//5s
                        //client.ReceiveTimeout = 5000;//5s
                        client.Connect(IPAddress.Parse(ServerIP), ServerPort);//it will throw an exception if failed to connect.

                        using (NetworkStream socketStream = client.GetStream())
                        {
                            Utils.Log("Send report with status: " + report.Status);
                            ReportSendReceiver.SendReport(report, socketStream);

                            report = ReportSendReceiver.ReceiveReport(socketStream);
                            Utils.Log("Receive report with status: " + report.Status);

                            if (report.IsServerDone() || report.HasError())
                            {
                                reportDone = true;
                            }
                            else
                            {
                                _reportConfirmedSignal.Reset();
                                _reportToConfirm = report;

                                if (ReportSendEvent != null)
                                {
                                    ReportSendEvent(new ReportSendEventArg()
                                    {
                                        HasError = false, Report = report, NeedConfirm = report.NeedConfirm()
                                    });
                                }

                                _reportConfirmedSignal.WaitOne();
                                _reportToConfirm = null;
                            }
                        }

                        client.Close();
                    }while (!reportDone);

                    //4. clear work
                    if (ReportSendEvent != null)
                    {
                        ReportSendEvent(new ReportSendEventArg()
                        {
                            HasError = report.HasError(), Report = report, NeedConfirm = report.NeedConfirm()
                        });
                    }

                    Utils.Log("Report done with status: " + report.Status);

                    if (report.HasError())
                    {
                        //move to error report folder
                        Utils.Log("Report has error, will send to errorbox. error: " + report.ErrorMessage);

                        string targetFile = Path.Combine(PdfReportFolder, "Errorbox", Path.GetFileName(pdfFile));
                        File.Move(pdfFile, targetFile);
                    }
                    else
                    {
                        File.Delete(pdfFile);
                    }
                }
                catch (Exception ex)
                {
                    if (ReportSendEvent != null)
                    {
                        ReportSendEvent(new ReportSendEventArg()
                        {
                            HasError = true, ErrorMessage = ex.Message
                        });
                    }

                    Utils.Log("Exception happen from Report Client main thread, message: " + ex.Message);
                    Utils.Log(ex.StackTrace);

                    Thread.Sleep(1000);
                }
            }

            _isRunning = false;
            Utils.Log("Report Client stopped");
        }
Exemplo n.º 3
0
        private static void HandleReport(ReportInfo report, NetworkStream socket)
        {
            Utils.Log("Start handle report with status:{0}, id:{1}, thread:{2}", report.Status, report.Id, Thread.CurrentThread.ManagedThreadId);

            if (report.Status == ReportStatus.SubmitInitial)
            {
                //save report
                SaveReport(report);

                string patientId = ParsePatientId(report);
                report.PatientId = patientId;
                Utils.Log("Try parse PatientId, and patientId is: " + patientId);

                if (string.IsNullOrEmpty(patientId))
                {
                    Utils.Log("Failed parse patientId, send to client to confirm");

                    report.Status = ReportStatus.FailedGetPatientId;
                    ReportSendReceiver.SendReport(report, socket);

                    return;
                }

                //check exist report
                byte[] existReport = GetExistReport(patientId);
                if (existReport != null)
                {
                    Utils.Log("Patient already has report, send client to confirm");

                    report.PdfReportExist = existReport;
                    report.Status         = ReportStatus.ConfirmExistReport;

                    ReportSendReceiver.SendReport(report, socket);
                    return;
                }

                //all is OK, let client confirm whether the patientId is correct.
                Utils.Log("All is OK, send client to confirm");
                report.Status = ReportStatus.ConfirmPatientId;
                ReportSendReceiver.SendReport(report, socket);
            }
            else if (report.Status == ReportStatus.SubmitNewPatientId)
            {
                Utils.Log("Client changed patientId, the new patienId is: " + report.PatientId);

                byte[] existReport = GetExistReport(report.PatientId);
                if (existReport != null)
                {
                    Utils.Log("Patient already has report, send client to confirm");

                    report.PdfReportExist = existReport;
                    report.Status         = ReportStatus.ConfirmExistReport;

                    ReportSendReceiver.SendReport(report, socket);
                    return;
                }

                Utils.Log("Client confirmed OK, report handle finish!");
                report.Status = ReportStatus.ConfirmOK;

                if (ReportReceiveEvent != null)
                {
                    ReportReceiveEventArg arg = new ReportReceiveEventArg();
                    arg.IsOverwriteExist = report.ExistReportAction == ExistReportAction.Overwrite;
                    arg.PatientId        = report.PatientId;
                    arg.CallingIP        = report.CallingIP;
                    arg.ReportPath       = report.GetReportPath();

                    ReportReceiveEvent(arg);
                }

                ReportSendReceiver.SendReport(report, socket);
            }
            else if (report.Status == ReportStatus.SubmitOK)
            {
                Utils.Log("Client confirmed OK, report handle finish!");
                report.Status = ReportStatus.ConfirmOK;

                if (ReportReceiveEvent != null)
                {
                    ReportReceiveEventArg arg = new ReportReceiveEventArg();
                    arg.IsOverwriteExist = report.ExistReportAction == ExistReportAction.Overwrite;
                    arg.PatientId        = report.PatientId;
                    arg.CallingIP        = report.CallingIP;
                    arg.ReportPath       = report.GetReportPath();

                    ReportReceiveEvent(arg);
                }

                ReportSendReceiver.SendReport(report, socket);
            }
        }