Top Banner
toad Fall 2012 © 2012 C Garrod, J Aldrich, and W Scherlis School of Computer Science Principles of Software Construction: Objects, Design and Concurrency Stream I/O in Java Jonathan Aldrich Charlie Garrod 15-214
26

Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

Oct 19, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

 toad    

Fall  2012  

© 2012 C Garrod, J Aldrich, and W Scherlis

School of Computer Science

Principles of Software Construction: Objects, Design and Concurrency Stream I/O in Java

Jonathan Aldrich Charlie Garrod

15-214

Page 2: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 2 15-­‐214    Garrod  

Administrivia

• Homework 6 team sign-ups due tonight §  You may not use late-days for the sign-up process § See the Piazza note for details

Page 3: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 3 15-­‐214    Garrod  

Last time: The Java Collections Framework

• Interfaces (in java.util)

• Default Implementations § ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap, LinkedHashSet, LinkedHashMap, …

• Algorithms § min, max, sort, reverse, binarySearch, shuffle, rotate, …

Collection

List Set Queue SortedMap

Map

SortedSet

Page 4: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 4 15-­‐214    Garrod  

A question for you:

• Why is this the Java Collections Framework? (…and not just the Java Collections Standard Library?)

Page 5: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 5 15-­‐214    Garrod  

A question for you:

• Why is this the Java Collections Framework? (…and not just the Java Collections Standard Library?) (Where is the extensibility?)

• One answer: § ArrayList, LinkedList, HashSet, etc. are merely default implementations • There are other specialty implementations • You can write your own

Page 6: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 6 15-­‐214    Garrod  

Today: Stream I/O and Networking in Java

• Basic I/O in Java

• Distributed systems

• Networking in Java § Communication via network sockets §  Java RMI

Page 7: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 7 15-­‐214    Garrod  

System.out is a java.io.PrintStream

• java.io.PrintStream: Allows you to conveniently print common types of data void close();!void flush();!void print(String s);!void print(int i);!void print(boolean b);!void print(Object o);!…!void println(String s);!void println(int i);!void println(boolean b);!void println(Object o);!…

Page 8: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 8 15-­‐214    Garrod  

The fundamental I/O abstraction: a stream of data

• java.io.InputStream void close();!abstract int read();!int read(byte[] b);

• java.io.OutputStream void close();!void flush();!abstract void write(int b);!void write(byte[] b);

• Aside: If you have an OutputStream you can construct a PrintStream: PrintStream(OutputStream out);!PrintStream(File file);!PrintStream(String filename);!…!

Page 9: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 9 15-­‐214    Garrod  

We typically want structured input, too

• e.g., java.util.Scanner Scanner(InputStream source);!Scanner(File source);!void close();!boolean hasNextInt();!int nextInt();!boolean hasNextDouble();!double nextDouble();!boolean hasNextLine();!String nextLine();!boolean hasNext(Pattern p);!String next(Pattern p);!…!

Page 10: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 10 15-­‐214    Garrod  

See the FileExample.java demo

• Note the output format

Page 11: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 11 15-­‐214    Garrod  

To read and write arbitrary objects

• Your object must implement the java.io.Serializable interface § Methods: none! §  If all of your data fields are themselves Serializable, Java can automatically serialize your class • If not, will get runtime NotSerializableException!

• See QABean.java and FileObjectExample.java

Page 12: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 12 15-­‐214    Garrod  

Distributed systems

• Multiple system components (computers) communicating via some medium (the network)

• Challenges: § Heterogeneity § Scale § Geography § Security § Concurrency §  Failures

(courtesy of http://www.cs.cmu.edu/~dga/15-440/F12/lectures/02-internet1.pdf

Page 13: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 13 15-­‐214    Garrod  

Communication protocols

• Agreement between parties for how communication should take place §  e.g., buying an airline ticket through a travel agent

Friendly greeting.

Muttered reply.

Destination?

Pittsburgh.

Thank you.

(courtesy of http://www.cs.cmu.edu/~dga/15-440/F12/lectures/02-internet1.pdf

Page 14: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 14 15-­‐214    Garrod  

Abstractions of a network connection

IP

TCP | UDP | …

HTTP | FTP | …

HTML | Text | JPG | GIF | PDF | …

data link layer

physical layer

Page 15: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 15 15-­‐214    Garrod  

Packet-oriented and stream-oriented connections

• UDP: User Datagram Protocol § Unreliable, discrete packets of data

• TCP: Transmission Control Protocol § Reliable data stream

Page 16: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 16 15-­‐214    Garrod  

Internet addresses and sockets

• For IP version 4 (IPv4) host address is a 4-byte number §  e.g. 127.0.0.1 § Hostnames mapped to host IP addresses via DNS § ~4 billion distinct addresses

• Port is a 16-bit number (0-65535) §  e.g. 80 § Assigned conventionally

• In Java: §  java.net.InetAddress!§  java.net.Inet4Address!§  java.net.Inet6Address!§  java.net.Socket!§  java.net.InetSocket!

Page 17: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 17 15-­‐214    Garrod  

Networking in Java

• The java.net.InetAddress: static InetAddress getByName(String host);!static InetAddress getByAddress(byte[] b);!static InetAddress getLocalHost();

• The java.net.Socket: Socket(InetAddress addr, int port);!boolean isConnected();!boolean isClosed();!void close();!InputStream getInputStream();!OutputStream getOutputStream();

• The java.net.ServerSocket: ServerSocket(int port);!Socket accept();!void close();!…!

Page 18: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 18 15-­‐214    Garrod  

A simple Sockets demo

• TextSocketClient.java

• TextSocketServer.java

• TransferThread.java

Page 19: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 19 15-­‐214    Garrod  

What do you want to do with your distributed system today?

Page 20: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 20 15-­‐214    Garrod  

Higher levels of abstraction

• Application-level communication protocols

• Frameworks for simple distributed computation § Remote Procedure Call (RPC) §  Today: Java Remote Method Invocation (RMI)

• Complex computational frameworks §  e.g., distributed map-reduce

Page 21: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 21 15-­‐214    Garrod  

Java Remote Method Invocation (RMI)

• Abstracts away the location of the computation § Use just like a method call § Automatic communication of arguments and return values

• Java-specific § L

RMI registry

Computation server

2: OK!

1: bind Foo -> Bar

Page 22: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 22 15-­‐214    Garrod  

Java Remote Method Invocation (RMI)

• Abstracts away the location of the computation § Use just like a method call § Automatic communication of arguments and return values

• Java-specific § L

RMI registry

Computation server

1: bind Foo -> Bar

2: OK! Computation

client

3: locate Foo

4: It's over there.

5: Bar.bar(x) 6: baz

Page 23: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 23 15-­‐214    Garrod  

Creating an RMI server

• Must implement java.rmi.Remote!§ No required methods, just a marker interface § All methods must throw java.rmi.RemoteException!

• Set a SecurityManager to allow RMI §  e.g., java.rmi.RMISecurityManager!

• Create a server stub §  java.rmi.server.UnicastRemoteObject!

• Remote exportObject(Remote obj, int port)!

• Bind your stub to a name at some RMI registry §  java.rmi.registry.LocateRegistry!

• Registry getRegistry(String host)!§  java.rmi.registry.Registry!

• void bind(String name, Remote obj)!• void rebind(String name, Remote obj)!

Page 24: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 24 15-­‐214    Garrod  

Creating an RMI client

• Set a SecurityManager that allows RMI

• Look up client stub using name in RMI registry §  java.rmi.registry.LocateRegistry!

• Registry getRegistry(String host)!§  java.rmi.registry.Registry!

• Remote lookup(String name)!

• Use the client as if it were a local object

• See: § Compute.java § Operation.java § AddOp.java § ComputeServer.java § ComputeClient.java

Page 25: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 25 15-­‐214    Garrod  

RMI: dealing with failure

• Problem: the network is unreliable

• java.rmi.RemoteException!§ Did the compute server receive my last request? §  Is the compute server running? § What happens if I send the same request again?

• How many times did the method run?

Page 26: Principles of Software Construction: Objects, Design and ...aldrich/courses/15-214-12fa/slides/stream … · ArrayList, LinkedList, HashSet, TreeSet, PriorityQueue, HashMap, TreeMap,

toad 26 15-­‐214    Garrod  

Next week:

• Concurrency in Java