4 * author: frank zirkelbach (frank.zirkelbach@physik.uni-augsburg.de)
6 * this program tries helping to understand the amorphous depuration
7 * and recrystallization of SiCx while ion implantation at temperatures
8 * below 400 degree celsius.
9 * hopefully the program will simulate the stabilization of the
10 * selforganizing lamella structure in the observed behaviour.
13 * - J. K. N. Lindner. Habil.Schrift, Universitaet Augsburg.
14 * - Maik Haeberlen. Diplomarbeit, Universitaet Augsburg.
16 * Copyright (C) 2004 Frank Zirkelbach
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
38 #include <sys/types.h>
52 #include "nlsop_general.h"
54 typedef struct s_priv {
68 * server specific stuff
74 printf("%s <listen port>\n",prog);
85 channel=network_manage_incoming(&(priv.net));
86 if(channel==N_E_ACCEPT) {
87 printf("accept failed!\n");
90 if(channel==N_E_MAXC) {
91 printf("maximum connections reached!\n");
94 printf("connection from %s port %d (ch: %d)\n",
95 priv.net.connection[channel].ip,priv.net.connection[channel].port,
98 /* are you client or gui? */
99 network_receive_chan(&(priv.net),channel,&data,1);
100 if(data==NLSOP_GUI) {
102 list_add_element(&(priv.gui),&gui_chan,sizeof(int));
103 printf("node is a gui\n");
105 else if(data==NLSOP_CLIENT) {
107 client.channel=channel;
108 list_add_element(&(priv.client),&client,sizeof(t_client));
109 printf("node is a client\n");
112 printf("not a client or gui - lets kick that ass out of here!\n");
113 network_close(&(priv.net),channel);
117 /* if we have a new node - care for it! */
118 event_math(priv.net.connection[channel].fd,&(priv.event),READ,ADD);
125 int save_job(unsigned char dc) {
134 j=(t_job *)priv.job.current->data;
136 printf("receiving data from client (#cells=%d)\n",j->size);
138 network_receive_chan(&(priv.net),gi,(unsigned char *)&d3l,
140 network_receive_chan(&(priv.net),gi,(unsigned char *)&info,sizeof(info));
141 network_receive_chan(&(priv.net),gi,j->ac,j->size*sizeof(unsigned char));
142 network_receive_chan(&(priv.net),gi,(unsigned char *)j->cc,
143 j->size*sizeof(int));
144 network_receive_chan(&(priv.net),gi,(unsigned char *)&(j->step),sizeof(int));
147 snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
148 j->info.b,j->info.c,j->info.s,
149 j->info.diff_rate,j->info.dr_ac,
150 j->step,j->info.steps);
151 if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
152 printf("FATAL: unable to open file %s\n",filename);
156 if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
157 printf("FATAL: write of d3_lattice failed\n");
161 if(write(fd,&info,sizeof(info))<sizeof(info)) {
162 printf("FATAL: write of info failed\n");
166 ret=write(fd,j->ac,j->size*sizeof(unsigned char));
167 if(ret<j->size*sizeof(unsigned char)) {
168 printf("FATAL: write of a/c states failed\n");
172 ret=write(fd,j->cc,j->size*sizeof(int));
173 if(ret<j->size*sizeof(int)) {
174 printf("FATAL: write of c.-conc. failed\n");
193 network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.x),sizeof(int));
194 network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.y),sizeof(int));
195 network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.z),sizeof(int));
196 network_receive_chan(&(priv.net),gi,(unsigned char *)&(job.info),
199 job.size=job.x*job.y*job.z;
201 job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
203 printf("unable to malloc a/c memory\n");
207 job.cc=(int *)malloc(job.size*sizeof(int));
209 printf("unable to malloc cc memory\n");
215 list_add_element(&(priv.job),&job,sizeof(t_job));
217 printf("job added: b=%f | c=%f | s=%f ...\n",
218 job.info.b,job.info.c,job.info.s);
223 int send_status(void) {
231 count=list_count(&(priv.job));
233 printf("sending job info\n");
235 network_send_chan(&(priv.net),gi,&data,sizeof(unsigned char));
236 network_send_chan(&(priv.net),gi,(unsigned char *)&count,sizeof(int));
238 list_reset(&(priv.job));
239 for(i=0;i<count;i++) {
240 network_send_chan(&(priv.net),gi,priv.job.current->data,sizeof(t_job));
241 list_next(&(priv.job));
247 int handle_node(void) {
253 for(gi=0;gi<MAX_CONNECTIONS;gi++) {
254 if(FD_ISSET(priv.net.connection[gi].fd,&(priv.event.rfds))) {
258 network_receive_chan(&(priv.net),gi,&data,1);
261 if(alert==1) return -1;
263 if(list_search_data(&(priv.client),&gi,sizeof(int))==L_SUCCESS) {
265 list_search_data(&(priv.job),&gi,sizeof(int));
266 j=(t_job *)priv.job.current->data;
267 c=(t_client *)priv.client.current->data;
273 /* free job memory */
276 /* delete job entry */
277 list_del_current(&(priv.job));
278 printf("job ended, saved and removed from list.\n");
283 /* inc progress state */
285 printf("job at next level, saved.\n");
290 /* network disconnect */
291 event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE);
292 network_close(&(priv.net),gi);
293 /* del from client list */
294 list_del_current(&(priv.client));
295 /* change job state */
297 printf("client terminating, job queued, client removed.\n");
301 else if(list_search_data(&(priv.gui),&gi,sizeof(int))==L_SUCCESS) {
303 if(data==GUI_ADDJOB) add_job();
305 else if(data==GUI_INFO) send_status();
307 else if(data==GUI_QUIT) {
308 printf("disconnecting gui on channel %d\n",gi);
309 event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE);
310 network_close(&(priv.net),gi);
311 list_del_current(&(priv.gui));
315 printf("unknown gui command\n");
321 printf("this chan is not in client or gui list! i disconnect now!\n");
322 event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE);
323 network_close(&(priv.net),gi);
333 int distribute_jobs(t_event *event,void *allineed) {
335 int count_j,count_c,min;
341 list_reset(&(priv.job));
342 list_reset(&(priv.client));
347 if((priv.client.current==NULL)||(priv.job.current==NULL)) return 2;
349 j=(t_job *)priv.job.current->data;
350 c=(t_client *)priv.client.current->data;
351 if(j->status==IN_QUEUE) count_j++;
352 if(c->status==IDLE) count_c++;
354 while(list_next(&(priv.job))!=L_NO_NEXT_ELEMENT) {
355 j=(t_job *)priv.job.current->data;
356 if(j->status==IN_QUEUE) count_j++;
358 while(list_next(&(priv.client))!=L_NO_NEXT_ELEMENT) {
359 c=(t_client *)priv.client.current->data;
360 if(c->status==IDLE) count_c++;
363 min=(count_c<count_j)?count_c:count_j;
366 printf("d: distributing jobs ...\n");
367 printf("%d queued jobs, %d idle clients\n\n",count_j,count_c);
370 list_reset(&(priv.job));
371 list_reset(&(priv.client));
374 j=(t_job *)priv.job.current->data;
375 c=(t_client *)priv.client.current->data;
376 while(c->status!=IDLE) {
377 list_next(&(priv.client));
378 c=(t_client *)priv.client.current->data;
380 while(j->status!=IN_QUEUE) {
381 list_next(&(priv.job));
382 j=(t_job *)priv.job.current->data;
385 /* direct current job to current client */
386 if(j->step==0) data=NLSOP_NJOB;
387 else data=NLSOP_CJOB;
390 j->channel=c->channel;
397 network_send_chan(&(priv.net),c->channel,&data,sizeof(unsigned char));
398 network_send_chan(&(priv.net),c->channel,(unsigned char *)&d3l,
400 network_send_chan(&(priv.net),c->channel,(unsigned char *)&(j->info),
403 if(data==NLSOP_CJOB) {
404 network_send_chan(&(priv.net),c->channel,j->ac,
405 j->size*sizeof(unsigned char));
406 network_receive_chan(&(priv.net),c->channel,&data,sizeof(unsigned char));
407 network_send_chan(&(priv.net),c->channel,(unsigned char *)&(j->cc),
408 j->size*sizeof(int));
409 network_receive_chan(&(priv.net),c->channel,&data,sizeof(unsigned char));
410 network_send_chan(&(priv.net),c->channel,(unsigned char *)&(j->step),
412 network_receive_chan(&(priv.net),c->channel,&data,sizeof(unsigned char));
416 list_next(&(priv.client));
417 list_next(&(priv.job));
423 int parse_incoming(t_event *event,void *allineed) {
425 /* decide what to do */
426 if(FD_ISSET(priv.net.l_fd,&(priv.event.rfds))) {
428 printf("new node ...\n");
432 /* client/gui interaction */
433 printf("node interaction ...\n");
440 void destroy_it(int signum) {
442 printf("connection to client (ch %d) fucked up!\n",gi);
443 event_math(priv.net.connection[gi].fd,&(priv.event),READ,REMOVE);
444 network_close(&(priv.net),gi);
445 if(list_search_data(&(priv.client),&gi,sizeof(int))==L_SUCCESS) {
446 list_del_current(&(priv.client));
447 printf("removed client from list\n");
449 if(list_search_data(&(priv.gui),&gi,sizeof(int))==L_SUCCESS) {
450 list_del_current(&(priv.gui));
451 printf("removed gui from list\n");
463 int main(int argc,char **argv)
472 if(argc==2) port=atoi(argv[1]);
475 event_init(&(priv.event),1);
476 /* 10 sec event timeout - distributing jobs */
477 event_set_timeout(&(priv.event),10,0);
480 list_init(&(priv.client),1);
481 list_init(&(priv.gui),1);
482 list_init(&(priv.job),1);
484 /* connect to server */
485 network_init(&(priv.net),1);
486 network_set_listen_port(&(priv.net),port);
487 if(network_listen(&(priv.net))!=N_SUCCESS) {
488 printf("unable to listen on port %d, aborting!\n",port);
492 /* install sighandler */
493 signal(SIGALRM,destroy_it);
495 /* wait for events :) */
496 event_math(priv.net.l_fd,&(priv.event),READ,ADD);
497 printf("\nNLSOP_SERVER started!\n\n");
498 event_start(&(priv.event),NULL,parse_incoming,distribute_jobs);