SlideShare a Scribd company logo
1 of 14
Download to read offline
Components of computer systems often have dependencies
--
other components that must be
installed before they will function properly. These dependencies are frequently shared by
multiple components. For example, both the TELNET client program and the FTP cli
ent
program require that the TCP/IP networking software be installed before they can operate. If you
install TCP/IP and the TELNET client program, and later decide to add the FTP client program,
you do not need to reinstall TCP/IP.
For some components it
would not be a problem if the components on which they depended were
reinstalled; it would just waste some resources. But for others, like TCP/IP, some component
configuration may be destroyed if the component was reinstalled.
It is useful to be able to r
emove components that are no longer needed. When this is done,
components that only support the removed component may also be removed, freeing up disk
space, memory, and other resources. But a supporting component, not explicitly installed, may
be removed
only if all components which depend on it are also removed. For example, removing
the FTP client program and TCP/IP would mean the TELNET client program, which was not
removed, would no longer operate. Likewise, removing TCP/IP by itself would cause the fa
ilure
of both the TELNET and the FTP client programs. Also if we installed TCP/IP to support our
own development, then installed the TELNET client (which depends on TCP/IP) and then still
later removed the TELNET client, we would not want TCP/IP to be remo
ved.
We want a program to automate the process of adding and removing components. To do this we
will maintain a record of installed components and component dependencies. A component can
be installed explicitly in response to a command (unless it is alrea
dy installed), or implicitly if it
is needed for some other component being installed. Likewise, a component, not explicitly
installed, can be explicitly removed in response to a command (if it is not needed to support
other components) or implicitly remov
ed if it is no longer needed to support another component.
Input
The input will contain a sequence of commands (as described below), each on a separate line
containing no more than eighty characters. Item names are case sensitive, and each is no longer
th
an ten characters. The command names (
DEPEND
,
INSTALL
,
REMOVE
and
LIST
) always
appear in uppercase starting in column one, and item names are separated from the command
name and each other by one or more spaces. All appropriate
DEPEND
commands will appear
before the occurrence of any
INSTALL
dependencies. The end of the input is marked by a line
containing only the word
END
.
Command Syntax
Interpretation/Response
DEPEND item1 item2 [item3 ...]
item1
depends on
item2
(and
item3
...)
INSTALL item1
install
item1
and those on which it depends
REMOVE item1
remove
item1
, and those on whch it depends, if possible
LIST
list the names of all currently
-
installed components
Output
Echo each line of input. Follow each echoed
INSTALL
or
REMOVE
line with the actions taken
in response, making certain that the actions are given in the proper order. Also identify
exceptional conditions (see
Expected Output
, below, for examples of all case
s). For the
LIST
command, display the names of the currently installed components. No output, except the echo,
is produced for a
DEPEND
command or the line containing
END
. There will be at most one
dependency list per item.
Sample Input
DEPEND TELNET TC
PIP NETCARD
DEPEND TCPIP NETCARD
DEPEND DNS TCPIP NETCARD
DEPEND BROWSER TCPIP HTML
INSTALL NETCARD
INSTALL TELNET
INSTALL foo
REMOVE NETCARD
INSTALL BROWSER
INSTALL DNS
LIST
REMOVE TELNET
REMOVE NETCARD
REMOVE DNS
REMOVE NETCARD
INSTALL NETCARD
REMOVE
TCPIP
REMOVE BROWSER
REMOVE TCPIP
LIST
END
Output for the Sample Output
DEPEND TELNET TCPIP NETCARD
DEPEND TCPIP NETCARD
DEPEND DNS TCPIP NETCARD
DEPEND BROWSER TCPIP HTML
INSTALL NETCARD
Installing NETCARD
INSTALL TELNET
Installing TCPIP
Installing TELNET
INSTALL foo
Installing foo
REMOVE NETCARD
NETCARD is still needed.
INSTALL BROWSER
Installing HTML
Installing BROWSER
INSTALL DNS
Installing DNS
LIST
HTML
BROWSER
DNS
NETCARD
foo
TCPIP
TELNET
REMOVE
TELNET
Removing TELNET
REMOVE NETCARD
NETCARD is still needed.
REMOVE DNS
Removing DNS
REMOVE NETCARD
NETCARD is still needed.
INSTALL NETCARD
NETCARD is already installed.
REMOVE TCPIP
TCPIP is still needed.
REMOVE BROWSER
Removing BROWSER
Removing HTML
Removing TCPIP
REMOVE TCPIP
TCPIP is not installed.
LIST
NETCARD
foo
END
Solution
TCPStream Class
Interface
The TCPStream class provides methods to send and receive data over a TCP/IP connection. It
contains a connected socket descriptor and information about the peer – either client or server –
in the form of the IP address and TCP port. TCPStream includes simple get methods that return
address and port, but not the socket descriptor which is kept private. One of the advantages of
programming with objects is the ability to logically group data members and methods to avoid
exposing data, in this case the socket descriptor, to the calling program that it does not need to
see. Each connection is completely encapsulated in each TCPStream object.
TCPStream objects are created by TCPConnector and TCPAcceptor objects only, so the
TCPStream constructors must be declared private to prevent them from being called directly by
any other objects. The TCPStream class grants friend privileges to the TCPConnector and
TCPAcceptor classes so they can access the TCPStream constructors to supply connected socket
descriptors.
#include
#include
#include
#include
using namespace std
class TCPStream
{
int m_sd;
string m_peerIP;
int m_peerPort;
public:
friend class TCPAcceptor;
friend class TCPConnector;
~TCPStream();
ssize_t send(char* buffer, size_t len);
ssize_t receive(char* buffer, size_t len);
string getPeerIP();
int getPeerPort();
private:
TCPStream(int sd, struct sockaddr_in* address);
TCPStream();
TCPStream(const TCPStream& stream);
};
Constructor
The constructor stores the connected socket descriptor then converts the socket information
structure fields to a peer IP address string and peer TCP port. These parameters can be inspected
with calls to TCPStream::getPeerIP() and TCPStream::getPeerPort().
#include
#include "tcpstream.h"
TCPStream::TCPStream(int sd, struct sockaddr_in* address) : msd(sd) {
char ip[50];
inet_ntop(PF_INET, (struct in_addr*)&(address->sin_addr.s_addr),
ip, sizeof(ip)-1);
m_peerIP = ip;
m_peerPort = ntohs(address->sin_port);
}
Destructor
The destructor simply closes the connection.
TCPStream::~TCPStream()
{
close(m_sd);
}
Network I/O Methods
TCPStream::send() and TCPStream::receive() simply wrap calls to read() and write(), returning
the number of bytes sent and bytes received, respectively. No additional buffering or other
capabilities are added.
Get Peer Information
TCPStream::getPeerIP() and TCPStream::getPeerPort() return the IP address and TCP port
information of the peer to which the network application, client or server, are connected. You
can get the same information from the sockets getpeername() function but it far easier to just
capture that information when the connections are established. Clients know in advance to where
they are connecting and the client’s socket address is returned the accept() function when the
server accepts a client connection – see the TCPAcceptor::accept() method definition. In both
cases the socket address information is passed to the TCPStream object when it is constructed.
TCPConnector Class
Interface
TCPConnector provides the connect() method to actively establish a connection with a server. It
accepts the server port and a string containing the server host name or IP address. If successful, a
pointer to a TCPStream object is returned to the caller.
#include
#include "tcpstream.h"
class TCPConnector
{
public:
TCPStream* connect(int port, const char* server);
private:
int resolveHost(const char* host, struct in_addr* addr);
};
Constructor/Destructor
The TCPConnector class does not use any member variables so the default constructor and
destructor generated by the C++ compiler are fine. No others are defined.
Connect to Server
[Lines 6-12] TCPConnector::connect() call takes a server host name or IP address string and the
server listening port as arguments. The server struct sockaddr_in sin_family is set to PF_INET
and the sin_port is set to the TCP port on which the server is listening for connections.
#include
#include
#include
#include "tcpconnector.h"
TCPStream* TCPConnector::connect(const char* server, int port)
{
struct sockaddr_in address;
memset (&address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_port = htons(port);
if (resolveHostName(server, &(address.sin_addr)) != 0) {
inet_pton(PF_INET, server, &(address.sin_addr));
}
int sd = socket(AF_INET, SOCK_STREAM, 0);
if (::connect(sd, (struct sockaddr*)&address, sizeof(address)) != 0) {
return NULL;
}
return new TCPStream(sd, &address);
}
int TCPConnector::resolveHostName(const char* hostname, struct in_addr* addr)
{
struct addrinfo *res;
int result = getaddrinfo (hostname, NULL, NULL, &res);
if (result == 0) {
memcpy(addr, &((struct sockaddr_in *) res->ai_addr)->sin_addr,
sizeof(struct in_addr));
freeaddrinfo(res);
}
return result;
}
TCPAcceptor Class
Interface
TCPAcceptor includes member variables for the listening socket descriptor, the socket address
information – IP address and TCP port – and a flag that indicates whether or not the
TCPAcceptor has started listening for connections.
Two public methods are supported. One to start the listening and the other to accept connections.
#include
#include
#include "tcpstream.h"
using namespace std;
class TCPAcceptor
{
int m_lsd;
string m_address;
int m_port;
bool m_listening;
public:
TCPAcceptor(int port, const char* address="");
~TCPAcceptor();
int start();
TCPStream* accept();
private:
TCPAcceptor() {}
};
Constructor
The constructor sets the member variables to as shown here. Setting m_lsd indicates that the
lisetning socket has not been created.
#include
#include
#include
#include "tcpacceptor.h"
TCPAcceptor::TCPAcceptor(int port, const char* address)
: m_lsd(0), m_port(port), m_address(address), m_listening(false) {}
Destructor
If the listening socket has been created then it is closed in the destructor.
TCPAcceptor::~TCPAcceptor()
{
if (m_lsd > 0) {
close(m_lsd);
}
}
Start Listening for Connections
[Line 3-5] Creating a listening socket involves the most socket calls of any operation. Before
going through the series of calls, TCPAcceptor::start() checks to see if a listening socket already
exists. If so, the method just returns 0.
int TCPAcceptor::start()
{
if (m_listening == true) {
return 0;
}
m_lsd = socket(PF_INET, SOCK_STREAM, 0);
struct sockaddr_in address;
memset(&address, 0, sizeof(address));
address.sin_family = PF_INET;
address.sin_port = htons(m_port);
if (m_address.size() > 0) {
inet_pton(PF_INET, m_address.c_str(), &(address.sin_addr));
}
else {
address.sin_addr.s_addr = INADDR_ANY;
}
int optval = 1;
setsockopt(m_lsd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
int result = bind(m_lsd, (struct sockaddr*)&address, sizeof(address));
if (result != 0) {
perror("bind() failed");
return result;
}
result = listen(m_lsd, 5);
if (result != 0) {
perror("listen() failed");
return result;
}
m_listening = true;
return result;
}
TCPStream* TCPAcceptor::accept()
{
if (m_listening == false) {
return NULL;
}
struct sockaddr_in address;
socklen_t len = sizeof(address);
memset(&address, 0, sizeof(address));
int sd = ::accept(m_lsd, (struct sockaddr*)&address, &len);
if (sd < 0) {
perror("accept() failed");
return NULL;
}
return new TCPStream(sd, &address);
}
#include
#include
#include "tcpacceptor.h"
int main(int argc, char** argv)
{
if (argc < 2 || argc > 4) {
printf("usage: server [] ");
exit(1);
}
TCPStream* stream = NULL;
TCPAcceptor* acceptor = NULL;
if (argc == 3) {
acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]);
}
else {
acceptor = new TCPAcceptor(atoi(argv[1]));
}
if (acceptor->start() == 0) {
while (1) {
stream = acceptor->accept();
if (stream != NULL) {
size_t len;
char line[256];
while ((len = stream->receive(line, sizeof(line))) > 0) {
line[len] = NULL;
printf("received - %s ", line);
stream->send(line, len);
}
delete stream;
}
}
}
perror("Could not start the server");
exit(-1);
}
Echo Client
The client application takes the server TCP port and IP address on the command line. For each
connection a string is displayed and sent to the server, the echoed string is received back and
displayed, then the connection is closed. The client will be defined in the file client.cpp
#include
#include
#include
#include "tcpconnector.h"
using namespace std;
int main(int argc, char** argv)
{
if (argc != 3) {
printf("usage: %s  ", argv[0]);
exit(1);
}
int len;
string message;
char line[256];
TCPConnector* connector = new TCPConnector();
TCPStream* stream = connector->connect(argv[2], atoi(argv[1]));
if (stream) {
message = "Is there life on Mars?";
stream->send(message.c_str(), message.size());
printf("sent - %s ", message.c_str());
len = stream->receive(line, sizeof(line));
line[len] = NULL;
printf("received - %s ", line);
delete stream;
}
stream = connector->connect(argv[2], atoi(argv[1]));
if (stream) {
message = "Why is there air?";
stream->send(message.c_str(), message.size());
printf("sent - %s ", message.c_str());
len = stream->receive(line, sizeof(line));
line[len] = NULL;
printf("received - %s ", line);
delete stream;
}
exit(0);
}
#include
#include
#include
#include
using namespace std
class TCPStream
{
int m_sd;
string m_peerIP;
int m_peerPort;
public:
friend class TCPAcceptor;
friend class TCPConnector;
~TCPStream();
ssize_t send(char* buffer, size_t len);
ssize_t receive(char* buffer, size_t len);
string getPeerIP();
int getPeerPort();
private:
TCPStream(int sd, struct sockaddr_in* address);
TCPStream();
TCPStream(const TCPStream& stream);
};

More Related Content

Similar to Components of computer systems often have dependencies--other co.pdf

Student Name _________________________________ Date _____________SE.docx
Student Name _________________________________  Date _____________SE.docxStudent Name _________________________________  Date _____________SE.docx
Student Name _________________________________ Date _____________SE.docxemelyvalg9
 
Arp Dan Ipconfig Syntax
Arp Dan Ipconfig  SyntaxArp Dan Ipconfig  Syntax
Arp Dan Ipconfig Syntaxguestcc37e8c
 
FMS Administration Seminar
FMS Administration SeminarFMS Administration Seminar
FMS Administration SeminarYoss Cohen
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in JavaTushar B Kute
 
CC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfCC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfsecunderbadtirumalgi
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in JavaTushar B Kute
 
1 SEC450 ACL Tutorial This document highlights.docx
1 SEC450 ACL Tutorial This document highlights.docx1 SEC450 ACL Tutorial This document highlights.docx
1 SEC450 ACL Tutorial This document highlights.docxdorishigh
 
QoS Classification on Cisco IOS Router
QoS Classification on Cisco IOS RouterQoS Classification on Cisco IOS Router
QoS Classification on Cisco IOS RouterNetProtocol Xpert
 
CSS L17 - DOS COMMANDS IN COMPUTER NETWORKING
CSS L17 - DOS COMMANDS IN COMPUTER NETWORKINGCSS L17 - DOS COMMANDS IN COMPUTER NETWORKING
CSS L17 - DOS COMMANDS IN COMPUTER NETWORKINGMarvin Bronoso
 
TCP WRAPPERS and XINETD
TCP WRAPPERS and XINETD TCP WRAPPERS and XINETD
TCP WRAPPERS and XINETD Tan Huynh Cong
 
Network configuration in Linux
Network configuration in LinuxNetwork configuration in Linux
Network configuration in LinuxMohammed Yazdani
 
Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3Max Kleiner
 

Similar to Components of computer systems often have dependencies--other co.pdf (20)

Student Name _________________________________ Date _____________SE.docx
Student Name _________________________________  Date _____________SE.docxStudent Name _________________________________  Date _____________SE.docx
Student Name _________________________________ Date _____________SE.docx
 
Arp Dan Ipconfig Syntax
Arp Dan Ipconfig  SyntaxArp Dan Ipconfig  Syntax
Arp Dan Ipconfig Syntax
 
Md13 networking
Md13 networkingMd13 networking
Md13 networking
 
FMS Administration Seminar
FMS Administration SeminarFMS Administration Seminar
FMS Administration Seminar
 
Network Programming in Java
Network Programming in JavaNetwork Programming in Java
Network Programming in Java
 
CC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdfCC++ echo serverThis assignment is designed to introduce network .pdf
CC++ echo serverThis assignment is designed to introduce network .pdf
 
Network programming in Java
Network programming in JavaNetwork programming in Java
Network programming in Java
 
1 SEC450 ACL Tutorial This document highlights.docx
1 SEC450 ACL Tutorial This document highlights.docx1 SEC450 ACL Tutorial This document highlights.docx
1 SEC450 ACL Tutorial This document highlights.docx
 
Np unit2
Np unit2Np unit2
Np unit2
 
Basic socket programming
Basic socket programmingBasic socket programming
Basic socket programming
 
QoS Classification on Cisco IOS Router
QoS Classification on Cisco IOS RouterQoS Classification on Cisco IOS Router
QoS Classification on Cisco IOS Router
 
Ip Access Lists
Ip Access ListsIp Access Lists
Ip Access Lists
 
CSS L17 - DOS COMMANDS IN COMPUTER NETWORKING
CSS L17 - DOS COMMANDS IN COMPUTER NETWORKINGCSS L17 - DOS COMMANDS IN COMPUTER NETWORKING
CSS L17 - DOS COMMANDS IN COMPUTER NETWORKING
 
TCP WRAPPERS and XINETD
TCP WRAPPERS and XINETD TCP WRAPPERS and XINETD
TCP WRAPPERS and XINETD
 
Lession2 Xinetd
Lession2 XinetdLession2 Xinetd
Lession2 Xinetd
 
Network configuration in Linux
Network configuration in LinuxNetwork configuration in Linux
Network configuration in Linux
 
Rpc mechanism
Rpc mechanismRpc mechanism
Rpc mechanism
 
Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3Arduino LED maXbox starter18_3
Arduino LED maXbox starter18_3
 
A.java
A.javaA.java
A.java
 
Networking chapter VI
Networking chapter VINetworking chapter VI
Networking chapter VI
 

More from alamodeindia1

Compute SS, variance, standard deviation, and range for the followin.pdf
Compute SS, variance, standard deviation, and range for the followin.pdfCompute SS, variance, standard deviation, and range for the followin.pdf
Compute SS, variance, standard deviation, and range for the followin.pdfalamodeindia1
 
Connect compensation components to organizational strategy.Solut.pdf
Connect compensation components to organizational strategy.Solut.pdfConnect compensation components to organizational strategy.Solut.pdf
Connect compensation components to organizational strategy.Solut.pdfalamodeindia1
 
Compute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdf
Compute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdfCompute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdf
Compute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdfalamodeindia1
 
compute the present value of a perpetuity bond that pays a monthly c.pdf
compute the present value of a perpetuity bond that pays a monthly c.pdfcompute the present value of a perpetuity bond that pays a monthly c.pdf
compute the present value of a perpetuity bond that pays a monthly c.pdfalamodeindia1
 
Consider a large lot of electronic components where there is interes.pdf
Consider a large lot of electronic components where there is interes.pdfConsider a large lot of electronic components where there is interes.pdf
Consider a large lot of electronic components where there is interes.pdfalamodeindia1
 
Compute the rate of return for the investment represented by the fol.pdf
Compute the rate of return for the investment represented by the fol.pdfCompute the rate of return for the investment represented by the fol.pdf
Compute the rate of return for the investment represented by the fol.pdfalamodeindia1
 
Consider a discrete random variable, X, having pmf pX(x) = (1 )^.pdf
Consider a discrete random variable, X, having pmf pX(x) = (1  )^.pdfConsider a discrete random variable, X, having pmf pX(x) = (1  )^.pdf
Consider a discrete random variable, X, having pmf pX(x) = (1 )^.pdfalamodeindia1
 
Consider a 6 x 4 matrix. Prove that every column in A is in the span.pdf
Consider a 6 x 4 matrix. Prove that every column in A is in the span.pdfConsider a 6 x 4 matrix. Prove that every column in A is in the span.pdf
Consider a 6 x 4 matrix. Prove that every column in A is in the span.pdfalamodeindia1
 
Consider a family tree that consists of just you, your parents,their.pdf
Consider a family tree that consists of just you, your parents,their.pdfConsider a family tree that consists of just you, your parents,their.pdf
Consider a family tree that consists of just you, your parents,their.pdfalamodeindia1
 
Consider a discrete random variable X with moment generating functio.pdf
Consider a discrete random variable X with moment generating functio.pdfConsider a discrete random variable X with moment generating functio.pdf
Consider a discrete random variable X with moment generating functio.pdfalamodeindia1
 
Consider a dinner table where n dining philosophers are seated. Each.pdf
Consider a dinner table where n dining philosophers are seated. Each.pdfConsider a dinner table where n dining philosophers are seated. Each.pdf
Consider a dinner table where n dining philosophers are seated. Each.pdfalamodeindia1
 
Consider a $6500 piece of machinery , with a 5 year depreciable like.pdf
Consider a $6500 piece of machinery , with a 5 year depreciable like.pdfConsider a $6500 piece of machinery , with a 5 year depreciable like.pdf
Consider a $6500 piece of machinery , with a 5 year depreciable like.pdfalamodeindia1
 
Conduct a company review of Starbucks. What are their unique growth .pdf
Conduct a company review of Starbucks. What are their unique growth .pdfConduct a company review of Starbucks. What are their unique growth .pdf
Conduct a company review of Starbucks. What are their unique growth .pdfalamodeindia1
 
Computer-Based Training (CBT) Methods Please respond to the foll.pdf
Computer-Based Training (CBT) Methods Please respond to the foll.pdfComputer-Based Training (CBT) Methods Please respond to the foll.pdf
Computer-Based Training (CBT) Methods Please respond to the foll.pdfalamodeindia1
 

More from alamodeindia1 (14)

Compute SS, variance, standard deviation, and range for the followin.pdf
Compute SS, variance, standard deviation, and range for the followin.pdfCompute SS, variance, standard deviation, and range for the followin.pdf
Compute SS, variance, standard deviation, and range for the followin.pdf
 
Connect compensation components to organizational strategy.Solut.pdf
Connect compensation components to organizational strategy.Solut.pdfConnect compensation components to organizational strategy.Solut.pdf
Connect compensation components to organizational strategy.Solut.pdf
 
Compute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdf
Compute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdfCompute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdf
Compute the Mean and the Standard deviation. Class 1, 0 up to 5 Fre.pdf
 
compute the present value of a perpetuity bond that pays a monthly c.pdf
compute the present value of a perpetuity bond that pays a monthly c.pdfcompute the present value of a perpetuity bond that pays a monthly c.pdf
compute the present value of a perpetuity bond that pays a monthly c.pdf
 
Consider a large lot of electronic components where there is interes.pdf
Consider a large lot of electronic components where there is interes.pdfConsider a large lot of electronic components where there is interes.pdf
Consider a large lot of electronic components where there is interes.pdf
 
Compute the rate of return for the investment represented by the fol.pdf
Compute the rate of return for the investment represented by the fol.pdfCompute the rate of return for the investment represented by the fol.pdf
Compute the rate of return for the investment represented by the fol.pdf
 
Consider a discrete random variable, X, having pmf pX(x) = (1 )^.pdf
Consider a discrete random variable, X, having pmf pX(x) = (1  )^.pdfConsider a discrete random variable, X, having pmf pX(x) = (1  )^.pdf
Consider a discrete random variable, X, having pmf pX(x) = (1 )^.pdf
 
Consider a 6 x 4 matrix. Prove that every column in A is in the span.pdf
Consider a 6 x 4 matrix. Prove that every column in A is in the span.pdfConsider a 6 x 4 matrix. Prove that every column in A is in the span.pdf
Consider a 6 x 4 matrix. Prove that every column in A is in the span.pdf
 
Consider a family tree that consists of just you, your parents,their.pdf
Consider a family tree that consists of just you, your parents,their.pdfConsider a family tree that consists of just you, your parents,their.pdf
Consider a family tree that consists of just you, your parents,their.pdf
 
Consider a discrete random variable X with moment generating functio.pdf
Consider a discrete random variable X with moment generating functio.pdfConsider a discrete random variable X with moment generating functio.pdf
Consider a discrete random variable X with moment generating functio.pdf
 
Consider a dinner table where n dining philosophers are seated. Each.pdf
Consider a dinner table where n dining philosophers are seated. Each.pdfConsider a dinner table where n dining philosophers are seated. Each.pdf
Consider a dinner table where n dining philosophers are seated. Each.pdf
 
Consider a $6500 piece of machinery , with a 5 year depreciable like.pdf
Consider a $6500 piece of machinery , with a 5 year depreciable like.pdfConsider a $6500 piece of machinery , with a 5 year depreciable like.pdf
Consider a $6500 piece of machinery , with a 5 year depreciable like.pdf
 
Conduct a company review of Starbucks. What are their unique growth .pdf
Conduct a company review of Starbucks. What are their unique growth .pdfConduct a company review of Starbucks. What are their unique growth .pdf
Conduct a company review of Starbucks. What are their unique growth .pdf
 
Computer-Based Training (CBT) Methods Please respond to the foll.pdf
Computer-Based Training (CBT) Methods Please respond to the foll.pdfComputer-Based Training (CBT) Methods Please respond to the foll.pdf
Computer-Based Training (CBT) Methods Please respond to the foll.pdf
 

Recently uploaded

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 

Recently uploaded (20)

Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Components of computer systems often have dependencies--other co.pdf

  • 1. Components of computer systems often have dependencies -- other components that must be installed before they will function properly. These dependencies are frequently shared by multiple components. For example, both the TELNET client program and the FTP cli ent program require that the TCP/IP networking software be installed before they can operate. If you install TCP/IP and the TELNET client program, and later decide to add the FTP client program, you do not need to reinstall TCP/IP. For some components it would not be a problem if the components on which they depended were reinstalled; it would just waste some resources. But for others, like TCP/IP, some component configuration may be destroyed if the component was reinstalled. It is useful to be able to r emove components that are no longer needed. When this is done, components that only support the removed component may also be removed, freeing up disk space, memory, and other resources. But a supporting component, not explicitly installed, may be removed only if all components which depend on it are also removed. For example, removing the FTP client program and TCP/IP would mean the TELNET client program, which was not removed, would no longer operate. Likewise, removing TCP/IP by itself would cause the fa ilure of both the TELNET and the FTP client programs. Also if we installed TCP/IP to support our own development, then installed the TELNET client (which depends on TCP/IP) and then still later removed the TELNET client, we would not want TCP/IP to be remo ved. We want a program to automate the process of adding and removing components. To do this we will maintain a record of installed components and component dependencies. A component can be installed explicitly in response to a command (unless it is alrea dy installed), or implicitly if it is needed for some other component being installed. Likewise, a component, not explicitly installed, can be explicitly removed in response to a command (if it is not needed to support other components) or implicitly remov ed if it is no longer needed to support another component. Input
  • 2. The input will contain a sequence of commands (as described below), each on a separate line containing no more than eighty characters. Item names are case sensitive, and each is no longer th an ten characters. The command names ( DEPEND , INSTALL , REMOVE and LIST ) always appear in uppercase starting in column one, and item names are separated from the command name and each other by one or more spaces. All appropriate DEPEND commands will appear before the occurrence of any INSTALL dependencies. The end of the input is marked by a line containing only the word END . Command Syntax Interpretation/Response DEPEND item1 item2 [item3 ...] item1 depends on item2 (and item3 ...) INSTALL item1 install item1 and those on which it depends REMOVE item1
  • 3. remove item1 , and those on whch it depends, if possible LIST list the names of all currently - installed components Output Echo each line of input. Follow each echoed INSTALL or REMOVE line with the actions taken in response, making certain that the actions are given in the proper order. Also identify exceptional conditions (see Expected Output , below, for examples of all case s). For the LIST command, display the names of the currently installed components. No output, except the echo, is produced for a DEPEND command or the line containing END . There will be at most one dependency list per item. Sample Input DEPEND TELNET TC PIP NETCARD DEPEND TCPIP NETCARD DEPEND DNS TCPIP NETCARD DEPEND BROWSER TCPIP HTML INSTALL NETCARD INSTALL TELNET INSTALL foo REMOVE NETCARD
  • 4. INSTALL BROWSER INSTALL DNS LIST REMOVE TELNET REMOVE NETCARD REMOVE DNS REMOVE NETCARD INSTALL NETCARD REMOVE TCPIP REMOVE BROWSER REMOVE TCPIP LIST END Output for the Sample Output DEPEND TELNET TCPIP NETCARD DEPEND TCPIP NETCARD DEPEND DNS TCPIP NETCARD DEPEND BROWSER TCPIP HTML INSTALL NETCARD Installing NETCARD INSTALL TELNET Installing TCPIP Installing TELNET INSTALL foo Installing foo REMOVE NETCARD NETCARD is still needed. INSTALL BROWSER Installing HTML Installing BROWSER INSTALL DNS Installing DNS LIST HTML BROWSER
  • 5. DNS NETCARD foo TCPIP TELNET REMOVE TELNET Removing TELNET REMOVE NETCARD NETCARD is still needed. REMOVE DNS Removing DNS REMOVE NETCARD NETCARD is still needed. INSTALL NETCARD NETCARD is already installed. REMOVE TCPIP TCPIP is still needed. REMOVE BROWSER Removing BROWSER Removing HTML Removing TCPIP REMOVE TCPIP TCPIP is not installed. LIST NETCARD foo END Solution TCPStream Class Interface The TCPStream class provides methods to send and receive data over a TCP/IP connection. It contains a connected socket descriptor and information about the peer – either client or server – in the form of the IP address and TCP port. TCPStream includes simple get methods that return
  • 6. address and port, but not the socket descriptor which is kept private. One of the advantages of programming with objects is the ability to logically group data members and methods to avoid exposing data, in this case the socket descriptor, to the calling program that it does not need to see. Each connection is completely encapsulated in each TCPStream object. TCPStream objects are created by TCPConnector and TCPAcceptor objects only, so the TCPStream constructors must be declared private to prevent them from being called directly by any other objects. The TCPStream class grants friend privileges to the TCPConnector and TCPAcceptor classes so they can access the TCPStream constructors to supply connected socket descriptors. #include #include #include #include using namespace std class TCPStream { int m_sd; string m_peerIP; int m_peerPort; public: friend class TCPAcceptor; friend class TCPConnector; ~TCPStream(); ssize_t send(char* buffer, size_t len); ssize_t receive(char* buffer, size_t len); string getPeerIP(); int getPeerPort(); private: TCPStream(int sd, struct sockaddr_in* address); TCPStream(); TCPStream(const TCPStream& stream); }; Constructor The constructor stores the connected socket descriptor then converts the socket information structure fields to a peer IP address string and peer TCP port. These parameters can be inspected with calls to TCPStream::getPeerIP() and TCPStream::getPeerPort().
  • 7. #include #include "tcpstream.h" TCPStream::TCPStream(int sd, struct sockaddr_in* address) : msd(sd) { char ip[50]; inet_ntop(PF_INET, (struct in_addr*)&(address->sin_addr.s_addr), ip, sizeof(ip)-1); m_peerIP = ip; m_peerPort = ntohs(address->sin_port); } Destructor The destructor simply closes the connection. TCPStream::~TCPStream() { close(m_sd); } Network I/O Methods TCPStream::send() and TCPStream::receive() simply wrap calls to read() and write(), returning the number of bytes sent and bytes received, respectively. No additional buffering or other capabilities are added. Get Peer Information TCPStream::getPeerIP() and TCPStream::getPeerPort() return the IP address and TCP port information of the peer to which the network application, client or server, are connected. You can get the same information from the sockets getpeername() function but it far easier to just capture that information when the connections are established. Clients know in advance to where they are connecting and the client’s socket address is returned the accept() function when the server accepts a client connection – see the TCPAcceptor::accept() method definition. In both cases the socket address information is passed to the TCPStream object when it is constructed. TCPConnector Class Interface TCPConnector provides the connect() method to actively establish a connection with a server. It accepts the server port and a string containing the server host name or IP address. If successful, a pointer to a TCPStream object is returned to the caller. #include #include "tcpstream.h" class TCPConnector {
  • 8. public: TCPStream* connect(int port, const char* server); private: int resolveHost(const char* host, struct in_addr* addr); }; Constructor/Destructor The TCPConnector class does not use any member variables so the default constructor and destructor generated by the C++ compiler are fine. No others are defined. Connect to Server [Lines 6-12] TCPConnector::connect() call takes a server host name or IP address string and the server listening port as arguments. The server struct sockaddr_in sin_family is set to PF_INET and the sin_port is set to the TCP port on which the server is listening for connections. #include #include #include #include "tcpconnector.h" TCPStream* TCPConnector::connect(const char* server, int port) { struct sockaddr_in address; memset (&address, 0, sizeof(address)); address.sin_family = AF_INET; address.sin_port = htons(port); if (resolveHostName(server, &(address.sin_addr)) != 0) { inet_pton(PF_INET, server, &(address.sin_addr)); } int sd = socket(AF_INET, SOCK_STREAM, 0); if (::connect(sd, (struct sockaddr*)&address, sizeof(address)) != 0) { return NULL; } return new TCPStream(sd, &address); } int TCPConnector::resolveHostName(const char* hostname, struct in_addr* addr) { struct addrinfo *res; int result = getaddrinfo (hostname, NULL, NULL, &res); if (result == 0) {
  • 9. memcpy(addr, &((struct sockaddr_in *) res->ai_addr)->sin_addr, sizeof(struct in_addr)); freeaddrinfo(res); } return result; } TCPAcceptor Class Interface TCPAcceptor includes member variables for the listening socket descriptor, the socket address information – IP address and TCP port – and a flag that indicates whether or not the TCPAcceptor has started listening for connections. Two public methods are supported. One to start the listening and the other to accept connections. #include #include #include "tcpstream.h" using namespace std; class TCPAcceptor { int m_lsd; string m_address; int m_port; bool m_listening; public: TCPAcceptor(int port, const char* address=""); ~TCPAcceptor(); int start(); TCPStream* accept(); private: TCPAcceptor() {} }; Constructor The constructor sets the member variables to as shown here. Setting m_lsd indicates that the lisetning socket has not been created. #include #include #include
  • 10. #include "tcpacceptor.h" TCPAcceptor::TCPAcceptor(int port, const char* address) : m_lsd(0), m_port(port), m_address(address), m_listening(false) {} Destructor If the listening socket has been created then it is closed in the destructor. TCPAcceptor::~TCPAcceptor() { if (m_lsd > 0) { close(m_lsd); } } Start Listening for Connections [Line 3-5] Creating a listening socket involves the most socket calls of any operation. Before going through the series of calls, TCPAcceptor::start() checks to see if a listening socket already exists. If so, the method just returns 0. int TCPAcceptor::start() { if (m_listening == true) { return 0; } m_lsd = socket(PF_INET, SOCK_STREAM, 0); struct sockaddr_in address; memset(&address, 0, sizeof(address)); address.sin_family = PF_INET; address.sin_port = htons(m_port); if (m_address.size() > 0) { inet_pton(PF_INET, m_address.c_str(), &(address.sin_addr)); } else { address.sin_addr.s_addr = INADDR_ANY; } int optval = 1; setsockopt(m_lsd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval); int result = bind(m_lsd, (struct sockaddr*)&address, sizeof(address)); if (result != 0) { perror("bind() failed");
  • 11. return result; } result = listen(m_lsd, 5); if (result != 0) { perror("listen() failed"); return result; } m_listening = true; return result; } TCPStream* TCPAcceptor::accept() { if (m_listening == false) { return NULL; } struct sockaddr_in address; socklen_t len = sizeof(address); memset(&address, 0, sizeof(address)); int sd = ::accept(m_lsd, (struct sockaddr*)&address, &len); if (sd < 0) { perror("accept() failed"); return NULL; } return new TCPStream(sd, &address); } #include #include #include "tcpacceptor.h" int main(int argc, char** argv) { if (argc < 2 || argc > 4) { printf("usage: server [] "); exit(1); } TCPStream* stream = NULL; TCPAcceptor* acceptor = NULL;
  • 12. if (argc == 3) { acceptor = new TCPAcceptor(atoi(argv[1]), argv[2]); } else { acceptor = new TCPAcceptor(atoi(argv[1])); } if (acceptor->start() == 0) { while (1) { stream = acceptor->accept(); if (stream != NULL) { size_t len; char line[256]; while ((len = stream->receive(line, sizeof(line))) > 0) { line[len] = NULL; printf("received - %s ", line); stream->send(line, len); } delete stream; } } } perror("Could not start the server"); exit(-1); } Echo Client The client application takes the server TCP port and IP address on the command line. For each connection a string is displayed and sent to the server, the echoed string is received back and displayed, then the connection is closed. The client will be defined in the file client.cpp #include #include #include #include "tcpconnector.h" using namespace std; int main(int argc, char** argv) { if (argc != 3) {
  • 13. printf("usage: %s ", argv[0]); exit(1); } int len; string message; char line[256]; TCPConnector* connector = new TCPConnector(); TCPStream* stream = connector->connect(argv[2], atoi(argv[1])); if (stream) { message = "Is there life on Mars?"; stream->send(message.c_str(), message.size()); printf("sent - %s ", message.c_str()); len = stream->receive(line, sizeof(line)); line[len] = NULL; printf("received - %s ", line); delete stream; } stream = connector->connect(argv[2], atoi(argv[1])); if (stream) { message = "Why is there air?"; stream->send(message.c_str(), message.size()); printf("sent - %s ", message.c_str()); len = stream->receive(line, sizeof(line)); line[len] = NULL; printf("received - %s ", line); delete stream; } exit(0); } #include #include #include #include using namespace std class TCPStream {
  • 14. int m_sd; string m_peerIP; int m_peerPort; public: friend class TCPAcceptor; friend class TCPConnector; ~TCPStream(); ssize_t send(char* buffer, size_t len); ssize_t receive(char* buffer, size_t len); string getPeerIP(); int getPeerPort(); private: TCPStream(int sd, struct sockaddr_in* address); TCPStream(); TCPStream(const TCPStream& stream); };