1 /***********************************************************************/
2 /* Open Visualization Data Explorer                                    */
3 /* (C) Copyright IBM Corp. 1989,1999                                   */
4 /* ALL RIGHTS RESERVED                                                 */
5 /* This code licensed under the                                        */
6 /*    "IBM PUBLIC LICENSE - Open Visualization Data Explorer"          */
7 /***********************************************************************/
8 
9 #include <dxconfig.h>
10 
11 #include <time.h>
12 #include <dx/dx.h>
13 #include "config.h"
14 #include "_variable.h"
15 #include "background.h"
16 #include "graph.h"
17 #include "log.h"
18 #include "distp.h"
19 #include "rq.h"
20 
21 typedef struct varupdateArg
22 {
23     char *name;
24     Object obj;
25     int cause_execution;
26 } varupdateArg;
27 
28 typedef struct vargetArg
29 {
30     char *name;
31     Object obj;
32 } vargetArg;
33 
34 static Error ExVariableGet (vargetArg *va);
35 
36 /*
37  * These routines serve to insulate from the dictionary routines.  They
38  * are used to insert variables into the dictionary.
39  */
40 
41 int
_dxf_ExVariableInsert(char * name,EXDictionary dict,EXO_Object obj)42 _dxf_ExVariableInsert(char *name, EXDictionary dict, EXO_Object obj)
43 {
44     int		ret;
45 
46     if (!strcmp(name, "NULL"))
47     {
48 	DXWarning ("#4630");
49 	return (OK);
50     }
51     else
52     {
53 	ret = _dxf_ExDictionaryInsert (dict, name, obj);
54 
55 	if (dict == _dxd_exGlobalDict)
56 	    _dxf_ExBackgroundChange ();
57 
58 
59 	return (ret);
60     }
61 }
62 
63 int
_dxf_ExVariableInsertNoBackground(char * name,EXDictionary dict,EXO_Object obj)64 _dxf_ExVariableInsertNoBackground(char *name, EXDictionary dict, EXO_Object obj)
65 {
66     int		ret;
67 
68     if (!strcmp(name, "NULL"))
69     {
70 	DXWarning ("#4630");
71 	return (OK);
72     }
73     else
74     {
75 	ret = _dxf_ExDictionaryInsert (dict, name, obj);
76 	return (ret);
77     }
78 }
79 
80 int
_dxf_ExVariableDelete(char * name,EXDictionary dict)81 _dxf_ExVariableDelete(char *name, EXDictionary dict)
82 {
83     return (_dxf_ExDictionaryDelete (dict, name));
84 }
85 
86 EXObj
_dxf_ExVariableSearch(char * name,EXDictionary dict)87 _dxf_ExVariableSearch (char *name, EXDictionary dict)
88 {
89     return (_dxf_ExDictionarySearch (dict, name));
90 }
91 
92 
93 /* global variable routines
94  */
95 
96 void
_dxf_ExGVariableSetStr(char * var,char * val)97 _dxf_ExGVariableSetStr (char *var, char *val)
98 {
99     gvar	*gv;
100     String	sobj;
101     GDictSend   pkg;
102 
103     sobj = DXNewString (val);
104     gv = _dxf_ExCreateGvar (GV_DEFINED);
105     _dxf_ExDefineGvar (gv, (Object)sobj);
106     gv->cost = CACHE_PERMANENT;
107     _dxf_ExVariableInsertNoBackground (var, _dxd_exGlobalDict, (EXObj)gv);
108     _dxf_ExCreateGDictPkg (&pkg, var, (EXObj)gv);
109     _dxf_ExDistributeMsg (DM_INSERTGDICT, (Pointer)&pkg, 0, TOSLAVES);
110 }
111 
112 char *
_dxf_ExGVariableGetStr(char * var)113 _dxf_ExGVariableGetStr(char *var)
114 {
115     char	*val;
116 
117     vargetArg *va;
118 
119     va = (vargetArg *) DXAllocate(sizeof(vargetArg));
120     va->name = var;
121     va->obj = NULL;
122     _dxf_ExRunOn (1, ExVariableGet, (Pointer)va, 0);
123     if (DXExtractString ((Object)va->obj, &val) == NULL)
124         val = NULL;
125     DXFree(va);
126     return (val);
127 }
128 
129 /* create a new executive object (a gvar) for a normal dx object,
130  * and insert it into the global dictionary.  if flag is not set,
131  * insert it without causing a new graph execution.
132  */
133 Error
_dxf_ExUpdateGlobalDict(char * name,Object obj,int cause_execution)134 _dxf_ExUpdateGlobalDict (char *name, Object obj, int cause_execution)
135 {
136     Error       rc;
137     gvar        *gv;
138     GDictSend   pkg;
139     int 	to;
140 
141     ExDebug("*1","adding %s to global dictionary", name);
142 
143     gv = _dxf_ExCreateGvar (GV_UNRESOLVED);
144     _dxf_ExDefineGvar (gv, obj);
145 
146     if(_dxd_exRemoteSlave)
147        to = TOPEER0;
148     else
149        to = TOSLAVES;
150     if (cause_execution) {
151 	rc =  _dxf_ExVariableInsert(name, _dxd_exGlobalDict, (EXObj)gv);
152         _dxf_ExCreateGDictPkg(&pkg, name, &gv->object);
153         _dxf_ExDistributeMsg(DM_INSERTGDICT, (Pointer)&pkg, 0, to);
154     }
155     else {
156 	rc =  _dxf_ExVariableInsertNoBackground(name, _dxd_exGlobalDict,
157 						(EXObj)gv);
158         _dxf_ExCreateGDictPkg(&pkg, name, &gv->object);
159         _dxf_ExDistributeMsg(DM_INSERTGDICTNB, (Pointer)&pkg, 0, to);
160     }
161 
162     return rc;
163 }
164 
ExVariableSet(varupdateArg * va)165 Error ExVariableSet(varupdateArg *va)
166 {
167     int ret;
168 
169     if(va == NULL)
170         return(ERROR);
171 
172     ret = _dxf_ExUpdateGlobalDict(va->name, va->obj, va->cause_execution);
173     DXFree(va);
174     return(ret);
175 }
176 
DXSetIntVariable(char * name,int val,int sync,int cause_execution)177 Error DXSetIntVariable(char *name, int val, int sync, int cause_execution)
178 {
179     Array       arr;
180 
181     ExDebug("*1","Setting IntVar %s to %d", name, val);
182 
183     if (!(arr = DXNewArray (TYPE_INT, CATEGORY_REAL, 0)))
184         return ERROR;
185 
186     if (!DXAddArrayData (arr, 0, 1, (Pointer) &val))
187         return ERROR;
188 
189     return(DXSetVariable(name, (Object)arr, sync, cause_execution));
190 }
191 
DXSetVariable(char * name,Object obj,int sync,int cause_execution)192 Error DXSetVariable(char *name, Object obj, int sync, int cause_execution)
193 {
194     varupdateArg *va;
195     int ret;
196 
197     ExDebug("*1","Setting Var %s to obj %x", name, obj);
198     if (DXProcessorId() == 0) {
199         _dxf_ExUpdateGlobalDict(name, obj, cause_execution);
200         return(OK);
201     }
202 
203     va = (varupdateArg *) DXAllocate(sizeof(varupdateArg));
204     va->name = name;
205     va->obj = obj;
206     va->cause_execution = cause_execution;
207     if(sync) {
208         ret = _dxf_ExRunOn (1, ExVariableSet, va, 0);
209         return(ret);
210     }
211     else {
212         _dxf_ExRQEnqueue(ExVariableSet, (Pointer)va, 1, 0, 1, 0);
213         return(OK);
214     }
215 
216 }
217 
ExVariableGet(vargetArg * va)218 static Error ExVariableGet (vargetArg *va)
219 {
220     gvar        *gv;
221 
222     if ((gv = (gvar *) _dxf_ExGlobalVariableSearch (va->name)) == NULL)
223         return (ERROR);
224 
225     va->obj = gv->obj;
226 
227     ExDelete (gv);
228 
229     return (OK);
230 }
231 
DXGetVariable(char * name,Object * obj)232 Error DXGetVariable(char *name, Object *obj)
233 {
234     vargetArg *va;
235     int ret;
236 
237     va = (vargetArg *) DXAllocate(sizeof(vargetArg));
238     va->name = name;
239     ret = _dxf_ExRunOn (1, ExVariableGet, va, 0);
240 
241     if (ret == OK)
242 	*obj = va->obj;
243     else
244 	*obj = NULL;
245 
246     DXFree(va);
247     return (ret);
248 }
249 
250 
DXGetIntVariable(char * name,int * val)251 Error DXGetIntVariable(char *name, int *val)
252 {
253     Object obj;
254 
255     if (!DXGetVariable(name, &obj))
256 	return ERROR;
257 
258     DXExtractInteger (obj, val);
259 
260     return OK;
261 }
262 
263 
264 
265 
266 /*
267  * find a variable by name in the global dictionary
268  */
_dxf_ExGlobalVariableSearch(char * name)269 EXObj _dxf_ExGlobalVariableSearch (char *name)
270 {
271     return _dxf_ExDictionarySearch (_dxd_exGlobalDict, name);
272 }
273 
274 
275