finished nlsop_client code
authorhackbard <hackbard>
Fri, 29 Oct 2004 13:38:47 +0000 (13:38 +0000)
committerhackbard <hackbard>
Fri, 29 Oct 2004 13:38:47 +0000 (13:38 +0000)
nlsop_client.c

index 403bfe0..c635892 100644 (file)
 #include <fcntl.h>
 #include <unistd.h>
 
+#include <signal.h>
+
 #include "nlsop.h"
 #include "dfbapi.h"
 #include "random.h"
 
-#include "../api/network.h"
-#include "../api/event.h"
-#include "../api/list.h"
+#include "network.h"
+#include "event.h"
 
 #define MAKE_AMORPH(N) *(N)|=AMORPH
 #define MAKE_CRYST(N) *(N)&=~AMORPH
 
+#define NLSOP_CLIENT 'c'
+#define NLSOP_NJOB 'N'
+#define NLSOP_CJOB 'C'
+
+/* globals */
+
+char p_file[MAX_CHARS];
+char n_e_file[MAX_CHARS];
+char r_file[MAX_CHARS];
+t_net *gnet;
+d3_lattice *gd3_l;
+info *gmy_info;
+int *gi;
+unsigned char dc;
+
+int get_data_and_calc(t_event *event,void *allineed);
+
 int usage(char *prog)
 {
  puts("usage:");
@@ -221,9 +239,21 @@ int distrib_c(d3_lattice *d3_l,info *my_info,int step,u32 rj_m,u32 *rj_g)
 }
 
 /* save to file --> send to server :)  --> T O D O <-- */
-int send_data( ) {
+void send_data(int signum) {
+
+  int c;
+
+  c=gd3_l->max_x*gd3_l->max_y*gd3_l->max_z;
+
+  network_send(gnet->connection[0].fd,&dc,1);
+  network_send(gnet->connection[0].fd,(unsigned char *)gd3_l,
+               sizeof(d3_lattice));
+  network_send(gnet->connection[0].fd,(unsigned char *)gmy_info,sizeof(info));
+  network_send(gnet->connection[0].fd,gd3_l->status,c*sizeof(unsigned char));
+  network_send(gnet->connection[0].fd,(unsigned char *)gd3_l->extra,
+               c*sizeof(int));
+  network_send(gnet->connection[0].fd,(unsigned char *)gi,sizeof(int));
 
-  return 1;
 }
 
 int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
@@ -261,78 +291,6 @@ int save_to_file(char *sf,d3_lattice *d3_l,info *my_inf)
  return 1;
 }
 
-/* load from file --> receive from server :) --> T O D O <-- */
-int receive_data(  ) {
-
-  return 1;
-}
-
-int load_from_file(char *lf,d3_lattice *d3_l,info *my_inf)
-{
-
- int lf_fd,c,pos,end,data,data_len,strip;
-
- if((lf_fd=open(lf,O_RDONLY))<0)
- {
-  puts("cannot open load file");
-  return -1;
- }
- if(read(lf_fd,d3_l,sizeof(d3_lattice))<sizeof(d3_lattice))
- {
-  puts("failed reading d3 lattice struct");
-  return -1;
- }
- pos=lseek(lf_fd,0,SEEK_CUR);
- printf("psition: %d (%d)\n",pos,sizeof(d3_lattice));
- data=d3_l->max_x*d3_l->max_y*d3_l->max_z;
- data_len=data*(sizeof(int)+sizeof(unsigned char));
- printf("there are %d volumes so we need %d of bytes\n",data,data_len);
- end=lseek(lf_fd,0,SEEK_END);
- c=end-pos-data_len;
- printf("end: %d => length: %d => guessed info size: %d bytes\n",end,end-pos,c);
- strip=sizeof(info)-c;
- printf("as real programs info size is %d, we strip %d bytes\n",sizeof(info),strip);
- lseek(lf_fd,pos,SEEK_SET);
- c=sizeof(info);
- if(strip>0) c-=strip;
- if(c<0)
- {
-  puts("info smaller then strip size");
-  return -1;
- }
- if(read(lf_fd,my_inf,c)<c)
- {
-  puts("failed reading info struct");
-  return-1;
- }
- if(strip>0) memset(my_inf+c,0,strip);
- if(strip<0) lseek(lf_fd,(-1*strip),SEEK_CUR);
- c=d3_l->max_x*d3_l->max_y*d3_l->max_z;
- if((d3_l->status=(unsigned char*)malloc(c*sizeof(unsigned char)))==NULL)
- {
-  puts("cannot allocate status buffer");
-  return -1;
- }
- if((d3_l->extra=(int *)malloc(c*sizeof(int)))==NULL)
- {
-  puts("cannot allocate concentration buffer");
-  return -1;
- }
- if(read(lf_fd,d3_l->status,c*sizeof(unsigned char))<c*sizeof(unsigned char))
- {
-  puts("failed reading status of d3 lattice sites");
-  return -1;
- }
- if(read(lf_fd,d3_l->extra,c*sizeof(int))<c*sizeof(int))
- {
-  puts("failed reading sites concentration");
-  return -1;
- }
- close(lf_fd);
-
- return 1;
-}
-
 u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
  double a,b;
  int i,j,k;
@@ -409,38 +367,20 @@ u32 get_reject_graph(info *my_info,d3_lattice *d3_l,char *file,u32 *graph) {
 int main(int argc,char **argv)
 {
 
-  u32 x,y,z,x_c,y_c,z_c;
-  int i,j;
-  int resave;
-  int c_step;
   char server_ip[16];
   int port;
-  char p_file[MAX_CHARS];
-  char n_e_file[MAX_CHARS];
-  char r_file[MAX_CHARS];
-  d3_lattice d3_l;
-  info my_info;
-  u32 *c_profile;
-  u32 *n_e_loss;
-  u32 ne_max,ip_max;
-  u32 *nel_z;
-  unsigned char err_dc,dc;
-#define DC_QUIT (1<<0)
-#define DC_OK (1<<1)
-#define DC_END (1<<2)
   t_net net;
+  t_event event;
+  unsigned char data[256];
+  int i;
+
+  gnet=&net;
 
   /* default values */
-  resave=RESAVE;
-  c_step=0;
   strcpy(server_ip,"");
   strcpy(p_file,IMP_PROFILE);
   strcpy(n_e_file,NEL_PROFILE);
   strcpy(r_file,"");
-  ne_max=0;
-  ip_max=0;
-  err_dc=0;
-  dc=0;
   port=1025;
 
   /* parse/check argv */
@@ -476,18 +416,92 @@ int main(int argc,char **argv)
     return -1;
   }
 
-  /* connect to server and notify about free resource */
+  /* event init */
+  event_init(&event,1);
+  event_set_timeout(&event,0,0);
+
+  /* connect to server */
   network_init(&net,1);
   network_set_connection_info(&net,0,server_ip,port);
-  network_connect(&net,0);
+  if(network_connect(&net,0)==N_E_CONNECT) {
+    printf("unable to connect to server, aborting ...\n");
+    return -1;
+  }
+  network_select(&net,0);
+
+  /* tell server: i am a client, i may work for you */
+  data[0]=NLSOP_CLIENT;
+  network_send(net.connection[0].fd,data,1);
 
   /* wait for job */
+  event_math(net.connection[0].fd,&event,READ,ADD);
+  event_start(&event,NULL,get_data_and_calc,NULL);
+
+  return 1;
+}
+
+int get_data_and_calc(t_event *event,void *allineed) {
+
+  d3_lattice d3_l;
+  info my_info;
+  u32 *c_profile;
+  u32 *n_e_loss;
+  u32 ne_max,ip_max;
+  u32 *nel_z;
+  u32 x_c,y_c,z_c;
+  int i,j;
+  int resave;
+  int c_step;
+#define DC_QUIT (1<<0)
+#define DC_OK (1<<1)
+#define DC_END (1<<2)
+  unsigned char data[256];
 
+  t_net *net;
+
+  resave=RESAVE;
+  c_step=0;
+  ne_max=0;
+  ip_max=0;
+
+  net=gnet;
+  gd3_l=&d3_l;
+  gmy_info=&my_info;
+  gi=&i;
+  dc=0;
+  
   /* get info (+data) */
+  network_receive(net->connection[0].fd,data,1);
+  if(data[0]==NLSOP_NJOB || data[0]==NLSOP_CJOB) {
+    network_receive(net->connection[0].fd,(unsigned char *)&d3_l,
+                    sizeof(d3_lattice));
+    network_receive(net->connection[0].fd,(unsigned char *)&my_info,
+                    sizeof(info));
+    c_step=0;
+    j=d3_l.max_x*d3_l.max_y*d3_l.max_z;
+    d3_l.status=(unsigned char *)malloc(j*sizeof(unsigned char));
+    if(d3_l.status==NULL) {
+      printf("status alloc failed\n");
+      return -1;
+    }
+    d3_l.extra=(int *)malloc(j*sizeof(int));
+    if(d3_l.extra==NULL) {
+      printf("extra malloc failed\n");
+      return -1;
+    }
+    if(data[0]==NLSOP_CJOB) {
+      network_receive(net->connection[0].fd,d3_l.status,
+                      j*sizeof(unsigned char));
+      network_receive(net->connection[0].fd,(unsigned char *)d3_l.extra,
+                      j*sizeof(int));
+      network_receive(net->connection[0].fd,(unsigned char *)&c_step,
+                      sizeof(int));
+    }
+  }
 
   /* care for signals */
-  err_dc=DC_QUIT;
-  signal(SIGTERM,send_data(&d3_l,&my_info,i,&err_dc));
+  dc=DC_QUIT;
+  signal(SIGTERM,send_data);
 
   /* rand init */
   if(!strcmp(r_file,"")) rand_init(NULL);
@@ -511,8 +525,7 @@ int main(int argc,char **argv)
     printf("failed allocating nel_z array mem\n");
     return -1;
   }
-  for(i=0;i<d3_l-max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
-
+  for(i=0;i<d3_l.max_z;i++) nel_z[i]=URAND_MAX*(1.0*n_e_loss[i]/ne_max);
 
   /* this should be obsolete - z is high enough - we check now! */
   if(c_profile[d3_l.max_z-1]!=0) {
@@ -541,7 +554,8 @@ int main(int argc,char **argv)
     distrib_c(&d3_l,&my_info,i,ip_max,c_profile);
     if(i%resave==0 && i!=0) {
       dc=DC_OK;
-      send_data(&d3_l,&my_info,i,&dc);
+      send_data(0);
+      dc=DC_QUIT;
     }
     i++;
     if(i%my_info.s_rate==0) sputter(&d3_l);
@@ -549,7 +563,8 @@ int main(int argc,char **argv)
 
   /* finished */
   dc=DC_END;
-  send_data(&d3_l,&my_info,i,&dc);
+  send_data(0);
+  dc=DC_QUIT;
 
   /* shutdown/free/close everything now ... */