Title: GSAMS Java
1GSAMS Java
2Networking Topics
- Overview A Short History of Java Networking
- Where from where to Are We There Yet?
- Competing Networking Models OSI v. Internet
- Whose Kung Fu is Better?
- Network Protocols in Java
- TCP
- UDP
- Multicast
- Threaded and Non-Threaded examples
- Remote Method Invocation (RMI)
3A Short History of Java Networking
- The Dark Ages C/C Networking and the Need for
Change - Before Java, simple network connectivity required
lots of code. - C/C provided many, many choices/options for
networking - Good for C programmers.
- Most programmers did not use all these options.
- Java 1.0
- Basic networking capability with high level
abstraction of URL handlers. - Low level choices were missing (e.g., connection
timeouts.) - Raw socket classes were final--java.net.Socket
and java.net.ServerSocket could not be extended. - One shot implementations with
java.net.SocketImpl allowed only one
implementation in each VM.
4A Short History of Java Networking
- JDK 1.1 (the big leap)
- Added four common socket options.
- Allowed raw data manipulations within high-level
abstractions. - Removed final designations.
- Added multicast connections (previously limited
to LAN and/or external native calls). - JDK 1.2 (a second big leap)
- Addition of fine-grained Java Security Model.
- ability to control processes/connections/ports
with very fine granular control over details. - Added functionality to APIs.
5Competing Networking ModelsOSI vs. Internet
Whose Kung Fu is Better?
- Two competing models are used to describe
networking - Open Systems Interconnection (OSI) architecture
- Internet Architecture
- Open Systems Interconnection Standard
- partitions network connectivity into seven
layers. - published by OSI and International
Telecommunications Union (ITU) in series of X
dot standards X.25, X.400, etc. - Internet Architecture Standard
- also known as TCP/IP architecture after its two
key protocols - evolved from packet-switched ARPANET of DoD
- Internet Engineering Task Force (IETF)
maintenance.
6A Comparison of the OSI Seven Layer Model and
the Taco Bell Seven Layer Burrito
Application
Data in use
Presentation
Syntax
Session
Packet recovery sessions between applications
Transport
Message quality end-to-end error correction
Network
Packet routing network connections
Data Link
Bit level organization reliability
Physical
Media, electrical properties
7OSI in Action
End Host
End Host
Network traffic in the OSI model must always
travel up and down the protocol stack to be
routed.
Application
Application
Presentation
Presentation
Session
Session
Nodes in Network
Transport
Transport
Network
Network
Network
Network
Data Link
Data Link
Data Link
Data Link
Physical
Physical
Physical
Physical
8Internet Architecture Model
Design theory rough consensus and running code
The IETF culture requires that new layers provide
a protocol specification and at least two or
three running implementations. Likely, the four
layer model will not grow in complexity (unless
the internet does first.)
9Internet Architecture Model
The Internet Architecture is also flexible
applications can reach the network layer
directly, or work through protocols such as
TCP/UDP and IP
APPLICATION
TCP
UDP
IP
NETWORK
10Internet Architecture Model
Transport through each layer requires the use of
additional headers.
Application Layer
DATA
Transport Layer
Internet Layer
Network Layer
11Network Protocols in Java
In terms of protocols and services, Java offers
API support for three basic types of
connectivity TCP -- Transport Control
Protocol UDP -- User Datagram Protocol Mcast
-- Multicast Protocol. -- Basic Terminology
-- A Socket is an abstraction of a
"communications link" between machines over some
network. Socket communication is the same
regardless of whether the network connection is
via a phone line, cable modem, ethernet, or
fiber-optic line. A packet is a discrete
quantity of information suitable for routed
transport over a shared network. Packet sizes
are limited, so a packet may be a fragment of a
large file or message.
java.net package
12Basic Terminology (contd)
Naming and Addressing The IPv4 addressing
scheme uses 32-bit addresses, often presented as
a set of four octets. Read from left to right,
we progressively define a particular
machine 192.168.1.200 The dotted quad or
dotted decimal format is quite common.
Numbering is controlled by the Internet
Assigned Numbers Authority (IANA), and later the
Internet Corporation for Assigned Names and
Numbers (ICANN).
13IP Address Classes
First bit 0
A
127.0.0.0 and below
127.0.1.0 to 191.255.255.255
B
First bits 1 0
192.0.1.0 to 223.255.255.255
First bits 1 1 0
C
Not allocated to networks -- mcast
First bits 1 1 1 0
D
E
240.0.0.0 and above not assigned
First bits 1 1 1 1
Special numbers 127.0.0.1 -- used for loopback
addresses 192.168.x.x -- class B private
networks 10.x.x.x. -- class A private networks
14IP Address Classes
The IPv4 address scheme is limited by potentially
wasteful address allocation. For example, the
jump from class C (one octet, or 254 addresses)
to class B (two octets, or 65334 addresses) is
quite large. So, a company with more than 250
computers might request a class B address, but
use far less than the 65,000 possible
combinations. This inefficiency consumes the
address space quickly. Solutions include more
efficient number allocations, ip chaining
(firewalls), and migration to IPv6 (128 bit
address), . . . even IPv8. Javas underlying
native code has support for IPv6, so it will be
ready if/when companies (notably Cisco) decide to
embrace IPv6.
15IP Addresses Names
To make IP addresses readable, a table of names
is used to represent IP numbers. The names are
in a hierarchical order however, there is no
one-to-one mapping between name hierarchies and
dotted quad number hierarchies. DNS, or Domain
Name Service, provides a name to IP addressing
service.
(Root)
On a Unix machine, one often uses nslookup or
some similar program to covert between names and
numbers.
16Ports
Most computers have a single network connection
used for all data. Most operating systems allow
multi-tasking, so several network applications
can be running at once. How does the computer
know which application gets the data? Ports
allow us to specify specific applications on the
host. The TCP and UDP protocols attach port
information (16 bit number) to each packet.
Thus, when creating connections between
computers, we need an IP address as well as the
port to specify the machine and client process
17The java.net. Package
Key Classes java.net.InetAddress IP
Address structures, and services java.net.ServerS
ocket This class implements server
sockets. java.net.Socket This class
implements client sockets (also called just
"sockets"). java.net.URL Class URL
represents a Uniform Resource Locator, a pointer
to a "resource" on the World Wide Web.
18Address representations
java.net.InetAddress -- A class representing an
IP address -- Performs validity checking on IP
names -- Thus, there are no public
constructors! -- Instead, just call a static
class method to obtain an InetAddress import
java.net. public class MyMachineName
public static void main (String arg)
InetAddress local null try
local InetAddress.getLocalHost()
catch (UnknownHostException e)
System.err.println ("Identity Crisis!")
System.exit(0) String
strAddress local.getHostName()
System.out.println ("Local Host "
strAddress)
InetAddress has no public constructors since it
performs validity checking of all IP names
19Converting IP Numbers to Strings
import java.net. public class MyMachineName
public static void main (String arg)
InetAddress local null try local
InetAddress.getLocalHost() catch
(UnknownHostException e) System.err.println
("Identity Crisis!") System.exit(0)
byte b local.getAddress() String
strAddress for (int i 0 i lt b.length
i) strAddress ((int)255 bi) .
System.out.println (Local strAddress)
This does things the hard way! Just
call getHostAddress() instead! Lesson Javas
net package has most every method you will
need!
20Other InetAddress Services
public boolean isMulticastAddress() static
InetAddress getAllByName(String host)
Determines all the IP addresses of a host,
given the host's name. static InetAddress
getByName(String host) Determines the IP
address of a host, given the host's
name. Lets duplicate the basic service of the
Unix command nslookup in Java. This will allow
us to convert any IP name into its valid IP
number. (We wont support the -opt switches, to
keep things simple.) From the Unix nslookup man
page(1M) nslookup is an interactive program
to query ARPA Internet domain name servers. The
user can contact servers to request information
about a specific host, or print a list of hosts
in the domain.
21import java.net. public class nslookup
public static void main(String arg) if
(arg.length 0) showUsage()
InetAddress names null try
names InetAddress.getAllByName(arg0)
catch (UnknownHostException e)
System.err.println("Error Unknown Host "
arg0) System.exit(1)
for (int i0 ilt names.length i)
System.out.println
("\nName " namesi.getHostName()
"\nAddress " namesi.getHostAddress())
//main public static void showUsage()
System.out.println
("Usage\n\tnslookup ltip namegt")
System.exit(0) //showUsage //nslookup
22Sockets . . . and Threads
Sockets -- The Basics TCP -- The
Not-So-Basics -- The Basics UDP Quick Notes
on Threads -- A Threaded Example
23Sockets
A java.net.Socket provides an easy interface to a
TCP connection. Of the eight Socket
constructors, there are generally four commonly
used public Socket(InetAddress address, int
port) Creates a stream socket and connects
it to the specified port number at the specified
IP address. public Socket(InetAddress address,
int port, InetAddress localAddr, int localPort)
Creates a socket and connects it to the
specified remote address on the specified remote
port. public Socket(String host, int port)
Creates a stream socket and connects it to the
specified port number on the named host. public
Socket(String host, int port, InetAddress
localAddr, int localPort) Creates a socket
and connects it to the specified remote host on
the specified remote port.
24Sockets
Additionally, there are several commonly used
Socket methods public InputStream
getInputStream() Returns an input stream for
this socket. public OutputStream
getOutputStream() Returns an output stream for
this socket. public void close() Closes this
socket. public int getPort() Returns the
remote port to which this socket is
connected. public InetAddress getInetAddress()
Returns the address to which the socket is
connected. public int getLocalPort()
Returns the local port to which this socket is
bound.
The streams returned from these accessors rely on
TCPs error correction and flow control.
25Sockets Simple Usage
We can therefore create a simple Socket
with Socket s new Socket (acme.gatech.edu,
13) We can obtain a Stream for receiving
information with InputStream in
s.getInputStream() We can obtain a stream for
sending information with OutputStream out
s.getOutputStream()
26Socket Options in Java
Often, it is necessary to change basic features
in Sockets. Berkeley-derived sockets allow one
to set various standard options Some options are
available only for ServerSockets (explained
later). Standard socket options available in
Java include SO_LINGER -- The Socket Option
Linger for when a close call catches material
still in the send buffer SO_TIMEOUT -- The
Socket Option Timeout determines how long a
connection may be idle before timeout. TCP_NODEL
AY -- The so-called Nagles Algorithm option
useful for WAN-based applications where small
packets are likely (rlogin, ssh, telnet, etc.),
and limited window sizes will lead to delay.
27Socket Options in Java
public void setSoLinger(boolean on, int linger)
Enable/disable SO_LINGER with the specified
linger time in seconds. public void
setSoTimeout(int timeout) Enable/disable
SO_TIMEOUT with the specified timeout, in
milliseconds. public void setTcpNoDelay(boolean
on) Enable/disable TCP_NODELAY
(disable/enable Nagle's algorithm). public int
getSoLinger() Returns setting for
SO_LINGER. public int getSoTimeout()
Returns setting for SO_TIMEOUT. public boolean
getTcpNoDelay() Tests if TCP_NODELAY is
enabled. public void setSendBufferSize(int
size) Sets the SO_SNDBUF option to the
specified value for this DatagramSocket.
28TCP/UDP Connections Distinguished
The foregoing examples used TCP connections
session oriented connections with a high degree
of congestion control and error correction. The
Socket abstraction hides the SocketImpl class,
which provides these services. To that extent,
TCP is said to provide reliable services.
There may be circumstances where we dont want
the overhead associated with reliability. UDP,
user datagram protocol, allows us to use
so-called unreliable networking services. UDP
is a datagram-oriented protocol, meaning there is
no session.
29A Day Time Client
From RFC 867 (J. Postel, 1983) One daytime
service is defined as a connection based
application on TCP. A server listens for TCP
connections on TCP port 13. Once a connection is
established the current date and time is sent out
the connection as a ascii character string (and
any data received is thrown away). The service
closes the connection after sending the
quote. Thus, we simply write a program to
connect to a servers port 13, and output the
message.
30import java.net. import java.io. public
class DayTimeClient public static final int
iDAY_TIME_PORT 13 // RFC 867 public static
final int BUFF 256 public static void main
(String arg) throws Exception if (arg.length
0) showUsage() Socket sock new
Socket(arg0, iDAY_TIME_PORT) InputStream is
sock.getInputStream() int i byte b
new byteBUFF while ( (iis.read(b)) !
-1) System.out.println(new String(b,
0, i)) public static void
showUsage() System.err.println
("Usage\n\tjava DayTimeClient ltIP
Addressgt") System.exit(0)
//showUsage //class DayTimeClient
31Reading and Writing From Sockets
Sockets in Java are full duplex--meaning that
they can both send and receive information. A
good example of this feature is an echo client.
The echo service listens to Port 7, and simply
returns or echoes back all information received
from the client. An echo client, therefore, has
to be able to send and receive over the same
socket. Lets look at an echo client written by
Sun. . . .
32public class EchoClient
public static void main(String args)
throws IOException Socket echoSocket
null PrintWriter out null
BufferedReader in null try
echoSocket new Socket("taranis", 7) out
new PrintWriter(echoSocket.getOutputStream(),
true) in new BufferedReader(new
InputStreamReader(
echoSocket.getInputStream())) catch
(UnknownHostException e) System.err.println
("Don't know about host taranis.")
System.exit(1) catch (IOException e)
System.err.println("Couldn't get I/O for
" "the connection to
taranis.") System.exit(1)
Source http//www.javasoft.com
33 BufferedReader stdIn new
BufferedReader(new InputStreamReader(System.in))
String userInput while ((userInput
stdIn.readLine()) ! null)
out.println(userInput)
System.out.println ("echo "
in.readLine()) out.close()
in.close() stdIn.close()
echoSocket.close()
34Analysis and An Idea
COMMENTS The entire program is a single
static method (This is fine for an example).
The program seems to repeat much of the code from
our example. In fact, at some level this
resembles most IO stream handlers. IDEA
Lets abstract out the common stream handling
features, and make a client that reads
from/writes to a stream. We can then use
this client for a variety of purposes. NOTION
We should thread the class we create. The
need for threading requires more explanation.
35ServerSockets
We may also wish to have a process listening on a
port for incoming calls. Perhaps weve written a
web browser that stands ready to send HTML
content when a connection is made. The
ServerSocket class allows us to capture incoming
calls.
ServerSocket outSock new ServerSocket
(6000) while (true) Socket inSock
outSock.accept() handleConnection(inSock)
inSock.close()
36ServerSockets
public ServerSocket(int port) Creates a server
socket on a specified port. public
ServerSocket(int port, int backlog) Creates a
server socket and binds it to the specified
local port number. public ServerSocket (int
port, int backlog, InetAddress bindAddr) Create
a server with the specified port, listen
backlog, and local IP address to bind
to. public Socket accept() Listens for a
connection to be made to this socket and accepts
it. public void close() Closes this
socket. public InetAddress getInetAddress()
Returns the local address of this server socket.
37Blocking Network Calls
When using ServerSockets, be aware the call to
accept() causes the program to wait until the
method returns. This blocking call can cause a
program to hang.
ServerSocket servSock new ServerSocket
(6000) while (true) Socket inSock
servSock.accept() handleConnection(inSock)
inSock.close()
If other operations must take place, we need some
way of placing the accept() call in its own
thread.
38Threads
Quickly, Incompletely Defined A Thread is a
lightweight process (chunk of code) that can act
independently of another any other code in an
application. They allow us to give the illusion
that more than one process is running at the same
time. How-To in Java Subclass
java.lang.Thread, or implement the interface
Runnable and provide a method public void
run() // threaded code goes
here The method run() is started via a call
to start(). (One can also override start(),
but this is not always necessary.)
39Simple Threads Example
public class Clock extends Thread String
strSound public Clock(String strSound)
this.strSound strSound public
void run() while (true)
System.out.println (strSound)
try this.sleep(1000)// DOES NOT BLOCK
catch (InterruptedException e)
public static
void main (String arg) Clock c1 new
Clock("Tick") Clock c2 new
Clock("Tock") c1.start()
c2.start() // class Clock
Note use of infinite loop in the run() method
This code can run while other threads are being
run!
Order of execution between c1 and c2 is not
guaranteed!
40Thread Issues
Race Conditions Access to shared objects may
result in race conditions. One can guard against
this mutexing or synchronizing blocks of
code public synchronized void
increaseAccount() //
mutexed operations
Deadlocks Pervasive use of semaphores can,
if improperly designed, result in deadlock
conditions.
41Threaded Socket Clients
Using these principles, we can design a simple
client that opens a TCP socket, and has
facilities for reading/writing to the socket.
SocketClient Interface
SocketClientConstants Interface
DefaultSocketClient Class
The key is that the client class Threads
itself--preventing it from blocking other code.
42Threaded Socket Clients Interfaces
43import java.net. import java.io. public class
DefaultSocketClient extends Thread
implements SocketClientInterface,
SocketClientConstants
private BufferedReader reader private
BufferedWriter writer private Socket sock
private String strHost private int
iPort public DefaultSocketClient(String
strHost, int iPort) setPort
(iPort) setHost (strHost)
//constructor public void run() if
(openConnection()) handleSession()
closeSession() //run
44public boolean openConnection() try
sock new Socket(strHost, iPort)
catch(IOException socketError)
if (DEBUG) System.err.println ("Unable to
connect to " strHost) return false
try reader new BufferedReader
(new InputStreamReader(sock.getInputStream()))
writer new BufferedWriter (new
OutputStreamWriter (sock.getOutputStream()))
catch (Exception e) if (DEBUG)
System.err.println ("Unable to obtain
stream to/from " strHost) return false
return true
45public void handleSession() String strInput
"" if (DEBUG) System.out.println ("Handling
session with " strHost ""
iPort) try while ( (strInput
reader.readLine()) ! null) handleInput
(strInput) catch (IOException e) if
(DEBUG) System.out.println ("Handling session
with " strHost "" iPort)
public void sendOutput(String strOutput)
try writer.write(strOutput, 0,
strOutput.length()) catch (IOException
e) if (DEBUG) System.out.println
("Error writing to " strHost)
46 public void handleInput(String strInput)
System.out.println(strInput)
public void closeSession() try
writer null reader null
sock.close() catch
(IOException e) if (DEBUG)
System.err.println ("Error closing
socket to " strHost)
public void setHost(String strHost)
this.strHost strHost public void
setPort(int iPort) this.iPort
iPort
47public static void main (String arg) /
debug main does daytime on local host /
String strLocalHost "" try
strLocalHost InetAddress.getLocalHost()
.getHostName() catch (UnknownHostException
e) System.err.println ("Unable to find
local host") DefaultSocketClient d new
DefaultSocketClient (strLocalHost,
iDAYTIME_PORT) d.start() // class
DefaultSocketClient
48Threaded Client Sockets
To use this threaded client, we merely subclass
it, override the handleConnection() method, and
spawn off new instances.
DefaultSocketClient
Networked GUI
start()
Our Specific Socket Client (e.g., web browser)
View (Buttons, etc.)
49Sending Mail
We can use our Threaded Client to write a simple
e-mail client. We wont implement the user
interface beyond a debug test main. (We could
design a complex GUI, but wont right now.)
DefaultSocketClient
QuickMail (Sends a test message)
MailSocket -- Changes handleSession() to match
RFC mail requirements
50Sending Mail
public class MailSocket extends
DefaultSocketClient String strMessage
String strRecipient String strSender
public MailSocket(String strServer, String
strSender, String
strRecipient, String strMessage)
super (strServer, iSMTP_PORT)
this.strMessage strMessage
this.strRecipient strRecipient
this.strSender strSender
/ contd . . . /
Note that we could also have more constructor
variables-- subject line, etc. etc.
51Sending Mail
public void handleSession() if (DEBUG)
System.out.println("Sending mail")
sendOutput("HELO fromQuickMail\n")
/ Some servers verify this!!/
sendOutput("MAIL FROM lt" strSender "gt\n")
sendOutput("RCPT TO " strRecipient
"\n") sendOutput("DATA\n")
sendOutput("Subject Here's some mail\n")
sendOutput("From Your Name lt" strSender "
gt\n") sendOutput("\n\n") /
send the message / sendOutput(strMessage)
sendOutput("\n.\n\n")
sendOutput("QUIT\n") if (DEBUG)
System.out.println("Sent mail") //
class MailSocket
52Sending Mail
public class QuickMail public static void
main (String arg) MailSocket mSock new
MailSocket ("mail.mindspring.com",
"david.dagon_at_mindspring.com",
"david.dagon_at_mindspring.com", "Hi Dave,\n
Heres some mail from GSMAS.\n")
mSock.start() // class QuickMail
Note I dont mind getting e-mail from GSAMS
participants you might want to use your own
e-mail address when testing.
53URLs
A URL specifies the location of networked
resource--a file, for example. A common URL is
one for a web resource
Host Name -- the name of the machine hosting
the resource Filename -- the pathname to the
file on the host Port Number -- the port number
to which to connect (optional). Reference -- a
reference to a named anchor within a resource
that usually identifies a specific location
within a file (optional).
Other protocols are possible ftp//
file// jdbc//
54URLs
The easiest way to create a URL in Java is to
start with a String try URL myURL new
URL (http//www.cc.gatech.edu) catch
(MalformedURLException e) System.err.println
(This method e.toString()) URLs can
also be created relative to an existing
URL try URL firstURL new URL(http//www.foo
.com/top_level) URL secondURL new
URL (firstURL, lower_level) catch (Exception
e)
55Using URLs
URLs contain many useful methods,
including getProtocol() returns the
protocol identifier component of the URL
(ftp/http, e.g.). getHost() returns the
host name component of the URL. getPort()
returns the port number component of the URL
(or -1 if not set). getFile() returns the
filename component of the URL. getRef()
returns the reference component of the URL.
56import java.net. import java.io. public
class URLReaderExample public static void main
(String args) throws Exception if
(args.length 0) showUsage() URL myURL new
URL("http// args0) BufferedReader in
new BufferedReader(new InputStreamReader
(myURL.openStream())) String
strInput while ((strInput in.readLine())
! null) System.out.println(strInput)
in.close() public static void showUsage()
System.err.println ("Usage\n\tjava
URLReaderExample ltIP Addressgt") System.exit(0)
//showUsage //URLReaderExample
57Connecting to a URL
One can also use openConnection() to
connect to a URL. This creates a communication
link between your Java program and the URL over
the network. For example try
URL myURL new URL("http//www.blarg
.foo.org/") myURL.openConnection()
catch (MalformedURLException e)
catch (IOException e) One
can then interact with the remote URL. (E.g.,
POST information to web pages
58Extracting the Stream from a URL
- import java.net.
- import java.io.
- public class URLConnectionReader
- public static void main
- (String args) throws Exception
- URL myURL
- new URL("http//www.cc.gatech.edu/")
- URLConnection uc myURL .openConnection()
- BufferedReader in new BufferedReader
- (new InputStreamReader
- (uc.getInputStream()))
- / see also getOutputStream() /
- String inputLine
- while ((inputLine in.readLine()) ! null)
- System.out.println(inputLine)
- in.close()
-
59- RMI
- Distributing the Object
Being a Analysis of Javas Remote Method
Invocation Capabilities
601.
Motivation
61CORBA RMITwo Flavors of Alphabet Soup?
Maybe youve heard of CORBA and/or RMI the new
hot thing in computing.
GUI
CORBA
(UDP)
HTML
Foo
HORB
OMG IDL
(TCP/IP)
Thin Clients
(CGI)
URL
Null Clients
(Non-type-safe Interaction)
Java ORB
Its hard to understand CORBA and RMI, in part
because of all the confusing terms...
62What is CORBA?
The Common Object Request Broker Architecture
defines a model of distributed computing.
Allows local invocation of operations on objects
located anywhere on a network.
63What is RMI?
Javas Remote Method Invocation defines a model
of distributed computing.
Allows local invocations of operations on objects
located anywhere on a network.
64Whats the Difference?
- CORBA supports multiple languages
- RMI is (relatively) easier to implement
- RMI allows optimization of protocols for
communication - CORBA separates interface definitions from
implementation - HORB, a variant of RMI, actually works in JDK
1.02 (and therefore in most browsers without
error).
Here, we only add to the confusion with more
terms. It might help to see an example of RMI.
652.
Hello RMI
66A Contrived Experiment With RMI
For this experiment, you will need Two
Java-ready Computers Two Fake Passports Three
round trip tickets to Moscow 3,500 Miles of
CAT-5 wiring An MI5 License to Thread
67Experiment With RMI (Contd)
68Experiment With RMI (Contd)
4.
Return home. You now have a network.
(You may use a different networking configuratio
n, if youre short on cable.)
RMI lets us invoke methods that are found on the
remote machine. So lets write a little program .
. .
69Program Matryoshka Doll Server
We will use this network to smuggle Matroyska
dolls into the U.S. Our Moscow server will take
in a doll, and return a new instance,
encapsulating our original reference.
Moscovite Implementation
Stubs
DollClient
DollServer
public MatroyskaDoll
getDoll (MatroyskaDoll d) // working
code
public MatroyskaDoll getDoll
(MatroyskaDoll d)
70RMI Design Cycle
CLIENT
SERVER
711. Common Data Type
public class Matryoska implements
java.io.Serializable private Matryoska
child private String strMicrofilm
public Matryoska(String strMicrofilm,
Matryoska child) setChild(child) set
Microfilm(strMicrofilm) public void
setMicrofilm (String strMicrofilm) this.strMicro
film strMicrofilm public void
setChild(Matryoska child) this.child child
public Matryoska getChild()
return child public String
getMicrofilm() return strMicrofilm //
class Matryoska
722. Define The Interface
import java.rmi.Remote import java.rmi.RemoteExce
ption public interface DollDesign extends
Remote Matryoska getDoll(Matryoska doll)
throws RemoteException
The java.rmi package java.rmi
java.rmi.activation java.rmi.dgc
java.rmi.registry java.rmi.server
733. Implement the Interface
Or extend java.rmi.activation. Activatable (JDK
1.2)
import java.rmi. import java.rmi.server. impor
t java.net. public class DollImplementation
extends UnicastRemoteObject implements
DollDesign public DollImplementation() throws
RemoteException super() public
Matryoska getDoll(Matryoska doll) throws
RemoteException return new Matryoska
(strMessage, doll) public
static String strMessageFrom Moscow With Stubs"
Causes object to be exported
743. Implement Interface (contd)
public static void main (String arg) if
(System.getSecurityManager() null)
System.setSecurityManager(new RMISecurityManager()
) try String name "secretAgent" /
or //host/secretAgent / strMessage
InetAddress.getLocalHost().getHostName()
DollDesign secretAgent new DollImplementation()
System.out.println ("Attempting to bind
name" name) Naming.rebind(name,
secretAgent) System.out.println ("Mission
accomplished." "\n\tOperative " name
"bound on " strMessage) catch
(Exception e) System.err.println
("Nyet.") System.err.println
(e.getMessage()) e.printStackTrace()
//main //DollImplemenation
Note the upcasting to the interface from
the implementation this exposes the stubs and
not the object!
754. Compile (javac rmic)
Rmic is an RMI compiler for java. Its in
the soup.
Skel Server Stubs Client
766. From Moscow With Stubs
import java.rmi. import java.net. public
class DollClient public static void main
(String arg) if (System.getSecurityManager(
)null) System.setSecurityManager
(new RMISecurityManager()) try String
name "//" arg0 "/secretAgent" String
agents Naming.list(name) for (int i0
iltagents.length i) System.out.println
("\t--gt" agentsi " found")
String localHost InetAddress.
getLocalHost().getHostName() DollDesign
secretAgent (DollDesign)
Naming.lookup(name) Matryoska doll new
Matryoska (localHost, null) doll
secretAgent.getDoll(doll) System.out.println
("Done) while (doll!null)
System.out.println ("Doll--gt"
doll.getMicrofilm()) dolldoll.getChild()
//try
Note use of debugging listing of
available objects
776. From Moscow With Stubs (contd)
catch (Exception e) System.err.println
("Error " e.getMessage())
e.printStackTrace() //Main //DollClient
NOTE We only create an instance of the
interface, not the remote implementation
NOTE Client must set a security manager
as well a similar policy must be invoked
NOTE Remote resolution of stubs requires use
of security manager
78Smuggling Matryoska Dolls
grant permission java.net.SocketPermission
"1024-65535",
connect,accept" permission
java.net.SocketPermission
"80", "connect"
7.
Bash rmiregistry 5 Pid 4523 Bash unset
CLASSPATH Bash java -Djava.security.policyjava.
policy DollImplementation 6 Pid 4524 Bash
OK. Fly back to Moscow and make a file called
java.policy
Set the policy file on startup with the
-D option, or else!
798. Start The Client
8.
Fly home and create a similar policy file for the
client. Start the service.
Failure to start with the policy option will
throw a security exception. This is new to JDK
1.2
803.
RMI Redux
81RMI Summary Value
RMI passes local objects by value--a copy gets
sent. RMI passes remote objects by
reference--not by copying. RMI stubs are proxies
for the client to remote object (just like CORBA).
82RMI Summary Errors GC
Additional errors can be encountered new
exceptions must be addressed. RMI uses a
reference counting gc a zero count marks the
distributed object as a weak reference. A
lease is made on remote objects with a dirty
call. Leases must be renewed or will
expire. The java.rmi.server.Unreferenced
interface allows objects to receive notification
just prior to finalization. (Single method
unreferenced() ).
83RMI Summary LocateRegistry
Default port 1099 Class java.rmi.registry.Locate
Registry helps locate remote registries public
static Registry getRegistry() / local
/ public static Registry getRegistry
(String host, int port) public
static Registry getRegistry
(String host) public static Registry
createRegistry (int port)
84java.rmi.Naming Class
void bind(String name, Remote obj) throws
RemoteException, AlreadyBoundException,
AccessException Binds the specified name to
a remote object. String list() throws
RemoteException, AccessException Returns an
array of the names bound in the registry. Remote
lookup(String name) throws RemoteException,
NotBoundException, AccessException
Returns a reference, a stub, for the remote
object associated with the specified name. void
rebind(String name, Remote obj) throws
RemoteException, AccessException Rebinds the
specified name to a new remote object. void
unbind(String name) throws RemoteException,
NotBoundException, AccessException Destroys
the binding for the specified name that is
associated with a remote object.
85RMI Dynamic Stub Loading
Dynamic stub loading is used where the client
does not have local copies of the remote
object. The stubs can even be generated on the
fly. Lookup 1) Local resolution attempted
via CLASSPATH -- no security manager
required 2) Server tells client the stubs
URL java.rmi.server.codebase
-- can even be a third party Registry !
-- requires security manager
86Java Native Invocation
- Return of the Native Method
87Native Methods--What?
- Through Java Native Interface (JNI), Java
supports the ability to call native
methods--shared object files written in languages
such as C/C - The native keyword is used to identify methods
that have implementation defined in such shared
object files - public void native sayHello()
Share Native object
JNI
Java program
88Native Methods -- Why?
- Before writing JNI, consider
- Could the program be rewritten in/ported to Java?
- Its possible to write programs quickly in Java
- What happens to portability?
- JNI provides a means of providing cross-platform
native code, but only if appropriate libraries
are generated for each machine. (Code is
portable, but not WORA--write once, run anywhere.)
89JNI Generation--Overview
Shared Object File (observe naming conventions)
Java Source with native
javac
Java Source File loading library
Java Class File (byte code)
compilation
javah -jni
C/C Header
Execution
include
JNI Implementation (C/C Code)
90Step 1 Organizing JNI vs. Java Functionality
First, decide what code belongs with native
methods, and what is better left to Java.
Consider 1) JNI means loss of visibility
modifiers (even private members can be
discovered!) 2) JNI means manual garbage
collection-- difficult at times 3) JNI
can imply a loss of OO control, unless
native implementation is in C, or a very
OO-oriented set of C libraries.
91public class HelloWorld public native void
sayHello(String strMessage) public void
speakUp() System.out.println
("Java Says Hello, C") sayHello("C
Says Hello, Java") // speakUp() // class
HelloWorld
92Step 2 Generate Header File
1) Use javah tool from JDK to create header
file. 2) Target your compiled class file. 3) Be
sure to use the -jni switch javah -jni
HelloWorld 4) For older, non-JNI header files,
use the -stub option with javah. (Ask why are
you using the old native invocation anyway?
Consider upgrading to 1.1) 5) Dont edit the
source Java file (e.g., even adding a package
statements invalidates the header file.) This
should create a header file essential for your
implementation . . .
93 / DO NOT EDIT THIS FILE - it is machine
generated / include ltjni.hgt /
Header for class HelloWorld / ifndef
_Included_HelloWorld define
_Included_HelloWorld ifdef __cplusplus
extern "C" endif /
Class HelloWorld Method
sayHello Signature (Ljava/lang/String
)V / JNIEXPORT void JNICALL
Java_HelloWorld_sayHello (JNIEnv ,
jobject, jstring) ifdef __cplusplus
endif endif
See the Do Not Edit Warning? Obey!
94Step 3 Implement Your JNI Method
1) include the header from javah 2) Use the
function prototypes generated by javah 3)
Remember that Java Strings are objects, not char
arrays. 4) Consult the JNI API for a list of
helpful functions and methods 5) Note that ALL
functions have two parameters, at least
JNIEnv env, jobject thisObj These are
references to the VM and this
object, respectively. They are the window into
the process running in the VM.
95 include ltstdio.hgt include "HelloWorld.h"
JNIEXPORT void JNICALL Java_HelloWorld_sayHello
(JNIEnv env, jobject thisObj, jstring
strMessage) const char str
(env)-gtGetStringUTFChars(env, strMessage, 0)
printf("s\n", str)
(env)-gtReleaseStringUTFChars(env, strMessage,
str) // JNI method
/Note need to convert chars from Unicode to UTF,
and then free created char buffer from the VM /
964. Compile the Object
Observe the naming convention libltShareObjectgt
.so
LINUX cc -IJDK_HOME/include
-IJDK_HOME/include/genunix \
-shared -o libHello.so Hello.c SOLARIS cc
-IJDK_HOME/include -IJDK_HOME/include/solaris
\ -G -o libHello.so
Hello.c WINDOWS Consider using a GNU port, or
the upcoming batch file ... ftp//go.cygnus.com/p
ub/ftp.cygnus.com/ gnu-win32/gnu-win32-b18/cdk.e
xe
97 Makefile for Linux JNI Compilation UPATH
/usr/bin/ JDK_PATH /usr/local/jdk117_v1a/
define utility programs and options CC
(UPATH)cc CFLAGS -I(JDK_PATH)include
-I(JDK_PATH)include/genunix -shared MAKE
(UPATH)make CTAGS (UPATH)ctags INDENT
(UPATH)indent -bl -c41 -i4 -l72 -pcs default
target - builds Hello executable Hello
Hello.c (CC) (CFLAGS) -o libHello.so
Hello.c "debug" target - builds executable
with debug code debug Hello.c
_at_CFLAGS"(CFLAGS) -DDEBUG"export CFLAGS(MAKE)
-e "pretty" target - beautify source
files pretty Hello.c ls ? xargs -p -n1
(INDENT) _at_touch pretty "clean" target -
remove unwanted object files and
executables clean rm -f Hello Hello.o pretty
tags lint nohup.out a.out core
For Solaris, change to /solaris and -G
98Windows Batch File
_at_echo Batch File for JNI W95 DevStudio 5 _at_echo
Making clean _at_del .obj _at_del .lib _at_del
.dll _at_del .pch _at_del .pdb _at_del .exp _at_del
.idb _at_echo Compiling all C files from this
directory... _at_cl -IJDKPATH\include
-IJDKPATH\include\win32 /nologo /MTd /W4
/Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D
"_WINDOWS" /YX /c /LD .c _at_echo linking
... _at_link /nologo /subsystemwindows /dll
/incrementalno /machineI386 /out1.dll
.obj
995. Load the Library
public class Test static /
Our library is in a file called "libHello.so",
but just pass in "Hello" since Java will
prepend "lib" and append the ".so"
extension. Windows users should omit the
.dll extension as well. /
System.loadLibrary("Hello") // static
load public static void main (String arg)
HelloWorld hw new HelloWorld()
hw.speakUp() // main // class Test
1006. Execute
1) Make sure the library is in the path of
the environment variable LD_LIBRARY_PATH 2)
For your shell, you can set LD_LIBRARY_PATH./
LD_LIBRARY_PATH 3) Windows users need to set
the PATH appropriately PATHPATHc\MyLibrary
1017. DEBUG (and repeat . . .)
Numerous problems may come up during
compilation. Resources for debugging JNI
problems http//www.codeguru.com/java/JNI/index
.shtml http//www.mindspring.com/david.dagon/jni
/Native.txt
102Concluding Thoughts
Thank you for your patience, and very
constructive suggestions! Ive enjoyed working
on this lecture series. If youre ever called
upon to deliver a satellite lecture, may I offer
some of my experiences? Lessons learned --
GSAMS lectures are a different beast. --
Two-way connectivity is present, but needs to be
facilitated. -- Lecture materials need can
develop contextual dependencies that make them
unsuitable/irrelevant for wider audiences. --
Provide supplemental feedback mechanisms the
television effect seems to limit some types of
discussion, and encourage others. -- The
lecture ends when the network goes down.