Class Notes for Object Oriented Programming II | CMSC 132, Study notes of Computer Science

Material Type: Notes; Professor: Padua-Perez; Class: OBJECT-ORIENTED PROG II; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 02/13/2009

koofers-user-uvx
koofers-user-uvx 🇺🇸

9 documents

1 / 24

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CMSC 132:
Object-Oriented Programming II
1
Project – Terp Idol
Department of Computer Science
University of Maryland, College Park
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Class Notes for Object Oriented Programming II | CMSC 132 and more Study notes Computer Science in PDF only on Docsity!

CMSC 132:

Object-Oriented Programming II

Project – Terp Idol

Department of Computer Science

University of Maryland, College Park

Overview

Goal

Implement online voting for a Terp Idol contest

Requires knowledge of

Networking Streams Multithreading

Terp Idol Online Voting

Requirements

Must use a client-server model Use threads to handle multiple clients Use synchronization to avoid data races

Classes

Contest, CommandProcessor Server, Client, Judge, Voter

General approach

Set up server

Set up clients

Handle client-server requests

  1. Set Up Server

Use the Contest class to create a Serverobject with a CommandProcessor forprocessing requests.

Server creates a ServerSocket, and the portnumber assigned to ServerSocket is stored inContest class.

Run Server in a separate daemon thread. Getready to accept incoming Client connections.

Server should handle incoming Clientconnections in separate threads (to supportmultiple Clients concurrently).

  1. Handle Client-Server Requests

A client can send requests (in the form of strings) tothe Server through its Socket connection

Server examines request from client. Two requests(PING, DISCONNECT) are handled directly

Other requests are passed to the CommandProcessor

The response for each request (if any) is passed backto the client as a string

Server continues to serve Client until DISCONNECT isreceived. Server then goes back to waiting for clients.

Requests & Responses Sent by Client Sends Server Returns PING SERVER RUNNING DISCONNECT INVALID REQUEST CONTESTANTS CONTESTANTS VOTE VOTE LOOKUP LOOKUP IMAGES IMAGES INVITE INVITE TALLY TALLY <names & # votes> NEW ROUND NEW ROUND Client^ Voter Judge

Client Programming

Basic steps

Determine server location – IP address & port

Open network connection to server

Write data to server (request)

Read data from server (response)

Close network connection

Stop client

Simple Server Programming

Basic steps

Determine server location - port (& IP address)

Create ServerSocket to listen for connections

Loop while (true) { Accept network connection from client Read data from client (request) Write data to client (response) Close network connection to client }

ServerSocket Class

Create socket on server Constructor specifies local port

Server listens to port

Usage

Begin waiting after invoking accept() Listen for connection (from client socket) Returns Socket for connection

Server Example public class Server { public static void main(String args[ ]) throws Exception { ServerSocket ss = new ServerSocket(4242); while (true) { Socket s = ss.accept(); BufferedReader r = new BufferedReader( new InputStreamReader(s.getInputStream())); PrintWriter out = new PrintWriter( new OutputStreamWriter(s.getOutputStream())); String name = r.readLine(); out.println("Hello " + name); out.flush(); s.close(); } } }

URL Class

Provides high-level access to network data Abstracts the notion of a connection Constructor opens network connection

To resource named by URL

Creating Threads in Java

Runnable Approach

Define class implementing Runnable interface public interface Runnable { public void run( ); }

Put work to be performed in run( ) method

Create instance of the “worker” class

Create thread to run it Create Thread object Pass worker object to Thread constructor Or hand the worker instance to an executor Alternative methods for running threads

Lock

Definition

Entity can be held by only one thread at a time

Properties

A type of synchronization Used to enforce mutual exclusion Thread can acquire / release locks Thread will wait to acquire lock (stop execution) If lock held by another thread

Lock Example public class DataRace extends Thread { static int common = 0; static Object o; // all threads use o’s lock public void run() { synchronized(o) { // single thread at once int local = common; // data race eliminated local = local + 1; common = local; } } public static void main(String[] args) { o = new Object(); … } }