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,"nlsop_b%f_c%f_s%f_ds%d_dr%f_Z%c__%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->info.z_diff?'y':'n',
129 job->step,job->info.steps);
130 if((fd=open(filename,O_WRONLY|O_CREAT))<0) {
131 printf("FATAL: unable to open file %s\n",filename);
135 memset(&d3l,0,sizeof(d3_lattice));
139 if(write(fd,&d3l,sizeof(d3_lattice))<sizeof(d3_lattice)) {
140 printf("FATAL: write of d3_lattice failed\n");
144 if(write(fd,&(job->info),sizeof(info))<sizeof(info)) {
145 printf("FATAL: write of info failed\n");
149 ret=write(fd,job->ac,job->size*sizeof(unsigned char));
150 if(ret<job->size*sizeof(unsigned char)) {
151 printf("FATAL: write of a/c states failed\n");
155 ret=write(fd,job->cc,job->size*sizeof(int));
156 if(ret<job->size*sizeof(int)) {
157 printf("FATAL: write of c.-conc. failed\n");
168 int add_job(t_net *net,int chan,t_list *jl) {
176 network_receive_chan(net,chan,(unsigned char *)&(job.x),sizeof(int));
177 network_receive_chan(net,chan,(unsigned char *)&(job.y),sizeof(int));
178 network_receive_chan(net,chan,(unsigned char *)&(job.z),sizeof(int));
179 network_receive_chan(net,chan,(unsigned char *)&(job.info),sizeof(info));
181 job.size=job.x*job.y*job.z;
183 job.ac=(unsigned char *)malloc(job.size*sizeof(unsigned char));
185 printf("unable to malloc a/c memory\n");
189 job.cc=(int *)malloc(job.size*sizeof(int));
191 printf("unable to malloc cc memory\n");
197 list_add_element(jl,&job,sizeof(t_job));
202 int send_status(t_net *net,int chan,t_list *jl) {
209 count=list_count(jl);
211 network_send_chan(net,chan,&data,sizeof(unsigned char));
212 network_send_chan(net,chan,(unsigned char *)&count,sizeof(int));
215 for(i=0;i<count;i++) {
216 network_send_chan(net,chan,jl->current->data,sizeof(t_job));
223 int handle_node(t_net *net,t_event *event,
224 t_list *c_list,t_list *g_list,t_list *job) {
231 for(i=0;i<MAX_CONNECTIONS;i++) {
232 if(FD_ISSET(net->connection[i].fd,&(event->rfds))) {
234 if(network_receive_chan(net,i,&data,1)==N_ERROR) {
235 printf("connection to client (ch %d) fucked up!\n",i);
236 event_math(net->connection[i].fd,event,READ,REMOVE);
237 network_close(net,i);
238 list_del_current(c_list);
242 if(list_search_data(c_list,&i,sizeof(int))==L_SUCCESS) {
244 list_search_data(job,&i,sizeof(int));
245 j=(t_job *)job->current->data;
246 c=(t_client *)c_list->current-data;
249 save_job(net,i,j,DC_END);
252 /* delete job entry */
253 list_del_current(job);
257 save_job(net,i,j,DC_OK);
258 /* inc progress state */
263 save_job(net,i,j,DC_QUIT);
264 /* network disconnect */
265 event_math(net->connection[i].fd,event,READ,REMOVE);
266 network_close(net,i);
267 /* del from client list */
268 list_del_current(c_list);
269 /* change job state */
274 else if(list_search_data(g_list,&i,sizeof(int))==L_SUCCESS) {
276 if(data==GUI_ADDJOB) add_job(net,i,job);
278 else if(data==GUI_INFO) send_status(net,i,job);
280 else if(data==GUI_QUIT) {
281 printf("disconnecting gui on channel %d\n",i);
282 event_math(net->connection[i].fd,event,READ,REMOVE);
283 network_close(net,i);
284 list_del_current(g_list);
288 printf("unknown gui command\n");
294 printf("this chan is not in client or gui list! i disconnect now!\n");
295 event_math(net->connection[i].fd,event,READ,REMOVE);
296 network_close(net,i);
304 int distribute_jobs(t_event *event,void *allineed) {
307 t_list *c_list,*g_list,*job;
314 net=(t_net *)allineed;
315 c_list=(t_list *)(allineed+sizeof(t_net));
316 g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list));
317 job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list));
319 count_j=list_count(job);
320 count_c=list_count(c_list);
324 while((count_c!=0)&&(count_j!=0)) {
325 j=(t_job *)job->current->data;
326 c=(t_client *)c_list->current->data;
327 while(c->status!=IDLE) {
329 c=(t_client *)c_list->current->data;
331 while(j->status!=IN_QUEUE) {
333 j=(t_job *)job->current->data;
336 /* direct current job to current client */
337 if(j->step==0) data=NLSOP_NJOB;
338 else data=NLSOP_CJOB;
341 j->channel=c->channel;
348 network_send_chan(net,c->channel,&data,sizeof(unsigned char));
349 network_send_chan(net,c->channel,(unsigned char *)&d3l,sizeof(d3_lattice));
350 network_send_chan(net,c->channel,(unsigned char *)&(j->info),sizeof(info));
352 if(data==NLSOP_CJOB) {
353 network_send_chan(net,c->channel,j->ac,j->size*sizeof(unsigned char));
354 network_send_chan(net,c->channel,(unsigned char *)&(j->cc),
355 j->size*sizeof(int));
367 int parse_incoming(t_event *event,void *allineed) {
370 t_list *c_list,*g_list,*job;
372 net=(t_net *)allineed;
373 c_list=(t_list *)(allineed+sizeof(t_net));
374 g_list=(t_list *)(allineed+sizeof(t_net)+sizeof(t_list));
375 job=(t_list *)(allineed+sizeof(t_net)+2*sizeof(t_list));
377 /* decide what to do */
378 if(FD_ISSET(net->l_fd,&(event->rfds))) {
380 printf("new node ...\n");
381 add_node(net,event,c_list,g_list);
384 /* client/gui interaction */
385 printf("node interaction ...\n");
386 handle_node(net,event,c_list,g_list,job);
396 int main(int argc,char **argv)
408 allyouneed=malloc(sizeof(t_net)+3*sizeof(t_list));
409 memcpy(allyouneed,&net,sizeof(t_net));
410 memcpy(allyouneed+sizeof(t_net),&c_list,sizeof(t_list));
411 memcpy(allyouneed+sizeof(t_net)+sizeof(t_list),&g_list,sizeof(t_list));
412 memcpy(allyouneed+sizeof(t_net)+2*sizeof(t_list),&job,sizeof(t_list));
418 if(argc==2) port=atoi(argv[1]);
421 event_init(&event,1);
422 /* 10 sec event timeout - distributing jobs */
423 event_set_timeout(&event,10,0);
425 /* connect to server */
426 network_init(&net,1);
427 network_set_listen_port(&net,port);
428 if(network_listen(&net)!=N_SUCCESS) {
429 printf("unable to listen on port %d, aborting!\n",port);
433 /* wait for events :) */
434 event_math(net.l_fd,&event,READ,ADD);
435 event_start(&event,allyouneed,parse_incoming,distribute_jobs);