aboutsummaryrefslogtreecommitdiff
path: root/src/SockIOreader.cc
blob: 392d28a0e60592b51f481c6bd55f13f317ebbdd0 (plain)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#include <iostream.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <setjmp.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "FlexArrayTmpl.H"

#include "SockIOreader.hh"

#if defined(T3E) || defined(__hpux)
#define SIGNAL_CAST
#else
#define SIGNAL_CAST (SIG_PF)
#endif

// Globals
FlexArray<SockIOreader*> sock_io_readers;
void IOsigHandler(int sig);
void reaper(int sig);

// enable signals (needs a refcount)
class SigMgr {
  static int sigrefcount;
  static int nwatchers;
public:
  static void AddObserver(){
    nwatchers++;
  }
  static void RemoveObserver(){
    nwatchers--;
    // should check refcount status change
  }
  static void SigOn(){
    //puts("sigOn");
    sigrefcount++;
    if(sigrefcount>nwatchers){
      // error
      sigrefcount=nwatchers;
      puts("SigMgr: Error... the Signal refcount is > numwatchers");
      puts("correcting");
    }
    if(sigrefcount==nwatchers){
      // call the handlertest just in case
      //IOsigHandler((int)SIGIO);
      for(int i=0; i<sock_io_readers.getSize();i++){
	//printf("poll reader %u\n",i);
	sock_io_readers[i]->sigHandler(SIGIO);
      }
      signal(SIGIO,SIGNAL_CAST IOsigHandler); // reenable the signal handler
      signal(SIGURG,SIGNAL_CAST IOsigHandler);// reenable the signal handler
    }
  }
  // disable signals until elemental IO operation completes
  static void SigOff(){
    //puts("sigOff");
    sigrefcount--;
    if(sigrefcount<0){
      puts("SigMgr::SigOff(): Signal refcount < 0... correcting");
      sigrefcount=0;
    }
    signal(SIGIO,SIG_DFL); // prevent double-interrupt
    signal(SIGURG,SIG_DFL);
  }
};

int SigMgr::sigrefcount=0;
int SigMgr::nwatchers=0;

// If any IO event occurs, this will call handlers for all availible SockIOreaders
// to see if they have an event to take care of.
void IOsigHandler(int sig){
  //puts("SigHandler: IO Interrupt");
  SigMgr::SigOff();
  // So here we look at the global list of SockIOreaders and pass the event on
  for(int i=0; i<sock_io_readers.getSize();i++){
    //printf("poll reader %u\n",i);
    sock_io_readers[i]->sigHandler(sig);
  }
  SigMgr::SigOn();
}

// This gobbles up zombie children... kind of like Night of the Living Dead
void reaper(int sig){
  puts("Reaper: Child Interrupt");
#ifndef T3E
  // Eat dead children :b
  struct rusage status;
  int statptr;
  while(wait3(&statptr,WNOHANG,&status)>=0) {} // get all the deadsters
#endif
  
  signal(SIGCHLD,SIGNAL_CAST reaper); // reenable the signal handler
}

// Alarm handler to escape from hanging socket reads (crash in mid-send)
jmp_buf senv,tenv;
void timeout(int sig){
  signal(SIGALRM,SIGNAL_CAST timeout); // reset the handler
  longjmp(tenv,1); // jump to the error recovery vector
}

/*
void InterruptRecovery(int sig){
  SockIOwriter::halt(); // kill app on first opportunity
  if(SockIOwriter::sendInProgress()>0){
    signal(SIGALRM,SIGNAL_CAST timeout); // reset the handler
    longjmp(tenv,1);
  }
  else
    exit(0);
} 
*/

// kludge
int SockIOreader::blockingRead(void *buffer,int size){
  int nremaining,nreceived; 
  if(!connected_client) return 0;
  // read in 1k blocks (perhaps have 15-30sec timeout)
  // should be able to set variable blocksize
  for(nremaining=size;nremaining>0;nremaining-=nreceived) 
  {
	  nreceived=connected_client->read((char*)buffer,nremaining);
	  if (nreceived<1) // read error
		  return size-nremaining;
  }
  return size; // can return alt value if timeout occurs
}
//=======================================================================================

void SockIOreader::sigHandler(int sig){
  // do a select...
  RawPort *port = master_mux.select();
  if(port){
    if(connected_client)
      //pending++; // (if we didn't prefer new clients....) but....
      delete connected_client; // has preference for new clients!!
    // pref for new clients is good for error recovery. (eg. death of client program)
    puts("\nOpen New Client Port\n");
    connected_client = ServerPort.accept();
    mux.addInport(connected_client); // add to select mux list
    // close and reopent the file trunc
    delete scratchfile;
    scratchfile=new IEEEIO(scratchfilename,IObase::Write);
    delete scratchfile;
    scratchfile=new IEEEIO(scratchfilename,IObase::Append); // kludge to purge file
  }
  scratchfile->seek(scratchfile->nDatasets()-1);
  while(mux.select()>0){
    int dims[5];
    char name[128];
    int bufsize;
    void *current_buffer;
    blockingRead(&current_rec,sizeof(RecordHdr));
    switch(current_rec.recordtype){
    case 1: // data record
      blockingRead(&current_data_rec,sizeof(DataRecordHdr));
      blockingRead(dims,sizeof(int)*current_data_rec.rank);
      bufsize=current_data_rec.datasize;
      break;
    case 2: // annotation record
      // do nothing for now.
      bufsize=current_rec.recordsize;
      break;
    case 3: // attribrecord
      blockingRead(&current_att_rec,sizeof(AttributeRecordHdr));
      blockingRead(name,current_att_rec.namesize);
      bufsize=current_att_rec.datasize;
      break;
    }
    current_buffer = new char[bufsize];
    blockingRead(current_buffer,bufsize);
    // now commit it to disk
    switch(current_rec.recordtype){
    case 1:
      scratchfile->write(IObase::Int2DataType(current_data_rec.numbertype),
			 current_data_rec.rank,
			 dims,
			 current_buffer);
      break;
    case 2:
      scratchfile->writeAnnotation((char*)current_buffer);
      break;
    case 3:
      scratchfile->writeAttribute(name,IObase::Int2DataType(current_att_rec.numbertype),
				  bufsize/IObase::sizeOf(IObase::Int2DataType(current_att_rec.numbertype)),
				  current_buffer);
      break;
    }
    delete current_buffer;
    current_buffer=0;
  }
  if (current_ds > 0)
    scratchfile->seek(current_ds);
}

//------------------------core stuff.....................
SockIOreader::SockIOreader(CONST char *scratchfilenm,int port):IObase(scratchfilenm,IObase::Read),
  scratchfile(0),ServerPort(port),
  pending(0),connected_client(0){
    current_ds = -1;
    strcpy(scratchfilename,scratchfilenm);
    scratchfile=new IEEEIO(scratchfilename,IObase::Write);
    delete scratchfile;// kludge to purge file
    scratchfile=new IEEEIO(scratchfilename,IObase::Append);
    // sockport is open, now set up the select
    fprintf(stderr,"ServerPort validity=%u\n",ServerPort.isAlive());
    master_mux.addInport(&ServerPort);
    // the master mux should always return immediately 
    // (a non-blocking select())
    master_mux.setTimeout(0,0); // seconds and microseconds =0
    mux.setTimeout(0,0);
    // set signals for capturing events
    sock_io_readers.append(this); // append to handler list
    SigMgr::AddObserver();
    puts("Signals On");
    SigMgr::SigOn();
    signal(SIGCHLD,SIGNAL_CAST reaper); // reenable the signal reaper handler
    puts("construction completed");
}

SockIOreader::SockIOreader(CONST char *scratchfilenm,int port,int windowsize):
  IObase(scratchfilenm,IObase::Read),
  scratchfile(0),ServerPort(port,windowsize),
  pending(0),connected_client(0){
    current_ds = -1;
    strcpy(scratchfilename,scratchfilenm);
    scratchfile=new IEEEIO(scratchfilename,IObase::Write);
    delete scratchfile;// kludge to purge file
    scratchfile=new IEEEIO(scratchfilename,IObase::Append);
    // sockport is open, now set up the select
    fprintf(stderr,"ServerPort validity=%u\n",ServerPort.isAlive());
    master_mux.addInport(&ServerPort);
    // the master mux should always return immediately 
    // (a non-blocking select())
    master_mux.setTimeout(0,0); // seconds and microseconds =0
    mux.setTimeout(0,0);
    // set signals for capturing events
    sock_io_readers.append(this); // append to handler list
    SigMgr::AddObserver();
    puts("Signals On");
    SigMgr::SigOn();
    signal(SIGCHLD,SIGNAL_CAST reaper); // reenable the signal reaper handler
    puts("construction completed");
}

SockIOreader::~SockIOreader(){
  SigMgr::SigOff();
  SigMgr::RemoveObserver();
  // shut all of the sockets down
  if(connected_client) 
    delete connected_client; // need to remove from mux list
  connected_client=0;
}

// should check server status too
int SockIOreader::isValid() { 
  if(scratchfile && connected_client) return 1;
  return 0;
}

// could use overloading to differentiate type here... (but I'm going simple)
int SockIOreader::readInfo(IObase::DataType &typeID,int &rank,int *dims,int maxdims){
  // just hand this off to the datafile (and hope that APPEND mode works correctly)
  SigMgr::SigOff();
  int retval = scratchfile->readInfo(typeID,rank,dims,maxdims);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::read(void *data){
  SigMgr::SigOff();
  int retval = scratchfile->read(data);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::seek(int dataset_index){
  SigMgr::SigOff();
  current_ds=scratchfile->seek(dataset_index);
  int retval = current_ds;
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::nDatasets(){
  SigMgr::SigOff();
  int retval = scratchfile->nDatasets(); // must not interrupt these operations...
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::readAnnotationInfo(int number,int &length){ // returns length (-1 if none left)
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->readAnnotationInfo(number,length);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::readAnnotation(int number,char *annotation,int maxsize){
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->readAnnotation(number,annotation,maxsize);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::nAnnotations(){
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->nAnnotations();
  SigMgr::SigOn();
  return retval;
} 
int SockIOreader::readAttributeInfo(int number,char *name,IObase::DataType &typeID,Long &nelem,int maxnamelen){
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->readAttributeInfo(number,name,typeID,nelem,maxnamelen);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::readAttributeInfo(CONST char *name,IObase::DataType &typeID,Long &nelem){ // returns number
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->readAttributeInfo(name,typeID,nelem);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::readAttribute(int number,void *data){
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->readAttribute(number,data);
  SigMgr::SigOn();
  return retval;
}
int SockIOreader::nAttributes(){
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->nAttributes();
  SigMgr::SigOn();
  return retval;
}
//-----------------Chunking Utilities..................
int SockIOreader::readChunk(CONST int *chunkdims,CONST int *chunkorigin,void *data){
  SigMgr::SigOff();
  scratchfile->seek(current_ds);
  int retval = scratchfile->readChunk(chunkdims,chunkorigin,data);
  SigMgr::SigOn();
  return retval;
}