COM3336
Programming Assignment II

Due:

October 26, at the start of class time: 6 p.m.

PART 1

Thread synchronization. What can go wrong with the program from PART 2 assignment 1? What if several threads add directories simultaneously? Correct your program by using synchronization. Also extend your program so that it can handle a richer subset of the shell: The following commands need to be added:
ln -s a b
rmdir f
touch f
rm f
To the commands
Simple = MakeDirectory | ChangeDirectoryUp | ChangeDirectoryDown |
  RecursiveCopy | DiskUsage | Find | Echo.
we have now added (implement RecursiveCopy and Find if you have not yet done so):
  SymbolicLink | RemoveDirectory | CreateEmptyFile | RemoveFile.
They have the following syntax:
SymbolicLink = "ln -s" FileName FileName.
RemoveDirectory = "rmdir" DirectoryName.

// "touch f" creates an empty file called f.
CreateEmptyFile = "touch" FileName.
RemoveFile = "rm" FileName.
The behavior of your program should again be similar to UNIX.

Programming style: It is very important that object-oriented programs that live in an operating system be written in good style. While experienced programmers follow a good style intutively, it is useful to know about style rules that tend to invite good programming style.

I have seen programs for hw 1 that are not written in good style. Correct them following the Law of Demeter and apply the Law of Demeter to all your future oo projects.

LoD contains background information.

LoD Formulation contains a precise formulation of the Law of Demeter that applies to Java.

PART 2

In assignment I we implemented a small part of the shell scripting language in Java to get hands-on experience how a file directory is structured. In this assignment we implement a small webscripting language in Java to get hands-on experience with simple networking as well as concurrent programming.

The services available are described below and their purpose is to deliver HTML source files or other text files. We only use URLs that point to text files.

Basic service
url(String)
this service fetches the resource associated with the URL indicated by
the string. The result returned is the content fetched, e.g.,
an HTML file. The service
fails if the fetch fails.

Example: url("http://www.ccs.neu.edu")
fetches the College web page. It is an HTML file.

Repetition
repeat(S)
The repeat service provides a way to repeatedly invoke a service until
it succeeds. The service repeat( S ) acts like S except that if S fails,
repeat(S) starts again.

Example:
repeat(url("http://www.ccs.neu.edu"))
tries repeatedly to fetch the College web page.

TimeOut
timeout( t , s)
The time out service allows a time limit to be placed on a service.
This service acts like S except that it fails after t milliseconds if
S has not completed yet.

Example:
timeout(5000, repeat(url("http://www.ccs.neu.edu")))
tries for 5 seconds to fetch the College web page.

Alternative 
alternative (S1 ?  S2)
the Alternative service allows a secondary service to be done in the case that
the primary service fails for some reason. Thus the 
service alternative(S1 ? S2) acts
like the service S1, except that if S1 fails then it acts like the service S2.

Example:
alternative(
  timeout(5000, repeat(url("http://www.ccs.neu.edu"))) ?
  url("http://www.neu.edu"))

If after 5 seconds we cannot get to the College home page, we want
the University home page.

Concurrent 
concurrent(S1 |  S2)
The concurrent service allows two services to be executed concurrently. The
service concurrent(S1 | S2) starts both services at the same time.

Example:
concurrent(
  url("http://www.ccs.neu.edu") |
  url("http://www.neu.edu"))
fetches with two concurrent threads the College and the University
home page.
The webscripting language has the following grammar (EBNF format):
WebScript = List(Service) *EOF*.

Service :
	  Url     |
          TimeOut |
          Repeat  |
	  Alternative |
	  Concurrent.


Url = "url(" 
       String 
      ")".

TimeOut = "timeout(" 
           float 
	  ","
           Service 
          ")".

Repeat = "repeat("
          Service
         ")".

Alternative = "alternative("
           
Service "?" Service ")". Concurrent = "concurrent(" Service "|" Service ")". List(S) ~ {S}. Main = .
The output of your program is a text file containing the pages retrieved in the order they were retrieved. For each URL retrieved, print the following information: protocol, host, filename, port. Put this information clearly marked at the beginning of each retrieved file. An example is:
protocol = http
host = java.sun.com
filename = /docs/books/tutorial/index.html
port = 80
If nothing was retrieved, print: The following service failed: ... Make sure that the web pages are not interleaved in case of concurrent retrieval.

You don't have to retrieve any images that are referenced in the web pages that you retrieve.

Hints:

I have made your job easier by generating a parser for you using the Java Compiler Compiler.

In this course you don't need to know about the theory behind parsing: that is covered in more detail in the required course "Principles of Programming Languages" and in "Compilers I" and in "Adaptive Object-Oriented Software Development".

I have also written important parts of your class definitions using Demeter/Java. You can use the provided classes in:

/home/lieber/.www/com3336/f99/hw/2/parser/gen
or you can write them from scratch. To compile and run the provided Java classes, you need to put the file at http://www.ccs.neu.edu/research/demeter/DemeterJava/rt.jar into your CLASSPATH. This jar file contains only a few classes like Ident that are needed to run the provided program. They are all in the package EDU.neu.ccs.demeter.*.

The same file is also at: /proj/demsys/demjava/rt.jar for users of CCS Solaris machines. To compile them, use:

INCORRECT:

jikes -classpath $CLASSPATH\:$JIKESPATH -d classes -g *.java
THE ABOVE IS INCORRECT. THE FOLLOWING IS CORRECT:

jikes -classpath $CLASSPATH\:$JIKESPATH -g *.java
If you prefer to use javac, you can simply use:
javac *.java
To run the program, use:
java Main < ../program.input
Make sure that ../program.input exists. You can, of course, also create your program.input in the directory where your Java files are and then just use:
java Main < program.input
Study class Main.java to see how the parser is invoked.

You probably want to use the following packages:

import java.util.* ;
import java.net.* ;
import java.io.*;
To fetch a URL, you can use:
In Url (basic service):
try{
URL url= new URL(get_string());
} 
catch(MalformedURLException e)
{;}
You can read from the URL-object, using openStream().
InputStream stream = url.openStream();
There are other possibilities, such as:
new BufferedReader(new InputStreamReader
  (url.openConnection().getInputStream()));
To read the time in milliseconds, use:
System.currentTimeMillis();

Study the class URL at:
URL class. More on URL class.

Grading

You turn in your solution by sending mail to com3336-grader as described in Assignment I. Turn in your Java files (no class files!) and the collection of webscript files you used to test your program. Also turn in the names of the files you modified and the list of files you added, for example:
The files I modified include: 
Parser.java ,
Service.java
Url.java,
Repeat.java
TimeOut.java
Alternative.java
Concurrent.java
...
the files I add include:
UrlRun.java
RepeatRun.java
TimeoutRun.java
AlterRun.java
ConcurRun.java
...

Credits : The Web scripting application is from SRC report 148 by Luca Cardelli and Rown Davies, http://www.research.digital.com/src/publications.