예제 #1
0
void serverListen(int port)
{
	int sd = UDP_Open(port);
	if(sd < 0)
	{
		printf("Error opening socket on port %d\n", port);
		exit(1);
	}

    printf("Starting server...\n");
    while (1) {
		struct sockaddr_in s;
		Net_Packet packet;
		int rc = UDP_Read(sd, &s, (char *)&packet, sizeof(Net_Packet));
		if (rc > 0) {
		    Net_Packet responsePacket;

		    switch(packet.message){
		    		
		    	case PAK_LOOKUP :
		    		responsePacket.inum = Server_Lookup(packet.inum, packet.name);
		    		break;

		    	case PAK_STAT :
		    		responsePacket.inum = Server_Stat(packet.inum, &(responsePacket.stat));
		    		break;

		    	case PAK_WRITE :
		    		responsePacket.inum = Server_Write(packet.inum, packet.buffer, packet.block);
		    		break;

		    	case PAK_READ:
		    		responsePacket.inum = Server_Read(packet.inum, responsePacket.buffer, packet.block);
		    		break;

		    	case PAK_CREAT:
		    		responsePacket.inum = Server_Creat(packet.inum, packet.type, packet.name);
		    		break;

		    	case PAK_UNLINK:
		    		responsePacket.inum = Server_Unlink(packet.inum, packet.name);
		    		break;

		    	case PAK_SHUTDOWN:
		  			break;
		    	
		    	case PAK_RESPONSE:
		    		break;
		    }

		    responsePacket.message = PAK_RESPONSE;
		    rc = UDP_Write(sd, &s, (char*)&responsePacket, sizeof(Net_Packet));
		    if(packet.message == PAK_SHUTDOWN)
		    	Server_Shutdown();
		}
	}
}
예제 #2
0
int main (int argc, char **argv)
{	
	

	char pname[MAXPATHNAMELEN];
	char *temp_buff;
	int pid;

	struct my_msg msg;
	struct read_struct msg_read;
	struct read_struct msg_write;
	struct stat_struct msg_stat;
	struct old_new msg_link;
	struct Stat res_stat;
	struct readlink msg_readlink;
	struct seek_struct msg_seek;

	printf("\nYFS: Started File Server");
	Initialize_Server();

	if (Fork() == 0) {
	printf("\nCHILD\n");
	pid=Exec(argv[1],argv);
	printf("\nval returned by exec is %d",pid);
	return 0;
    	}
    	else
	 printf("\nPARENT\n");

	while(1)
	{
		pid=Receive(&msg);
		//printf("\nInside while");
		switch(msg.type)
		{	case CREATE:		//for create,type=1,data2=inode of current dir,ptr=ptr to path
			{	printf("\nInside Server Create");

				CopyFrom(pid,(void *)pname,msg.ptr,MAXPATHNAMELEN);
				msg.data2=Server_Create(pname,msg.data2);
				printf("\nInside Server Returning %d",msg.data2);
				if(msg.data2!=ERROR)
					msg.type=0;
				else
					msg.type=ERROR;
				printf("\nInside Server Returning %d",msg.type);

				Reply(&msg,pid);
				break;
			}
			case OPEN:		//for Open,type=1,data2=inode of current dir,ptr=ptr to path
			{
				CopyFrom(pid,(void *)pname,msg.ptr,MAXPATHNAMELEN);
				msg.data2=Server_Open(pname,msg.data2);
				printf("\nVal returned by open is %d\n",msg.data2);
				if(msg.data2!=ERROR)
					msg.type=0;
				else
					msg.type=ERROR;

				printf("\nInside Server Returning %d\n",msg.type);
				Reply(&msg,pid);
				break;
			}
			case MKDIR:		//for MkDir,type=1,data2=inode of current dir,ptr=ptr to path
			{
				CopyFrom(pid,(void *)pname,msg.ptr,MAXPATHNAMELEN);
				msg.data2=Server_MkDir(pname,msg.data2);
				printf("\nInside Server MkDir with name as %s and curr inode %d\n",pname,msg.data2);
				if(msg.data2!=ERROR)
					msg.type=0;
				else
					msg.type=ERROR;

				Reply(&msg,pid);
				break;
			}
			case CHDIR:		//for ChDir,type=1,data2=inode of current dir,ptr=ptr to new dir
			{
				CopyFrom(pid,(void *)pname,msg.ptr,MAXPATHNAMELEN);
				printf("\nInside Server ChDir with name as %s and curr inode %d\n",pname,msg.data2);
				msg.data2=Server_ChDir(pname,msg.data2);
				if(msg.data2!=ERROR)
					msg.type=0;
				else
					msg.type=ERROR;
				Reply(&msg,pid);
				break;
			}
			case RMDIR:		//for ChDir,type=1,data2=inode of current dir,ptr=ptr to dir
			{
				CopyFrom(pid,(void *)pname,msg.ptr,MAXPATHNAMELEN);
				msg.type=Server_RmDir(pname,msg.data2);
				Reply((void *)&msg,pid);
				break;
			}
			case READ:
			{	
				CopyFrom(pid,(void *)&msg_read,msg.ptr,sizeof(struct read_struct));
				temp_buff=(char *)malloc(msg_read.len+1);
				msg.type=Server_Read(msg.data2,msg_read.len,msg_read.offset,temp_buff);
				CopyTo(pid,msg_read.buff,temp_buff,msg_read.len);
				printf("\nInside Server ReturningRead %d",msg.type);
				printf("\n");
				Reply((void *)&msg,pid);
				//free(temp_buff);
				break;
			}
			case WRITE:
			{	printf("\nInside write in while");
				CopyFrom(pid,(void *)&msg_write,msg.ptr,sizeof(struct read_struct));
				temp_buff=(char *)malloc(msg_write.len+1);
				temp_buff[msg_write.len]='\0';
				CopyFrom(pid,temp_buff,msg_write.buff,msg_write.len);
				printf("\nData for writing is %s and len =%d",temp_buff,msg_write.len);
				msg.type=Server_Write(msg.data2,msg_write.len,msg_write.offset,temp_buff);
				//
				//free(temp_buff);
				Reply((void *)&msg,pid);
				
				break;
			}
			case STAT:
			{
			
				CopyFrom(pid,(void *)&msg_stat,msg.ptr,sizeof(struct stat_struct));
				msg.type=Server_Stat(msg.data2,msg_stat.pathname,&res_stat);
				if(msg.type!=ERROR)
					CopyTo(pid,(void *)msg_stat.statbuf,(void *)&res_stat,sizeof(struct Stat));
				Reply(&msg,pid);
				break;
			}
			
			case LINK:
			{
				CopyFrom(pid,(void *)&msg_link,msg.ptr,sizeof(struct old_new));
				msg.data2=Server_Link(msg.data2,msg_link.oldname,msg_link.newname);
				if(msg.data2==ERROR)
					msg.type=ERROR;
				else
					msg.type=0;
				Reply(&msg,pid);
				break;
			}
			case UNLINK:
			{
				temp_buff=(char *)malloc(MAXPATHNAMELEN*sizeof(char));
				CopyFrom(pid,(void *)temp_buff,msg.ptr,MAXPATHNAMELEN);
				msg.type=Server_UnLink(msg.data2,temp_buff);
				Reply(&msg,pid);
				free(temp_buff);
				break;
			}
			case READLINK:
			{
				CopyFrom(pid,(void *)&msg_readlink,msg.ptr,sizeof(struct readlink));
				temp_buff=(char *)malloc(msg_readlink.len+1);
				msg.type=Server_ReadLink(msg.data2,msg_readlink.pathname,msg_readlink.len,temp_buff);
				CopyTo(pid,msg_readlink.buf,temp_buff,msg_readlink.len);
				Reply(&msg,pid);
				break;
			}
			case SYMLINK:
			{
				CopyFrom(pid,(void *)&msg_link,msg.ptr,sizeof(struct old_new));
				msg.data2=Server_SymLink(msg.data2,msg_link.oldname,msg_link.newname);
				if(msg.data2==ERROR)
					msg.type=ERROR;
				else
					msg.type=0;
				Reply(&msg,pid);
				break;
			}
			case SEEK:
			{
				CopyFrom(pid,(void *)&msg_seek,msg.ptr,sizeof(struct stat_struct));
				msg.type=Server_Seek(msg.data2,msg_seek.curr_offset,msg_seek.new_offset,msg_seek.whence);
				Reply(&msg,pid);
				break;
			}
			case SHUTDOWN:
			{	//Server_Sync();
				TtyPrintf(0,"\nShutting Down Yalnix File Server!");
				msg.type=0;
				Reply((void *)&msg,pid);
				Exit(0);
				break;
			}
			case SYNC:
			{
			Reply((void *)&msg,pid);
			//Server_sync();
			
			break;
			}
			default:
			{
				msg.type=ERROR;
				Reply(&msg,pid);
				break;
			}

		}
	Reply(&msg,pid);
	}
	printf("\nExiting Yfs");
	return 0;;
	
}
예제 #3
0
파일: server.c 프로젝트: tshao4/CS537
int main(int argc, char *argv[])
{

	if(argc != 3){
		fprintf(stderr,"usage: server [portnum] [file-system-image]\n");
		fprintf(stderr,"you supplied %d args\n", argc);
		exit(1);
	}
	int portnum = atoi( argv[1] );
	if( ! (portnum > 0 ) ) {
		fprintf(stderr, "  portnum = %d;  this should be a pos number",portnum);
	}

	const int sd = UDP_Open(portnum);
	assert(sd > -1);

	inode_table = (Inode_t *)malloc(MFS_BLOCK_NUMS * sizeof(Inode_t));
	data_region = (Block_t *)malloc(MFS_BLOCK_NUMS * sizeof(Block_t));
	if(inode_table == NULL || data_region == NULL){
		fprintf(stderr, "malloc error\n");
		exit(-1);
	}

	if( (image_fd = open(argv[2],O_RDWR)) == -1){
		image_fd = Image_Init(argv[2]);
	}

	int rc = -1;
	struct sockaddr_in s;
	char buffer_read[ UDP_BUFFER_SIZE];
	char buffer_reply[UDP_BUFFER_SIZE];
	char buffer[BUFFER_SIZE];
	char * ptr = buffer_read;

    int block  = -1;
	int blocks = -1;
	int pinum  = -1;
	int inum   = -1;
	int size   = -1;
	int status = -1;
	int type   = -1;
	char * name   = NULL;

	int * iptr   = NULL;
	char * cptr  = NULL;
	MFS_Stat_t * mptr  = NULL;

	while (1) {

		block  = -1;
		blocks = -1;
		pinum  = -1;
		inum   = -1;
		size   = -1;
		status = -1;
		type   = -1;
		name   = NULL;
		iptr  = NULL;
		cptr  = NULL;

		rc = UDP_Read(sd, &s, buffer_read, UDP_BUFFER_SIZE);
		ptr = buffer_read;
		if (rc < 1) {
			rc = -1;
			continue;
		}

		iptr = (int*) ptr;
		int * op_id = iptr;
		iptr++;
		switch(*op_id)
		{
			case 0:
				printf("op_id == 0 \n");
				break;

			case 1:
				pinum = *( iptr );
				iptr++;
				cptr = (char*)iptr;
				name = cptr;

				inum = Server_LookUp(pinum, name );

				iptr = (int*) buffer_reply;
				*iptr = inum;

				break;


			case 2:


				inum = *( iptr );
				iptr++;
				mptr = (MFS_Stat_t*)iptr;

				status = Server_Stat(inum, mptr );

				iptr = (int*) buffer_reply;
				*iptr = status;

				iptr++;
				memcpy((void*) iptr, (void*)mptr, sizeof( MFS_Stat_t ) );

				break;

			case 3:

				inum = *( iptr );
				iptr++;
				cptr = (char*)iptr;
				memcpy( buffer, cptr, BUFFER_SIZE );
				cptr += BUFFER_SIZE;
				iptr = (int*) cptr;
				block = *iptr;

				status = Server_Write( inum, buffer, block );

				iptr = (int *) buffer_reply;
				*iptr = status;

				break;

			case 4:

				inum = *( iptr );
				iptr++;
				cptr = (char*)iptr;
				memcpy( buffer, cptr, BUFFER_SIZE );
				cptr += BUFFER_SIZE;
				iptr = (int*) cptr;
				block = *iptr;

				status = Server_Read( inum, buffer, block );

				iptr = (int *) buffer_reply;
				*iptr = status;
                iptr++;
                cptr = (char*) iptr;
                memcpy(cptr, buffer, BUFFER_SIZE);

				break;

			case 5:

				pinum = *( iptr );
				iptr++;
				type = *iptr;
				iptr++;
				name = (char*) iptr;

				status = Server_Creat( pinum, type, name );

				iptr = (int *) buffer_reply;
				*iptr = status;

				break;

			case 6:

				pinum  = *(iptr++);
				name   = (char*)(iptr);

				status = Server_Unlink( pinum, name );
				iptr  = (int*) buffer_reply;
				*iptr = status;

				break;

			default:
				fprintf(stderr, "bad function number %d\n", *op_id );
				exit(1);
		}
        
		rc = UDP_Write(sd, &s, buffer_reply, UDP_BUFFER_SIZE);

	}

	return 0;
}