added nlsop_gui.c (not finished yet) + fixes to server and client code
[physik/nlsop.git] / nlsop_server.c
index 14cda4f..528c634 100644 (file)
 #include "event.h"
 #include "list.h"
 
-#define NLSOP_GUI 'g'
-#define NLSOP_CLIENT 'c'
-#define NLSOP_NJOB 'N'
-#define NLSOP_CJOB 'C'
-
-typedef struct s_client {
-  int channel;
-  unsigned char status;
-#define IDLE (1<<0)
-#define WORK (1<<1)
-} t_client;
-
-typedef struct s_job {
-  int channel;
-  unsigned char status;
-#define IN_QUEUE (1<<0)
-#define IN_WORK (1<<1)
-  int progress;
-  unsigned char *ac;
-  int *cc;
-  int x,y,z;
-  info info;
-  int step;
-} t_job;
+#include "nlsop_general.h"
 
 int usage(char *prog)
 {
@@ -89,7 +66,6 @@ int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
   int channel;
   unsigned char data;
   t_client client;
-  t_job job;
   int gui_chan;
 
   channel=network_manage_incoming(net);
@@ -106,7 +82,7 @@ int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
                                                  channel);
 
   /* are you client or gui? */
-  network_receive_chan(net,chanel,&data,1);
+  network_receive_chan(net,channel,&data,1);
   if(data==NLSOP_GUI) {
     gui_chan=channel;
     list_add_element(g_list,&gui_chan,sizeof(int));
@@ -131,35 +107,121 @@ int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
 int save_job(t_net *net,int channel,t_job *job,unsigned char dc) {
 
   char filename[128];
-  inf fd;
+  int fd;
   int ret;
+  d3_lattice d3l;
+  info info;
 
   ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
   if(ret==N_ERROR) printf("FATAL: getting ac status failed\n");
-  ret=network_receice_chan(net,channel,job->cc,job->size*sizeof(int));
+  ret=network_receive_chan(net,channel,(unsigned char *)job->cc,
+                           job->size*sizeof(int));
   if(ret==N_ERROR) printf("FATAL: getting cc failed\n");
-  ret=network_receive_chan(net,channel,&(job->step),sizeof(int));
+  ret=network_receive_chan(net,channel,(unsigned char *)&(job->step),
+                           sizeof(int));
   if(ret==N_ERROR) printf("FATAL: getting step number failed\n");
 
   if(dc!=DC_QUIT) {
-    snprintf(filename,"nlsop_b%f_c%f_s%f_ds%d_dr%f_Z%c__%d_of_%d.save",
-             job->info->b,job->info->c,job->info->s,
-             job->info->diff_rate,job->info->dr_ac,
-             job->info->z_diff?'y':'n',
-             job->step,job->info->steps);
+    snprintf(filename,128,"nlsop_b%f_c%f_s%f_ds%d_dr%f_Z%c__%d_of_%d.save",
+             job->info.b,job->info.c,job->info.s,
+             job->info.diff_rate,job->info.dr_ac,
+             job->info.z_diff?'y':'n',
+             job->step,job->info.steps);
     if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
       printf("FATAL: unable to open file %s\n",filename);
       return -1;
     }
 
+    memset(&d3l,0,sizeof(d3_lattice));
+    d3l.max_x=job->x;
+    d3l.max_y=job->y;
+    d3l.max_z=job->z;
+    if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
+      printf("FATAL: write of d3_lattice failed\n");
+      return -1;
+    }
+
+    if(write(fd,&(job->info),sizeof(info))<sizeof(info)) {
+      printf("FATAL: write of info failed\n");
+      return -1;
+    }
+
+    ret=write(fd,job->ac,job->size*sizeof(unsigned char));
+    if(ret<job->size*sizeof(unsigned char)) {
+      printf("FATAL: write of a/c states failed\n");
+      return -1;
+    }
+   
+    ret=write(fd,job->cc,job->size*sizeof(int));
+    if(ret<job->size*sizeof(int)) {
+      printf("FATAL: write of c.-conc. failed\n");
+      return -1;
+    }
 
+    close(fd);
     
   }
 
   return 1;
 }
 
-int handle_node(net,event,c_list,g_list,job) {
+int add_job(t_net *net,int chan,t_list *jl) {
+
+  t_job job;
+
+  job.channel=-1;
+  job.status=IN_QUEUE;
+  job.progress=0;
+
+  network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
+  network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
+  network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
+  network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
+
+  job.size=job.x*job.y*job.z;
+
+  job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
+  if(job.ac==NULL) {
+    printf("unable to malloc a/c memory\n");
+    return -1;
+  }
+
+  job.cc=(int *)malloc(job.size*sizeof(int));
+  if(job.cc==NULL) {
+    printf("unable to malloc cc memory\n");
+    return -1;
+  }
+
+  job.step=0;
+
+  list_add_element(jl,&job,sizeof(t_job));
+
+  return 1;
+}
+
+int send_status(t_net *net,int chan,t_list *jl) {
+
+  unsigned char data;
+  int count;
+  int i;
+
+  data=GUI_INFO;
+  count=list_count(jl);
+
+  network_send_chan(net,chan,&data,sizeof(unsigned char));
+  network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
+
+  list_reset(jl);
+  for(i=0;i<count;i++) {
+    network_send_chan(net,chan,jl->current->data,sizeof(t_job));
+    list_next(jl);
+  }
+
+  return 1;
+}
+
+int handle_node(t_net *net,t_event *event,
+                t_list *c_list,t_list *g_list,t_list *job) {
 
   int i;
   unsigned char data;
@@ -186,7 +248,6 @@ int handle_node(net,event,c_list,g_list,job) {
         if(data==DC_END) {
           save_job(net,i,j,DC_END);
           /* reset client */
-          c->channel=i;
           c->status=IDLE;
           /* delete job entry */
           list_del_current(job);
@@ -212,7 +273,21 @@ int handle_node(net,event,c_list,g_list,job) {
 
       else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
         /* its a gui */
-          
+        if(data==GUI_ADDJOB) add_job(net,i,job);
+
+        else if(data==GUI_INFO) send_status(net,i,job);
+
+        else if(data==GUI_QUIT) {
+          printf("disconnecting gui on channel %d\n",i);
+          event_math(net->connection[i].fd,event,READ,REMOVE);
+          network_close(net,i);
+          list_del_current(g_list);
+        }
+
+        else {
+          printf("unknown gui command\n");
+          return -1;
+        }
       }
 
       else {
@@ -226,6 +301,69 @@ int handle_node(net,event,c_list,g_list,job) {
   return 1;
 }
 
+int distribute_jobs(t_event *event,void *allineed) {
+
+  t_net *net;
+  t_list *c_list,*g_list,*job;
+  int count_j,count_c;
+  t_job *j;
+  t_client *c;
+  unsigned char data;
+  d3_lattice d3l;
+
+  net=(t_net *)allineed;
+  c_list=(t_list *)(allineed+sizeof(t_net));
+  g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list));
+  job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list));
+
+  count_j=list_count(job);
+  count_c=list_count(c_list);
+
+  list_reset(job);
+  list_reset(c_list);
+  while((count_c!=0)&&(count_j!=0)) {
+    j=(t_job *)job->current->data;
+    c=(t_client *)c_list->current->data;
+    while(c->status!=IDLE) {
+      list_next(c_list);
+      c=(t_client *)c_list->current->data;
+    }
+    while(j->status!=IN_QUEUE) {
+      list_next(job);
+      j=(t_job *)job->current->data;
+    }
+
+    /* direct current job to current client */
+    if(j->step==0) data=NLSOP_NJOB;
+    else data=NLSOP_CJOB;
+
+    c->status=WORK;
+    j->channel=c->channel;
+    j->status=IN_WORK;
+
+    d3l.max_x=j->x;
+    d3l.max_y=j->y;
+    d3l.max_z=j->z;
+
+    network_send_chan(net,c->channel,&data,sizeof(unsigned char));
+    network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
+    network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
+
+    if(data==NLSOP_CJOB) {
+      network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
+      network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
+                        j->size*sizeof(int));
+    }
+
+    --count_c;
+    --count_j;
+    list_next(c_list);
+    list_next(job);
+  }
+
+  return 1;
+}
+
 int parse_incoming(t_event *event,void *allineed) {
 
   t_net *net;
@@ -237,7 +375,7 @@ int parse_incoming(t_event *event,void *allineed) {
   job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list));
 
   /* decide what to do */
-  if(FD_ISSET(net->l.fd,&(event->rfds))) {
+  if(FD_ISSET(net->l_fd,&(event->rfds))) {
     /* new node */
     printf("new node ...\n");
     add_node(net,event,c_list,g_list);
@@ -269,9 +407,9 @@ int main(int argc,char **argv)
   /* tzzz ... */
   allyouneed=malloc(sizeof(t_net)+3*sizeof(t_list));
   memcpy(allyouneed,&net,sizeof(t_net));
-  memcpy(allyouneed+sizeof(t_net),&c_list,sizeof(list));
-  memcpy(allyouneed+sizeof(t_net)+sizeof(t_list),&g_list,sizeof(list));
-  memcpy(allyouneed+sizeof(t_net)+2*sizeof(t_list),&job,sizeof(list));
+  memcpy(allyouneed+sizeof(t_net),&c_list,sizeof(t_list));
+  memcpy(allyouneed+sizeof(t_net)+sizeof(t_list),&g_list,sizeof(t_list));
+  memcpy(allyouneed+sizeof(t_net)+2*sizeof(t_list),&job,sizeof(t_list));
   
   /* default values */
   port=1025;
@@ -281,7 +419,8 @@ int main(int argc,char **argv)
 
   /* event init */
   event_init(&event,1);
-  event_set_timeout(&event,0,0);
+  /* 10 sec event timeout - distributing jobs */
+  event_set_timeout(&event,10,0);
 
   /* connect to server */
   network_init(&net,1);
@@ -293,7 +432,7 @@ int main(int argc,char **argv)
 
   /* wait for events :) */
   event_math(net.l_fd,&event,READ,ADD);
-  event_start(&event,allyouneed,parse_incoming,NULL);
+  event_start(&event,allyouneed,parse_incoming,distribute_jobs);
 
   return 1;
 }