Line data Source code
1 : /*
2 : START OF LICENSE STUB
3 : DeDOS: Declarative Dispersion-Oriented Software
4 : Copyright (C) 2017 University of Pennsylvania, Georgetown University
5 :
6 : This program is free software: you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation, either version 3 of the License, or
9 : (at your option) any later version.
10 :
11 : This program is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with this program. If not, see <http://www.gnu.org/licenses/>.
18 : END OF LICENSE STUB
19 : */
20 : /**
21 : * @file: runtime_dfg.c
22 : * Interactions with the global dfg from an individual runtime's perspective
23 : */
24 : #include "runtime_dfg.h"
25 : #include "dfg_reader.h"
26 : #include "logging.h"
27 : #include "dfg_instantiation.h"
28 : #include "msu_type.h"
29 :
30 : #include <stdlib.h>
31 :
32 : /** Static (global) variable for accessing a lodaed Dfg */
33 : static struct dedos_dfg *DFG = NULL;
34 : /** Static (global) variable holding this runtime's dfg */
35 : static struct dfg_runtime *LOCAL_RUNTIME = NULL;
36 :
37 0 : void set_local_runtime(struct dfg_runtime *rt) {
38 0 : LOCAL_RUNTIME = rt;
39 0 : }
40 :
41 0 : int init_runtime_dfg(char *filename, int runtime_id) {
42 :
43 0 : if (DFG != NULL) {
44 0 : log_error("Runtime DFG already instantiated!");
45 0 : return -1;
46 : }
47 :
48 0 : DFG = parse_dfg_json_file(filename);
49 0 : if (DFG == NULL) {
50 0 : log_error("Error initializing DFG");
51 0 : return -1;
52 : }
53 0 : set_dfg(DFG);
54 :
55 0 : LOCAL_RUNTIME = get_dfg_runtime(runtime_id);
56 0 : if (LOCAL_RUNTIME == NULL) {
57 0 : log_error("Error finding runtime %d in DFG %s",
58 : runtime_id, filename);
59 0 : return -1;
60 : }
61 :
62 0 : if (init_dfg_msu_types(DFG->msu_types, DFG->n_msu_types) != 0) {
63 0 : log_error("Error instantiating MSU types found in %s",
64 : filename);
65 0 : return -1;
66 : }
67 :
68 0 : if (instantiate_dfg_runtime(LOCAL_RUNTIME) != 0) {
69 0 : log_error("Error instantiating runtime %d from %s",
70 : runtime_id, filename);
71 0 : return -1;
72 : }
73 :
74 0 : return 0;
75 : }
76 :
77 0 : int controller_address(struct sockaddr_in *addr) {
78 0 : if (DFG == NULL) {
79 0 : log_error("Runtime DFG not instantiated");
80 0 : return -1;
81 : }
82 :
83 0 : bzero(addr, sizeof(*addr));
84 0 : addr->sin_family = AF_INET;
85 0 : addr->sin_addr.s_addr = DFG->global_ctl_ip;
86 0 : addr->sin_port = htons(DFG->global_ctl_port);
87 :
88 0 : return 0;
89 : }
90 :
91 4 : int local_runtime_id() {
92 4 : if (LOCAL_RUNTIME == NULL) {
93 0 : log_error("Runtime DFG not instantiated");
94 0 : return -1;
95 : }
96 4 : return LOCAL_RUNTIME->id;
97 : }
98 :
99 0 : int local_runtime_port() {
100 0 : if (LOCAL_RUNTIME == NULL) {
101 0 : log_error("Runtime DFG not instantiated");
102 0 : return -1;
103 : }
104 0 : return LOCAL_RUNTIME->port;
105 : }
106 :
107 1 : uint32_t local_runtime_ip() {
108 1 : if (LOCAL_RUNTIME == NULL) {
109 0 : log_error("Runtime DFG not implemented");
110 0 : return -1;
111 : }
112 1 : return LOCAL_RUNTIME->ip;
113 : }
114 :
115 0 : struct dedos_dfg *get_dfg() {
116 0 : return DFG;
117 : }
118 :
119 0 : void free_runtime_dfg() {
120 0 : log_info("Freeing runtime DFG");
121 0 : free_dfg(DFG);
122 : //TODO: This shouldn't be in this function
123 0 : destroy_msu_types();
124 0 : }
|