1 #ifndef PSGS_DISPATCHER__HPP
2 #define PSGS_DISPATCHER__HPP
3 
4 /*  $Id: psgs_dispatcher.hpp 629837 2021-04-22 12:47:49Z ivanov $
5  * ===========================================================================
6  *
7  *                            PUBLIC DOMAIN NOTICE
8  *               National Center for Biotechnology Information
9  *
10  *  This software/database is a "United States Government Work" under the
11  *  terms of the United States Copyright Act.  It was written as part of
12  *  the author's official duties as a United States Government employee and
13  *  thus cannot be copyrighted.  This software/database is freely available
14  *  to the public for use. The National Library of Medicine and the U.S.
15  *  Government have not placed any restriction on its use or reproduction.
16  *
17  *  Although all reasonable efforts have been taken to ensure the accuracy
18  *  and reliability of the software and data, the NLM and the U.S.
19  *  Government do not and cannot warrant the performance or results that
20  *  may be obtained by using this software or data. The NLM and the U.S.
21  *  Government disclaim all warranties, express or implied, including
22  *  warranties of performance, merchantability or fitness for any particular
23  *  purpose.
24  *
25  *  Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Sergey Satskiy
30  *
31  * File Description: PSG server request dispatcher
32  *
33  */
34 
35 #include <list>
36 #include <mutex>
37 #include "ipsgs_processor.hpp"
38 
39 
40 
41 /// Based on various attributes of the request: {{seq_id}}; NA name;
42 /// {{blob_id}}; etc (or a combination thereof)...
43 /// provide a list of Processors to retrieve the requested data
44 class CPSGS_Dispatcher
45 {
46 public:
CPSGS_Dispatcher()47     CPSGS_Dispatcher()
48     {}
49 
50     /// Register processor (one to serve as a processor factory)
51     void AddProcessor(unique_ptr<IPSGS_Processor> processor);
52 
53     /// Return list of processors which can be used to process the request.
54     /// The caller accepts the ownership.
55     list<IPSGS_Processor *> DispatchRequest(shared_ptr<CPSGS_Request> request,
56                                             shared_ptr<CPSGS_Reply> reply);
57 
58     /// The processor signals that it is going to provide data to the client
59     IPSGS_Processor::EPSGS_StartProcessing
60         SignalStartProcessing(IPSGS_Processor *  processor);
61 
62     /// The processor signals that it finished one way or another; including
63     /// when a processor is cancelled.
64     void SignalFinishProcessing(IPSGS_Processor *  processor);
65 
66     /// An http connection can be cancelled so this method will be invoked for
67     /// such a case
68     void SignalConnectionCancelled(IPSGS_Processor *  processor);
69 
70 private:
71     void x_PrintRequestStop(shared_ptr<CPSGS_Request> request,
72                             CRequestStatus::ECode  status);
73 
74 private:
75     // Registered processors
76     list<unique_ptr<IPSGS_Processor>>   m_RegisteredProcessors;
77 
78 private:
79     // From the dispatcher point of view each request corresponds to a group of
80     // processors and each processor can be in one of the state:
81     // - created and doing what it needs (Up)
82     // - the Cancel() was called for a processor (Cancelled)
83     // - a processor reported that it finished (Finished)
84     enum EPSGS_ProcessorStatus {
85         ePSGS_Up,
86         ePSGS_Cancelled,
87         ePSGS_Finished
88     };
89 
90     // Auxiliary structure to store a processor properties
91     struct SProcessorData
92     {
93         // It does not owe the processor.
94         // CPendingOperation owns it.
95         IPSGS_Processor *               m_Processor;
96         EPSGS_ProcessorStatus           m_DispatchStatus;
97         IPSGS_Processor::EPSGS_Status   m_FinishStatus;
98 
SProcessorDataCPSGS_Dispatcher::SProcessorData99         SProcessorData(IPSGS_Processor *  processor,
100                        EPSGS_ProcessorStatus  dispatch_status,
101                        IPSGS_Processor::EPSGS_Status  finish_status) :
102             m_Processor(processor), m_DispatchStatus(dispatch_status),
103             m_FinishStatus(finish_status)
104         {}
105     };
106 
107     // The dispatcher owns the created processors. The map below makes a
108     // correspondance between the request id (size_t; generated in the request
109     // constructor) and a list of processors with their properties.
110     map<size_t,
111         list<SProcessorData>>       m_ProcessorGroups;
112     mutex                           m_GroupsLock;
113 };
114 
115 
116 #endif  // PSGS_DISPATCHER__HPP
117 
118