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"
62 * server specific stuff
68 printf("%s <listen port>\n",prog);
72 int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
79 channel=network_manage_incoming(net);
80 if(channel==N_E_ACCEPT) {
81 printf("accept failed!\n");
84 if(channel==N_E_MAXC) {
85 printf("maximum connections reached!\n");
88 printf("connection from %s port %d (ch: %d)\n",net->connection[channel].ip,
89 net->connection[channel].port,
92 /* are you client or gui? */
93 network_receive_chan(net,channel,&data,1);
96 list_add_element(g_list,&gui_chan,sizeof(int));
97 printf("node is a gui\n");
99 else if(data==NLSOP_CLIENT) {
101 client.channel=channel;
102 list_add_element(c_list,&client,sizeof(t_client));
103 printf("node is a client\n");
106 printf("not a client or gui - lets kick that ass out of here!\n");
107 network_close(net,channel);
111 /* if we have a new node - care for it! */
112 event_math(net->connection[channel].fd,event,READ,ADD);
117 int save_job(t_net *net,int channel,t_job *job,unsigned char dc) {
125 ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
126 if(ret==N_ERROR) printf("FATAL: getting ac status failed\n");
127 ret=network_receive_chan(net,channel,(unsigned char *)job->cc,
128 job->size*sizeof(int));
129 if(ret==N_ERROR) printf("FATAL: getting cc failed\n");
130 ret=network_receive_chan(net,channel,(unsigned char *)&(job->step),
132 if(ret==N_ERROR) printf("FATAL: getting step number failed\n");
135 snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
136 job->info.b,job->info.c,job->info.s,
137 job->info.diff_rate,job->info.dr_ac,
138 job->step,job->info.steps);
139 if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
140 printf("FATAL: unable to open file %s\n",filename);
144 memset(&d3l,0,sizeof(d3_lattice));
148 if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
149 printf("FATAL: write of d3_lattice failed\n");
153 if(write(fd,&(job->info),sizeof(info))<sizeof(info)) {
154 printf("FATAL: write of info failed\n");
158 ret=write(fd,job->ac,job->size*sizeof(unsigned char));
159 if(ret<job->size*sizeof(unsigned char)) {
160 printf("FATAL: write of a/c states failed\n");
164 ret=write(fd,job->cc,job->size*sizeof(int));
165 if(ret<job->size*sizeof(int)) {
166 printf("FATAL: write of c.-conc. failed\n");
177 int add_job(t_net *net,int chan,t_list *jl) {
185 network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
186 network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
187 network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
188 network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
190 job.size=job.x*job.y*job.z;
192 job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
194 printf("unable to malloc a/c memory\n");
198 job.cc=(int *)malloc(job.size*sizeof(int));
200 printf("unable to malloc cc memory\n");
206 list_add_element(jl,&job,sizeof(t_job));
211 int send_status(t_net *net,int chan,t_list *jl) {
218 count=list_count(jl);
220 network_send_chan(net,chan,&data,sizeof(unsigned char));
221 network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
224 for(i=0;i<count;i++) {
225 network_send_chan(net,chan,jl->current->data,sizeof(t_job));
232 int handle_node(t_net *net,t_event *event,
233 t_list *c_list,t_list *g_list,t_list *job) {
242 for(i=0;i<MAX_CONNECTIONS;i++) {
243 if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
247 network_receive_chan(net,i,&data,1);
250 if(alert==1) return -1;
252 if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
254 list_search_data(job,&i,sizeof(int));
255 j=(t_job *)job->current->data;
256 c=(t_client *)c_list->current-data;
259 save_job(net,i,j,DC_END);
262 /* delete job entry */
263 list_del_current(job);
267 save_job(net,i,j,DC_OK);
268 /* inc progress state */
273 save_job(net,i,j,DC_QUIT);
274 /* network disconnect */
275 event_math(net->connection[i].fd,event,READ,REMOVE);
276 network_close(net,i);
277 /* del from client list */
278 list_del_current(c_list);
279 /* change job state */
284 else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
286 if(data==GUI_ADDJOB) add_job(net,i,job);
288 else if(data==GUI_INFO) send_status(net,i,job);
290 else if(data==GUI_QUIT) {
291 printf("disconnecting gui on channel %d\n",i);
292 event_math(net->connection[i].fd,event,READ,REMOVE);
293 network_close(net,i);
294 list_del_current(g_list);
298 printf("unknown gui command\n");
304 printf("this chan is not in client or gui list! i disconnect now!\n");
305 event_math(net->connection[i].fd,event,READ,REMOVE);
306 network_close(net,i);
314 int distribute_jobs(t_event *event,void *allineed) {
317 t_list *c_list,*g_list,*job;
323 unsigned int addr[4];
325 memcpy(addr,allineed,4*sizeof(unsigned int));
327 net=(t_net *)addr[0];
328 c_list=(t_list *)addr[1];
329 g_list=(t_list *)addr[2];
330 job=(t_list *)addr[3];
332 count_j=list_count(job);
333 count_c=list_count(c_list);
337 while((count_c!=0)&&(count_j!=0)) {
338 j=(t_job *)job->current->data;
339 c=(t_client *)c_list->current->data;
340 while(c->status!=IDLE) {
342 c=(t_client *)c_list->current->data;
344 while(j->status!=IN_QUEUE) {
346 j=(t_job *)job->current->data;
349 /* direct current job to current client */
350 if(j->step==0) data=NLSOP_NJOB;
351 else data=NLSOP_CJOB;
354 j->channel=c->channel;
361 network_send_chan(net,c->channel,&data,sizeof(unsigned char));
362 network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
363 network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
365 if(data==NLSOP_CJOB) {
366 network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
367 network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
368 j->size*sizeof(int));
380 int parse_incoming(t_event *event,void *allineed) {
383 t_list *c_list,*g_list,*job;
384 unsigned int addr[4];
386 memcpy(addr,allineed,4*sizeof(unsigned int));
388 net=(t_net *)addr[0];
389 c_list=(t_list *)addr[1];
390 g_list=(t_list *)addr[2];
391 job=(t_list *)addr[3];
393 /* decide what to do */
394 if(FD_ISSET(net->l_fd,&(event->rfds))) {
396 printf("new node ...\n");
397 add_node(net,event,c_list,g_list);
400 /* client/gui interaction */
401 printf("node interaction ...\n");
402 handle_node(net,event,c_list,g_list,job);
408 void destroy_it(int signum) {
410 printf("connection to client (ch %d) fucked up!\n",*gi);
411 event_math(gnet->connection[*gi].fd,gevent,READ,REMOVE);
412 network_close(gnet,*gi);
413 list_del_current(gc_list);
424 int main(int argc,char **argv)
434 unsigned int addr[4];
441 allyouneed=(void *)addr;
442 addr[0]=(unsigned int)&net;
443 addr[1]=(unsigned int)&c_list;
444 addr[2]=(unsigned int)&g_list;
445 addr[3]=(unsigned int)&job;
451 if(argc==2) port=atoi(argv[1]);
454 event_init(&event,1);
455 /* 10 sec event timeout - distributing jobs */
456 event_set_timeout(&event,10,0);
459 list_init(&c_list,1);
460 list_init(&g_list,1);
463 /* connect to server */
464 network_init(&net,1);
465 network_set_listen_port(&net,port);
466 if(network_listen(&net)!=N_SUCCESS) {
467 printf("unable to listen on port %d, aborting!\n",port);
471 /* install sighandler */
472 signal(SIGALRM,destroy_it);
474 /* wait for events :) */
475 event_math(net.l_fd,&event,READ,ADD);
476 printf("\nNLSOP_SERVER started!\n\n");
477 event_start(&event,allyouneed,parse_incoming,distribute_jobs);