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>
51 #include "nlsop_general.h"
56 printf("%s <listen port>\n",prog);
61 * server specific stuff
64 int add_node(t_net *net,t_event *event,t_list *c_list,t_list *g_list) {
71 channel=network_manage_incoming(net);
72 if(channel==N_E_ACCEPT) {
73 printf("accept failed!\n");
76 if(channel==N_E_MAXC) {
77 printf("maximum connections reached!\n");
80 printf("connection from %s port %d (ch: %d)\n",net->connection[channel].ip,
81 net->connection[channel].port,
84 /* are you client or gui? */
85 network_receive_chan(net,channel,&data,1);
88 list_add_element(g_list,&gui_chan,sizeof(int));
90 else if(data==NLSOP_CLIENT) {
92 client.channel=channel;
93 list_add_element(c_list,&client,sizeof(t_client));
96 printf("not a client or gui - lets kick that ass out of here!\n");
97 network_close(net,channel);
101 /* if we have a new node - care for it! */
102 event_math(net->connection[channel].fd,event,READ,ADD);
107 int save_job(t_net *net,int channel,t_job *job,unsigned char dc) {
115 ret=network_receive_chan(net,channel,job->ac,job->size*sizeof(unsigned char));
116 if(ret==N_ERROR) printf("FATAL: getting ac status failed\n");
117 ret=network_receive_chan(net,channel,(unsigned char *)job->cc,
118 job->size*sizeof(int));
119 if(ret==N_ERROR) printf("FATAL: getting cc failed\n");
120 ret=network_receive_chan(net,channel,(unsigned char *)&(job->step),
122 if(ret==N_ERROR) printf("FATAL: getting step number failed\n");
125 snprintf(filename,128,"./data/nlsop_b%f_c%f_s%f_ds%d_dr%f_-_%d_of_%d.save",
126 job->info.b,job->info.c,job->info.s,
127 job->info.diff_rate,job->info.dr_ac,
128 job->step,job->info.steps);
129 if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
130 printf("FATAL: unable to open file %s\n",filename);
134 memset(&d3l,0,sizeof(d3_lattice));
138 if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
139 printf("FATAL: write of d3_lattice failed\n");
143 if(write(fd,&(job->info),sizeof(info))<sizeof(info)) {
144 printf("FATAL: write of info failed\n");
148 ret=write(fd,job->ac,job->size*sizeof(unsigned char));
149 if(ret<job->size*sizeof(unsigned char)) {
150 printf("FATAL: write of a/c states failed\n");
154 ret=write(fd,job->cc,job->size*sizeof(int));
155 if(ret<job->size*sizeof(int)) {
156 printf("FATAL: write of c.-conc. failed\n");
167 int add_job(t_net *net,int chan,t_list *jl) {
175 network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
176 network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
177 network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
178 network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
180 job.size=job.x*job.y*job.z;
182 job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
184 printf("unable to malloc a/c memory\n");
188 job.cc=(int *)malloc(job.size*sizeof(int));
190 printf("unable to malloc cc memory\n");
196 list_add_element(jl,&job,sizeof(t_job));
201 int send_status(t_net *net,int chan,t_list *jl) {
208 count=list_count(jl);
210 network_send_chan(net,chan,&data,sizeof(unsigned char));
211 network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
214 for(i=0;i<count;i++) {
215 network_send_chan(net,chan,jl->current->data,sizeof(t_job));
222 int handle_node(t_net *net,t_event *event,
223 t_list *c_list,t_list *g_list,t_list *job) {
230 for(i=0;i<MAX_CONNECTIONS;i++) {
231 if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
233 if(network_receive_chan(net,i,&data,1)==N_ERROR) {
234 printf("connection to client (ch %d) fucked up!\n",i);
235 event_math(net->connection[i].fd,event,READ,REMOVE);
236 network_close(net,i);
237 list_del_current(c_list);
241 if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
243 list_search_data(job,&i,sizeof(int));
244 j=(t_job *)job->current->data;
245 c=(t_client *)c_list->current-data;
248 save_job(net,i,j,DC_END);
251 /* delete job entry */
252 list_del_current(job);
256 save_job(net,i,j,DC_OK);
257 /* inc progress state */
262 save_job(net,i,j,DC_QUIT);
263 /* network disconnect */
264 event_math(net->connection[i].fd,event,READ,REMOVE);
265 network_close(net,i);
266 /* del from client list */
267 list_del_current(c_list);
268 /* change job state */
273 else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
275 if(data==GUI_ADDJOB) add_job(net,i,job);
277 else if(data==GUI_INFO) send_status(net,i,job);
279 else if(data==GUI_QUIT) {
280 printf("disconnecting gui on channel %d\n",i);
281 event_math(net->connection[i].fd,event,READ,REMOVE);
282 network_close(net,i);
283 list_del_current(g_list);
287 printf("unknown gui command\n");
293 printf("this chan is not in client or gui list! i disconnect now!\n");
294 event_math(net->connection[i].fd,event,READ,REMOVE);
295 network_close(net,i);
303 int distribute_jobs(t_event *event,void *allineed) {
306 t_list *c_list,*g_list,*job;
313 net=(t_net *)allineed;
314 c_list=(t_list *)(allineed+sizeof(t_net));
315 g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list));
316 job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list));
318 count_j=list_count(job);
319 count_c=list_count(c_list);
323 while((count_c!=0)&&(count_j!=0)) {
324 j=(t_job *)job->current->data;
325 c=(t_client *)c_list->current->data;
326 while(c->status!=IDLE) {
328 c=(t_client *)c_list->current->data;
330 while(j->status!=IN_QUEUE) {
332 j=(t_job *)job->current->data;
335 /* direct current job to current client */
336 if(j->step==0) data=NLSOP_NJOB;
337 else data=NLSOP_CJOB;
340 j->channel=c->channel;
347 network_send_chan(net,c->channel,&data,sizeof(unsigned char));
348 network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
349 network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
351 if(data==NLSOP_CJOB) {
352 network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
353 network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
354 j->size*sizeof(int));
366 int parse_incoming(t_event *event,void *allineed) {
369 t_list *c_list,*g_list,*job;
371 net=(t_net *)allineed;
372 c_list=(t_list *)(allineed+sizeof(t_net));
373 g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list));
374 job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list));
376 /* decide what to do */
377 if(FD_ISSET(net->l_fd,&(event->rfds))) {
379 printf("new node ...\n");
380 add_node(net,event,c_list,g_list);
383 /* client/gui interaction */
384 printf("node interaction ...\n");
385 handle_node(net,event,c_list,g_list,job);
395 int main(int argc,char **argv)
407 allyouneed=malloc(sizeof(t_net)+3*sizeof(t_list));
408 memcpy(allyouneed,&net,sizeof(t_net));
409 memcpy(allyouneed+sizeof(t_net),&c_list,sizeof(t_list));
410 memcpy(allyouneed+sizeof(t_net)+sizeof(t_list),&g_list,sizeof(t_list));
411 memcpy(allyouneed+sizeof(t_net)+2*sizeof(t_list),&job,sizeof(t_list));
417 if(argc==2) port=atoi(argv[1]);
420 event_init(&event,1);
421 /* 10 sec event timeout - distributing jobs */
422 event_set_timeout(&event,10,0);
424 /* connect to server */
425 network_init(&net,1);
426 network_set_listen_port(&net,port);
427 if(network_listen(&net)!=N_SUCCESS) {
428 printf("unable to listen on port %d, aborting!\n",port);
432 /* wait for events :) */
433 event_math(net.l_fd,&event,READ,ADD);
434 event_start(&event,allyouneed,parse_incoming,distribute_jobs);