Introduction to Distributed Systems

advertisement
Practical Workbook
Introduction to Distributed Systems
Name
: ________________________
Year
: ________________________
Batch
: ________________________
Roll No
: ________________________
Department: ________________________
3rd Edition –2015
Department of Computer & Information Systems Engineering
NED University of Engineering & Technology,
Karachi – 75270, Pakistan
INTRODUCTION
‗Introduction to Distributed Systems‘ is offered in final year of engineering. In an age of ever increasing
information collection and the need to evaluate it, building systems which utilize the available compute
resources in everyone‘s home and hands should be driving the development of more sophisticated
distributed computing systems. Learning about this area of engineering is the need of the day.
The first and second lab sessions discuss Linux local sockets and Linux sockets over network respectively
in detail. In this way, students are familiarized with another classical inter process communication
mechanism. All the Linux system calls with the complete implementation of client side & server side
algorithms are discussed in first two lab sessions.
Lab Session three, four & five introduces XML. XML's set of tools allows developers to create web
pages, allow developers to set standards defining the information that should appear in a document,
making it easy to reuse that content in other applications or for other presentation environments. XML is
an international standard and it‘s beneficial to learn it. Lab Session six and seven are related to the
Implementation of Web services in ASP.Net and then it explains the consumption of that service in any
web application.
In lab session eight, programming in java is introduced. In lab nine, concept of multi-threading in Java is
introduced. In lab session ten applet programming in Java is discussed. Lab session eleventh elaborates
the indirect communication mechanism with group communication. It covers complete programming
model and implementation issues. It also covers basics of JGroup toolkit. Lab session twelfth deals with
implementation & usage of message queues using Java messaging service (JMS). It covers programming
details with JMS including the implementation (with installation and execution steps) of standard JMS
specification, OpenJMS.
Lab Session thirteenth gives detailed insight to ―Torrents‖. It explains the complete working flow and
specific terminologies related to them. Also method for creating torrents is discussed.
In the end, students are encouraged to develop a mini project related to Java Remote Method Invocation
(RMI). Java RMI is a mechanism that allows one to invoke a method on an object that exists in another
address space (either on same or different machine). The aim of this section is to motivate the students for
creating a very simple project using Java RMI showing a basic example of a distributed system.
CONTENTS
Lab #
Objective
Page #
1
2
3
4
5
6
7
8
9
10
11
Socket programming in Linux – local sockets
Socket programming in Linux over the network
Getting acquainted with XML
Understanding and creating DTD in XML
Understanding the concept of root elements and attributes in XML
Learning Web Services in ASP.Net
Consuming Web Services in Web Applications
Introduction to Java Programming
Multi-threaded Programming in Java
Programming Applets in Java
Understanding the concept of Indirect Communication and
exploring group communication mechanism via JGroup Toolkit.
Understanding the concept of message queues via Java Messaging
Service (JMS)
Getting Started with Torrents – Peer to Peer Computing
1
9
16
20
28
34
37
41
44
47
49
12
13
54
61
Mini Project
xx
Implementation of Group Chat Application using Java RMI with
Graphical User Interface (GUI)
64
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 01
OBJECT
Socket programming in Linux – local sockets
THEORY
A socket is a bidirectional communication device that can be used to communicate with another
process on the same machine or with a process running on other machines.
System calls for sockets
Theseare the system calls involving sockets:
a) socket—Creates a socket
#include <sys/socket.h>
sockfd = socket(intprotocol_family, intsocket_type, int
protocol);
The protocol modules are grouped into protocol families like AF_INET, AF_IPX, AF_PACKET
and socket types like SOCK_STREAM or SOCK_DGRAM.
Protocol Families
Name
Purpose
AF_UNIX, AF_LOCAL Local communication
AF_INET
IPv4 Internet protocols
AF_INET6
IPv6 Internet protocols
SOCK_STREAM provides sequenced, reliable, two-way, connection-based byte streams. An
out-of-band data transmission mechanism may be supported. SOCK_DGRAM supports
datagrams (connectionless, unreliable messages of a fixed maximum length).
The protocol specifies a particular protocol to be used with the socket. Normally only a single
protocol exists to support a particular socket type within a given protocol family, in which case
protocol can be specified as 0. However, it is possible that many protocols may exist, in which
case a particular protocol must be specified in this manner.
On success, a file descriptor for the new socket is returned. On error, -1 is returned, and errnois
set appropriately.
b) close—Destroys a socket
#include <unistd.h>
int close(intfd);
close() closes a file descriptor, so that it no longer refers to any file and may be reused.
close() returns zero on success. On error, -1 is returned, and errno is set appropriately.
1
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
c) connect—Creates a connection between two sockets
#include <sys/socket.h>
int connect(intsockfd, conststructsockaddr *addr,
socklen_taddrlen);
The connect() system call connects the socket referred to by the file descriptor sockfd to the
address specified by addr. The addrlen argument specifies the size of addr. The format of the
address in addr is determined by the address space of the socket sockfd.
The sockaddr structure is the basic structure for all system calls and functions that deal with
socket addresses. All pointers to other socket address structures are often cast to
pointerstosockaddrbeforeuseinvarious functions and system calls:
#include <netinet/in.h>
structsockaddr {
unsignedshortsa_family; // address family, AF_xxx
charsa_data[14];
// 14 bytes of protocol address
};
The Unix domain socket address structure is:
#include <sys/un.h>
structsockaddr_un{
shortsun_family;
//AF_UNIX
charsun_PATH[108]; // path name
};
If the socket sockfd is of type SOCK_DGRAM then addr is the address to which datagrams are
sent by default, and the only address from which datagrams are received. If the socket is of type
SOCK_STREAM, this call attempts to make a connection to the socket that is bound to the
address specified by addr. Generally, connection-based protocol sockets may successfully
connect() only once; connectionless protocol sockets may use connect() multiple times to change
their association.
If the connection or binding succeeds, zero is returned. On error, -1 is returned, and errno is set
appropriately.
d) bind—Labels a socket with an address
#include <sys/socket.h>
int bind(intsockfd, conststructsockaddr *addr,socklen_taddrlen);
When a socket is created with socket(), it exists in a name space (address family) but has no
address assigned to it. bind() assigns the address specified to by addr to the socket referred to by
the file descriptor sockfd. addrlen specifies the size, in bytes, of the address structure pointed to
by addr. Traditionally, this operation is called "assigning a name to a socket". It is normally
necessary to assign a local address using bind() before a SOCK_STREAM socket may receive
connections.
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
2
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
e) listen—Configures a socket to accept conditions
#include <sys/socket.h>
int listen(intsockfd, int backlog);
listen() marks the socket referred to by sockfd as a passive socket, that is, as a socket that will be
used to accept incoming connection requests using accept(). The backlog argument defines the
maximum length to which the queue of pending connections for sockfd may grow. If a
connection request arrives when the queue is full, the client may receive an error with an
indication of ECONNREFUSED or, if the underlying protocol supports retransmission, the
request may be ignored so that a later reattempt at connection succeeds.
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.
f) accept—Accepts a connection and creates a new socket for the connection
#include <sys/socket.h>
int accept(intsockfd, structsockaddr *addr, socklen_t *addrlen);
The accept() system call is used with connection-based socket types (SOCK_STREAM). It
extracts the first connection request on the queue of pending connections for the listening socket,
sockfd, creates a new connected socket, and returns a new file descriptor referring to that socket.
The newly created socket is not in the listening state. The original socket sockfd is unaffected by
this call. The argument addr is a pointer to a sockaddr structure. This structure is filled in with
the address of the peer socket, as known to the communications layer. When addr is NULL,
nothing is filled in; in this case, addrlen is not used, and should also be NULL.
The addrlen argument is a value-result argument: the caller must initialize it to contain the size
(in bytes) of the structure pointed to by addr; on return it will contain the actual size of the peer
address. The returned address is truncated if the buffer provided is too small; in this case, addrlen
will return a value greater than was supplied to the call.
If no pending connections are present on the queue, and the socket is not marked as nonblocking, accept() blocks the caller until a connection is present. If the socket is marked nonblocking and no pending connections are present on the queue, accept() fails with the error
EAGAIN or EWOULDBLOCK. In order to be notified of incoming connections on a socket, you
can use select() or poll(). A readable event will be delivered when a new connection is attempted
and you may then call accept() to get a socket for that connection.
On success, this returns a nonnegative integer that is a descriptor for the accepted socket. On
error, -1 is returned, and errno is set appropriately.
Making a server
The steps involved in establishing a socket on the server side are as follows:
1. Create a socket with the socket() system call.
2. Bind the socket to an address using the bind() system call. For a server socket on the Internet,
an address consists of a port number on the host machine.
3. Listen for connections with the listen() system call.
4. Accept a connection with the accept() system call. This call typically blocks until a client
connects with the server.
5. Send and receive data.
3
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Data isn‘tread and written directly via the server socket; instead, each time a program accepts anew
connection, Linux creates a separate socket to use in transferring data over thatconnection.
Making a client
The steps involved in establishing a socket on the client side are as follows:
1. Create a socket with the socket() system call.
2. Connect the socket to the address of the server using the connect() system call.
3. Send and receive data. There are a number of ways to do this, but the simplest is to use the
read() and write() system calls.
Local Sockets
Sockets connecting processes on the same computer can use the local namespacerepresented by the
synonyms AF_LOCAL and AF_UNIX.These are called local sockets orUNIX-domain sockets.Their
socket addresses, specified by filenames, are used only whencreating connections.
The socket‘s name is specified in structsockaddr_un. We must set the sun_familyfield to AF_LOCAL,
indicating that this is a local namespace.
The sun_path field specifiesthe filename to use and may be, at most, 108 bytes long.Any filename
canbe used, but the process must have directory write permissions, which permit addingfiles to the
directory.To connect to a socket, a process must have read permission forthe file. Even though different
computers may share the same file system, only processesrunning on the same computer can
communicate with local namespace sockets.
The only permissible protocol for the local namespace is 0.Because it resides in a file system, a local
socket is listed as a file.
Compilation and Execution
Use the following format for compiling the codes:
gccsourcefile.c –o outputfile
As a result you should get two output files. Run the output files in two separate terminals,
making sure you pass the socket filename as argument to the unix server at the command line.
CODING
un_server.c
//The pathname of the socket address is passed as an argument.
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/un.h>
#include <stdio.h>
void error(const char *)
{
perror(msg);
exit(0);
4
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
}
int main(intargc, char *argv[])
{
intsockfd, newsockfd, servlen, n;
socklen_tclilen;
structsockaddr_uncli_addr, serv_addr;
charbuf[80];
if ((sockfd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
error("creating socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, argv[1]);
servlen=strlen(serv_addr.sun_path)
sizeof(serv_addr.sun_family);
if(bind(sockfd,(structsockaddr *)&serv_addr,servlen)<0)
error("binding socket");
listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(structsockaddr)&cli_addr,&clilen);
if (newsockfd< 0)
error("accepting");
n=read(newsockfd,buf,80);
printf("A connection has been established\n");
write(1,buf,n);
write(newsockfd,"I got your message\n",19);
close(newsockfd);
close(sockfd);
return 0;
}
un_client.c
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdio.h>
void error(const char *)
{
perror(msg);
exit(0);
}
int main(intargc, char *argv[])
{
intsockfd, servlen,n;
structsockaddr_unserv_addr;
char buffer[82];
bzero((char *)&serv_addr,sizeof(serv_addr));
serv_addr.sun_family = AF_UNIX;
strcpy(serv_addr.sun_path, argv[1]);
servlen = strlen(serv_addr.sun_path) + sizeof(serv_addr.sun_family);
if ((sockfd = socket(AF_UNIX, SOCK_STREAM,0)) < 0)
error("Creating socket");
if (connect(sockfd, (structsockaddr*)&serv_addr, servlen)< 0)
error("Connecting");
printf("Please enter your message: ");
bzero(buffer,82);
fgets(buffer,80,stdin);
write(sockfd,buffer,strlen(buffer));
5
+
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
n=read(sockfd,buffer,80);
printf("The return message was\n");
write(1,buffer,n);
close(sockfd);
return 0;
}
EXERCISES
a. Implement the above server and client on your linux machine and write atleast two sample
inputs and outputs.
b. List three other mechanisms of interprocess communication that are implemented in Linux,
and explain how they work.
6
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
c. What does 1 represent in the line write(1,buffer,n)? What would 0 or 2 represent in
the same place?
d. Explain the following:
i. void perror(const char *s);
ii. void bzero(void *s, size_t n);
iii. ssize_t read(intfd, void *buf, size_t count);
iv.
ssize_t write(intfd, const void *buf, size_t count);
v. socklen_t
7
Introduction to Distributed Computer Systems
Lab Session 1
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
8
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 02
OBJECT
Socket programming in Linux over the network
THEORY
A socket is a bidirectional communication device that can be used to communicate with another
process on the same machine or with a process running on other machines. Internet programs
such as Telnet, rlogin, FTP, talk, and the World Wide Web use sockets.
Here, we focus on sockets set up over a network, enabling communication between two different
machines. The protocol families for such sockets are AF_INET and AF_INET6.
The socket address structure for IPv4 sockets is:
structsockaddr_in {
shortsin_family;
unsigned shortsin_port;
structin_addrsin_addr;
charsin_zero[8];
};
structin_addr {
unsigned long s_addr;
};
// e.g. AF_INET, AF_INET6
// e.g. htons(3490)
// see structin_addr, below
// zero this if you want to
// load with inet_pton()
The socket address structure for IPv6 sockets is:
struct sockaddr_in6 {
u_int16_tsin6_family;
u_int16_tsin6_port;
u_int32_tsin6_flowinfo;
struct in6_addrsin6_addr;
u_int32_tsin6_scope_id;
};
struct in6_addr {
unsigned chars6_addr[16];
};
// AF_INET6
// port number, Network Byte Order
// IPv6 flow information
// IPv6 address
// Scope ID
// load with inet_pton()
Pointers to both the above structures have to be type casted to sockaddr structure pointers. The
sockaddr structure has already been described in the previous lab dealing with local sockets.
A function that requires mention is:
#include <netdb.h>
structhostent *gethostbyname(const char *name);
The gethostbyname() function returns a structure of type hostent for the given host name. Here
name is either a hostname, or an IPv4 address in standard dot notation, or an IPv6 address in
colon (and possibly dot) notation. The hostent structure is defined as follows:
structhostent {
char*h_name;
char **h_aliases;
//official name of host
//alias list
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
inth_addrtype;
inth_length;
char **h_addr_list;
}
#define h_addrh_addr_list[0]
//host address type
//length of address
//list of addresses
//for backward compatibility
Another function that you may use is:
#include <netinet/in.h>
uint16_thtons(uint16_t hostshort);
The htons() function converts the unsigned short integer hostshort from host byte order to
network byte order.
Compilation and execution shall be done on two separate machines.
ALGORITHMS
Algorithm A1: Setting up a simple server in the internet domain using TCP
The port number is passed as an argument at the command line.
1. If the port number has not been passed as argument by the user, report ―missing port number‖
and exit.
2. Create a stream socket of AF_INET family with TCP protocol.
3. Initialize structure serv_addr, of type sockaddr_in, to zero.
4. Prepare the elements of serv_addr in the following manner:
a. Assign AF_INET to sin_family.
b. Convert the port number from ascii to integer.
c. Use htons(port_no) to fill in sin_port.
d. Assign INADDR_ANY to s_addr in sin_addr structure in serv_addr.
5. Bind the socket with the serv_addr structure.
6. Listen for connections to this socket.
7. Accept a connection to this socket. (A new socket will be established)
8. Initialize an array buffer of adequate size to zero.
9. Read from the new socket into the buffer array, and display its contents.
10. Write to the new socket: ―I got your message‖.
11. Close the new socket.
12. Close the original listening socket.
Algorithm A2: Setting up a simple client in the internet domain using TCP
The server‘s hostname and server port number are passed as arguments at the command line.
1. If the server‘s hostname has not been passed as argument by the user, report ―missing
hostname‖ and exit.
2. If the port number has not been passed as argument by the user, report ―missing port number‖
and exit.
3. Create a stream socket of AF_INET family with TCP protocol.
4. Initialize structure serv_addr, of type sockaddr_in, to zero.
5. Prepare the elements of serv_addr in the following manner:
a. Assign AF_INET to sin_family.
b. Convert the port number from ascii to integer. Use htons(port_no) to fill in sin_port.
10
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
c. Use gethostbyname(server_hostname) to get a hostent structure for the server.
d. Copy the server‘s address from the h_addr element of the hostent structure returned to
s_addr in sin_addr structure in serv_addr.
6. Connect to the server using the serv_addr structure and the socket established in step 3.
7. Initialize an array buffer of adequate size to zero.
8. Prompt the user for input.
9. Store the user‘s input in the buffer array.
10. Write the buffer array to the socket.
11. Reinitialize the array buffer to zero.
12. Read from the socket into the buffer array, and display its contents.
13. Close the socket.
EXERCISES
a. Implement the given server algorithm in C for your Linux machine.
11
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
b. Implement the given client algorithm in C for your Linux machine.
12
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
c. Why do we assign INADDR_ANY to s_addr in sin_addr structure in serv_addr?
d. Implement the given server and client algorithms in C for your Linux machine, changing
them such that:
 Datagram connections are formed using UDP,
 The server keeps accepting client connections and never closes the socket.
13
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
14
Introduction to Distributed Computer Systems
Lab Session 2
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
15
Introduction to Distributed Computer Systems
Lab Session 3
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 03
OBJECTIVE
Getting acquainted with XML
THEORY
XML was released in the late 90's and has since received a great amount of hype. The XML
standard was created by W3C to provide an easy to use and standardized way to store selfdescribing data (self-describing data is data that describes both its content and its structure).
XML is nothing by itself. XML is more of a "common ground" standard. The main benefit of
XML is that you can use it to take data from a program like Microsoft SQL, convert it into XML,
and then share that XML with a slough of other programs and platforms. Each of these receiving
platforms can then convert the XML into a structure the platform uses normally.
What makes XML truly powerful is the international acceptance it has received. Many
individuals and corporations have put forth their hard work to make XML interfaces for
databases, programming, office application, mobile phones and more. It is because of this hard
work that the tools exist to do these conversions from whatever platform into standardized XML
data or convert XML into a format used by that platform.
In the past, attempts at creating a standardized format for data that could be interpreted by many
different platforms (or applications) failed miserably. XML has largely succeeded in doing this.
Before continuing you should have a basic understanding of the following:
 HTML
 JavaScript
Applications of XML
Although there are countless numbers of applications that use XML, here are a few examples of
the current platforms and applications that are making use of this technology:



Cell Phones - XML data is sent to some cell phones. The data is then formatted by the
specification of the cell phone software designer to display text or images, and even to play
sounds!
File Converters - Many applications have been written to convert existing documents into the
XML standard. An example is a PDF to XML converter.
VoiceXML - Converts XML documents into an audio format so that you can listen to an
XML document.
If you are attempting to use XML for the first time, chances are you don't know or have the tools
that are necessary to view XML files. The good news is there are surpluses of free viewers
readily available to you. Chances are the web browser you have on your computer is XML ready,
so you need not bother.
16
Introduction to Distributed Computer Systems
Lab Session 3
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
If you have already programmed in HTML, XML syntax shouldn't be that much of a leap to
grasp. While there are a couple of new items in XML, it is still just a markup language that relies
on tags to get the job done.
For example, this XML document was used to keep track of students in a Computer Application's
Course.
XML Code:
<?xml version="1.0" encoding="ISO-8859-15"?>
<class_list>
<student>
<name>Robert</name>
<grade>A+</grade>
</student>
<student>
<name>Lenard</name>
<grade>A-</grade>
</student>
</class_list>
If you spend a few moments reading through the above XML document, you can see it has two
students, Robert and Lenard. This simple XML document readily displays the benefits of XML.
The data seems to almost describe itself, which is one of the main reasons XML was created!
Let‘s take a look at XML syntax by looking at the different parts of above example.
I think the only thing not easily understandable in above XML document example is that first
line with all the symbols and attributes. This rather confusing line in above example is referred to
as the XML Prolog.
XML Prolog
The prolog is an optional component of the XML document. If included, the prolog must
appear before the root element. A prolog consists of two parts: the XML declaration and the Document
Type Declaration (DTD). Depending on your needs, you can choose to include both, either, or neither of
these items in your XML document.
The Document Type Declaration is a file that contains the necessary rules that the XML code in this file
must follow. You may think of the DTD as the grammar that the XML document must abide by to be a
valid XML file. This is where you define what version of XML you are using. The current version is 1.0,
which it has been for quite some time now.
Note: DTD will be discussed in detail in upcoming lab sessions.
<?xml version="1.0" encoding="ISO-8859-15"?>
This line with special symbols and attributes is what is referred to as the XML declaration. It
simply states what version of XML you are using and the type of encoding you are using. Don't
worry too much about this line; just always include it exactly as we have done above.
17
Introduction to Distributed Computer Systems
Lab Session 3
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
XML Tag
The XML tag is very similar to that of the HTML tag. Each element is marked with an opening
tag and a closing tag. Below, we have highlighted both the opening and closing tags from a piece
of our example XML file.
<name>Robert</name>
<grade>A+</grade>
XML Elements
XML Elements are the meat and potatoes of XML. They contain the opening and closing tags,
child elements, and data. In our XML document the student element comprisesall of the above
information. Notice how it has a couple of child elements.
<student>
<name>Robert</name>
<grade>A+</grade>
</student>
XML Attributes:
Our simple XML document above did not include any XML attribute. Attributes are used to
specify additional information about the element. It may help to think of attributes as a means of
specializing generic elements to fit your needs. An attribute for an element appears within the
opening tag.
If there are multiple values an attribute may have, then the value of the attribute must be
specified. For example, if a tag had a color attribute then the value would be: red, blue, green,
etc.
The syntax for including an attribute in an element is:
<element attributeName="value">
In this example we will be using a made up XML element named "friend" that has an optional
attribute age.
XML Code:
<friend age="23">Jaseem</friend>
Note:
 Elements are used to classify data in an XML document so that the data becomes "selfexplanatory".
 Opening and closing tags represent the start and end of an element.
 Attributes are used to include additional information on top of the data that falls between the
opening and closing tag.
18
Introduction to Distributed Computer Systems
Lab Session 3
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
EXERCISES
a. What does XML stand for?
b. There is a way of describing XML data, how?
c. XML‘s goal is to replace HTML, justify.
d. What is the correct syntax of the declaration which defines the XML version?
e. What does DTD stand for?
19
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 04
OBJECTIVE
Understanding and creating DTD in XML
THEORY
XML is a standard, simple, self-describing way of encoding both text and data so that content
can be processed with relatively little human intervention and exchanged across diverse
hardware, operating systems, and applications.
XML offers a widely adopted standard way of representing text and data in a format that can be
processed without much human or machine intelligence. Information formatted in XML can be
exchanged across platforms, languages, and applications, and can be used with a wide range of
development tools and utilities.
XML is similar enough to HTML in its actual format (both are closely related to the SGML
markup definition language that has been an ISO standard since 1986) so that those familiar with
HTML can fairly easily pick up basic XML knowledge. But there are two fundamental
differences:


Separation of form and content -- HTML mostly consists of tags defining the appearance
of text; in XML the tags generally define the structure and content of the data, with actual
appearance specified by a specific application or an associated style sheet.
XML is extensible -- tags can be defined by individuals or organizations for some
specific application, whereas the HTML standard tag set is defined by the World Wide
Web Consortium (W3C).
There are two types of XML documents: well-formed or valid. The only difference between the
two is that one uses a DTD and the other doesn't.
Well-formed XML:
Well-formed documents conform with XML syntax. They contain text and XML tags.
Everything is entered correctly. They do not, however, refer to a DTD.
Valid XML:
Validdocuments not only conform to XML syntax but they also are error checked against a
Document Type Definition (DTD). A DTD, as defined in previous lab, is a set of rules outlining
which tags are allowed, what values those tags may contain, and how the tags relate to each
other.
Typically, you'll use a valid document when you have documents that require error checking, that
use an enforced structure, or are part of a company- or industry-wide environment in which many
documents need to follow the same guidelines.
DTD:
A Document Type Definition (DTD) is a set of rules that defines the elements, element attribute
and attribute values, and the relationship between elements in a document.
20
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
When your XML document is processed, it is compared to its associated DTD to be sure it is
structured correctly and all tags are used in the proper manner. This comparison process is called
validation and is performed by a tool called a parser.
Note:you don't need to have a DTD to create an XML document; you only need a DTD for a
valid XML document.
Here are a few reasons you'd want to use a DTD:
 Your document is part of a larger document set and you want to ensure that the whole set
follows the same rules.
 Your document must contain a specific set of data and you want to ensure that all
required data has been included.
 Your document is used across your industry and need to match other industry-specific
documents.
 You want to be able to error check your document for accuracy of tag use.
Deciding on a DTD
Using a DTD doesn't necessarily mean you have to create one from scratch. There are a number
of existing DTDs, with more being added everyday.
Shared DTDs
As XML becomes wide-spread, your industry association or company is likely to have one or
more published DTDs that you can use and link to. These DTDs define tags for elements that are
commonly used in your applications. You don't need to recreate these DTDs -- you just point to
them in your doctype tag in your XML file, and follow their rules when you create your XML
document.
Some of these DTDs may be public DTDs, like the HTML DTD. Others may belong to your
company. If you are interested in using a DTD, ask around and see if there is a good match that
already exists.
Create Your Own DTD
Another option is to create your own DTD. The DTD can be very simple and basic or it can be
large and complex. The DTD will be a reflection of the needs of your document.
It is perfectly acceptable to have a DTD with just four or five basic elements if that is what your
document needs. Don't feel that creating a DTD necessarily needs to be a huge undertaking.
However, if your documents are complex, do plan on setting aside time -- several days or several
weeks -- to understand the document and the document elements and create a solid DTD that will
really work for you over time.
Make an Internal DTD
You can insert DTD data within your DOCTYPE definition. If you're worked with CSS styles,
you can think of this as being a little like putting style data into your file header. DTDs inserted
this way are used in that specific XML document. This might be the approach to take if you want
to validate the use of a small number of tags in a single document or to make elements that will
be used only for one document.
Remember, the primary use for a DTD is to validate that the tags you enter in your XML
document are entered as specified in the DTD. It is an error-checking process that ensures your
data conforms to a set of rules.
21
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Going back to the types of XML documents:
Well-formed XML
A document that conforms to the XML syntax rules is called "well-formed" if all your tags are
correctly formed and follow XML guidelines then your document is considered a well-formed
XML document. That's one of the nice things about XML - you don't need to have a DTD in
order to use it.
To begin a well-formed document, type the XML declaration:
<?xml version="1.0" standalone="yes" encoding="UTF-8"?>
If you are embedding XML, it will go after the <HTML> and <HEAD> tags, and before any
Javascript.
If you are creating an XML-only document, it will be the first thing in the file.
Version
You must include the version attribute for the XML declaration. The version is currently "1.0."
Defining the version lets the browser know that the document that follows is an XML document,
using XML 1.0 structure and syntax.
Standalone
The next step is to declare that the document "stands alone." The application that is processing
this document knows that it doesn't need to look for a DTD and validate the XML tags.
Encoding
Finally, declare the encoding of the document. In this case, the encoding is UTF-8, which is the
default encoding for XML. You can leave off this attribute and the processor will default to UTF8.
Remember the root element
After the declaration, enter the tag for the root element of your document. This is the top-most
element, under which all elements are grouped.
Valid XML
A valid document conforms to the XML syntax rules and follows the guidelines of a Document
Type Definition (DTD).
The process of comparing the XML document to the DTD is called validation. This process is
performed by a tool called a parser.
To begin a well-formed document, type the XML declaration:
<?xml version="1.0" standalone="no" encode="UTF-8"?>
If you are embedding XML, it will go after the <HTML> and <HEAD> tags, and before any
Javascript.
If you are creating an XML-only document, it will be the first thing in the file.
Version
You must include the version attribute for the XML declaration. The version is currently "1.0."
22
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Defining the version lets the browser know that the document that follows is an XML document,
using XML 1.0 structure and syntax.
Standalone
The standalone="no" attribute tells the computer that it must look for a DTD and validate the
XML tags.
Encoding
Finally, declare the encoding of the document. You can leave off this attribute and the processor
will default to UTF-8.
To learn XML, open a text editor like SimpleText or Notepad and type:
<?xml version="1.0"?>
<PARENT>
<CHILD>
This is content.
</CHILD>
<EMPTY/>
</PARENT>
Save this as wellform.xml.
Description:
<?xml version="1.0"?> declares your XML is version 1.0, so programs know to use it
like XML 1.0.
<PARENT> is markup. XML is divided into markup and content. Markup is information about
content, describing it to any level of detail desired. Correct markup must follow certain rules.
First, markup containing content must have opening and closing tags. <PARENT> is an opening
tag </PARENT> is a closing tag. Opening tags start with < and close with >. Closing tags start
with </ and close with >.
Second, markup must nest properly. Markup tags divide into parents and children. Parent markup
encloses child markup. A child's opening and closing tags must be contained within its parent's
opening and closing tags.
You can have
<PARENT>
<CHILD>
This is content.
</CHILD>
</PARENT>
<PARENT>
<CHILD>
This is content.
...but not... </PARENT>
</CHILD>
<CHILD>
<PARENT>
This is content.
...or... </CHILD>
</PARENT>
Third, if markup contains no content, it must begin with < and end with /> like <EMPTY/>.
So if you declare your XML version, begin and end opening tags with < and > and closing tags
with </ and > , and insure child markup nests completely within parent markup. Start empty
markup with < and end it with />.
23
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
This XML is well-formed. wellform.xml is the simplest XML possible. If you need more power,
you need valid XML. Using your text editor, type in:
<?xml version="1.0"?>
<!DOCTYPE PARENT [
<!ELEMENT PARENT (CHILD*)>
<!ELEMENT CHILD (MARK?,NAME+)>
<!ELEMENT MARK EMPTY>
<!ELEMENT NAME (LASTNAME+,FIRSTNAME+)*>
<!ELEMENT LASTNAME (#PCDATA)>
<!ELEMENT FIRSTNAME (#PCDATA)>
<!ATTLIST MARK
NUMBER ID #REQUIRED
LISTED CDATA #FIXED "yes"
TYPE (natural|adopted) "natural">
<!ENTITY STATEMENT "This is well-formed XML">
]>
<PARENT>
&STATEMENT;
<CHILD>
<MARK NUMBER="1" LISTED="yes" TYPE="natural"/>
<NAME>
<LASTNAME>child</LASTNAME>
<FIRSTNAME>second</FIRSTNAME>
</NAME>
</CHILD>
</PARENT>
Save it as valid.xml. Valid XML is more complex than the well-formed XML used
in simple.xml
Description:
<?xml version="1.0"?>, fills the same role it did in simple.xml. The second line, <!DOCTYPE
PARENT [, declares this section is a document type definition or DTD and its name
(PARENT).
A DTD is the primary distinction between well-formed and valid XML. Well-formed XML can
give you a vague idea of an XML document's purpose but leaves room for doubt. A DTD
eliminates this by providing a stringent standard to measure a document against.
A DTD declares each part of an XML document and its proper form exactly. This DTD is
named PARENT. DTD's are enclosed between an opening [ and a closing ]>. <!ELEMENT
PARENT (CHILD)*>, shows a DTD's most basic part, the element.
An element defines markup's name and form.
<!ELEMENT PARENT (CHILD*)> declares:
 The markup tag's name (PARENT).
 The name of any child markup found within it (CHILD).
24
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering

How often it and any child markup within it are needed and can appear. (Both are optional
and can appear more than once, as indicated by the *).
The next line, <!ELEMENT CHILD (MARK?,NAME+)>, lists the children of the child
CHILD. It lists:
 The element's name (CHILD).
 The name of its children (MARK and NAME).
 That some of its children appear once or not at all (MARK, as indicated by the ?).
 That one of the children must appear one or more times (NAME, as indicated by the +).
You can have
<CHILD>
<NAME>
</NAME>
</CHILD>
Or
<CHILD>
<MARK/>
<NAME>
</NAME>
</CHILD>
but never
<CHILD>
<MARK/>
</CHILD>
<!ELEMENT MARK EMPTY>shows the element value EMPTY. This indicates markup
containing no content like...
<MARK/>
...or...
<EMPTY/>
from simple.xml.
The following line, <!ELEMENT NAME (LASTNAME+,FIRSTNAME+)*>, tells us:
 The element name (NAME).
 That the children of the element must appear sequentially (as indicated by the comma).
 That these choices may be made more than once or not at all (as indicated by the *).
The next two lines contain #PCDATA. #PCDATA indicates when markup contains content.
This can be anything and does not have to follow the same rules as markup.
The following line contains another DTD fundamental, the attribute. An attribute is a description
given to an element to further define it. Attributes are declared in attribute lists. The attribute list
in valid.xml is
<!ATTLIST MARK
NUMBER ID #REQUIRED
LISTED CDATA #FIXED "yes"
TYPE (natural|adopted) "natural">
This tells you:
 The element the attribute list is attached to (MARK).
 That the first attribute (NUMBER) is unique text (as indicated by ID) and required (as
indicated by #REQUIRED).
 That the second attribute (LISTED) is regular text (as indicated by CDATA) and fixed (as
indicated by #FIXED).
 That the third attribute (TYPE) is a choice (as indicated by the |) between two values
"natural" and "adopted") and what the default choice is ("natural").
25
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
The result is:
<MARK NUMBER="1" LISTED="yes" TYPE="natural"/> where, of the attributes
of MARK, "1" is required and unique, "yes" is regular text and fixed, and "natural" is the
chosen, default choice.
VALID.XML next brings up a third DTD part, the entity. The entity points to a something that
can be inserted at any point in the XML document. The line <!ENTITY STATEMENT "This is
well-formed XML"> inserts This is well-formed XML whenever &STATEMENT appears.
The DTD then closes with ]> and the rest of the XML document follows it as outlined.
EXERCISES
a. Is this a "well formed" XML document?
<?xml version="1.0"?>
<note>
<to>Jaseem</to>
<from>Kashif</from>
<heading>Reminder</heading>
<body>Don't forget to take me to ARENA this weekend!</body>
</note>
b. Is this a "well formed" XML document?
<?xml version="1.0"?>
<to>Kashif</to>
<from>Jaseem</from>
<heading>Reminder</heading>
<body>Don't forget me for dinner this weekend!</body>
c.




Which statement is true?
All XML elements must be lower case
All the statements are true
All XML documents must have a DTD
All XML elements must be properly closed
d. Is this a "well formed" XML document?
<?xml version="1.0"?>
<note>
<to age="29">Farrukh</to>
<from>Jaseem</from>
</note>
26
Introduction to Distributed Computer Systems
Lab Session 4
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
e. Is this a "well formed" XML document?
<?xml version="1.0"?>
<note>
<to age=29>Farrukh</to>
<from>Jaseem</from>
</note>
27
Introduction to Distributed Computer Systems
Lab Session 5
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 05
OBJECTIVE
Understanding the concept of root elements and attributes in XML
THEORY
In any markup language, the first element to appear is called the "root element", which defines
what kind of document the file will be. In an HTML file, the <html> tag is the root element. An
HTML file will always have the HTML element as the root element, while in an XML file, it can
be anything.
An element is the basic building block of HTML and XML documents. Elements are identified
by a tag. The tag consists of angle brackets and content, and looks like this:
<AUTHOR>Syed Jaseemuddin</AUTHOR>
In HTML, you use a pre-defined set of elements. In XML you create your own set of elements.
Attributes are like adjectives, in that they further describe elements. Each attribute has a name
and a value. Attributes contain an attribute values. The value might be a number, a word, or a
URL. Attribute values follow the attribute and an equal sign. In XML, attribute values are always
surrounded by quotation marks. Attributes are entered as part of the tag, like this:
<AUTHOR dob="1986">Syed Jaseemuddin</AUTHOR>
You use a tag to identify a piece of data by element name. Tags usually appear in pairs,
surrounding the data. The opening tag contains the element name. The closing tag contains a
slash and the element's name, like this:
<AUTHOR>Syed Jaseemuddin</AUTHOR>
The DTD defines the elements, attributes, and relationships between elements for an XML
document. A DTD is a way to check that the document is structured correctly, but you do not
need to use one in order to use XML, as debated earlier.
Now the root element, also referred to as the "document", dictates what kind of XML document
it is. When creating an HTML file that is XML complaint, the root element will be <html>. The
root element must be the first element in an XML document and there can only be one root
element per file!
The core of an XML document comes from the elements that are contained within the root
element. Each element represents a different type of data that is being stored in the document.
The element <p> might represent paragraph text, while the element <gif> may contain data for a
GIF image.
Although not required, elements often have attributes that are associated with them. XML
attributes are similar to HTML attributes in that they have a name: value relationship. An
28
Introduction to Distributed Computer Systems
Lab Session 5
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
example attribute for an <img> element might be "src", which represents the source location of
the image.
XML Element & Attribute Code:
<imgsrc = "C:/MyDocs/Happy.gif"></img>
In addition to the information stored in the element itself (attributes), the bulk of data in XML
usually appears between the opening and closing tag of an XML element. This is often referred
to as the XML's "content". Below, we have an imaginary XML document that stores a story.
XML Code:
<story>
<author>Jill Doe</author>
<title>The Truth About the Family</title>
<page>
<paragraph>Here's a story of a polite person, Jaseem, who...</paragraph>
<paragraph>...he became the lecturer at NED...</paragraph>
</page>
</story>
Can you tell which element is the root element in the above XML code?
As specified earlier that in an XML file, there can only be one root element. The root element
must encapsulate all other elements-meaning; these other elements must show up after the
opening root tag and before the closing root tag. Here is an example of an XML document with
the root element "phonebook".
<phonebook>
<number>
</number>
<name>
</name>
</phonebook>
Notice how the root element "phonebook" surrounds the other elements in XML file. Below is a
broken XML file. Try to see if you can find what is wrong with this file before reading the
caption below.
<phonebook>
<number>
</number>
<name>
</phonebook>
</name>
29
Introduction to Distributed Computer Systems
Lab Session 5
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
You should have noticed that the root element "phonebook" did not contain all other elements
because the closing name tag </name> is not between the root element tags <phonebook> and
</phonebook>
Another rule for XML files is that only one root element per file is allowed. Our previous
example followed this rule, but our example below does not because it has two root elements.
<phonebook>
<number>
</number>
<name>
</name>
</phonebook>
<diary>
<date>
</date>
</diary>
What are the two root elements? If you said the two root elements were "phonebook" and
"diary", then you got it right! Phone book is the first element to appear in this file, so it is
automatically a root element.
After the phonebook element is closed, no other elements should follow because XML can only
have one root element per file. Because the "diary" element did not follow this rule, it
transformed this XML file into a lawless, rule-breaking file!
EXERCISES
a. ►<breakfast_menu>...</breakfast_menu>
Above XML is a compressed XML document for food menu
This is how it looks like after expanding:
▼<breakfast_menu>
▼<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
▼<description>
two of our famous Belgian Waffles with plenty of real maple syrup
</description>
<calories>650</calories>
</food>
▼<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
▼<description>
light Belgian waffles covered with strawberries and whipped cream
</description>
<calories>900</calories>
</food>
▼<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
30
Introduction to Distributed Computer Systems
Lab Session 5
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
▼<description>
light Belgian waffles covered with an assortment of fresh berriesand whipped cream
</description>
<calories>900</calories>
</food>
▼<food>
<name>French Toast</name>
<price>$4.50</price>
▼<description>
thick slices made from our homemade sourdough bread
</description>
<calories>600</calories>
</food>
▼<food>
<name>Homestyle Breakfast</name>
<price>$6.95</price>
▼<description>
two eggs, bacon or sausage, toast, and our ever-popular hash browns
</description>
<calories>950</calories>
</food>
</breakfast_menu>
Identify the root elements if any.
b. Write an XML CD catalog document having CATALOG as the root element and following
sub-elements: [Assume their attributes if possible.]
Sub-elements: TITLE, ARTIST, COUNTRY, COMPANY, PRICE and YEAR
Note: Should cover at least 10 different titles.
31
Introduction to Distributed Computer Systems
Lab Session 5
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
c. Write an XML Students catalog document having UNDERGRAD as the root element and
following sub-elements: [Assume their attributes yourself.]
Sub-elements: NAME, DEPARTMENT, YEAR etc. as per your convenience.
Note: Should cover at least 10 different names.
32
Introduction to Distributed Computer Systems
Lab Session 5
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
33
Introduction to Distributed Computer Systems
Lab Session 6
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 06
OBJECTIVE
Learning Web Services in ASP.NET
THEORY
Web Services
Web service is the
platform applications that
communication
allows
platform
using
between
their
two
different
web
or same
method.
In the preceding definition of web service; different or same platform application means that I
can create a web service in any language, such as Java or other languages and that the language
web service can be used in a .Net based application and also a .Net web service or in another
application to exchange the information. and the second is web method that means method in
web services always start with [webMethod] attributes, it means that it is a web method that is
accessible anywhere, the same as my web application.
The World Wide Web Consortium (W3C) has defined the web services. ―Web Services are the
message-based design frequently found on the Web and in enterprise software. The Web of
Services is based on technologies such as HTTP, XML, SOAP, WSDL, SPARQL, and others.‖
Some of the major advantages include reusability, interoperability, loose coupling, ease of
integration and deploy-ability.
Creating Web Service in ASP.Net
Let us start using a different way to add a web service using a template.
1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
2. "File" - "New Project" - "C#" - "Empty Web Application" (to avoid adding a master
page).
3. Provide the web site a name such as "agetodays" or another as you wish and specify the
location.
4. Then right-click on Solution Explorer - "Add New
Item" - you see the web service templates.
5. Select Web Service Template and click on add
button. then after that the Solution Explorer look
like as follows:
6. Write the following code in Service.cs
using
using
using
using
using
System;
System.Collections.Generic;
System.Linq;
System.Web;
System.Web.Services;
Fig 6.1
34
Introduction to Distributed Computer Systems
Lab Session 6
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment
the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
public Service () {
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int converttodaysweb(int day, int month, int year)
{
DateTime dt = new DateTime(year, month, day);
int datetodays = DateTime.Now.Subtract(dt).Days;
return datetodays;
}
}
In the code above we have declared one integer method named converttodaysweb with the three
parameters day, month and year for accepting day, month and year from the user.
Then after that we have created an object of date time and passed those variables that I get from
the users. Then we declared another variable in the method that is datetodays to store the number
of days remaining from the user's input date to the current date and finally return that variable.
Fig 6.2: Execute the application
35
Introduction to Distributed Computer Systems
Lab Session 6
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Fig 6.3: Provide Inputs and click Invoke
Fig 6.4: Output
Exercises
1. Create an ASP.Net web service for four functions calculator following the steps discussed
in the lab.
36
Introduction to Distributed Computer Systems
Lab Session 7
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 07
OBJECTIVE
Consuming Web Services in ASP.Net Web Applications
THEORY
Consuming means to use the web services in an application. In previous lab, we learned how to
create a simple Web Service. This lab explains how to consume the Web Service in an ASP.Net
web Application. But the requirement is that we need to keep a Web Service application in
running mode so it can be accessible for use. Similarly we can use the same Web Service in
Windows, console application, Java and other applications.
Let us create the simple ASP.Net Web Application as:
1. "Start" - "All Programs" - "Microsoft Visual Studio 2010".
2. "File" - "New" - "Project..." then in the New Project window "C#" - "WebSite". Give the
project a name, such as "Consumingwebservice" or another as you wish and specify the
location.
3. Then drag three Text Boxes, one Button, and one Label onto the "Default.aspx" page.
In the web service‘s code, we take the three input values day, month and year so we have taken
the three text boxes and button click event. We will call the Web Service method and the output
will be displayed on the label control so we have also taken one button and label control.
Adding a Web Service Reference in ASP.Net Web Application
Right-click on the ASP.Net Web Application and click on "Add Service Reference".
Then after clicking on the above option, a new window will appear, and then click on the
"Advanced" tab.
2. Now after clicking on the Advanced tab, it will show the ―Service Reference Window‖
then click on the "Add Web Reference" option.
3. After clicking on the Add Web Reference tab, it will show the following window. Now
this is a very important step, when adding the web reference to the ASP.Net web
Application. Since you see "URL" option in the following window, on that window we
need to paste or type the Web Service URL address.
4. After clicking on the Add Web Reference tab, it will show the following window. Now
this is a very important step, when adding the web reference to the ASP.Net web
Application. Since you see "URL" option in the following window, on that window we
need to paste or type the Web Service URL address.
1.
37
Introduction to Distributed Computer Systems
Lab Session 7
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
5.
Run the web service we created in the previous lab. As you clearly see there, in the
window below, it displays the method named "converttodaysweb" as we created in our
Web Service, now just you need to copy URL and paste it into the Step 4 window URL
option, then the Step 4 window will look as in the following:
After pasting the URL in the preceding window box, click on the green right headed
arrow button, it will discover the Web Services available related to that URL address and
you see that in that related URL one Web Service is found message is displayed along
with the Web Service name, "Web Services" in the preceding right hand side window.
7. In the Solution Explorer of web application, web reference is now added.
6.
Calling Web Service Method from ASP.Net Web Application
The following is the procedure:
38
Introduction to Distributed Computer Systems
Lab Session 7
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
1. Go to the Default.aspx page of our ASP.Net Web application and double-click on the button
that we have placed on the Default.aspx page.
2. Now write the following code in the button click to create the object of the Web Service class:
protected void Button1_Click(object sender, EventArgs e)
{
localhost.Service age = new localhost.Service();
int day = int.Parse(TextBox1.Text.ToString());
int month = int.Parse(TextBox2.Text.ToString());
int year = int.Parse(TextBox3.Text.ToString());
int a = age.converttodaysweb(day, month, year);
Label1.Text = "You are currently" + "" + a.ToString() + " " + "Days old";
}
In the code above, we first created the object of the Web Service class named "age" followed by
Web reference name and Web Service class. Then we declared three integer variables "day",
"month" and "year" to store the values provided by the user as input from the Textbox1,
Textbox2 and Textbox3. Now, in the next step, as we know our new Web Service method takes
three parameters, so we passed the three input parameters "day", "month" and "year" to the Web
Service method "converttodaysweb". And finally I displayed the values returned by the Web
Service method "converttodaysweb" on the label control.
Now, run the ASP.Net web application and provide the input of day, month and year. Enter your
Date of birth and check the output.
39
Introduction to Distributed Computer Systems
Lab Session 7
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
EXERCISE
1. Create a web application that consumes the Web Service for four function calculator
developed in Lab # 06.
40
Introduction to Distributed Computer Systems
Lab Session 8
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 08
OBJECTIVE
Introduction to Java Programming
THEORY
The objects that interact in a distributed system need to be dealt with in ways that are intrinsically
different from objects that interact in a single address space. These differences are required because
distributed systems require that the programmer be aware of latency, have a different model of memory
access, and take into account issues of concurrency and partial failure.
Why Java?
Java is not restricted to any one medium, domain or technology. It is an ideal language for development
on the server. Its garbage collection support removes the tiresome need for developers to concern
themselves with the ownership semantics for objects, at the cost of some performance. Its simplistic
syntax reduces the learning curve for developers new to Java and its similarity to C++ allows for easy
migration of C++ developers to Java.
ClassLoaders
ClassLoaders are one of the most powerful technologies in Java; by allowing us, as developers, to control
from where code can be loaded, we can now distribute applications in ways that we couldn‘t dream about
five years ago.
Consider a system in which customized behavior needs to be developed for a series of clients, varying not
only on a per-client basis, but on a per-entity basis within the client. For example, an insurance company
wants to perform different tasks on the call-center representative‘s PC during an insurance sales call,
depending on what data is entered. Some sample ideas might be:
 Pop up a message box reminding the rep to suggestive-sell life-insurance policies to callers over
the age of 30
 Introduce new specials on various policies, but only if the candidate fits a particular criteria
 Remind the call center rep of the month‘s current internal promotional program, reminding
him/her to undertake particular actions based on the rep‘s proximity to the promotional target
Realistically, these sorts of monthly changes could drive a developer mad – a new release every month?
Recording, retesting, everything, every month?
Instead of coding these sorts of mutable rules directly within the application code, a custom ClassLoader
is set up. Create the custom ClassLoader at a particular point during the call and load code associated with
this call directly from the database, or from a socket, so long as the code is coming from a code source
separate from the application itself. This allows the developers to change the code associated with the
database without having to modify the existing code base.
On-the-fly code upgrades
For years, developers have been searching for ways to upgrade code without bringing the server (or any
clients using the code at the time of upgrade) completely down. Java allows us to do this sort of dynamic,
on-the-fly upgrade.
Your First Java Program
Let us lead you into the world of Java programming by taking you through the three basic steps required
to get a simple program running. The Java system is a collection of applications not unlike any of the
other applications that you are accustomed to using (such as your word processor, e-mail program, or
41
Introduction to Distributed Computer Systems
Lab Session 8
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
internet browser). As with any application, you need to be sure that Java is properly installed on your
computer. You also need an editor and a terminal application.
Programming in Java
We break the process of programming in Java into three steps:
1. Create the program by typing it into a text editor and saving it to a file named, say,
MyProgram.java.
2. Compile it by typing "javac MyProgram.java" in the terminal window.
3. Run (or execute)it by typing "java MyProgram" in the terminal window.
The first step creates the program; the second translates it into a language more suitable for machine
execution (and puts the result in a file named MyProgram.class); the third actually runs the program.

Creating a Java program:. A program is nothing more than a sequence of characters, like
a sentence, a paragraph, or a poem. To create one, we need only define that sequence
characters using a text editor. HelloWorld.java is an example program. Type these
characters into your text editor and save it into a file namedHelloWorld.java.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}

Compiling a Java program: At first, it might seem to you as though the Java
programming language is designed to be best understood by the computer. Actually, to
the contrary, the language is designed to be best understood by the programmer (that's
you). A compiler is an application that translates programs from the Java language to a
language more suitable for executing on the computer. It takes a text file with the.java
extension as input (your program) and produces a file with a .class extension (the
computer-language version). To compile HelloWorld.java type the boldfaced text
below at the terminal. (We use the % symbol to denote the command prompt, but it may
appear different depending on your system.)
% javac HelloWorld.java
If you typed in the program correctly, you should see no error messages. Otherwise, go
back and make sure you typed in the program exactly as it appears above.

Executing a Java program: Once you compile your program, you can run it. This is the
exciting part, where the computer follows your instructions. To run the
HelloWorldprogram, type the following at the terminal:
%java HelloWorld
If all goes well, you should see the following response:
Hello World
EXERCISES
a. Write a program TenHelloWorlds.java that prints "Hello World" ten times?
42
Introduction to Distributed Computer Systems
Lab Session 8
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
b. Describe what happens if, in HelloWorld.java, you omit/miss-spell:
i. public
ii. static
iii. void
iv.
args
c. I typed in the following program. It compiles fine, but when I execute it, I get the error
java.lang.NoSuchMethodError: main. What am I doing wrong?
Public class Hello {
Public static void main() {
System.out.println(Doesn’t execute”);
d. Study the basic programming constructs (conditional statements, loops, taking input from
user etc. in Java) and develop any program including all these constructs. Attach the print
out of source code and output. You may refer to internet resources & good Java Books.
43
Introduction to Distributed Computer Systems
Lab Session 9
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 09
OBJECTIVE
Multi-threaded Programming in Java
THEORY
In a networked world, it is common practice to share resources among multiple users. Therefore
application Programs designed to be deployed in a network should be designed to serve
simultaneously multiple users requests. With the increasing popularity of multicore processors, it
is common to see even desktop and laptop computers with an ability to carry out multiple tasks
concurrently. If two applications are run on a computer (MS Word, MS Access), two processes
are created. Multitasking of two or more processes is known as process-based multitasking.
Multitasking of two or more threads is known as thread-based multitasking. The concept of
multithreading in a programming language refers to thread-based multitasking. It can be
controlled by the programmer to some extent in a program.
Threads in Java
Fig: Main Thread & Children Threads
Fig: Creation of Threads in Java
Threads are objects in the Java language. They can be created by using two different
mechanisms.
 Create a class that extends the standard Thread class.
 Create a class that implements the standard Runnable interface.
Extending the Thread Class
The steps for creating a thread by using the first mechanism are:
1. Create a class by extending the Thread class and override the run () method:
class MyThread extends Thread {
public void run () {
// thread body of execution}
44
Introduction to Distributed Computer Systems
Lab Session 9
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
}
2. Create a thread object:
MyThread thr1 = new MyThread();
3. Start Execution of created thread:
thr1.start ();
An example program illustrating creation and invocation of a thread object is given below:
/* ThreadEx1.java: A simple program creating and invoking a thread object by
extending the standard Thread class. */
class MyThread extends Thread {
public void run() {
System.out.println(“ this thread is running ... ”);
}
}
class ThreadEx1 {
public static void main(String [] args ) {
MyThread t = new MyThread();
t.start();
}
}
Implementing the Runnable Interface
The steps for creating a thread by using the second mechanism are:
1. Create a class that implements the interface Runnable and override run () method:
class MyThread implements Runnable {
…
public void run() {
// thread body of execution
}
}
2. Creating Object:
MyThread myObject = new MyThread();
3. Creating Thread Object:
Thread thr1 = new Thread(myObject);
4. Start Execution:
thr1.start();
An example program illustrating creation and invocation of a thread object is given below:
/* ThreadEx2.java: A simple program creating and invoking a thread object by
implementing Runnable interface. */
class MyThread implements Runnable {
public void run() {
System.out.println(“ this thread is running ... ”);
}
}
class ThreadEx2 {
public static void main(String [] args ) {
Thread t = new Thread(new MyThread());
t.start();
45
Introduction to Distributed Computer Systems
Lab Session 9
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
}
}
Thread Class versus Runnable Interface
It is important to understand the implication of using these two different approaches. By
extending the thread class, the derived class itself is a thread object and it gains full control over
the thread life cycle. Implementing the Runnable interface does not give developers any control
over the thread itself, as it simply defines the unit of work that will be executed in a thread. When
extending the Thread class, the derived class cannot extend any other base classes because Java
only allows single inheritance. By implementing the Runnable interface, the class can still extend
other base classes if necessary.
EXERCISE
1. Explain the Life Cycle of Threads in Java. Clearly explain the operation of each state.
2. Implement the following equation in Java with multiple threads and observe the result
and attach Print out of Java Source Code & output.
p = sin (x) + cos (y) + tan (z)
Hint: As these trigonometric functions are independent operations without any dependencies
between them, they can be executed concurrently. After that their results can be combined to
produce the final result. You are also required to look at Java Maths Library and start & join
operations.
46
Introduction to Distributed Computer Systems
Lab Session 10
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 10
OBJECTIVE
Programming Applets in Java
THEORY
A Java applet is a small application written in Java and delivered to users in the form of bytecode. The user launches the Java applet from a web page and it is then executed within a Java
Virtual Machine (JVM) in a process separate from the web browser itself. A Java applet can
appear in a frame of the web page, a new application window, Sun's AppletViewer or a standalone tool for testing applets. Java applets were introduced in the first version of the Java
language in 1995.
Applets are used to provide interactive features to web applications that cannot be provided
by HTML alone. They can capture mouse input and also have controls like buttons or check
boxes. In response to the user action an applet can change the provided graphic content. This
makes applets well suitable for demonstration and visualization. An applet can also be a text area
only, providing, for instance, a cross platform command-line interface to some remote system.
Steps for Creating a Simple Applet
1.
2.
3.
4.
Write a simple applet in Java
Compile the Java source code
Create a HTML page that references the applet
Open the HTML page in a browser
An applet program is a written as inheritance of the java.Applet class. There is no main()
method in an Applet and an applet uses AWT for graphics.
Create a Java Source File
import java.applet.Applet;
import java.awt.*;
// Applet code for the "Hello, world!" example.
// This should be saved in a file named as "HelloWorld.java".
public class HelloWorld extends Applet {
// Print a message on the screen (x=20, y=10).
public void paint(Graphics g) {
g.drawString("Hello, world!", 20, 10);
// Draws a circle on the screen (x=40, y=30).
g.drawArc(40, 30, 20, 20, 0, 360);
}
}
47
Introduction to Distributed Computer Systems
Lab Session 10
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Compile the Source File
If the compilation succeeds, the compiler creates a file named HelloWorld.class in the same
directory (folder) as the Java source file (HelloWorld.java). This class file contains Java
bytecodes.
Create an HTML File that Includes the Applet
After compilation, the resulting .class file can be placed on a web server and invoked within
an HTML page by using an <applet> or an<object> tag.
<!DOCTYPE html>
<html>
<head>
<title>HelloWorld_example.html</title>
</head>
<body>
<h1>A Java applet example</h1>
<p>Here it is: <applet code="HelloWorld.class" height="40" width="200">
This is where HelloWorld.class runs.
</applet></p>
</body>
</html>
Run the Applet
When the page is accessed it will read as follows:
A Java applet example
Here it is: Hello, world!
EXERCISES
1. Create a sample Java Applet following the steps discussed in lab exploring more Java
graphics functions. Attach Print outs of Java Source file, HTML file and output screen.
2. Can Applets be written in any programming language other than Java? Support your
answer with proper reasoning.
48
Introduction to Distributed Computer Systems
Lab Session 11
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 11
OBJECTIVE
Understanding the concept of Indirect Communication and exploring group communication
mechanism via JGroup Toolkit.
THEORY
Indirect Communication
Indirect communication is defined as communication between entities in a distributed system
through an intermediary with no direct coupling between the sender and the receiver(s). The
precise nature of the intermediary varies from approach to approach.
The techniques that are all based on a direct coupling between a sender and a receiver, and this
leads to a certain amount of rigidity in the system in terms of dealing with change. To illustrate
this, consider a simple client-server interaction. Because of the direct coupling, it is more
difficult to replace a server with an alternative one offering equivalent functionality. Similarly, if
the server fails, this directly affects the client, which must explicitly deal with the failure. In
contrast, indirect communication avoids this direct coupling and hence inherits interesting
properties. The literature refers to two key properties stemming from the use of an intermediary:
 Space uncoupling, in which the sender does not know or need to know the identity of the
receiver(s), and vice versa. Because of this space uncoupling, the system developer has
many degrees of freedom in dealing with change: participants (senders or receivers) can
be replaced, updated, replicated or migrated.
 Time uncoupling, in which the sender and receiver(s) can have independent lifetimes. In
other words, the sender and receiver(s) do not need to exist at the same time to
communicate. This has important benefits, for example, in more volatile environments
where senders and receivers may come and go.
Group Communication
Group communication provides our first example of an indirect communication paradigm. Group
communication offers a service whereby a message is sent to a group and then this message is
delivered to all members of the group. In this action, the sender is not aware of the identities of
the receivers. Group communication represents an abstraction over multicast communication and
may be implemented over IP multicast or an equivalent overlay network, adding significant extra
value in terms of managing group membership, detecting failures and providing reliability and
ordering guarantees.
The Programming Model
In group communication, the central concept is that of a group with associated group
membership, whereby processes may join or leave the group. Processes can then send a message
to this group and have it propagated to all members of the group with certain guarantees in terms
of reliability and ordering. Thus, group communication implements multicast communication, in
which a message is sent to all the members of the group by a single operation. Communication to
49
Introduction to Distributed Computer Systems
Lab Session 11
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
all processes in the system, as opposed to a subgroup of them, is known as broadcast, whereas
communication to a single process is known as unicast.
The essential feature of group communication is that a process issues only one multicast
operation to send a message to each of a group of processes (in Java this operation is
aGroup.send (aMessage)) instead of issuing multiple send operations to individual processes.
Process Groups and Object Groups
Most work on group services focuses on the concept of process groups, that is, groups where the
communicating entities are processes. Such services are relatively low-level in that:
• Messages are delivered to processes and no further support for dispatching is provided.
• Messages are typically unstructured byte arrays with no support for marshalling of complex
data types (as provided, for example, in RPC or RMI).
The level of service provided by process groups is therefore similar to that of sockets. In contrast,
object groups provide a higher-level approach to group computing. An object group is a
collection of objects (normally instances of the same class) that process the same set of
invocations concurrently, with each returning responses. Client objects need not be aware of the
replication. They invoke operations on a single, local object, which acts as a proxy for the group.
The proxy uses a group communication system to send the invocations to the members of the
object group. Object parameters and results are marshalled as in RMI and the associated calls are
dispatched automatically to the right destination objects/methods.
Other Key Distinctions
A wide range of group communication services has been developed, and they vary in the
assumptions they make:
Closed and Open Groups: A group is said to be closed if only members of the group may
multicast to it. A process in a closed group delivers to itself any message that it multicasts to the
group. A group is open if processes outside the group may send to it. (The categories ‗open‘ and
‗closed‘ also apply with analogous meanings to mailing lists). Closed groups of processes are
useful, for example, for cooperating servers to send messages to one another that only they
should receive. Open groups are useful, for example, for delivering events to groups of interested
processes.
Overlapping and Non-Overlapping Groups: In overlapping groups, entities (processes or
objects) may be members of multiple groups, and non-overlapping groups imply that
membership does not overlap (that is, any process belongs to at most one group). Note that in
real-life systems, it is realistic to expect that group membership will overlap.
Implementation Issues

Reliability and Ordering In Multicast
In group communication, all members of a group must receive copies of the messages sent to the
group, generally with delivery guarantees. As well as reliability guarantees, group
communication demands extra guarantees in terms of the relative ordering of messages delivered
to multiple destinations.
50
Introduction to Distributed Computer Systems
Lab Session 11
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
 FIFO Ordering: First-in-first-out (FIFO) ordering (also referred to as source ordering) is
concerned with preserving the order from the perspective of a sender process, in that if a
process sends one message before another, it will be delivered in this order at all
processes in the group.
 Causal Ordering: Causal ordering takes into account causal relationships between
messages, in that if a message happens before another message in the distributed system
this so-called causal relationship will be preserved in the delivery of the associated
messages at all processes.
 Total Ordering: In total ordering, if a message is delivered before another message at one
process, then the same order will be preserved at all processes.

Group Membership Management
 Providing an interface for group membership changes: The membership service
provides operations to create and destroy process groups and to add or withdraw a
process to or from a group. In most systems, a single process may belong to several
groups at the same time (overlapping groups).
 Failure detection: The service monitors the group members not only in case theyshould
crash, but also in case they should become unreachable because of acommunication
failure. The detector marks processes as Suspected or Unsuspected.The service uses the
failure detector to reach a decision about the group‘smembership: it excludes a process
from membership if it is suspected to have failedor to have become unreachable.
 Notifying members of group membership changes: The service notifies the group‘s
memberswhen a process is added, or when a process is excluded (through failure orwhen
the process is deliberately withdrawn from the group)
 Performing group address expansion: When a process multicasts a message, it supplies
the group identifier rather than a list of processes in the group. The membership
management service expands the identifier into the current group membership for
delivery.
The JGroup Toolkit
JGroups is a toolkit for reliable group communication written in Java
(http://www.jgroups.org/index.html). JGroups supports process groups in which processes are
able to join or leave a group, send a message to all members of the group or indeed to a single
member, and receive messages from the group. The toolkit supports a variety of reliability and
ordering guarantees, which are discussed in more detail below, and also offers a group
membership service.
The architecture of JGroups has the following main components of the JGroups implementation:



Channels represent the most primitive interface for application developers, offering the
core functions of joining, leaving, sending and receiving.
Building blocks offer higher-level abstractions, building on the underlying service offered
by channels.
Protocol Stack provides the underlying communication protocol, constructed as a stack of
protocol layers.
JGroup API
51
Introduction to Distributed Computer Systems
Lab Session 11
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
The API of JGroups is very simple. The code is always the same, regardless of the protocol stack
used.
To be able to send/receive messages, a channel has to be created. The reliability of a channel is
specified via XML, which then causes the creation of the underlying protocol stack.
The example below creates a channel and sends/receives 1 message:
JChannel channel=new JChannel("/home/bela/udp.xml");
channel.setReceiver(new ReceiverAdapter() {
public void receive(Message msg) {
System.out.println("received
msg
from
"
+
msg.getSrc()
msg.getObject());
}
});
channel.connect("MyCluster");
channel.send(new Message(null, "hello world"));
channel.close();
+
":
"
+
The channel's configuration is defined in the constructor. In the sample code, we use an XML file
with an absolute path. If we use a relative path, then the file is looked up on the classpath.
The XML file contains a list of protocols to be used by the new channel.
To join a cluster, connect() is called. It returns when the member has successfully joined the
cluster named "MyCluster", or when it has created a new cluster (if it is the first member).
Then a message is sent using the send() method. A message contains the receiver's address (null
= all cluster nodes) and a byte buffer. In the example, the string "hello world" is set to be the
message's contents. It is serialized into the message's byte buffer.
Since the message is sent to all members, the sender will also receive it. This is done via the
receive() callback, which was registered with the channel before.
Finally, the member closes the channel and thus leaves the cluster. This result in a notification
being sent to all members who are registered for membership change notifications.
JGroup Demos
Students are advised to visit these sites: http://www.jgroups.org/manual/html_single/#d0e226
and http://www.jgroups.org/demos.html in order to have an insight of installation &
configuration of toolkit and also to have clear understanding of different examples present in the
demonstration.
Exercises
Q.1) Explain briefly Indirect Communication with at least two examples. Why do we need
Group Communication?
52
Introduction to Distributed Computer Systems
Lab Session 11
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Q.2) Explore JGroupToolkit. Discuss its key features also explain its Flexible Protocol
Stack.
53
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 12
OBJECTIVE
Understanding the concept of message queues via Java Messaging Service (JMS)
THEORY
Message queues (or more accurately, distributed message queues) are another important category
of indirect communication systems. Whereas groups and publish subscribe provide a one-tomany style of communication, message queues provide a point-to-point service using the concept
of a message queue as an indirection, thus achieving the desired properties of space and time
uncoupling. Here sender places the message into a queue, and it is then removed by a single
process. Message queues are also referred to as Message-Oriented Middleware. This is a major
class of commercial middleware with key implementations including IBM‘s WebSphere MQ,
Microsoft‘s MSMQ and Oracle‘s Streams Advanced Queuing (AQ).
Java Messaging Service (JMS)
The Java Messaging Service (JMS) is a specification of a standardized way for distributed Java
programs to communicate indirectly. A wide variety of implementations of the common
specification are now available, including Joram from OW2, Java Messaging fromJBoss, Sun‘s
Open MQ, Apache ActiveMQ and OpenJMS. Other platforms, including WebSphere MQ, also
provide a JMS interface on to their underlying infrastructure.
JMS distinguishes between the following key roles:
• A JMS client is a Java program or component that produces or consumesmessages, a JMS
producer is a program that creates and produces messages and aJMS consumer is a program that
receives and consumes messages.
• A JMS provider is any of the multiple systems that implement the JMSspecification.
• A JMS message is an object that is used to communicate information betweenJMS clients (from
producers
to
consumers).
Figure 12.1
54
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
JMS API Programming Model
Programming with JMS
The programming model offered by the JMS API is captured in Figure 1.1
To interact with a JMS provider, it is first necessary to create a connection between a client
program and the provider. This is created through a connection factory (a service responsible for
creating connections with the required properties). The resultant connection is a logical channel
between the client and provider; the underlying implementation may, for example, map onto a
TCP/IP socket if implemented over the Internet. Note that two types of connection can be
established, a TopicConnectionor a QueueConnection, thus enforcing a clear separation between
the two modes of operation within given connections. Connections can be used to create one or
more sessions – a session is a series of operations involving the creation, production and
consumption of messages related to a logical task. The resultant session object also supports
operations to create transactions, supporting all-or-nothing execution of a series of operations.
There is a clear distinction between topic sessions and queue sessions in that a
TopicConnectioncan support one or more topic sessions and a QueueConnectioncan support one
or more queue sessions, but it is not possible to mix session styles in a connection. The session
object is central to the operation of JMS, supporting methods for the creation of messages,
message producers and message consumers.
In JMS, a message consists of three parts: a header, a set of properties and the body of the
message. The header contains all the information needed to identify and route the message.
Properties are all user-defined and can be used to associate other application-specific metadata
elements with a message. For example, if implementing a context-aware system, the properties
can be used to express additional context associated with the message, including a location field.
In JMS, the body can be any one of a text message, a byte stream, a serialized Java object, stream
of primitive Java values or a more structured set of name/value pairs.
Getting Started with OpenJMS
OpenJMS is an open source implementation of Sun Microsystems's Java Message Service API
1.1 Specification.
Downloads & Installation
Before you can start using OpenJMS, you need to download the OpenJMS distribution to your
system. OpenJMS releases are available in both install and source archives from the SourceForge
download page. (http://openjms.sourceforge.net/downloads.html) For convenience, they are
provided in zip and tar/gzip formats. These include the OpenJMS server, the OpenJMS client
JARS, 3rd party JARS required by the client and server, the complete set of documentation,
scripts to run the server on Windows and UNIX and sample programs.
After you have downloaded a distribution, you need to install this on your system before you can
start using OpenJMS (http://openjms.sourceforge.net/adminguide/install.html).OpenJMS will run
on any platform where there is a suitable Java 2 runtime environment. Then you need to unpack
install archive. The install archive contains everything required to run OpenJMS on your system.
55
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
The archive has a single top-level directory named openjms-0.7.7-beta-1 with all the OpenJMS
related files beneath that.
Install archives with a .zip extension can be unpacked using winzip, or the jar tool (distributed
with the Java 2 SDK). E.g.:
jarxvf openjms-0.7.7-beta-1.zip
Install archives with a .tar.gz extension can be unpacked with gzip and tar. E.g.:
gzip -cd openjms-0.7.7-beta-1.tar.gz | tar xvf The OpenJMS server uses the following Environment variables.


JAVA_HOME
OPENJMS_HOME
Java Development Kit installation directory.
OpenJMS installation directory.
Running OpenJMS
Once you've installed OpenJMS, you need to start the OpenJMSserver.To start the OpenJMS
server, open a command prompt and type:
cd %OPENJMS_HOME%\bin
startup
(In Windows)
cd $OPENJMS_HOME/bin
startup.sh
(In UNIX)
Executing Programs
OpenJMS is distributed with a number of example programs that demonstrate writing simple
applications using JMS.
 Building the examples
Helper scripts are provided to compile the examples with the appropriate class path. To run these,
open a command prompt and type:
cd %OPENJMS_HOME%\examples\basic
build
(In Windows)
cd $OPENJMS_HOME/examples/basic
build.sh
(In Unix)

Running the examples
Helper scripts are also provided to run the examples. To run these, open a command prompt and
type:
run<classname>
(In Windows)
run.sh <classname> (In Unix)
56
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Example Code
Sender.java
importjavax.jms.*;
importjavax.naming.*;
public class Sender {
public static void main(String[] args) {
Context context = null;
ConnectionFactory factory = null;
Connection connection = null;
String factoryName = "ConnectionFactory";
String destName = null;
Destination dest = null;
int count = 1;
Session session = null;
MessageProducer sender = null;
String text = "Message ";
try {
// create the JNDI initial context.
context = new InitialContext();
// look up the ConnectionFactory
factory = (ConnectionFactory) context.lookup(factoryName);
// look up the Destination
dest = (Destination) context.lookup(destName);
// create the connection
connection = factory.createConnection();
// create the session
session = connection.createSession(
false, Session.AUTO_ACKNOWLEDGE);
// create the sender
sender = session.createProducer(dest);
// start the connection, to enable message sends
connection.start();
for (int i = 0; i < count; ++i) {
TextMessage message = session.createTextMessage();
message.setText(text + (i + 1));
sender.send(message);
System.out.println("Sent: " + message.getText());
}
} catch (JMSException exception) {
exception.printStackTrace();
} catch (NamingException exception) {
exception.printStackTrace();
} finally {
// close the context
if (context != null) {
try {
context.close();
57
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
} catch (NamingException exception) {
exception.printStackTrace();
}
}
// close the connection
if (connection != null) {
try {
connection.close();
} catch (JMSException exception) {
exception.printStackTrace();
}
}
}
}
}
Receiver.java
importjavax.jms.*;
importjavax.naming.*;
public class Receiver {
public static void main(String[] args) {
Context context = null;
ConnectionFactory factory = null;
Connection connection = null;
String factoryName = "ConnectionFactory";
String destName = null;
Destination dest = null;
int count = 1;
Session session = null;
MessageConsumer receiver = null;
try {
// create the JNDI initial context
context = new InitialContext();
// look up the ConnectionFactory
factory = (ConnectionFactory) context.lookup(factoryName);
// look up the Destination
dest = (Destination) context.lookup(destName);
// create the connection
connection = factory.createConnection();
// create the session
session = connection.createSession(
false, Session.AUTO_ACKNOWLEDGE);
// create the receiver
receiver = session.createConsumer(dest);
// start the connection, to enable message receipt
connection.start();
for (int i = 0; i < count; ++i) {
Message message = receiver.receive();
58
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
if (message instanceofTextMessage) {
TextMessage text = (TextMessage) message;
System.out.println("Received: " + text.getText());
} else if (message != null) {
System.out.println("Received non text message");
}
}
} catch (JMSException exception) {
exception.printStackTrace();
} catch (NamingException exception) {
exception.printStackTrace();
} finally {
// close the context
if (context != null) {
try {
context.close();
} catch (NamingException exception) {
exception.printStackTrace();
}
}
// close the connection
if (connection != null) {
try {
connection.close();
} catch (JMSException exception) {
exception.printStackTrace();
}
}
}
}
}
Exercises
Q.1) Explain the basic building blocks of JMS API Programming Model.
59
Introduction to Distributed Computer Systems
Lab Session 12
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Q.2) Give the step by step flow of example programs discussed in this lab session.
60
Introduction to Distributed Computer Systems
Lab Session 13
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Lab Session 13
OBJECTIVE
Getting Started with Torrents – Peer to Peer Computing
THEORY
BitTorrent (often abbreviated to 'BT') is a protocol that allows you to download files quickly and
efficiently. It is a peer to peer protocol, which means you download and upload to other people
downloading the same file. BitTorrent is often used for distribution of large files (especially
multi-media files) or popular content as it is a cheap, fast, efficient way to distribute files to
users. µTorrent is a BitTorrent client, so it speaks the BitTorrent protocol, much like a browser
would speak HTTP. Just as there are multiple web browsers, there are multiple BitTorrent
clients, and µTorrent is the most popular.
Some of the more common terms related to BitTorrent include:

Leechers - People who download files but do not share files on their own computer with others

Seed or seeder - A computer with a complete copy of a BitTorrent file (At least one seed
computer is necessary for a BitTorrent download to operate.)

Swarm - A group of computers simultaneously sending (uploading) or receiving (downloading)
the same file

.torrent - A pointer file that directs your computer to the file you want to download

Tracker - A server that keeps track of the peers and seeds in a swarm. A tracker does not have a
copy of the file itself, but it helps manage the file transfer process.

Client - the BitTorrent software used to download and upload files.
Steps for Installing and Configuring BitTorrent
1. Download and install the BitTorrent client software (Eg.µTorrent)
2. Find files to download.
3. Download and open the .torrent pointer file.
4. Let BitTorrent give and receive pieces of the file.
5. Stay connected after the download completes to share your .torrent files with others.
BitTorrent is designed to distribute data in such a way that the original distributor would be able
to decrease bandwidth usage while still being able to reach at least the same amount of people.
The idea is to "break" the file being transferred into smaller segments called pieces. To save
bandwidth, each person downloading (more commonly referred to as peers in the BitTorrent
community) would have the pieces that they acquired available for upload to other peers in
61
Introduction to Distributed Computer Systems
Lab Session 13
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
the swarm (the entire network of people connected to a single torrent). In this way, much of the
load of sharing the file to every peer interested in it is offloaded to the peers.
While on the surface, it appears that the only way to maintain a swarm's health is for there to
always be a seed connected to the swarm that is not the case. The most important factor to
determining whether a swarm can continue to allow peers to complete a torrent is
the availability. The availability of a torrent is the number of complete copies of the torrent
contents there are distributed in the part of the swarm you're connected to, including the data you
have. In most cases, if there is an availability of 1.0 or greater, then even if one single person
does not have all the pieces, they are all still distributed across the entire swarm and can be
acquired to form the complete file.
In order for everyone to be able to locate one another, there needs to be some centralized location
that peers could connect to in order to obtain the other peers' IP addresses. BitTorrent trackers
serve as this centralized location. In the most basic explanation, for each given swarm, a tracker
only needs to collect a peer's IP address and port number to share with other peers connecting to
that same swarm.
A torrent file is a computer file that contains metadata about files and folders to be distributed,
and usually also a list of the network locations of trackers. It does not contain the content to be
distributed; it only contains information about those files, such as their names, sizes, folder
structure, and cryptographic hash values for verifying file integrity. Depending on context,
a torrent may be the torrent file or the referenced content. Torrent files are normally named with
the extension .torrent, as in MyFile.torrent.
Create a New Torrent

A new torrent easily using µTorrent's built-in torrent maker: µTorrent menu > File > Create
a New torrent. This opens the "Create a new .torrent" dialog box Select "Add a File" or "Add
a directory." Browse to the location of file or directory you would like to share.

Enter Tracker: A tracker controls the transfer of data for torrent. A tracker looks similar to an
ordinary URL. A list of working public bittorrent trackers can be Googled. Some popular
trackers are listed below:
http://open.tracker.thepiratebay.org/announce
http://www.torrent-downloads.to:2710/announce
http://denis.stalker.h3q.com:6969/announce
udp://denis.stalker.h3q.com:6969/announce
http://www.sumotracker.com/announce
If you are using a private tracker then be sure to tick the box to mark it as private. Do not tick
this box otherwise.

Add any additional information: Any comment for the torrent can be added. The piece size
should be left at the automatic setting unless you know what you're doing. Once you have
filled in all the required areas, create your torrent. You may be asked where to save and what
to call the .torrent file.
62
Introduction to Distributed Computer Systems
Lab Session 13
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering

Start seeding & Upload torrent: Some clients have a seed mode specifically for this. If you
do not seed your torrent it will be useless because nobody can download your files. Upload
your torrent in any torrents website where ―Upload‖ option is available.

Naming
Convention:
A
torrent
movies_1996_CAM_MPG_BYME.torrent"
"title_episode_quality_format_maker.torrent".
file
which
named
would
"My
Vacation
translate
as:
 "Title" should be simple and descriptive if needed.
 "Episodes" is a number used when dealing with sequential torrents. It can be a date, a
release, or a label with season and episode (common formats include 102, 1x02, and
s01e02, which all translate to season 1, episode 2).
 "Quality" denotes the source or the type of file.
 "Format" gives peers an indicator of what will be needed to use the content (xvid, doc,
avi, mp3, iso, etc).
 "Maker" shows the credits for the torrent and/or the content.
EXERCISE
1. Share your ―Distributed Systems‖ book publically by creating a new torrent following the
steps discussed in lab. Take the snapshot of every step and attach the print out.
63
Introduction to Distributed Computer Systems
Mini Project
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
MINI PROJECT
Java Remote Method Invocation (RMI)
Remote Method Invocations (RMI) facilitates object function calls between Java Virtual
Machines (JVMs). JVMs can be located on separate computers - yet one JVM can invoke
methods belonging to an object stored in another JVM. Methods can even pass objects that a
foreign virtual machine has never encountered before, allowing dynamic loading of new classes
as required. This is a powerful feature.
Consider the following scenario:


Developer A writes a service that performs some useful function. He regularly updates
this service, adding new features and improving existing ones.
Developer B wishes to use the service provided by Developer A. However, it's
inconvenient for A to supply B with an update every time.
Java RMI provides a very easy solution! Since RMI can dynamically load new classes,
Developer B can let RMI handle updates automatically for him. Developer A places the new
classes in a web directory, where RMI can fetch the new updates as they are required.
Figure 1 - Connections made when client uses RMI
Figure 1 shows the connections made by the client when using RMI. Firstly, the client must
contact an RMI registry, and request the name of the service. Developer B won't know the exact
location of the RMI service, but he knows enough to contact Developer A's registry. This will
point him in the direction of the service he wants to call.
Developer A's service changes regularly, so Developer B doesn't have a copy of the class. Not to
worry, because the client automatically fetches the new subclass from a webserver where the two
developers share classes. The new class is loaded into memory, and the client is ready to use the
new class. This happens transparently for Developer B - no extra code need to be written to fetch
the class.
Following steps are performed in order to implement any Java RMI service:
64
Introduction to Distributed Computer Systems
Mini Project
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering





Define the remote interface
Implement the server
Implement the client
Compile the source files
Start the Java RMI registry, server, and client
The diagram illustrates the process of setting up and accessing a remote object.
Figure 2 – Accessing a Remote Object
The RMI Layered System
The RMI system consists of three layers:



The stub/skeleton layer - client-side stubs (proxies) and server-side skeletons
The remote reference layer - remote reference behavior (e.g. invocation to a single object
or to a replicated object)
The transport layer - connection set up and management and remote object tracking
The application layer sits on top of the RMI system. The relationship between the layers is shown
in the following figure.
65
Introduction to Distributed Computer Systems
Mini Project
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Figure 3 – RMI Layered System
A remote method invocation from a client to a remote server object travels down through the
layers of the RMI system to the client-side transport, then up through the server-side transport to
the server.
A client invoking a method on a remote server object actually makes use of a stub or proxy for
the remote object as a conduit to the remote object. A client-held reference to a remote object is a
reference to a local stub. This stub is an implementation of the remote interfaces of the remote
object and forwards invocation requests to that server object via the remote reference layer.
The remote reference layer is responsible for carrying out the semantics of the invocation. For
example the remote reference layer is responsible for determining whether the server is a single
object or is a replicated object requiring communications with multiple locations. Each remote
object implementation chooses its own remote reference semantics-whether the server is a single
object or is a replicated object requiring communications with multiple locations.
The transport is responsible for connection set-up, connection management, and keeping track of
and dispatching to remote objects (the targets of remote calls) residing in the transport's address
space.
In order to dispatch to a remote object, the transport forwards the remote call up to the remote
reference layer. The remote reference layer handles any server-side behavior that needs to be
done before handing off the request to the server-side skeleton. The skeleton for a remote object
makes an up-call to the remote object implementation which carries out the actual method call.
The return value of a call is sent back through the skeleton, remote reference layer and transport
on the server side, and then up through the transport, remote reference layer and stub on the
client side.
The RMI package only includes UnicastRemoteObject, which is a class in the RMI package
that is extended by the client interface object to allow point-to-point communication. If other
strategies are desired, they would have to be developed.
66
Introduction to Distributed Computer Systems
Mini Project
NED University of Engineering & Technology – Department of Computer & Information Systems Engineering
Project Description:
In this project, implement a Group Chat example using Java RMI with a Graphical User
Interface. Students are allowed to use any Integrated Development Environment (e.g. Eclipse or
Net Beans) for the ease in project development.
This project must accomplish the following tasks namely:






Implementation of Server Side with RMI registry
Implementation of Client Side
Packaging the project as a Java tool
Correct execution of both Server Side & Client Side
At least 2 Test Cases for verifying the correctness of program‘s logic
Prepare a 2 to 3 pages write up for the given project including the detailed problem
statement and the possible solution
This could be the possible screen shot of your final Group Chat Application.
Figure 4 – Group Chat Application using Java RMI
67
Download