00001
00002
00003
00004 #include <arpa/inet.h>
00005 #include <errno.h>
00006 #include <netdb.h>
00007 #include <netinet/in.h>
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <sys/socket.h>
00011 #include <sys/types.h>
00012 #include <unistd.h>
00013 #include "ZSO-SPNFS_network.hpp"
00014
00015 #include "zsospnfs.h"
00016
00017 #define REMOTEFILENAME "./Entries.txt"
00018 const int LINELEN = 80;
00019
00020 int main(int argc, char **argv){
00021 in_addr_t addr;
00022 int sock;
00023 int portnum;
00024 int retint;
00025 struct sockaddr_in servername;
00026
00027 if(argc <=4){
00028 printf("Client usage:\n%s ip port username password\n", argv[0]);
00029 return 0;
00030 };
00031 addr = inet_addr(argv[1]);
00032 if(addr==(-1)){
00033 perror ("Wrong ip address! ip - address (inet_addr).");
00034 exit (EXIT_FAILURE);
00035 };
00036 portnum = atoi(argv[2]);
00037
00038
00039 sock = socket (PF_INET, SOCK_STREAM, 0);
00040 if (sock < 0) {
00041 perror ("socket (client)");
00042 exit (EXIT_FAILURE);
00043 }
00044
00045
00046 servername.sin_family = AF_INET;
00047 servername.sin_port = htons ( portnum );
00048 servername.sin_addr = *(struct in_addr *) &addr;
00049 if (0 > connect (sock, (struct sockaddr *) &servername, sizeof (servername))) {
00050 perror ("connect (client)");
00051 exit (EXIT_FAILURE);
00052 };
00053
00054 set_TCP_NODELAY(sock);
00055
00056 printf("Logining (user=%s,pass=%s) ... %s\n", argv[3], argv[4], (retint=spnfs_connect(sock,argv[3],argv[4]))==0?"SUCCESS":"FAILED");
00057 if(retint!=0) return 0;
00058
00059 spnfs_new(sock, REMOTEFILENAME);
00060
00061 int fid = spnfs_open(sock, REMOTEFILENAME);
00062
00063 if(fid < 0){
00064 printf("Sorry... I couldn't open remote file " REMOTEFILENAME);
00065 spnfs_disconnect(sock);
00066 close(sock);
00067 return 0;
00068 };
00069
00070 int n;
00071 long long size;
00072
00073 const int bufsize = 10240;
00074 char *buf=new char[bufsize];
00075 while(1){
00076 printf("Select option ([l/e/c/t/q] for {List/Edit/Cat/Trunc/Quit}):\n");
00077 scanf("%s", buf);
00078 switch(buf[0]){
00079 case 'l':
00080 case 'L':
00081 spnfs_seek(sock,fid,0,SEEK_SET);
00082 size = spnfs_size(sock,REMOTEFILENAME);
00083 n=0;
00084 for(; size>0; size-=retint){
00085 retint = spnfs_read(sock,fid,buf,LINELEN);
00086 if(retint<0){
00087 fprintf(stderr,"Error while reading from remote file!\n");
00088 return 0;
00089 };
00090 buf[retint] = '\0';
00091 printf("%3d: %s\n", n++, buf);
00092 };
00093 break;
00094 case 'c':
00095 case 'C':
00096 spnfs_seek(sock,fid,0,SEEK_SET);
00097 size = spnfs_size(sock,REMOTEFILENAME);
00098 for(; size>0; size-=retint){
00099 retint = spnfs_read(sock,fid,buf,bufsize);
00100 if(retint<0){
00101 fprintf(stderr,"Error while reading from remote file!\n");
00102 return 0;
00103 };
00104 write(1,buf,retint);
00105 };
00106 break;
00107 case 'e':
00108 case 'E':
00109 printf("Line number (each has %d bytes):", (int)LINELEN);
00110 scanf("%d", &n);
00111 retint = spnfs_seek(sock,fid,n*LINELEN,SEEK_SET);
00112 if( (n*LINELEN) != spnfs_pos(sock,fid) ){
00113 printf("Couldn't reach %d byte of file\nMaybe to small file?",n*LINELEN);
00114 continue;
00115 };
00116 retint = spnfs_read(sock,fid,buf,bufsize);
00117 if(retint<0){
00118 fprintf(stderr,"Error while reading from remote file!\n");
00119 return 0;
00120 };
00121 buf[retint] = '\0';
00122 printf("Current line contains:\n%s", buf);
00123 printf("Please give new data (it will be truncated to LINELEN=%d):\n", LINELEN);
00124 do{
00125 fgets(buf, bufsize, stdin);
00126 } while( strlen(buf) <= 1 );
00127 buf[strlen(buf)-1] = '\0';
00128 buf[LINELEN] = '\0';
00129 spnfs_seek(sock,fid,n*LINELEN,SEEK_SET);
00130 if( (n*LINELEN) != spnfs_pos(sock,fid) ){
00131 printf("Couldn't reach %d byte of file\nMaybe to small file?",n*LINELEN);
00132 continue;
00133 };
00134 retint = spnfs_write(sock,fid,buf,LINELEN);
00135 break;
00136 case 't':
00137 case 'T':
00138 printf("To which line file should be truncated? (each has %d bytes):", (int)LINELEN);
00139 scanf("%d", &n);
00140 retint = spnfs_seek(sock,fid,n*LINELEN,SEEK_SET);
00141 if( (n*LINELEN) != spnfs_pos(sock,fid) ){
00142 printf("Couldn't reach %d byte of file\nMaybe to small file?",n*LINELEN);
00143 continue;
00144 };
00145 spnfs_trunc(sock,fid);
00146 break;
00147 case 'q':
00148 case 'Q':
00149 printf("Goodbye!");
00150 delete buf;
00151 spnfs_close(sock,fid);
00152 spnfs_disconnect(sock);
00153 close (sock);
00154 return 0;
00155 break;
00156 default:
00157 printf("First letter unrecognized\nTry again\n");
00158 };
00159 };
00160
00161 delete buf;
00162 spnfs_close(sock,fid);
00163 spnfs_disconnect(sock);
00164 close (sock);
00165 return 0;
00166 };
00167