4 <meta charset="utf-8"/>
8 <link href="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/styles/github.min.css" rel="stylesheet"/>
9 <script src="http://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.1/highlight.min.js">
12 hljs.initHighlightingOnLoad();
14 <script src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML" type="text/javascript">
16 <script type="text/javascript">
17 MathJax.Hub.Config({"showProcessingMessages" : false,"messageStyle" : "none","tex2jax": { inlineMath: [ [ "$", "$" ] ] }});
19 <style type="text/css">
20 @import url(http://fonts.googleapis.com/css?family=Indie+Flower);body{font-family:Indie Flower,Daniel,cursive}h1,h2{text-align:center}blockquote{margin:1.5em 10px;padding:.5em 10px}blockquote:before{color:#000;content:open-quote;font-size:2em;line-height:.1em;margin-right:.25em;vertical-align:-.4em}blockquote:after{color:#000;content:close-quote;font-size:2em;line-height:.1em;margin-left:.25em;vertical-align:-.4em}blockquote p{display:inline}hr{border:0;border-top:dashed 2px gray}table{padding:0}table tr{border-top:1px solid #ccc;background-color:#fff;margin:0;padding:0}table tr:nth-child(2n){background-color:#aaa}table tr th{font-weight:700;border:1px solid #ccc;text-align:left;margin:0;padding:6px 13px}table tr td{border:1px solid #ccc;text-align:left;margin:0;padding:6px 13px}table tr td :first-child,table tr th :first-child{margin-top:0}table tr td:last-child,table tr th :last-child{margin-bottom:0}
24 <h1 id="we-stealz-your-dataz-wsyd-social-network">
25 We Stealz Your Dataz (WSYD) social network
28 This document is created as
32 in Markdown text. It can be manually converted to HTML using pandoc with the command:
34 <pre><code>pandoc --standalone --from=markdown --to=html5 --output=index.html index.md
36 <h2 id="introduction">
40 In this Java project I wanted to challenge myself to use some of the same concepts I’ve used in previous C++ projects in order to contrast and understand the different approaches taken in both languages.
43 The project criteria already suggested to me that the design would need to implement
47 and as there was no specific requirement to use a database I opted to use
51 which would provide two useful functions: persisting the member data to file and passing pure java objects over the network.
58 standard for all my code annotations and as a result the
60 <a href="dist/javadoc/index.html">
61 entire code base is consistently documented
64 in the same style as the Java API in HTML without requiring me to write separate documentation.
68 <a href="Made with Remarkable!">
74 <a href="http://daringfireball.net/projects/markdown/syntax">
79 editor with live HTML preview, to create this report and allow the report and the Javadoc to link together as one HTML web.
82 After becoming familiar with the NetBeans IDE I explored its Test-Driven Development (TDD) support using
84 <a href="http://junit.org">
88 and found it provided valuable feedback on bugs in my code that were not obvious before the tests were written.
91 I set out to implement the assessment criteria specified for a
101 The criteria requires a minimum of three separate executable processes: GUI User client, Social Server and Chat Server. I opted to add a fourth: GUI Server Management client. This allowed me to implement the Social and Chat servers as true
103 background service daemons
105 and have them communicate with the GUI Server Management client over the network.
108 As well as displaying log messages received from the servers the GUI Server Management client allows the
110 independent restarting or stopping
115 The GUI User client makes extensive use of Swing components. The NetBeans IDE is responsible for generating most of the GUI skeleton code for handling user actions via
123 event listeners and several function-specific child windows and dialogs for presentation during log-in, profile editing, chat and configuration. After the GUI skeleton code was complete I added support for the network services background threads and then implemented the links between them and the the GUI components.
126 Since the criteria requires a range of network functionality shared across clients and servers I implemented a
128 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/package-frame.html">
129 common network library
132 . This network library, and the server services it implements, needs to support
136 especially on the client since listening for incoming connections is an
138 operation that blocks
140 in the operating system kernel. If those blocking operations were performed on the GUI Event Dispatch thread it would cause
144 in the user interface.
148 <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html">
150 javax.swing.SwingWorker
153 class was designed especially for this purpose. As it implements the
154 <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html">
159 interface too it made the
164 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html">
166 NetworkServerAbstract
169 class because I could use it for both service daemon (non-GUI) and client (Swing GUI) applications.
172 I considered adopting an existing network protocol (or designing my own) for the communications between servers and clients and clients with clients. I considered Hyper Text Transport Protocol (HTTP) for the server to client connections. For client chat I looked at Internet Relay Chat (IRC) and its related Client To Client Protocol (CTCP) and the Direct Client Connect (DCC) sub-protocol (that supports file transfer) which together implement a server-mediated peer-to-peer (P2P) approach as a chat solution that has been in use since the 1990s.
175 The problem I found was that each
179 of communication would require a different protocol to be implemented. As well as requiring a lot of work to implement this had the risk of introducing
181 subtle hard-to-find runtime bugs
183 in the live communications.
186 I investigated using some form of pre-existing
187 <a href="http://blog.codepath.com/2013/01/06/asynchronous-processing-in-web-applications-part-2-developers-need-to-understand-message-queues/">
192 implementation such as Java
194 Enterprise Edition (EE)
196 <a href="http://docs.oracle.com/javaee/6/tutorial/doc/bncdq.html">
198 Java Message Service (JMS)
201 but decided most of these would be too over-engineered and complex for my requirements and therefore I would
203 create a simple message queue
205 implementation myself.
208 I’d already decided I’d use serialization for persisting member data and after looking at some examples of how easy it is to
210 serialize Java objects
212 over any kind of stream, including
216 , I realised it could make network communications reliable and consistent with minimal additional code both at run-time and at coding time since Java provides serialization as part of the Virtual Machine. The only requirement to support serialization is that each class of object that would be passed over the network should implement
217 <a href="http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html">
222 - which usually only requires a single addition:
224 <pre><code>public MyClass extends SomeClass implements java.io.Serializable {
227 This design decision makes it as
229 easy to pass an image or other file-type over the network
231 as it does to send simple text and with 100% reliability. In my implementation I wrapped each object being sent over the network in a
232 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
237 wrapper class that contains additional information such as sender and target service and the
244 A further issue I considered was
246 how the clients would discover the servers
248 . Since in the lab setting there will be no facility for adding Domain Name System (DNS) records to the local DNS server zone file it would require either:
252 hard-coding an IP address into the source-code and rebuilding the code each time the project was demonstrated
255 providing for the user to enter the IP address manually when each application starts
258 Using some form of automatic network discovery
262 I decided to opt for (3) implemented using
263 <a href="http://docs.oracle.com/javase/7/docs/api/java/net/MulticastSocket.html">
265 java.net.MulticastSocket
268 with a fall-back to (2) in case automatic discovery fails.
271 Clients will create long-running Transmission Control Protocol (TCP) connections to the server for passing serialized
272 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
277 objects back and forth, starting with log-in.
280 Clients can discover each other via the Organisation-scope Multicast group, or if it isn’t available, by requests to the Chat server which will mediate chat authorisations.
283 I implemented the asynchronous update from server to client requirement with User Datagram Protocol (UDP). If the Multicast group isn’t active the update service will fall-back to using Unicast to each known client.
286 To avoid using something other than object serialization for sending notifications via UDP packets (which do not have support for streams) I used the
287 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
292 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#serialize(uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage)">
298 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#deserialize(byte[])">
303 methods to convert the object to and from a
307 that can be transported by
308 <a href="http://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html">
310 java.net.DatagramPacket
315 <h2 id="functionality">
322 Describe each application’s core functionality and stand-out features
324 <h3 id="social-server">
330 <h3 id="gui-server-management-client">
331 GUI Server Management client
333 <h3 id="gui-user-client">
340 I began by designing and creating the skeleton GUI client using the NetBeans IDE.
343 Questions that came up during GUI client design led to coding
344 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html">
349 , the class that wraps the data structure representing each member of the WSYD social network. As well as data storage I implemented a static CSV parser
350 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html#createWSYD_Member(java.lang.String)">
356 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html#toString()">
361 , and support for the
362 <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html">
368 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html#compareTo(uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member)">
373 . I inherited serialization for free because all the attribute classes already support the
374 <a href="http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html">
382 The networking library was the next task. I investigated Java’s networking support and looked at several code snippets and examples at sites such as
386 as well as the official Java API documentation. I realised there were two requirements: the network services themselves, and the object passing.
389 Objects being passed need to encode the destination. For example, a “
393 ” notification needs to be handled by a client method that knows what to do with that information. The network services only deal with transport not context. Therefore
394 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
400 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#getSender()">
406 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#setTarget(java.lang.String)">
421 s that allow received messages to be dispatched by the server service to the correct handler and for acknowledgements to be sent in return.
424 The mechanism for passing received objects from the server service to the GUI (or server process) implements a custom event
425 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.html">
431 <a href="http://docs.oracle.com/javase/7/docs/api/java/util.EventObject.html">
433 java.util.EventObject
437 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventGenerator.html">
439 NetworkMessageEventGenerator
443 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html">
445 NetworkServerAbstract
448 which registers objects of classes that implement the
449 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListener.html">
451 NetworkMessageEventListener
454 interface. These objects are notified when a new
455 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
463 The network server classes all inherit their core functionality from
464 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html">
466 NetworkServerAbstract
469 . Common functionality implemented here includes
470 <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html">
472 javax.swing.SwingWorker
476 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.html">
481 dispatching, the socket listener main loop in
482 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html#doInBackground()">
487 , logging, and unsolicited outbound message queuing via
488 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html#queueMessage(uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage)">
494 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html#dequeueMessage()">
502 The implementing classes
512 NetworkServerMulticast
514 add minimal code via method overrides to support protocol specific functionality.
520 discuss other classes and notable features and coding/learning experiences
535 outstanding tasks, and
539 to call attention to important notes.
545 I used the NetBeans-generated
549 test class templates as the basis for my
555 close to 100% coverage
557 of the non-network library code. I didn’t write tests for network connections functionality although they would have been very useful due to the amount of time, and the difficulty, I estimated it would require to develop them - much more than writing the network library itself.
563 add examples of bugs the testing revealed and experiences in writing the more complicated test cases.
565 <h2 id="bibliography">
569 This is a list of many, but not all, of the very many resources I looked at in developing this project. Some were of more use than others but all of these contributed something to the development, design and coding.
575 <a href="http://en.wikipedia.org/wiki/Multicast_address#Administratively_Scoped_IPv4_Multicast_addresses">
576 Administratively Scoped IPv4 Multicast Addresses
579 <h3 id="html-formatted-note-taking">
580 HTML formatted note taking
583 <a href="http://pandoc.org/demo/example9/pandocs-markdown.html">
584 Pandoc extended Markdown syntax
588 <a href="http://daringfireball.net/projects/markdown/syntax">
592 <h3 id="netbeans-ide">
596 <a href="http://stackoverflow.com/questions/15922390/netbeans-how-to-change-author">
597 How to configure \@author tag
601 <a href="https://netbeans.org/kb/docs/java/junit-intro.html#Exercise_20">
605 <h3 id="java-documentation">
609 <a href="http://docs.oracle.com/javase/7/docs/index.html">
610 Java Platform Standard Edition 7 Documentation
614 <a href="http://docs.oracle.com/javase/7/docs/api/">
619 <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html">
620 Controlling Access to Members of a Class
623 <h3 id="java-generics">
627 <a href="https://docs.oracle.com/javase/tutorial/java/generics/">
628 Java Tutorials: Generics
632 <a href="https://docs.oracle.com/javase/tutorial/extra/generics/index.html">
633 Java Tutorials: Generics (by Gilad Bracha)
636 <h3 id="serialization">
640 <a href="http://www.javaworld.com/article/2077539/learn-java/java-tip-40--object-transport-via-datagram-packets.html">
641 Serialize Object over UDP
645 <a href="http://howtodoinjava.com/2012/11/08/a-guide-to-object-cloning-in-java/">
646 Deep cloning techniques
650 <a href="http://stackoverflow.com/questions/8642012/why-should-a-comparator-implement-serializable">
651 Why should a Comparator implement Serializable
654 <h3 id="multi-threading">
658 <a href="http://wenda.soso.io/questions/2765593/using-swingworker-to-implement-a-udp-client-server">
659 Using SwingWorker to implement a UDP Client/Server
663 <a href="http://www.tutorialspoint.com/javaexamples/net_multisoc.htm">
664 Multi-threaded Network Server
668 <a href="http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java">
669 How do I use SwingWorker in Java
673 <a href="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/">
674 Concurrency in Swing (multi-threading for background tasks)
678 <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html">
683 <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html">
684 Synchronized Statements
691 <a href="https://docs.oracle.com/javase/tutorial/networking/nifs/parameters.html">
692 Getting Network Interface Parameters
696 <a href="http://www.markhneedham.com/blog/2013/07/14/java-testing-a-socket-is-listening-on-all-network-interfaceswildcard-interface/">
697 Testing a socket is listening on all network interfaces/wildcard interface
701 <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/socket_logging">
702 Socket Logging (network logger)
709 <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html">
710 How to Write Doc Comments for the Javadoc Tool
714 <a href="http://examples.javacodegeeks.com/core-java/io/java-imageio-write-image-to-file/">
719 <a href="http://stackoverflow.com/questions/1318980/how-to-iterate-over-a-treemap">
720 Iterating over a TreeMap
724 <a href="http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java">
725 How do I call one constructor from another in Java
729 <a href="http://stackoverflow.com/questions/3108297/james-goslings-explanation-of-why-javas-byte-is-signed">
730 James Gosling’s explanation of why Java’s byte is signed
734 <a href="http://stackoverflow.com/questions/7300676/when-to-use-flush-in-java">
735 When to use flush() in Java
739 <a href="http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java">
740 How do I check if a file exists in Java
744 <a href="http://stackoverflow.com/questions/767372/java-string-equals-versus">
745 Java String.equals() versus ==
749 <a href="http://stackoverflow.com/questions/3983360/calculating-byte-size-of-java-object">
750 Calculating byte-size of Java object
754 <a href="http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html">
755 Mr. Happy Object teaches custom events