]> hackdaworld.org Git - physik/nlsop.git/commitdiff
added nlsop_gui.c (not finished yet) + fixes to server and client code
authorhackbard <hackbard>
Wed, 3 Nov 2004 18:09:39 +0000 (18:09 +0000)
committerhackbard <hackbard>
Wed, 3 Nov 2004 18:09:39 +0000 (18:09 +0000)
nlsop_client.c
nlsop_gui.c [new file with mode: 0644]
nlsop_server.c

index c31429908e84350b8f1be4d38d276b1a71af056d..6467827129298eae329a196d15a04474a8e3e2c9 100644 (file)
@@ -49,6 +49,8 @@
 #include "network.h"
 #include "event.h"
 
+#include "nlsop_general.h"
+
 #define MAKE_AMORPH(N) *(N)|=AMORPH
 #define MAKE_CRYST(N) *(N)&=~AMORPH
 
@@ -417,9 +419,6 @@ int get_data_and_calc(t_event *event,void *allineed) {
   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;
diff --git a/nlsop_gui.c b/nlsop_gui.c
new file mode 100644 (file)
index 0000000..7949c55
--- /dev/null
@@ -0,0 +1,216 @@
+/*
+ * nlsop gui code
+ *
+ * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
+ *
+ * this program tries helping to understand the amorphous depuration
+ * and recrystallization of SiCx while ion implantation at temperatures
+ * below 400 degree celsius.
+ * hopefully the program will simulate the stabilization of the
+ * selforganizing lamella structure in the observed behaviour.
+ *
+ * refs: 
+ *  - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
+ *  - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
+ *
+ * Copyright (C) 2004 Frank Zirkelbach
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "nlsop.h"
+#include "dfbapi.h"
+#include "random.h"
+
+#include "network.h"
+#include "event.h"
+#include "display.h"
+
+#include "nlsop_general.h"
+
+#define LOGFILE "~/.nlsop_logfile"
+
+int usage(char *prog)
+{
+ puts("usage:");
+ printf("%s -i <ip> -p <port> -l <logfile>\n",prog);
+ return 1;
+}
+
+/*
+ * gui internal functions
+ */
+
+int display_new_line(t_display *display,char *text) {
+
+  return 1;
+}
+
+int send_and_wait_for_answer(t_net *net,t_display *display) {
+
+  return 1;
+}
+
+int send_job(t_net *net,t_input *input,t_display *display) {
+
+  return 1;
+}
+
+int get_command(t_event *event,void *allineed) {
+
+  t_net *net;
+  t_display *display;
+  t_input *input;
+
+  unsigned char data[64];
+
+  net=(t_net *)allineed;
+  display=(t_display *)(allineed+sizeof(t_net));
+  input=(t_input *)(allineed+sizeof(t_net)+sizeof(t_display));
+
+  switch(input->content[0]) {
+    case GUI_INFO:
+      send_and_wait_for_answer(net,display);
+      break;
+    case GUI_ADDJOB:
+      send_job(net,input,display)
+      break;
+    case GUI_QUIT:
+      /* stop everything */
+      event_math(0,event,READ,REMOVE);
+      network_shutdown(net);
+      display_shutdown(display);
+      input_shutdown(input);
+      event_shutdown(event);
+      return 2;
+    default:
+      display_new_line(display,"unknown gui command");
+      break;
+  }
+
+  return 1;
+}
+
+/*
+ * main program
+ */
+
+int main(int argc,char **argv)
+{
+
+  char server_ip[16];
+  char logfile[64];
+  int port;
+
+  t_net net;
+  t_event event;
+  t_display display;
+  t_input input;
+
+  unsigned char data;
+  int i;
+
+  int fd;
+
+  void *allyouneed;
+
+  /* default values */
+  strcpy(logfile,LOGFILE);
+  strcpy(server_ip,"");
+  port=1025;
+
+  /* parse/check argv */
+  for(i=1;i<argc;i++) {
+    if(argv[i][0]=='-') {
+      switch(argv[i][1]) {
+        case 'h':
+          usage(argv[0]);
+          return -1;
+        case 'i':
+          strncpy(server_ip,argv[++i],16);
+          break;
+        case 'p':
+          port=atoi(argv[++i]);
+          break;
+        case 'l':
+          strncpy(logfile,argv[++i],64);
+          break;
+        default:
+          usage(argv[0]);
+          return -1;
+      }
+    }
+  }
+  if(!strcmp(server_ip,"")) {
+    usage(argv[0]);
+    return -1;
+  }
+
+  if((fd=open(logfile,O_WRONLY,O_CREAT))<0) {
+    printf("unable to open file %s\n",logfile);
+    return -1;
+  }
+
+  allyouneed=malloc(sizeof(t_net)+sizeof(t_display)+sizeof(t_input));
+  memcpy(allyouneed,&net,sizeof(t_net));
+  memcpy(allyouneed+sizeof(t_net),&display,sizeof(t_display));
+  memcpy(allyouneed+sizeof(t_net)+sizeof(t_display),sizeof(t_input));
+
+  /* input init */
+  input_init(&input,fd);
+  input.mode=CONTENT_BUFFER|LINE_BUFFERED|INPUT_ECHO;
+  input_ios_init(&input);
+
+  /* event init */
+  event_init(&event,fd);
+  event_set_timeout(&event,0,0);
+
+  /* display init */
+  display_init(&display,fd);
+
+  /* user interaction */
+  event_math(0,&event,READ,ADD);
+
+  /* connect to server */
+  network_init(&net,fd);
+  network_set_connection_info(&net,0,server_ip,port);
+  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=NLSOP_GUI;
+  network_send(net.connection[0].fd,&data,1);
+
+  /* wait for job */
+  event_start(&event,allyouneed,get_command,NULL);
+
+  free(allyouneed);
+  close(fd);
+
+  return 1;
+}
index 14cda4fe082931bbe52c3ec21771561cbed2c051..528c63465eb242d2ac54c51b1950881b11b192d9 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;
 }