CS242 Advanced Programming Concepts in Java

1 / 25
About This Presentation
Title:

CS242 Advanced Programming Concepts in Java

Description:

CS242. Advanced Programming Concepts in Java. 11/08/07. Intro to Networking ... (argv.length() == 1)? argv[0] : 'localhost'; int tcp_port = 80; try ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 26
Provided by: janicets

less

Transcript and Presenter's Notes

Title: CS242 Advanced Programming Concepts in Java


1
CS242Advanced Programming Concepts in Java
  • 11/08/07
  • Intro to Networking

Prof. Searleman jets_at_clarkson.edu
2
Outline
  • Design Patterns
  • Composite
  • Decorator
  • Networking in Java (java.net)
  • Reading Java Tutorial, custom networking
  • HW5 posted, now due 11/15/07
  • Exam2 Tues, Nov 27, 700 pm, SC 362
  • practice problems

3
Design Patterns
  • cf OODP Chapter 5
  • Iterator
  • Observer
  • Strategy
  • Composite
  • Decorator

4
Strategy Design Pattern
  • idea allows you to select one of several
    algorithms dynamically
  • example using a layout manager

5
Composite Design Pattern
  • idea a component may be an individual object or
    a collection of objects
  • example group of buttons arranged in a panel

6
Decorator Pattern
  • idea provides a way to enhance the behavior of a
    class
  • example scroll bar

7
Example invoice application
  • an invoice is composed of a list of line items,
    each of which has a description and a price
  • some line items are a group of related
    individual items (i.e. they are composite items)
  • Use the Composite, Decorator, Observer Iterator
    patterns
  • cf. OODP, pp. 200 - 212

8
/ A line item in an invoice. / public
interface LineItem / Gets the price of
this line item. / double getPrice()
/ Gets the description of this line item./
String toString()
9
/ A product with a price and description.
/ public class Product implements LineItem
/ Constructs a product. / public
Product(String desc, double price)
this.description desc this.price
price public double getPrice()
return price public String toString()
return description private String
description private double price
10
/ A bundle of line items that is again a line
item./ public class Bundle implements LineItem
/ Constructs a bundle with no items. /
public Bundle() items new
ArrayListltLineItemgt() / Adds an item to
the bundle. / public void add(LineItem
item) items.add(item) public double
getPrice() double price 0 for
(LineItem item items) price
item.getPrice() return price
public String toString() / appends the
descriptions / private
ArrayListltLineItemgt items
11
/ A decorator for an item that applies a
discount. / public class DiscountedItem
implements LineItem public
DiscountedItem(LineItem item, double discount)
this.item item this.discount
discount public double getPrice()
return item.getPrice() (1 - discount /
100) public String toString()
return item.toString() " (Discount "
discount ")" private LineItem
item private double discount
12
/ An invoice for a sale, consisting of line
items. / public class Invoice private
ArrayListltLineItemgt items private
ArrayListltChangeListenergt listeners public
Invoice() items new ArrayListltLineItemgt
() listeners new ArrayListltChangeListene
rgt() public void addItem(LineItem
item) items.add(item) // Notify
all observers of the change to the invoice
ChangeEvent event new ChangeEvent(this)
for (ChangeListener listener listeners)
listener.stateChanged(event)
13
/ public class Invoice (cont). / / Adds a
change listener to the invoice. / public void
addChangeListener(ChangeListener listener)
listeners.add(listener)
14
/ public class Invoice (cont). / / Returns
an iterator that iterates through the items.
/ public IteratorltLineItemgt getItems()
return new IteratorltLineItemgt() public
boolean hasNext() return current lt
items.size() public LineItem next()
return items.get(current) public void
remove() throw new
UnsupportedOperationException()
private int current 0
15
Networking in Java
  • socket-based code
  • programmer responsible for low-level details
  • high-level code layered on top of sockets
  • e.g. RMI, CORBA, EJB, JDBC

16
Network layers
Definition TCP (Transmission Control Protocol)
is a connection-based protocol that provides a
reliable flow of data between two computers.
Definition UDP (User Datagram Protocol) is a
protocol that sends independent packets of data,
called datagrams, from one computer to another
with no guarantees about arrival. UDP is not
connection-based like TCP.
17
TCP/UDP
  • TCP is reliable, point-to-point
  • Example phone conversation
  • (http, ftp, telnet are services which require
    TCP)
  • UDP is best attempt at sending datagram
  • Example postal mail
  • (clock service can use UDP)

18
Client-Server
1. Connect to polaris 2. Request service.
How? Different port numbers are associated
with different services

lt 255 public apps 255..1023 marketed apps gt
1023 unregulated
19
Client-Server
  • client requests a service via a port
  • server responds by establishing a
  • connection (or refusing it)

20
Example time of day service
  • National Institute of Standards Technology in
    Boulder, CO
  • Host time-A.timefreq.bldrdoc.gov
  • Port 13
  • To contact a server using TCP/IP
  • open a socket
  • A socket is a software abstraction for
    communication

21
Client-side using TCP
  • To contact a server
  • Socket skt new Socket(ltservergt,ltportgt)
  • If a time-out is desired
  • skt.setSoTimeout(10000) // 10 seconds
  • Use getInputStream() to associate the socket with
    an input stream
  • Read from the input stream

22
  • public class SocketTest
  • public static void main(String args)
  • String hostName time-A.timefreq.bldrdoc.gov
  • try
  • Socket s new Socket(hostName, 13)
  • BufferedReader is
  • new BufferedReader(
  • new InputStreamReader(
  • s.getInputStream() ))
  • String remoteTime is.readLine()
  • System.out.println(Time on hostName
  • is remoteTime)
  • catch // errors

23
  • public class Daytime
  • public static final short TIME_PORT 13
  • // argument to main specifies a hostname
  • // (if absent, then use localhost)
  • public static void main(String args)
  • String hostName
  • hostName (args.length 0)? localhost
    args0

24
  • try
  • Socket skt new Socket(hostName, TIME_PORT)
  • BufferedReader is
  • new BufferedReader(
  • new InputStreamReader(skt.getInputStrea
    m()))
  • String time is.readLine()
  • System.out.println(Time on hostName
  • is time)
  • catch (IOException e)
  • System.err.println(e)

25
  • public static void main(String argv)
  • String server_name
  • (argv.length() 1)? argv0
    localhost
  • int tcp_port 80
  • try
  • Socket skt new Socket(server_name,
    tcp_port)
  • System.out.println( connected to
    server_name)
  • / read and write here /
  • skt.close()
  • catch (UnknownHostException uhe)
  • System.err.println(server_name Unknown
    host.)
  • catch (NoRouteToHostException nre)
  • System.err.println(server_name
    Unreachable.)
  • catch (ConnectException ce)
  • System.err.println(server_name
    Connection refused.)
  • catch (IOException ioe)
  • System.err.println(server_name
    ioe.getMessage())
Write a Comment
User Comments (0)