PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/source/DR_client_comm.cpp

http://github.com/graceli/DRsample
C++ | 775 lines | 591 code | 91 blank | 93 comment | 56 complexity | f619f97f9648347d5960bc6fa6a833cd MD5 | raw file
  1. /*
  2. * This file is part of Distributed Replica.
  3. * Copyright May 9 2009
  4. *
  5. * Distributed Replica manages a series of simulations that separately sample phase space
  6. * and coordinates their efforts under the Distributed Replica Potential Energy Function.
  7. * See, for example T. Rodinger, P.L. Howell, and R. Pomès, "Distributed Replica Sampling"
  8. * J. Chem. Theory Comput., 2:725 (2006).
  9. *
  10. * Distributed Replica is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * Distributed Replica is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Distributed Replica. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /*
  24. Chris Neale Oct 2 2007
  25. - Added ability to move additional files like the job.force file -- this currently does not impact the forcedatabase
  26. - SendBinFile now returns an integer to flag if the file was not found for use with additional_data files
  27. Chris Neale June 6 2008
  28. - Added ability to deal with NNI!=1
  29. Chris Neale June 6 2008
  30. - Added free() statements for all mallocs
  31. */
  32. #include <time.h>
  33. #include <sys/time.h>
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <math.h>
  37. #include <unistd.h>
  38. #include <errno.h>
  39. #include <string.h>
  40. #include <netdb.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/stat.h>
  44. #include <fcntl.h>
  45. #include <ctype.h>
  46. #include <arpa/inet.h>
  47. #include <fstream>
  48. #include <string>
  49. #include <zlib.h>
  50. #include <netinet/in.h>
  51. #if defined(__ICC)
  52. // icc doesn't like GNU __extension__ functions
  53. // this has to happen AFTER !!
  54. #undef htons
  55. #undef ntohs
  56. #undef htonl
  57. #undef ntohl
  58. #define htons(x) __bswap_constant_16(x)
  59. #define ntohs(x) __bswap_constant_16(x)
  60. #define htonl(x) __bswap_constant_32(x)
  61. #define ntohl(x) __bswap_constant_32(x)
  62. #endif // defined(_ICC)
  63. #include "DR_protocol.h"
  64. #include "read_input_script_file.h" //this means that it requires math.h (that header has NAN)
  65. #include "string_double.h"
  66. #define IDSIZE sizeof(struct ID_struct)
  67. #define INTSIZE sizeof(int)
  68. #define COMPRESS_RESTART true
  69. unsigned int do_compress2(int ifd, char *ocp);
  70. void do_uncompress2(int total, char *icp, int ofd);
  71. void sendFile(int sockfd, char *filename, enum command_enum command, bool compress);
  72. int sendBinFile(int sockfd, char *filename, enum command_enum command);
  73. void sendCrdFile(int sockfd, char *filename, enum command_enum command);
  74. void sendJID(int sockfd, float jid);
  75. void sendTCS(int sockfd, float tcs);
  76. void sendReplicaID(int sockfd, struct ID_struct ID);
  77. void receiveReplicaID(int sockfd, struct ID_struct *ID);
  78. void send_NextNonInteracting(int sockfd);
  79. enum command_enum readCommand(int socketfd);
  80. void receiveFile(int sockfd, char *fileName, int fileSize);
  81. void receiveFileUncompressed(int sockfd, char *fileName, int fileSize);
  82. void receiveParCHARMM(int sockfd, struct ID_struct *ID, int fileSize);
  83. void read4K(int sockfd, void *buff, int nbytes);
  84. using namespace std;
  85. void showUsage(const char *c){
  86. fprintf(stderr,"Usage: %s IP-address port replicaIDcode time-client-started job-id\n",c);
  87. fprintf(stderr,"OR: %s IP-address port '**' time-client-started job-id\n",c);
  88. fprintf(stderr," * time-client-started is manditory, but is only used with a mobile server (send 0 if you don't care)\n");
  89. fprintf(stderr," the time should be given in seconds since January 1, 1970.\n");
  90. fprintf(stderr," if you send <=0 then the server will track times internally (sub-optimal for mobile server).\n");
  91. fprintf(stderr," * job-id is manditory, but is only used for tracking (send 0 if you don't know the job ID on the client)\n");
  92. }
  93. int main(int argc, char *argv[]){
  94. int sockfd;
  95. int jid,tcs;
  96. struct sockaddr_in their_addr; // connector's address information
  97. if (argc != 6) {
  98. showUsage(argv[0]);
  99. exit(1);
  100. }
  101. sscanf(argv[4],"%d",&tcs);
  102. sscanf(argv[5],"%d",&jid);
  103. if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  104. perror("socket");
  105. exit(1);
  106. }
  107. their_addr.sin_family = AF_INET; // host byte order
  108. their_addr.sin_addr.s_addr = inet_addr(argv[1]);
  109. their_addr.sin_port = htons(atoi(argv[2])); // short, network byte order
  110. memset(&(their_addr.sin_zero), '\0', 8); // zero the rest of the struct
  111. if(connect(sockfd, (struct sockaddr *)&their_addr, sizeof(struct sockaddr)) == -1) {
  112. perror("connect");
  113. exit(1);
  114. }
  115. /************************************************************************/
  116. struct ID_struct ID;
  117. unsigned int baseNameLength=strlen(argv[3]);
  118. char rstFileName[64];
  119. char energyFileName[64];
  120. char forceFileName[64];
  121. char addFileName[64];
  122. char crdFileName[64];
  123. char tmpName[64];
  124. char IDbaseName[60];
  125. char IDbaseName2[60]; //decrease sequence_number by 1 for restart file
  126. char IDrstFileName[80];
  127. char IDenergyFileName[80];
  128. char IDforceFileName[80];
  129. //char IDsetupFileName[80];
  130. unsigned int protocol_version=PROTOCOL_VERSION;
  131. int old_replica_number=-1;
  132. int new_replica_number=-1;
  133. int addfile,sbf;
  134. char saveArgv3[50];
  135. int nni;
  136. write(sockfd,&protocol_version,PROTOCOL_VERSION_SIZE); // first thing we do is send the protocol version we are using
  137. if(argv[3][0]=='*' && argv[3][1]=='*'){
  138. ID.title[0]=ID.title[1]='*';
  139. sendTCS(sockfd,(float)tcs);
  140. sendJID(sockfd,(float)jid);
  141. sendReplicaID(sockfd,ID);
  142. }else{
  143. //sendID
  144. if(argv[3][2]!='w' && argv[3][2]!='W' ){ fprintf(stderr,"wrong id\n"); exit(1); }
  145. for(int i=0; i<3; ++i)
  146. if(isupper(argv[3][i])) (argv[3][i])+=('a'-'A');
  147. strcpy(rstFileName,argv[3]);
  148. strcpy(energyFileName,argv[3]);
  149. strcpy(forceFileName,argv[3]);
  150. strcpy(crdFileName,argv[3]);
  151. strcat(rstFileName,".rst");
  152. strcat(energyFileName,".energy");
  153. strcat(forceFileName,".force");
  154. strcat(crdFileName,".crd");
  155. strcpy(saveArgv3,argv[3]); //keep it for iterations with addFileName
  156. argv[3][2]=' ';
  157. int len=strlen(argv[3]);
  158. for(int i=3; i<len; ++i){
  159. if(argv[3][i]=='.') argv[3][i]=' ';
  160. }
  161. sscanf(argv[3],"%s%d%u",ID.title,&ID.replica_number,&ID.sequence_number);
  162. old_replica_number=new_replica_number=ID.replica_number;
  163. /*
  164. //CN removed this as it is unexpected
  165. if(ID.sequence_number>0){
  166. char old_RST_filename[60];
  167. sprintf(old_RST_filename,"%sw%d.%u.rst",ID.title,ID.replica_number,ID.sequence_number-1);
  168. fprintf(stderr,"deleting old RST file [%s]\n",old_RST_filename); //##DEBUG
  169. int e=unlink(old_RST_filename); // if we are receiving a new RST file then delete old one
  170. if(e!=0) fprintf(stderr,"delete failed with errno %d\n",errno); //##DEBUG
  171. }
  172. */
  173. fprintf(stderr,"sending ID\n"); //##DEBUG
  174. sendReplicaID(sockfd,ID); //send ReplicaID
  175. fprintf(stderr,"ID sent\n"); //##DEBUG
  176. sendTCS(sockfd,(float)tcs);
  177. sendJID(sockfd,(float)jid);
  178. for(nni=1;;nni++){
  179. sprintf(tmpName,"%s.nni%d",energyFileName,nni);
  180. fprintf(stderr,"sending energy file\n"); //##DEBUG
  181. sbf=sendBinFile(sockfd,tmpName,TakeMoveEnergyData); //send move_energy_data
  182. if(sbf!=0){
  183. //If the file did not exist then assume that all is well.
  184. fprintf(stderr," * Above file does not exist, assuming end of non-interacting systems, this is not an error message.\n"); //##DEBUG
  185. break;
  186. }
  187. fprintf(stderr,"energy file sent\n"); //##DEBUG
  188. sprintf(tmpName,"%s.nni%d",forceFileName,nni);
  189. fprintf(stderr,"sending force file\n"); //##DEBUG
  190. sendBinFile(sockfd,tmpName,TakeSampleData); //send SampleData, force file
  191. fprintf(stderr,"force file sent\n"); //##DEBUG
  192. //It is absolutely essential that the sample data is sent before additional data
  193. //This loop will break when a numbered file des not exist
  194. for(addfile=1;;addfile++){
  195. sprintf(tmpName,"%s.add%d.nni%d",saveArgv3,addfile,nni);
  196. fprintf(stderr,"sending additional file %s\n",tmpName); //##DEBUG
  197. sbf=sendBinFile(sockfd,tmpName,TakeSampleData); //send additional data file(s)
  198. if(sbf!=0){
  199. //If the file did not exist then assume that all is well.
  200. fprintf(stderr," * Above file does not exist, assuming end of additional data, this is not an error message.\n"); //##DEBUG
  201. break;
  202. }
  203. fprintf(stderr,"additional file %s sent\n",tmpName); //##DEBUG
  204. }
  205. fprintf(stderr,"sending crd file\n"); //##DEBUG
  206. sprintf(tmpName,"%s.nni%d",crdFileName,nni);
  207. sendCrdFile(sockfd,tmpName,TakeCoordinateData); //send CoordinateData, force crd file
  208. fprintf(stderr,"crd file sent\n"); //##DEBUG
  209. fprintf(stderr,"sending rst file\n"); //##DEBUG
  210. if(nni==1){
  211. sendFile(sockfd,rstFileName,TakeRestartFile,COMPRESS_RESTART); //send restart file
  212. fprintf(stderr,"rst file sent\n"); //##DEBUG
  213. }else{
  214. //send indication of next NNI
  215. send_NextNonInteracting(sockfd); //line added by CN August 6 2008
  216. }
  217. //send TakeThisFile
  218. //sendFile(sockfd,"test.123",TakeThisFile,false);
  219. }
  220. }
  221. enum command_enum command;
  222. bool done=false;
  223. char oneKbuff[1024];
  224. int readFileSize,oneK=1024;
  225. //command_enum {ReplicaID, TakeThisFile, TakeRestartFile, TakeSampleData, TakeMoveEnergyData, TakeSimulationParameters, TakeCoordinateData, TakeTCS, TakeJID, NextNonInteracting, Exit, Snapshot, InvalidCommand};
  226. while(!done){
  227. fprintf(stderr,"trying to get a command\n"); //##DEBUG
  228. command=readCommand(sockfd);
  229. fprintf(stderr,"received a command: %hhu\n",command); fflush(stderr);//##DEBUG
  230. switch(command){
  231. case ReplicaID:
  232. fprintf(stderr,"received RepicaID command\n"); //##DEBUG
  233. receiveReplicaID(sockfd,&ID);
  234. new_replica_number=ID.replica_number;
  235. sprintf(IDbaseName,"%sw%d.%u",ID.title,ID.replica_number,ID.sequence_number);
  236. sprintf(IDenergyFileName,"%s.energy.nni%d",IDbaseName,nni);
  237. sprintf(IDforceFileName,"%s.force.nni%d",IDbaseName,nni);
  238. //sprintf(IDsetupFileName,"%s.setup",IDbaseName);
  239. sprintf(IDbaseName2,"%sw%d.%u",ID.title,ID.replica_number,ID.sequence_number-1);
  240. sprintf(IDrstFileName,"%s.rst",IDbaseName2); //CN removed a tailing .nni%d -- don't need .nni there for .rst he thinks -- also, even if we did, the , nni); was not there so it was a random int
  241. printf("%s\n",IDbaseName);
  242. //fprintf(stderr,"IDbaseName=%s\n",IDbaseName); //##DEBUG
  243. //fprintf(stderr,"IDbaseName2=%s\n",IDbaseName2); //##DEBUG
  244. //fprintf(stderr,"IDrstFileName=%s\n",IDrstFileName); //##DEBUG
  245. break;
  246. case TakeThisFile: //uncompressed, untested
  247. int total,count,ofd,fnSize;
  248. fprintf(stderr,"received TakeThisFile command\n"); //##DEBUG
  249. read4K(sockfd,&total,INTSIZE);
  250. count=total<oneK?total:oneK;
  251. read4K(sockfd,oneKbuff,count);
  252. fnSize=strlen(oneKbuff)+1;
  253. ofd=open(oneKbuff,O_WRONLY|O_CREAT|O_TRUNC,0644);
  254. if(ofd==-1){
  255. fprintf(stderr,"cannot open %s\n",oneKbuff);
  256. exit(1);
  257. }
  258. write(ofd,oneKbuff+fnSize,count-fnSize);
  259. if(total-=count>0){
  260. char tbuf[total];
  261. read4K(sockfd,tbuf,total);
  262. write(ofd,tbuf,total);
  263. }
  264. close(ofd);
  265. break;
  266. case TakeRestartFile: //compressed
  267. fprintf(stderr,"received TakeRestartFile command\n"); //##DEBUG
  268. new_replica_number=-1;
  269. read4K(sockfd,&readFileSize,INTSIZE);
  270. if(COMPRESS_RESTART){
  271. receiveFile(sockfd,IDrstFileName,readFileSize);
  272. }else{
  273. receiveFileUncompressed(sockfd,IDrstFileName,readFileSize);
  274. }
  275. fprintf(stderr,"wrote restart file to file [%s]\n",IDrstFileName); //##DEBUG
  276. break;
  277. //case TakeSampleData:
  278. //break;
  279. case TakeMoveEnergyData: //uncompressed
  280. read4K(sockfd,&readFileSize,INTSIZE);
  281. receiveFileUncompressed(sockfd,IDenergyFileName,readFileSize);
  282. break;
  283. case TakeSimulationParameters:
  284. read4K(sockfd,&readFileSize,INTSIZE);
  285. //receiveFileUncompressed(sockfd,IDsetupFileName,readFileSize);
  286. receiveParCHARMM(sockfd,&ID,readFileSize);
  287. done=true;
  288. break;
  289. //case TakeCoordinateData:
  290. //break;
  291. default:
  292. fprintf(stderr,"received an unexpected command\n"); //##DEBUG
  293. done=true;
  294. break;
  295. }
  296. }
  297. fprintf(stderr,"done is true, exiting loop\n"); //##DEBUG
  298. /*
  299. //CN removed this as it is unexpected
  300. if( (new_replica_number!=old_replica_number) && (old_replica_number!=-1) ){
  301. fprintf(stderr,"deleting old RST file [%s]\n",rstFileName); //##DEBUG
  302. int e=unlink(rstFileName); // if we are receiving a new RST file then delete old one
  303. if(e!=0) fprintf(stderr,"delete failed with errno %d\n",errno); //##DEBUG
  304. }
  305. */
  306. close(sockfd);
  307. fprintf(stderr,"socket closed; helper finished\n\n\n"); //##DEBUG
  308. return 0;
  309. }
  310. void sendJID(int sockfd, float jid){
  311. //send jid as a float since there are already routines to handle floats in the server
  312. int sz1,sz2;
  313. unsigned int jidsize;
  314. sz1=KEY_SIZE+COMMAND_SIZE;
  315. jidsize=sizeof(jid);
  316. sz2=sz1+INTSIZE+jidsize;
  317. char cmd[sz2];
  318. memcpy(cmd,COMMAND_KEY,KEY_SIZE);
  319. cmd[COMMAND_LOCATION]=TakeJID;
  320. memcpy(cmd+sz1,&jidsize,INTSIZE);
  321. memcpy(cmd+sz1+INTSIZE,&jid,jidsize);
  322. int n=write(sockfd,cmd,sz2);
  323. fprintf(stderr,"sz2=%d wrote JID %d byte to socket %d\n",sz2,n,sockfd); //##DEBUG
  324. }
  325. void sendTCS(int sockfd, float tcs){
  326. //send tcs as a float since there are already routines to handle floats in the server
  327. int sz1,sz2;
  328. unsigned int tcssize;
  329. sz1=KEY_SIZE+COMMAND_SIZE;
  330. tcssize=sizeof(tcs);
  331. sz2=sz1+INTSIZE+tcssize;
  332. char cmd[sz2];
  333. memcpy(cmd,COMMAND_KEY,KEY_SIZE);
  334. cmd[COMMAND_LOCATION]=TakeTCS;
  335. memcpy(cmd+sz1,&tcssize,INTSIZE);
  336. memcpy(cmd+sz1+INTSIZE,&tcs,tcssize);
  337. int n=write(sockfd,cmd,sz2);
  338. fprintf(stderr,"sz2=%d wrote TCS %d byte to socket %d\n",sz2,n,sockfd); //##DEBUG
  339. }
  340. void sendReplicaID(int sockfd, struct ID_struct ID){
  341. int sz1,sz2;
  342. sz1=KEY_SIZE+COMMAND_SIZE;
  343. sz2=sz1+IDSIZE;
  344. char cmd[sz2];
  345. memcpy(cmd,COMMAND_KEY,KEY_SIZE);
  346. cmd[COMMAND_LOCATION]=ReplicaID;
  347. memcpy(cmd+sz1,&ID,IDSIZE);
  348. int n=write(sockfd,cmd,sz2);
  349. fprintf(stderr,"sz2=%d wrote ID %d byte to socket %d\n",sz2,n,sockfd); //##DEBUG
  350. }
  351. void receiveReplicaID(int sockfd, struct ID_struct *ID){
  352. read4K(sockfd,ID,IDSIZE);
  353. }
  354. void send_NextNonInteracting(int sockfd){
  355. unsigned int send_size;
  356. unsigned char buff[KEY_SIZE+COMMAND_SIZE];
  357. memcpy(buff,COMMAND_KEY,KEY_SIZE);
  358. buff[COMMAND_LOCATION]=NextNonInteracting;
  359. send_size=KEY_SIZE+COMMAND_SIZE;
  360. if(write(sockfd,buff,send_size)!=send_size){
  361. fprintf(stderr,"Error: cannot send the identifier for NextNonInteracting\n");
  362. //Do not exit as the server will take care of erroring out
  363. }
  364. }
  365. void sendCrdFile(int sockfd, char *filename, enum command_enum command){
  366. ifstream inp(filename);
  367. if(!inp.is_open()) return; // line added by T. Rodinger
  368. string s;
  369. while(inp>>s){
  370. if(s[0]=='*')
  371. getline(inp,s,'\n');
  372. else
  373. break;
  374. }
  375. int natom((int)string_double(s));
  376. float xyz[natom*3];
  377. int n=0;
  378. while(inp>>s>>s>>s>>s){
  379. inp>>xyz[n++]>>xyz[n++]>>xyz[n++];
  380. inp>>s>>s>>s;
  381. }
  382. unsigned int fileSize=0;
  383. fileSize=natom*3*sizeof(float);
  384. int sz1,sz1a,sz2;
  385. sz1=KEY_SIZE+COMMAND_SIZE;
  386. sz1a=INTSIZE; //fileSize
  387. sz2=sz1+sz1a;
  388. char cmd[sz2];
  389. memcpy(cmd,COMMAND_KEY,KEY_SIZE);
  390. cmd[COMMAND_LOCATION]=command;
  391. memcpy(cmd+sz1,&fileSize,sz1a);
  392. // if(fileSize==0) while(1) sleep(10);
  393. write(sockfd,cmd,sz2);
  394. write(sockfd,xyz,fileSize);
  395. inp.close();
  396. }
  397. int sendBinFile(int sockfd, char *filename, enum command_enum command){
  398. unsigned int fileSize=0;
  399. FILE *fp;
  400. float value;
  401. int line=0;
  402. char buff[100]; // added by T. Rodinger
  403. int i;
  404. // if((fp=fopen(filename,"r"))==NULL){ fprintf(stderr,"cannot open %s\n",filename); exit(1); }
  405. if((fp=fopen(filename,"r"))==NULL) return 1; // the original line above was modified by T. Rodinger
  406. char *substring;
  407. i=0;
  408. // while loop and contents added by T. Rodinger
  409. while(!feof(fp)){
  410. if(fgets(buff, 99, fp)==NULL) continue;
  411. buff[strlen(buff)-1]=0;
  412. substring=strtok(buff," ");
  413. while(substring!=NULL){
  414. substring=strtok(NULL, " ");
  415. i++;
  416. }
  417. }
  418. // fprintf(stderr,"%d data items found in the force file\n",i); //##DEBUG
  419. fileSize=i*sizeof(float);
  420. rewind(fp);
  421. float allV[i];
  422. char *string_end;
  423. i=0;
  424. // while loop and contents added by T. Rodinger
  425. while(!feof(fp)){
  426. if(fgets(buff, 99, fp)==NULL) continue;
  427. buff[strlen(buff)-1]=0;
  428. // fprintf(stderr,"Got line: [%s]\n",buff); //##DEBUG
  429. substring=strtok(buff," ");
  430. while(substring!=NULL){
  431. // fprintf(stderr,"Got substring: [%s]\n",substring); //##DEBUG
  432. allV[i]=strtod(substring,&string_end);
  433. if(string_end==substring) allV[i]=1.0e20;
  434. substring=strtok(NULL, " ");
  435. i++;
  436. }
  437. }
  438. int sz1,sz1a,sz2;
  439. sz1=KEY_SIZE+COMMAND_SIZE;
  440. sz1a=INTSIZE; //fileSize
  441. sz2=sz1+sz1a;
  442. char cmd[sz2];
  443. memcpy(cmd,COMMAND_KEY,KEY_SIZE);
  444. cmd[COMMAND_LOCATION]=command;
  445. memcpy(cmd+sz1,&fileSize,sz1a);
  446. // if(fileSize==0) while(1) sleep(10);
  447. write(sockfd,cmd,sz2);
  448. write(sockfd,allV,fileSize);
  449. fclose(fp);
  450. return 0;
  451. }
  452. void sendFile(int sockfd, char *filename, enum command_enum command, bool compress){
  453. int fd;
  454. unsigned int fileSize=0;
  455. fd=open(filename,O_RDONLY);
  456. if(fd==-1){
  457. fprintf(stderr,"cannot open %s\n",filename);
  458. exit(1);
  459. }
  460. int fullSize=lseek(fd,0,SEEK_END);
  461. lseek(fd,0,SEEK_SET);
  462. char buffer[fullSize];
  463. if(compress){
  464. fileSize=do_compress2(fd,buffer);
  465. }else{
  466. fileSize=fullSize;
  467. read4K(fd,buffer,fileSize);
  468. }
  469. int sz1,sz1a,sz2,sz2a=0;
  470. sz1=KEY_SIZE+COMMAND_SIZE;
  471. sz1a=sizeof(unsigned int); //fileSize
  472. sz2=sz1+sz1a;
  473. if(command==TakeThisFile){
  474. sz2a=strlen(filename);
  475. sz2+=(sz2a+1);
  476. }
  477. char cmd[sz2];
  478. memcpy(cmd,COMMAND_KEY,KEY_SIZE);
  479. cmd[COMMAND_LOCATION]=command;
  480. //memcpy(cmd+sz1,&fileSize,sz1a);
  481. if(command==TakeThisFile){
  482. unsigned int tmp=fileSize;
  483. tmp+=(sz2a+1);
  484. memcpy(cmd+sz1,&tmp,sz1a);
  485. memcpy(cmd+sz1+sz1a,filename,sz2a);
  486. char c=0;
  487. memcpy(cmd+sz2-1,&c,1);
  488. write(sockfd,cmd,sz2);
  489. }else{
  490. memcpy(cmd+sz1,&fileSize,sz1a);
  491. write(sockfd,cmd,sz2);
  492. }
  493. // if(fileSize==0) while(1) sleep(10);
  494. write(sockfd,buffer,fileSize);
  495. int fd1; //##DEBUG
  496. fd1=open("RESTART",O_WRONLY|O_CREAT|O_TRUNC,0644); //##DEBUG
  497. write(fd1,buffer,fileSize); //##DEBUG
  498. close(fd1); //##DEBUG
  499. fprintf(stderr,"********************** WROTE RESTART FILE **********************\n"); //##DEBUG
  500. close(fd);
  501. }
  502. enum command_enum readCommand(int sockfd){
  503. char buff[KEY_SIZE+COMMAND_SIZE];
  504. read4K(sockfd,buff,KEY_SIZE+COMMAND_SIZE);
  505. if( strncmp(buff+KEY_LOCATION,COMMAND_KEY,KEY_SIZE)!=0 ){
  506. fprintf(stderr,"the key did not match\n"); //##DEBUG
  507. exit(1);
  508. }
  509. return (enum command_enum)buff[COMMAND_LOCATION];
  510. }
  511. void receiveFile(int sockfd, char *fileName, int fileSize){
  512. char buffer[fileSize];
  513. int fd;
  514. fd=open(fileName,O_WRONLY|O_CREAT|O_TRUNC,0644);
  515. if(fd==-1){
  516. fprintf(stderr,"cannot open %s\n",fileName);
  517. exit(1);
  518. }
  519. read4K(sockfd,buffer,fileSize);
  520. fprintf(stderr,"received file, size is: %d\n",fileSize); //##DEBUG
  521. do_uncompress2(fileSize,buffer,fd);
  522. close(fd);
  523. }
  524. void receiveFileUncompressed(int sockfd, char *fileName, int fileSize){
  525. char buffer[fileSize];
  526. int fd;
  527. fd=open(fileName,O_WRONLY|O_CREAT|O_TRUNC,0644);
  528. if(fd==-1){
  529. fprintf(stderr,"cannot open %s\n",fileName);
  530. exit(1);
  531. }
  532. read4K(sockfd,buffer,fileSize);
  533. fprintf(stderr,"received file, size is: %d\n",fileSize); //##DEBUG
  534. write(fd,buffer,fileSize);
  535. fprintf(stderr,"file written to disk\n"); //##DEBUG
  536. close(fd);
  537. }
  538. char *stringCat(char *d, const char *s){
  539. int len=strlen(s);
  540. for(int i=0; i<len; ++i)
  541. *(d++)=*(s++);
  542. return d;
  543. }
  544. void receiveParCHARMM(int sockfd, struct ID_struct *ID, int fileSize){
  545. char base1[20];
  546. char base2[60];
  547. sprintf(base1,"%sw%d.",ID->title,ID->replica_number);
  548. sprintf(base2,"%s%u",base1,ID->sequence_number);
  549. char fileName[]="setup";
  550. char buffer[fileSize];
  551. int fd;
  552. fd=open(fileName,O_WRONLY|O_CREAT|O_TRUNC,0644);
  553. if(fd==-1){
  554. fprintf(stderr,"cannot open %s\n",fileName);
  555. exit(1);
  556. }
  557. read4K(sockfd,buffer,fileSize);
  558. //fprintf(stderr,"start*****************************************\n"); //##DEBUG
  559. //buffer[fileSize]=0; //##DEBUG
  560. //fprintf(stderr,"%s\n",buffer); //##DEBUG
  561. //fprintf(stderr,"end*******************************************\n"); //##DEBUG
  562. char buff2[2048];
  563. char *c;
  564. c=buff2;
  565. c=stringCat(c,"set ");
  566. for(int i=0; i<fileSize; ++i){
  567. *(c++)=buffer[i];
  568. if(buffer[i]=='\n'){
  569. c=stringCat(c,"set ");
  570. }
  571. }
  572. if(ID->sequence_number==0){
  573. c=stringCat(c,"iob -1\n");
  574. }else{
  575. char num[40];
  576. sprintf(num,"%d",ID->sequence_number-1);
  577. c=stringCat(c,"iob \"");
  578. c=stringCat(c,base1);
  579. c=stringCat(c,num);
  580. c=stringCat(c,"\"\n");
  581. }
  582. c=stringCat(c,"set job \"");
  583. c=stringCat(c,base2);
  584. c=stringCat(c,"\"\n");
  585. *c=0;
  586. int len=strlen(buff2);
  587. if(len>2040) fprintf(stderr,"Warning: setup file may be too big.\n"); //##DEBUG
  588. write(fd,buff2,strlen(buff2));
  589. fprintf(stderr,"PARAMETER FILE:\n %s\n",buff2); //##DEBUG
  590. close(fd);
  591. }
  592. void read4K(int sockfd, void *buff, int nbytes){
  593. int count,fourK=4096,offset=0;
  594. int iread,total=nbytes;
  595. while(nbytes>0){
  596. count=nbytes<fourK?nbytes:fourK;
  597. //fprintf(stderr,"attempting to read %d bytes\n",count); //##DEBUG
  598. iread=read(sockfd,(char *)buff+offset,count);
  599. //fprintf(stderr,"bytes actually read: %d\n",iread); //##DEBUG
  600. if(iread==0){
  601. fprintf(stderr,"read4K: read return 0, expecting %d more bytes\n",nbytes);
  602. exit(1);
  603. }else if(iread<0){
  604. fprintf(stderr,"read4K: read return -1\n");
  605. perror("read");
  606. exit(1);
  607. }
  608. nbytes-=iread;
  609. offset+=iread;
  610. }
  611. }
  612. #define CHUNK 4096
  613. unsigned int do_compress2(int ifd, char *ocp){
  614. z_stream z;
  615. char input_buffer[CHUNK];
  616. char output_buffer[CHUNK];
  617. z.avail_in = 0;
  618. z.next_out = (Bytef*)output_buffer;
  619. z.avail_out = CHUNK;
  620. z.zalloc = 0;
  621. z.zfree = 0;
  622. z.opaque = 0;
  623. deflateInit(&z,5);
  624. int i,count;
  625. unsigned int total=0;
  626. for(;;){
  627. if(z.avail_in == 0){
  628. z.next_in = (Bytef*)input_buffer;
  629. z.avail_in = read(ifd,input_buffer,sizeof(input_buffer));
  630. }
  631. if(z.avail_in == 0) break;
  632. deflate( &z, Z_SYNC_FLUSH );
  633. count = CHUNK - z.avail_out;
  634. total+=count;
  635. i=0;
  636. while(count--){
  637. *(ocp++)=output_buffer[i++];
  638. }
  639. z.next_out = (Bytef*)output_buffer;
  640. z.avail_out = CHUNK;
  641. }
  642. deflateEnd(&z);
  643. return total;
  644. }
  645. void do_uncompress2(int total, char *icp, int ofd){
  646. z_stream z;
  647. char input_buffer[CHUNK];
  648. char output_buffer[CHUNK];
  649. z.avail_in = 0;
  650. z.next_out = (Bytef*)output_buffer;
  651. z.avail_out = CHUNK;
  652. z.zalloc = 0;
  653. z.zfree = 0;
  654. z.opaque = 0;
  655. inflateInit(&z);
  656. int i,count,isz;
  657. isz=sizeof(input_buffer);
  658. for(;;){
  659. if(z.avail_in == 0){
  660. z.next_in = (Bytef*)input_buffer;
  661. if(total>isz){
  662. z.avail_in=isz;
  663. count=isz;
  664. i=0;
  665. while(count--) input_buffer[i++]=*(icp++);
  666. total-=isz;
  667. }else{
  668. z.avail_in=total;
  669. i=0;
  670. while(total--) input_buffer[i++]=*(icp++);
  671. }
  672. }
  673. inflate( &z, Z_SYNC_FLUSH );
  674. count = CHUNK - z.avail_out;
  675. if(count) write(ofd,output_buffer,count);
  676. if(total<0 && z.avail_in==0) break;
  677. z.next_out = (Bytef*)output_buffer;
  678. z.avail_out = CHUNK;
  679. }
  680. inflateEnd(&z);
  681. }