-
Notifications
You must be signed in to change notification settings - Fork 6
/
getHistory.c
176 lines (155 loc) · 5.69 KB
/
getHistory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*************************************************************************\
* Copyright (c) 1994-2004 The University of Chicago, as Operator of Argonne
* National Laboratory.
* Copyright (c) 1997-2003 Southeastern Universities Research Association,
* as Operator of Thomas Jefferson National Accelerator Facility.
* Copyright (c) 1997-2002 Deutches Elektronen-Synchrotron in der Helmholtz-
* Gemelnschaft (DESY).
* This file is distributed subject to a Software License Agreement found
* in the file LICENSE that is included with this distribution.
\*************************************************************************/
#include "StripHistory.h"
#include "StripDataSource.h"
#ifdef USE_AAPI
#include "AAPI_client.h"
#include "get_AAPI_data.h"
#endif
#ifdef USE_ARCHIVE_RECORD
#include "getArchiveRecord.h"
#endif
#include "getHistory.h"
#define DEBUG 0
#define DEBUG1 0
#define DEBUG2 0
unsigned long getHistory(StripHistory the_shi,
char* name,
struct timeval* begin,
struct timeval* end,
struct timeval** times,
short** status,
double** data,
unsigned long * count)
{
struct timeval right_endpoint; /* right end for AAPI request */
unsigned long commonCount=0;
int i;
double *returnedDataIOC =NULL;
struct timeval *returnedTimeIOC=NULL;
short *returnedStatusIOC=NULL;
long returnedCountIOC=0;
double *returnedDataAAPI=NULL;
struct timeval *returnedTimeAAPI=NULL;
short *returnedStatusAAPI=NULL;
long returnedCountAAPI=0;
*count=0;
right_endpoint.tv_sec = end->tv_sec;
right_endpoint.tv_usec = end->tv_usec;
#ifdef USE_ARCHIVE_RECORD
if(getArchiveRecord(name,begin,end,
REQUEST_MODE_CONTINUE ,&returnedTimeIOC,
&returnedDataIOC, &returnedStatusIOC,
&returnedCountIOC,&needMoreData) != 0)
{
if(DEBUG1) fprintf(stderr,"%s:getArchiveRecord Error\n",name);
returnedCountIOC=0;/*???? return (-1); */
}
if(returnedCountIOC>0)
{
if(compare_times(&returnedTimeIOC[0],&right_endpoint) <=0)
{
right_endpoint.tv_sec = returnedTimeIOC[0].tv_sec;
right_endpoint.tv_usec= returnedTimeIOC[0].tv_usec;
commonCount=returnedCountIOC;
}
}
if(DEBUG) {
printf("name=%s;returnedCountIOC=%ld\n",name,returnedCountIOC);
printf("IOC FROM=%s",ctime(&(begin->tv_sec)));
printf("IOC TO =%s",ctime(&( end->tv_sec)));
if(DEBUG2) for(i=0;i<returnedCountIOC;i++)
printf("IOC=%s",ctime(&((returnedTimeIOC)[i].tv_sec)));
}
#endif
#if defined(USE_AAPI) || defined(USE_CAR)
if((needMoreData)&&(begin->tv_sec < right_endpoint.tv_sec))
{
if(DEBUG1) printf("history req is here\n");
#ifdef USE_AAPI
if(get_AAPI_data(name,begin ,&right_endpoint,
&returnedTimeAAPI,
&returnedStatusAAPI, &returnedDataAAPI,
&returnedCountAAPI) != 0)
#endif
#ifdef USE_CAR
if(get_CAR_data(the_shi,name,begin ,&right_endpoint,
&returnedTimeAAPI,
&returnedStatusAAPI, &returnedDataAAPI,
&returnedCountAAPI) != 0)
#endif
{
if(DEBUG) fprintf(stderr,"%s:getArchiveAPI Error\n",name);
returnedCountAAPI=0; /* ???? return (-1); */
}
if(DEBUG1) {
printf("returnedCountAAPI=%ld\n",returnedCountAAPI);
printf("AAPI FROM=%s",ctime(&(begin->tv_sec)));
printf("AAPI TO =%s",ctime(&(right_endpoint.tv_sec)));
if(DEBUG2) for(i=0;i<returnedCountAAPI;i++)
printf("AAPI=%s",ctime(&((returnedTimeAAPI)[i].tv_sec)));
}
commonCount += returnedCountAAPI;
}
else { if(DEBUG1) printf("don't need history req\n");}
#endif /* USE_AAPI || USE_CAR */
if(commonCount>0)
{
*data =(double *)calloc(commonCount,sizeof(double));
*status=(short *) calloc(commonCount,sizeof(short));
*times =(struct timeval *)calloc(commonCount,sizeof(struct timeval));
if((*data == NULL) ||(*times == NULL) || (*status == NULL))
{
fprintf(stderr,"can't alloc %ld answers\n",commonCount);
if(*data) free (*data);
if(*times) free (*times);
if(*status) free (*status);
if (returnedDataAAPI) free (returnedDataAAPI);
if (returnedStatusAAPI) free (returnedStatusAAPI);
if (returnedTimeAAPI) free (returnedTimeAAPI);
if (returnedDataIOC) free (returnedDataIOC);
if (returnedStatusIOC) free (returnedStatusIOC);
if (returnedTimeIOC) free (returnedTimeIOC);
return (-1);
}
if(returnedCountAAPI>0)
{
memcpy(*data, returnedDataAAPI, returnedCountAAPI*(sizeof(double)));
memcpy(*status,returnedStatusAAPI,returnedCountAAPI*(sizeof(short)));
memcpy(*times, returnedTimeAAPI, returnedCountAAPI*(sizeof(struct timeval)));
}
if(returnedCountIOC>0) {
memcpy(&((*data) [returnedCountAAPI]), returnedDataIOC,
returnedCountIOC*(sizeof(double)));
memcpy(&((*status)[returnedCountAAPI]),returnedStatusIOC,
returnedCountIOC*(sizeof(short)));
memcpy(&((*times) [returnedCountAAPI]), returnedTimeIOC,
returnedCountIOC*(sizeof(struct timeval)));
}
/* ATTENTION: Strip status is no CA status! */
for(i=0;i<(int)commonCount;i++) (*status)[i] |= DATASTAT_PLOTABLE;
}
if(DEBUG1) {
printf("commonCount=%ld\n",commonCount);
printf("COM FROM=%s",ctime((const time_t *)&(begin->tv_sec)));
printf("COM TO =%s",ctime((const time_t *)&(end->tv_sec)));
if(DEBUG2) for(i=0;i<(int)commonCount;i++)
printf("TIME=%s",ctime((const time_t *)&((*times)[i].tv_sec)));
}
if (returnedDataAAPI) free (returnedDataAAPI);
if (returnedStatusAAPI) free (returnedStatusAAPI);
if (returnedTimeAAPI) free (returnedTimeAAPI);
if (returnedDataIOC) free (returnedDataIOC);
if (returnedStatusIOC) free (returnedStatusIOC);
if (returnedTimeIOC) free (returnedTimeIOC);
*count=commonCount;
return (0);
}