1 /*
2     This file is part of GNU APL, a free implementation of the
3     ISO/IEC Standard 13751, "Programming Language APL, Extended"
4 
5     Copyright (C) 2008-2015  Dr. Jürgen Sauermann
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __PROCESSOR_ID_HH_DEFINED__
22 #define __PROCESSOR_ID_HH_DEFINED__
23 
24 #include <vector>
25 
26 using namespace std;
27 
28 #ifdef AP_NUM
29 
30 /// a simple ProcessorID to be used by APs
31 class ProcessorID
32 {
33 public:
34    /// return the current ID (proc, parent, and grandparent)
get_id()35    static const AP_num3 & get_id()        { return id; }
36 
set_id(AP_num3 ap3)37    static void set_id(AP_num3 ap3)       { id = ap3; }
38 
clear_id()39    static void clear_id()
40       { id.proc = id.parent = id.grand = AP_NULL; }
41 
42    /// set the current ID
set_own_ID(AP_num ap)43    static void set_own_ID(AP_num ap)      { id.proc = ap; }
44 
45    /// return the parent's ID
get_parent_ID()46    static AP_num get_parent_ID()          { return id.parent; }
47 
48    /// set the parent's ID
set_parent_ID(AP_num ap)49    static void set_parent_ID(AP_num ap)   { id.parent = ap; }
50 
51    /// return the grandparent's ID
get_grand_ID()52    static AP_num get_grand_ID()           { return id.grand; }
53 
54    /// set the grandparent's ID
set_grand_ID(AP_num ap)55    static void set_grand_ID(AP_num ap)    { id.grand = ap; }
56 
57 protected:
58    /// the current ID (proc, parent, and grandparent)
59    static    AP_num3 id;
60 };
61 
62 #else
63 
64 // the normal ProcessorID to be used by the APL interpreter
65 
66 /// a mapping between the left argument of ⎕SVO or ⎕SVQ and AP numbers
67 struct SvoPid
68 {
SvoPidSvoPid69    SvoPid() : id(NO_AP, AP_NULL, AP_NULL) {}
70 
71    int svopid;           ///< left argument of ⎕SVO and ⎕SVQ
72    int ip_addr;          ///< remote IP address
73    char user[32];        ///< user account (login name)
74    AP_num3 id;           ///< processor, parent and grandparent
75 };
76 
77 /// A processor authentication
78 struct ProcAuth
79 {
80    /// constructor: no ID
ProcAuthProcAuth81    ProcAuth() : id(NO_AP, AP_NULL, AP_NULL) {}
82 
83    /// the ID
84    AP_num3 id;
85 
86    /// the allowed remote processors
87    std::vector<int> rsvopid;   ///< left argument(s) of remote ⎕SVO and ⎕SVQ
88 };
89 
90 /// A network profile
91 struct Network_Profile
92 {
93    /// left argument of ⎕SVO and ⎕SVQ
94    std::vector<SvoPid> svo_pids;
95 
96    /// processor authentications
97    std::vector<ProcAuth> proc_auths;
98 
99    /// clear everything
clearNetwork_Profile100    void clear()
101       {
102         svo_pids.clear();
103         proc_auths.clear();
104       }
105 };
106 
107 /** One APL processor. APL interpreters have numbers > 1000 while auxiliary
108     processors have numbers < 1000
109  */
110 /// One APL processor
111 class ProcessorID
112 {
113 public:
114    /// initialize our own process ID, return non-0 on error.
115    /// proc_id == 0 uses the next free ID > 1000; otherwise proc_id is used.
116    /// \b do_sv defines if an APnnn process for incoming ⎕SVO offers
117    /// shall be forked.
118    static bool init(bool log_startup);
119 
120    /// return the own id, parent, and grand-parent
get_id()121    static const AP_num3 & get_id()        { return id; }
122 
123    /// return the processor ID of this apl interpreter
get_own_ID()124    static AP_num get_own_ID()   { return id.proc; }
125 
126    /// return the processor ID of the parent of this apl interpreter
get_parent_ID()127    static AP_num get_parent_ID()   { return id.parent; }
128 
129    /// read the network profile file from its default location
130    static int read_network_profile();
131 
132    /// disconnect from APnnn process
133    static void disconnect();
134 
135 protected:
136    /// read the network profile file from file \b file
137    static int read_network_profile(const char * filename);
138 
139    /// read one SvoPid entry from \b file
140    static const char * read_svopid(FILE * file, SvoPid & svopid, int & line);
141 
142    /// read one ProcAuth entry from \b file
143    static const char * read_procauth(FILE * file, ProcAuth & procauth,
144                                      int & line);
145 
146    /// the processor, parent, and grandparent of this apl interpreter
147    static AP_num3 id;
148 
149    /// the network profile currently used
150    static Network_Profile network_profile;
151 };
152 
153 #endif
154 
155 #endif // __PROCESSOR_ID_HH_DEFINED__
156