In this module we introduce the basic concepts behind networking. We use Java's networking libraries to design simple programs that interact with web pages as well as write simple client-server programs.

We conclude this module with an introduction to Mocking and Mockito. We show how to use mocking of objects in your tests in order to simulate the behavior of dependent code, and assist with unit tests.

  1. Given a URL, identify each component.
  2. Explain the difference between a server socket and a normal socket.
  3. Write a program that reads the contents of a public web page by using the Java HttpURLConnection class.
  4. Write a server-client program that uses Java sockets to exchange data.
  5. Given a Java application, use Maven to introduce a new library dependency.
  6. Given a Java class with dependencies, mock all dependency calls in a JUnit test by using the Mockito library.

  • DUE DATE: August 4th 12:00pm (NOON)

Prototype messaging system

You are working with a team responsible for building a distributed game. The game is not yet completely designed but the decision has been made that game players who will be on different computers connected via a network, will be allowed to communicate.

The communication however needs to be encrypted. Part of the game will be for players to try and intercept messages not intended for them and try and decrypt the communication. To try this idea the team is asking you to develop a simple client-server program that will allow two players to communicate by sending messages. However, the two players communicating get to choose the encryption algorithm used to encrypt their message. The sender gets to choose from a list of encryption algorithms before they send their message.

We should be able to run the programs on two separate IPs. The programs should allow us to provide the IP and port of the machine we want to connect one of the machines will be the sender of the message and the other machine will be the receiver of the message. Once connected the programs on both sides should provide text prompts. On the sender's machine

  1. A text prompt that will ask the user to select their encryption algorithm first.
  2. A text prompt that will allow the user to type in a message. The message ends when the user presses the enter (return) key on their keyboard.

On the receiver's machine a line to indicate that we are awaiting a text message will do for now.

Once the message is completed (the sender pressed the enter key) the message must be encrypted and send to the receiver. The receiver should then see on their screen the decrypted message that was send to them.

Here is a sample interaction between two players after they have created a connection between their two machines. On the user side the interaction goes as follows.

                > Please choose encryption (ROT-N, Morse, Phone) : ROT-4
                > Type in your message (encryption = ROT-4): Attack James next round.
                > Please choose encryption (ROT-N, Morse, Phone) : 
                

On the receiver's side the interaction goes as follows.

                > Waiting for message...
                  Attack James next round.
                > Waiting for message...
                

In order to help us debug, we should be able to run the receiver in a mode that will display both the encrypted text and the decrypted text. The ability to select the debug mode for the receiver should be provided as a command line argument.

The default encryption algorithms that the game provides are

  1. ROT-N : This is a rotational cipher. The value N indicates the positions to rotate a letter in the English alphabet to the right, assuming the English alphabet is written on a line, in order, from left to right. For example in ROT-13, the letter a becomes n. So the word Why encrypts to Jul. Spaces and punctuation remain unchanged during encryption.
  2. International Morse Code : We represent dots using . and dashes using -. Letters in a word are separated by one space, words in a sentence are separated by 3 spaces and a period is encrypted into =. So for example the letter s encrypts to ..., and the word sos encrypts to ... --- .... Punctuation is not allowed in Morse Code.
  3. Phone Numbers : We represent characters using a digital phone's dial pad.. So the letter A encrypts to 2 while the letter B encrypts to 22. Encrypted letters in a word are separated by one space and encrypted words in an encrypted sentence are separated by 3 spaces. So for example the word Hello encrypts to 44 33 555 555 666. Punctuation remains unchanged.

The team anticipates that players will write their own encryption algorithms as a way to make it difficult for their opponents to decrypt the messages send between team members. Your design and documentation should make it clear how a player can introduce their own encryption (and decryption). The process of adding a new encryption algorithm should be easy and try to limit the location of code changes in your solution. Ideally, the player should provide their code for the encryption algorithm and nothing more.

  • DUE DATE: August 11th 12:00pm (NOON)

Simple Text Document Processor update

The text processor that you developed received a lot of feedback and the team would like to extend it so that it will now generate HTML instead of a text document.

The new version of your program will still be used as a command line tool. Your command line should accept the name of the input file and the name of the output file. The output file name must have the extension .html.

Upon successful execution of your program, the HTML file should contain valid HTML that will render in your browser. The team has decided on the following features.

HTML header

The filename passed as argument to your program becomes the string value for the title HTML tag, e.g., passing a file named doc.txt should generate

<title> doc.txt  </title>
                

Paragraphs

A paragraph in the text documented is specified by a series of lines that contain text. The paragraph text may contain other markup. Paragraphs are separated by empty new lines one or more. For example

                    
This is a one line paragraph. 

This is a longer paragraph that contains 
more than one line. The paragraph *continues*
until we find an empty line. 

                

In the preceding example we have 2 paragraphs. In the HTML output paragraphs are surrounded with <p></p> HTML tags.

Headers

Headers in the text document are specified on a line on their own. The line starts with 1 or more `#` symbols. The number of `#` symbols designate the level of the header. Any header that has 6 or more `#` will be translated to a level 6 header. Following the `#` there must be a space followed by text up to the end of the line. Any markup in the text of a header is ignored and displayed verbatim in the output. The header levels in the document do not have to be in strict order, i.e., a header of level 1 can be followed by a header of level 3.

Consider the following example

# How to confuse *and* irritate people

This book covers the popular topics on how to confuse and irritate others. 

### Disclaimer

You will be confused, maybe irritated, by the writings in this book. Achieving both emotions is the goal of the author.    
                

Should generate


<h1>How to confuse *and* irritate people</h1>

<p>This book covers the popular topics on how to confuse and irritate others. </p>

<h3>Disclaimer</h3>

<p>You will be confused, maybe irritated, by the writings in this book. Achieving both emotions is the goal of the author.</p>    
                

Blockquotes

Any line whose first character is the symbol `>` followed by a space should be indented. By adding more `>` in a sequence causes our indentation to increase the same number of times as the number of `>`. The HTML output should use the <blockquote></blockquote> tag for each instance of ``>. Here is an example fragment

Answers inline 

> What is the time line for the project? 

We are aiming for the first on June. 

> What are the resources allocated? 
> > Are you asking in terms of headcount or machines? 
> > > Headcount

We are budgeted for 40 people.   

                

Should generate

<p>Answers inline </p>

<blockquote>
<p>What is the time line for the project? </p>
</blockquote>

<p>We are aiming for the first on June. </p>

<blockquote>
<p>What are the resources allocated? </p>

<blockquote>
<p>Are you asking in terms of headcount or machines? </p>

<blockquote>
<p>Headcount</p>
</blockquote>
</blockquote>
</blockquote>

<p>We are budgeted for 40 people. </p>

                

Lists

We will support both numbered and bullet lists. Lists start on a line of their own. Elements of the list are designated with `1.` for number lists and `*` for bullet lists. Nesting of lists is achieved by indenting the entries of the nested list by 2 spaces. Nesting between the two types of lists is allowed. Here is an example.

Numbered list 

1. one 
1. two 
  1. inner one 
  1. inner two 
    1. inner inner one
    1. inner inner two
  1. inner three
1. three 

Bullet list 

* one 
* two 
  * inner one 
  * inner two 
    * inner inner one
    * inner inner two
  * inner three
* three 


Mixing lists 

1. one 
1. two 
  * inner one 
  * inner two 
    1. inner inner one
    1. inner inner two
  * inner three
1. three 

1. one 
1. two 
*  inner one 
*  inner two 
1. inner inner one
1. inner inner two
*  inner three
1. three 

                

Should generate

                <p>Numbered list </p>

<ol>
<li>one </li>
<li>two 

<ol>
<li>inner one </li>
<li>inner two 

<ol>
<li>inner inner one</li>
<li>inner inner two</li>
</ol>
</li>
<li>inner three</li>
</ol>
</li>
<li>three </li>
</ol>

<p>Bullet list </p>

<ul>
<li>one </li>
<li>two 

<ul>
<li>inner one </li>
<li>inner two 

<ul>
<li>inner inner one</li>
<li>inner inner two</li>
</ul>
</li>
<li>inner three</li>
</ul>
</li>
<li>three </li>
</ul>

<p>Mixing lists </p>

<ol>
<li>one </li>
<li>two 

<ul>
<li>inner one </li>
<li>inner two 

<ol>
<li>inner inner one</li>
<li>inner inner two</li>
</ol>
</li>
<li>inner three</li>
</ul>
</li>
<li>three </li>
</ol>

<p>And another one </p>

<ol>
<li>one </li>
<li>two </li>
</ol>
<ul>
<li> inner one </li>
<li> inner two </li>
</ul>
<ol>
<li>inner inner one</li>
<li>inner inner two</li>
</ol>
<ul>
<li> inner three</li>
</ul>
<ol>
<li>three<br>
</li>
</ol>

Horizontal Lines

If a line starts with 4 or more consecutive `*` followed by a new line or spaces and a new line then the HTML output should generate a horizontal line using the <hr> HTML tag. For example

This concludes our parenthetical comment on irritating behaviour. 

****

But we could not stop ourselves from making another parenthetical comment here 

******

Nesting parenthetical arguments in written word causes confusion, or does it? 

****      

Should generate

<p>This concludes our parenthetical comment on irritating behaviour. </p>
<hr>
<p>But we could not stop ourselves from making another parenthetical comment here </p>
<hr>
<p>Nesting parenthetical arguments in written word causes confusion, or does it? </p>
<hr>

Emphasis

The input text can contain markup to denote bold and italicized text. Bold text is surrounded with `*`. Italicized text is surrounded with `_`. Emphasis markup can be used anywhere except in headers. So we can have emphasized text in the string for a list, inside blockquotes etc. For example.

So here is some *bold* text and some _italicized_ text.

We can also have *_this_*, as well as well as _*this*_.

Should generate

 <p>So here is some <strong>bold</strong> text and some <em>italicized</em> text. </p>

<p>We can also have <strong><em>this</em></strong>, as well as <em><strong>this</strong></em></p>

Hyperlinks

The text document should allow for links. The format for specifying links wraps the text of the link using square brackets and the destination URL address using normal parenthesis, for example


You can see an example [Confused](https://media.giphy.com/media/wi9yHmX7Sztuw/giphy.gif). 
You can read more [here](https://en.wikipedia.org/wiki/Confusion)

Should generate

     <p>You can see an example <a href="https://media.giphy.com/media/wi9yHmX7Sztuw/giphy.gif">Confused</a>. 
You can read more <a href="https://en.wikipedia.org/wiki/Confusion">here</a></p>

You can read more on how to create links in HTML at the W3C site.

Restrictions

  1. You may, if you choose, use a previous assignment as a starting point and update and extend it. If you do choose to extend a previous assignment, you must indicate the repo you used in a README file that you place inside your repository.