Saturday, June 8, 2013

Phase 2 - Creating Frames

What to do
1) Decide the structure of a frame (Need help? please look at the file Help_Sess2_B04.pdf). 

struct frame {
    unsigned int Seq     // the sequence of the frame being sent, starts from 0
    unsigned int Ack     //the ack number, not used yet in this phase
    unsigned int EOM        // if the end of message
    unsigned int lenOfLastFrame     // same as lenOfLastPacket
    unsigned char Content[128]    // the payload (according to the requirement, the maximum payload of a frame is 128 bytes)    frame_kind Kind  // {ACK, NAK, DATA} // the kind of the frame, in this phase, there is only one kind, i.e. DATA    message_type Type   // {COM, FILEDATA} // same as the Type in packets
    crc CRC   // checksum, crc is of type unsigned int, this member will be used in phase 3  }
The size of each frame is 156 bytes.

Bugs encountered and solutions
1) You should not used function strncpy to pass bytes in a packet to a frame (took me 5 hours to find this bug). You got no problem with assembling the target file at server side but when you are trying to open the file, it just failed, i.e. you cannot view image.jpg or play music.mp3. Use function memcpy instead. !!!

Structure Graph


Goal Of Phase 2
Based on the code we finished on Phase 1, we add additional functionality. In this phase, the client program needs to be able to create frames based on the  packets received at Datalink Layer, transfer these frames one by one to the server. Each time the server receives a frame, it create a packet according to the data in that frame and sends the packet to the Application Layer. 

Phase 1 - Sending Packets from Client to Server

What to do
1) At the very beginning of this project, you need to get familiar with (if you have no exposure to operating system before) the linux functions relevant to the following topics: 
  • Create process
  • Partition a large file (e.g .mp3) into packets of fixed size, e.g. 128 bytes each
  • Use pipe to send packets from one process to another
    • At phase 1, we used named pipe instead of normal pipe. The reason is that normal pipe does not have the feature that the writing end will be blocked if no process reads from the pipe, or the reading end will be blocked if no process writes to the pipe, i.e. a basic flow control is achieved when named pipe is used. Note that in order to implement Selective Repeat, we will eventually replace named pipe with normal pipe, which we will accomplish in phase 4.  
2) Decide the structure of a packet
struct packet { 
    unsigned char Content[128]; // payload 
    unsigned int EOM;           //End Of Message
    unsigned int lenOfLastPacket;  //because the payload is of fixed size, the sender need to tell the receiver the number of bytes it needs to read from the last packet     message_type Type;       // {COM, FILEDATA}
} 
The size of each packet is 140 bytes. We call all the data stored in the payload of packets "messages". Messages can be commands, which is identified as COM, or binary data, which is identified as FILEDATA. The reason we divide the message into two types is the receiver need to respond differently to these two types of message.

Structure Graph


Phase 1 Goal
Write client and server programs. In both client and server programs, there are two processes, parent process as Application Layer and child process as Datalink Layer. For a large file, the client program partitions the file into packets, send these packets one by one to the child process, datalink layer, then the child process send each packet to the server. Sever receives packets at Datalink Layer, sends them to Application Layer, and reassembles them into a single file. (Need help? Please refer to the attached file in my google drive folderHelp_Sess3_B05.pdf)

Useful resources
What's file and file handling in C
How to do file IO, text and binary, in C
Basic binary file manipulation function, e.g fseek, ftell
C, howto read binary file into buffer (set the size to be fileLen, and the nmenb to be 1)
What's process?
Intro to pipe
Interprocess commucation with pipes
pipe functions in c, pipe
pipe functions in c, popen and pclose
function read and write
process functions, fork

What's named pipe and how to create named pipe?
Solution to the "Permission Denied" exception when opening named pipe.
Need to specify the permission argument, mknod("pipe", S_IFIFO|S_IRUSR|S_IWUSR, 0)
Delete named pipe, i.e. unlink
How to define and declare a structure in C.
http://cprogramminglanguage.net/c-structure.aspx

Simulation of Network Datalink Layer, WPI Intro to Network Course Project, Mar - Aug 2012

Team members: David Zheng, Rong Li.
Programming Language: C
Related Topics: Network protocol simulation, mutiple-process programming, network programming, Selective Repeat Protocol and
sliding windows.
Description: This website documents step by step the entire process of how we build our project. This project is based on the requirement of the WPI Intro to Network Course Project, taught by Professor Robert Kinicki. Please check the requirement file in subpage Resource.
Application Demo:
Valid commands includes (use # as delimiter): 
UP#file_name#   - upload a file
DL#file_name#   - download a file
RM#file_name#  - remove a file
LI#                    - list the files in the current directory of the server
LO#                  - logout