Sei sulla pagina 1di 2

Approach for solution

The first part of the task is to create a server that listens for the user commands.
The second part will be to create a client that executes the MVCP commands on the
different video servers.
Since the order in which the user commands need to be processed depends on the priority
and time of request submission, storing the data in the following format in a hash makes
it easier to sort:
server->priority->timestamp->file
->recording_duration
For every request there is flag (server->priority->timestamp->done)

whose value
indicates if that request has already been processed or not.
Since recordings at different video servers can proceed simultaneously, so a forked child
process will handle requests for each different server.
However as there is a limitation that a video server can record only one file at a time, so
while a server is processing one particular request, we need not bother looking for any
pending requests at that server. This is achieved by a flag (server->recording) which is
set only when a file is being recorded at that server.
The parent process keeps track of the different children and toggles the correspoding
recording flag whenever some child exits. The PID of a launched child is stored
(server->priority->timestamp->pid) for comparison purpose.
A child program, say C1, launched by the parent scheduler program, say P, will act as a
client by connecting to a video server. MVCP commands need to be written to this server
and also the responses from the server to these commands need to be stored. So the
program C1 will in turn fork a child, say C11, that will read responses from the server
while C1 will write the MVCP commands to the server.

Description of solution
Lines 1-18: Initialization
Different pragmas and modules that are being used are listed. Subroutines and some
variables are declared.
Lines 20-23: Command line options
Getopt::Long is used to capture the two mandatory parameters: -file and -port.
Missing parameters or specifying the -help parameter will display the correct usage.
Lines 25-32: Description file
Read description file containing different video server details and store them. Any line
beginning with a # in the description file is treated as a comment and ignored.
Lines 34-39: Create a socket
A socket is created listening on the port provided in the command line. The constant
SOMAXCONN from the Socket module is used to set the size of Listen queue to the

maximum allowed by the operating system. The ReuseAddr value is set in order to avoid
any address already in use errors while restarting the program.
Lines 41-42: Install signal handlers
Handlers for CHLD and INT signals are installed.
Lines 44-64: Loop for storing incoming commands
Whenever an incoming connection is found a session is establised. All incoming data
from this session is read and stored in a hash (whose structure has been explained above).
Any incoming data that does not conform to the expected command format is ignored.
Any command that contains a server whose details are not present in the description file
is also ignored apart from a warning message. The session is closed when the client
terminates the connection.
Lines 65-93: Ordering stored commands
Putting the ordering of stored commands and launching of pending requests within the
continue block ensures that this portion of the code is executed on every iteration of the
loop, even when there are no incoming connections. Whenever there is a pending request
that can be processed immediately a child is forked and the process_req subroutine is
called.
Lines 95-97: End of program
Flow exits out of the main loop on receiving an INT signal. The listening socket is closed
and a message is printed before the main program exits.
Lines 99-123: Subroutine process_req()
Creates a socket connection to a video server. Forks a child that reads responses from the
server while the parent writes the required MVCP commands sequentially into the server.
A measured sleep in the parent ensures relevant recording duration at the video server.
The child writes all responses into a tempfile. On completion of writing of all MVCP
commands the parent shuts down its own connection with the server and awaits for its
child to exit. The server having recieved the EOF from the parent in turn closes its
connection with the child. The child now exits and so does the waiting parent.
Lines 125-147: Subroutine chld_handler()
Everytime a child exits its PID is compared with the stored PIDs and the corresponding
server's recording flag is unset. The done flag is turned on to indicate that this request
has been processed and the stored pid is deleted.

Potrebbero piacerti anche