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 body{font:16px Helvetica,Arial,sans-serif;line-height:1.4;color:#333;word-wrap:break-word;background-color:#fff;padding:10px 15px}strong{font-weight:700}h1{font-size:2em;margin:.67em 0;text-align:center}h2{font-size:1.75em}h3{font-size:1.5em}h4{font-size:1.25em}h1,h2,h3,h4,h5,h6{font-weight:700;position:relative;margin-top:15px;margin-bottom:15px;line-height:1.1}h1,h2{border-bottom:1px solid #eee}hr{height:0;margin:15px 0;overflow:hidden;background:0 0;border:0;border-bottom:1px solid #ddd}a{color:#4183C4}a.absent{color:#c00}ol,ul{padding-left:15px;margin-left:5px}ol{list-style-type:lower-roman}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}img{max-width:100%}code{padding:0 5px;background-color:#d3d3d3}blockquote{padding: 0 15px;border-left:4px solid #ccc}
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
45 <h2 id="introduction">
49 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.
52 The project criteria already suggested to me that the design would need to implement
56 and as there was no specific requirement to use a database I opted to use
60 which would provide two useful functions: persisting the member data to file and passing pure java objects over the network.
67 standard for all my code annotations and as a result the
69 <a href="dist/javadoc/index.html">
70 entire code base is consistently documented
73 in the same style as the Java API in HTML without requiring me to write separate documentation.
77 <a href="Made with Remarkable!">
83 <a href="http://daringfireball.net/projects/markdown/syntax">
88 editor with live HTML preview, to create this report and allow the report and the Javadoc to link together as one HTML web.
91 After becoming familiar with the NetBeans IDE I explored its Test-Driven Development (TDD) support using
93 <a href="http://junit.org">
97 and found it provided valuable feedback on bugs in my code that were not obvious before the tests were written.
100 I set out to implement the assessment criteria specified for a
110 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
112 background service daemons
114 and have them communicate with the GUI Server Management client over the network.
117 As well as displaying log messages received from the servers the GUI Server Management client allows the
119 independent restarting or stopping
124 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
132 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 GUI components.
135 Since the criteria requires a range of network functionality shared across clients and servers I implemented a
137 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/package-frame.html">
138 common network library
141 . This network library, and the server services it implements, needs to support
145 especially on the client since listening for incoming connections is an
147 operation that blocks
149 in the operating system kernel. If those blocking operations were performed on the GUI Event Dispatch thread it would cause
153 in the user interface.
157 <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html">
159 javax.swing.SwingWorker
162 class was designed especially for this purpose. As it implements the
163 <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Runnable.html">
168 interface too it made the
173 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html">
175 NetworkServerAbstract
178 class because I could use it for both service daemon (non-GUI) and client (Swing GUI) applications.
181 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.
184 The problem I found was that each
188 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
190 subtle hard-to-find runtime bugs
192 in the live communications.
195 I investigated using some form of pre-existing
196 <a href="http://blog.codepath.com/2013/01/06/asynchronous-processing-in-web-applications-part-2-developers-need-to-understand-message-queues/">
201 implementation such as Java
203 Enterprise Edition (EE)
205 <a href="http://docs.oracle.com/javaee/6/tutorial/doc/bncdq.html">
207 Java Message Service (JMS)
210 but decided most of these would be too over-engineered and complex for my requirements and therefore I would
212 create a simple message queue
214 implementation myself.
217 I’d already decided I’d use serialization for persisting member data and after looking at some examples of how easy it is to
219 serialize Java objects
221 over any kind of stream, including
225 , 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
226 <a href="http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html">
231 - which usually only requires a single addition:
233 <pre><code>public MyClass extends SomeClass implements java.io.Serializable {
236 This design decision makes it as
238 easy to pass an image or other file-type over the network
240 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
241 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
246 wrapper class that contains additional information such as sender and target service and the
253 A further issue I considered was
255 how the clients would discover the servers
257 . 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:
261 hard-coding an IP address into the source-code and rebuilding the code each time the project was demonstrated
264 providing for the user to enter the IP address manually when each application starts
267 Using some form of automatic network discovery
271 I decided to opt for (3) implemented using
272 <a href="http://docs.oracle.com/javase/7/docs/api/java/net/MulticastSocket.html">
274 java.net.MulticastSocket
277 with a fall-back to (2) in case automatic discovery fails.
280 Clients will create long-running Transmission Control Protocol (TCP) connections to the server for passing serialized
281 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
286 objects back and forth, starting with log-in.
289 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.
292 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.
295 To avoid using something other than object serialization for sending notifications via UDP packets (which do not have support for streams) I used the
296 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
301 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#serialize(uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage)">
307 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#deserialize(byte[])">
312 methods to convert the object to and from a
316 that can be transported by
317 <a href="http://docs.oracle.com/javase/7/docs/api/java/net/DatagramPacket.html">
319 java.net.DatagramPacket
324 <h2 id="functionality">
331 Describe each application’s core functionality and stand-out features
333 <h3 id="social-server">
339 <h3 id="gui-server-management-client">
340 GUI Server Management client
342 <h3 id="gui-user-client">
349 I began by designing and creating the skeleton GUI client using the NetBeans IDE.
352 Questions that came up during GUI client design led to coding
353 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html">
358 , 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
359 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html#createWSYD_Member(java.lang.String)">
365 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html#toString()">
370 , and support for the
371 <a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html">
377 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/WSYD_Member.html#compareTo(uk.ac.ntu.n0521366.wsyd.libs.WSYD_Member)">
382 . I inherited serialization for free because all the attribute classes already support the
383 <a href="http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html">
391 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
395 as well as the official Java API documentation. I realised there were two requirements: the network services themselves, and the object passing.
398 Objects being passed need to encode the destination. For example, a “
402 ” 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
403 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
409 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#getSender()">
415 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html#setTarget(java.lang.String)">
430 s that allow received messages to be dispatched by the server service to the correct handler and for acknowledgements to be sent in return.
433 The mechanism for passing received objects from the server service to the GUI (or server process) implements a custom event
434 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.html">
440 <a href="http://docs.oracle.com/javase/7/docs/api/java/util.EventObject.html">
442 java.util.EventObject
446 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventGenerator.html">
448 NetworkMessageEventGenerator
452 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html">
454 NetworkServerAbstract
457 which registers objects of classes that implement the
458 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEventListener.html">
460 NetworkMessageEventListener
463 interface. These objects are notified when a new
464 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessage.html">
472 The network server classes all inherit their core functionality from
473 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html">
475 NetworkServerAbstract
478 . Common functionality implemented here includes
479 <a href="http://docs.oracle.com/javase/7/docs/api/javax/swing/SwingWorker.html">
481 javax.swing.SwingWorker
485 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkMessageEvent.html">
490 dispatching, the socket listener main loop in
491 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html#doInBackground()">
496 , logging, and unsolicited outbound message queuing via
497 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html#queueMessage(uk.ac.ntu.n0521366.wsyd.libs.net.NetworkMessage)">
503 <a href="dist/javadoc/uk/ac/ntu/n0521366/wsyd/libs/net/NetworkServerAbstract.html#dequeueMessage()">
511 The implementing classes
521 NetworkServerMulticast
523 add minimal code via method overrides to support protocol specific functionality.
529 discuss other classes and notable features and coding/learning experiences
544 outstanding tasks, and
548 to call attention to important notes.
554 I used the NetBeans-generated
558 test class templates as the basis for my
564 close to 100% coverage
566 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.
572 add examples of bugs the testing revealed and experiences in writing the more complicated test cases.
574 <h2 id="bibliography">
578 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.
584 <a href="http://en.wikipedia.org/wiki/Multicast_address#Administratively_Scoped_IPv4_Multicast_addresses">
585 Administratively Scoped IPv4 Multicast Addresses
588 <h3 id="html-formatted-note-taking">
589 HTML formatted note taking
592 <a href="http://pandoc.org/demo/example9/pandocs-markdown.html">
593 Pandoc extended Markdown syntax
597 <a href="http://daringfireball.net/projects/markdown/syntax">
601 <h3 id="netbeans-ide">
605 <a href="http://stackoverflow.com/questions/15922390/netbeans-how-to-change-author">
606 How to configure \@author tag
610 <a href="https://netbeans.org/kb/docs/java/junit-intro.html#Exercise_20">
614 <h3 id="java-documentation">
618 <a href="http://docs.oracle.com/javase/7/docs/index.html">
619 Java Platform Standard Edition 7 Documentation
623 <a href="http://docs.oracle.com/javase/7/docs/api/">
628 <a href="https://docs.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html">
629 Controlling Access to Members of a Class
632 <h3 id="java-generics">
636 <a href="https://docs.oracle.com/javase/tutorial/java/generics/">
637 Java Tutorials: Generics
641 <a href="https://docs.oracle.com/javase/tutorial/extra/generics/index.html">
642 Java Tutorials: Generics (by Gilad Bracha)
645 <h3 id="serialization">
649 <a href="http://www.javaworld.com/article/2077539/learn-java/java-tip-40--object-transport-via-datagram-packets.html">
650 Serialize Object over UDP
654 <a href="http://howtodoinjava.com/2012/11/08/a-guide-to-object-cloning-in-java/">
655 Deep cloning techniques
659 <a href="http://stackoverflow.com/questions/8642012/why-should-a-comparator-implement-serializable">
660 Why should a Comparator implement Serializable
663 <h3 id="multi-threading">
667 <a href="http://wenda.soso.io/questions/2765593/using-swingworker-to-implement-a-udp-client-server">
668 Using SwingWorker to implement a UDP Client/Server
672 <a href="http://www.tutorialspoint.com/javaexamples/net_multisoc.htm">
673 Multi-threaded Network Server
677 <a href="http://stackoverflow.com/questions/782265/how-do-i-use-swingworker-in-java">
678 How do I use SwingWorker in Java
682 <a href="https://docs.oracle.com/javase/tutorial/uiswing/concurrency/">
683 Concurrency in Swing (multi-threading for background tasks)
687 <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/syncmeth.html">
692 <a href="https://docs.oracle.com/javase/tutorial/essential/concurrency/locksync.html">
693 Synchronized Statements
700 <a href="https://docs.oracle.com/javase/tutorial/networking/nifs/parameters.html">
701 Getting Network Interface Parameters
705 <a href="http://www.markhneedham.com/blog/2013/07/14/java-testing-a-socket-is-listening-on-all-network-interfaceswildcard-interface/">
706 Testing a socket is listening on all network interfaces/wildcard interface
710 <a href="https://blogs.oracle.com/CoreJavaTechTips/entry/socket_logging">
711 Socket Logging (network logger)
718 <a href="http://www.oracle.com/technetwork/java/javase/documentation/index-137868.html">
719 How to Write Doc Comments for the Javadoc Tool
723 <a href="http://examples.javacodegeeks.com/core-java/io/java-imageio-write-image-to-file/">
728 <a href="http://stackoverflow.com/questions/1318980/how-to-iterate-over-a-treemap">
729 Iterating over a TreeMap
733 <a href="http://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java">
734 How do I call one constructor from another in Java
738 <a href="http://stackoverflow.com/questions/3108297/james-goslings-explanation-of-why-javas-byte-is-signed">
739 James Gosling’s explanation of why Java’s byte is signed
743 <a href="http://stackoverflow.com/questions/7300676/when-to-use-flush-in-java">
744 When to use flush() in Java
748 <a href="http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java">
749 How do I check if a file exists in Java
753 <a href="http://stackoverflow.com/questions/767372/java-string-equals-versus">
754 Java String.equals() versus ==
758 <a href="http://stackoverflow.com/questions/3983360/calculating-byte-size-of-java-object">
759 Calculating byte-size of Java object
763 <a href="http://www.javaworld.com/article/2077333/core-java/mr-happy-object-teaches-custom-events.html">
764 Mr. Happy Object teaches custom events
768 <a href="https://www.daniweb.com/software-development/java/threads/350345/multipe-swing-timers">
769 Multiple Swing Timers
773 <a href="http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html#browse%28java.net.URI%29">
774 Launching a browser for a web page
778 <a href="http://www.java2s.com/Code/Java/Swing-JFC/CreateaJButtonthatdoesnotshowfocus.htm">
779 Jbutton that does not show focus
783 <a href="http://www.areaofthoughts.com/2011/08/netbeans-jframe-properties-iconimage.html">
784 Set JFrame iconImage in NetBeans
788 <a href="http://stackoverflow.com/questions/853020/jtable-scrolling-to-a-specified-row-index">
789 JTable scrolling to a specific row