36
Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java ______________________________________________________________________________________________ 1 / 36 Abstract Scalability and reusability are the main benefits of the object paradigm. These properties, to become effective have to be planned as soon as possible at the beginning of a project. Many steps compose a project. Here are the main steps : - needs analysis and system specifications ; - system analysis ; - system conception ; - system implementation ; The aim of this case studies is, given a chat application analysis and a corresponding conception, to implement a solution in the Java language. To learn more about the Java language : http://java.sun.com/javase/6/docs/api/ The analysis and the conception use the UML language. The rest of this document contains two parts : - system analysis ; - system conception. Advice : we recommend careful study of this document ; you will learn how to apply object-oriented concepts to many stages of the software development life cycle.

Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

  • Upload
    others

  • View
    9

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

1 / 36

Abstract

Scalability and reusability are the main benefits of the object paradigm. These properties, to become effective have to be planned as soon as possible at the beginning of a project. Many steps compose a project. Here are the main steps :

- needs analysis and system specifications ; - system analysis ; - system conception ; - system implementation ; The aim of this case studies is, given a chat application analysis and a corresponding

conception, to implement a solution in the Java language. To learn more about the Java language : http://java.sun.com/javase/6/docs/api/ The analysis and the conception use the UML language. The rest of this document contains two parts : - system analysis ; - system conception. Advice : we recommend careful study of this document ; you will learn how to apply

object-oriented concepts to many stages of the software development life cycle.

Page 2: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

2 / 36

A chat application : system analysis

Typically, an analysis is composed of the stages below : - domain analysis ; - application analysis ; - results comparison. The first two steps are independent and can be done concurrently.

Page 3: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

3 / 36

1 . 1 . A p p l i c a t i o n a n a l y s i s

Application analysis focuses on a particular chat application. It concerns especially functional analysis.

The figure below shows a use case diagram.

Figure 1 : use case diagram of the Chat System

to dialog

Identification

Chatroom selection

registration

Chatroom creation

« include »

« extends » If the topic does not exist

« include »

« extends »

If the user is unknown

Chat System

Page 4: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

4 / 36

The activity diagram depicted below describes the main functions of the application.

Figure 2 : activity diagram of the Chat System

Identification

[OK] [not OK]

chatroom selection

Registration

[registration OK]

End of the application

[registration not OK]

To dialog chatroom creation

[the chatroom exists

[the chatroom does not exit ]

Activity Chat System

Page 5: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

5 / 36

1 . 2 . D o m a i n a n a l y s i s

Domain analysis concern the chat domain whit out taking into account any particular application. The class diagram below is able to support any chat application.

Figure 3 : domain class diagram

Users

ChatRoom

Chatter

alias

User

password

1..n

0..n

< dialog with

Message

Page 6: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

6 / 36

1 . 3 . R e s u l t s c o m p a r i s o n

The following figures show how to use classes from the domain class diagram to realize the use cases. .

Figure 4 : sequence diagram for the main functions

The addition of new classes (Controller classes) enables the domain analysis and the application analysis to be linked. These classes are parts of a an MVC model (Model View Controller) for the Chat Application widely used in object oriented programming

openASesson() : Session

sd Chat System

: Chat System

ref

Indentification(( inout : Server, : Session ) : User

ref

strict

Chatter

ChatRoomSelection( inout : Session, : Server ) : ChatRoom

ref

toDialog( inout : ChatRoom )

ref

Page 7: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

7 / 36

The figure below shows the opening of a session. Two new controller classes are introduced (Server et Session). Figure 6 shows the interaction between these new classes and the other existing classes.

Figure 5 : opening a session

Chatter

to connect

sd OpeningASession() : Session

: Server

: Session

Page 8: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

8 / 36

Figure 6 : class diagram with controller classes

Users

Session

Server

0..n

ChatRoom Chatter

alias

User

password

1..n

0..n

Server side client management

< registred

< dialog with

0..n opened

clientsNumber Message

Message Header

Client

Client network management

Page 9: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

9 / 36

Figures 7 and 8 detail the diagram depicted in figure 4.

Figure 7 : identification of a chatter

Chatter registrationDemand( alias,

password )

sd Identification( inout : Server, : Session ) : User

: Server : Session

findUser( alias, password ) : boolean

: Users

findUser( alias, password ) : User

registredUser: User

findUser( alias, password ) : true

ChatRoomsList = registrationDemand( alias, password )

findUser( alias, password ) : registredUser

Page 10: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

10 / 36

Figure 8 : the chatroom selection

Chatter

sd ChatRoomSelection( inout : Session, : Server ) : ChatRoom

: Server : Session chat : ChatRoom

printTheDialogWindow

toJoinAChatRoom( idenrtificationChatRoom )

toJoinAChatRoom( identificationChatRoom ) : ChatRoom

toJoinAChatRoom ( idenrtificationChatRoom ) : chat

Page 11: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

11 / 36

The object diagram below details how to dialog (see figure 4). .

Figure 9 : an object diagram to show how a dialog takes place

Server : server

s1 : session s2 : session s3 : session s4 : session

: client : client : client : client

registredUsers : users cr1 : croom

cr2 : croom u1 : user

u2 : user

: chatter : chatter : chatter : chatter

1 sendMessage()

2 : toServer()

3 : getChatroom()

4.1 toClient() 4.2 toClient()

4 : toRoom()

5.1.1 sendMessage() 4.2.1: sendMessage()

Page 12: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

12 / 36

The Conception

The analysis models address domain and functional analyses : based on the Model View Controller modelisation, it shows how Controller classes realize the use cases, while the application domain, the most stable part of a system, is modelised by a class diagram. The next stage of development is concerned with the partition of the class diagram into packages. The classes in one package can be accessed through an Application Program Interface composed of one or many interfaces. Thus the internal view of a package (the implementation) and the external view (the package contract specified by the interfaces) can be separated.

S t e p 1 : P a r t i t i o n i n t o p a c k a g e .

Figure 4 (see the analysis above) shows sequences of significance :

- Opening a session points out the importance of the network in a Chat application. Many classes deal with the network managment. All these classes are grouped into 3 packages : Net, Client and Server. Note that Client and Server have been introduced to separate the application client view from the application server view.

- The identification use classes are grouped in the package Authentification. - The chat room selection requires a dialog topic manager to find and create chat rooms.

The related classes are grouped into the package Topics. Topics is linked with the package Chatter through unidirectional association.

- The Dialog package points out the strong interaction between the chat rooms and the chatters (the association between the classes ChatRoom and Chatter is bidirectional).

Page 13: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

13 / 36

The chart below indicates dependence between packages.

Figure 1. A preliminary partition into packages.

Chatroom

Chatter

TopicsManager

Topics

Dialog

Authentification

Net

Client

Server

Avoid bidirectional dependency between packages

Page 14: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

14 / 36

partition includes several good aspects :

- In the Net package, Client and Server are independent. - The package Net is separated from the other packages : no dependency start from the

package Net. - Authentification is not linked to the packages Client and Server. Note that this partition

breaks the inheritance between the classes User and Chatter (see figure 6 from the analysis). Thus the user's login and the chat session alias are independant. Furthermore, a « guest » account may have different aliases. Two attributes (login and password) act as a substitute for the inheritance.

- No dependency converge to the packages Client and Server and then Topic and Dialog are the reusable parts of the application.

Nevertheless some problems come to light :

- The topic manager manages the chat rooms, and then the Dialog package depends on the Topics package.

- Furthermore, the Chatter have to deal with the topics manager in order to find and to create a chatroom. And then a dependency appears from the Dialog package to the Topics package.

Page 15: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

15 / 36

This partition must be changed : the two packages Topics and Dialog should be grouped into a single package named Chat. (see figure 2)

Figure 2. The final partition. The Chat package is now completely independent and the reusability is better.

Chatroom

Chatter

TopicsManager

Chat

Authentification

Net

Client

Server

Page 16: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

16 / 36

Etape 2 : package review

S e s s i o n 1 : t h e C h a t p a c k a g e Let's have a look at the dependencies between the classes grouped into the Chat package. First of all let's pay attention to the packages interfaces : these interfaces can be deduced from a sequence diagram (figure 3).

Page 17: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

17 / 36

Figure 3 : main sequence diagram. Looking for a ChatRoom requires an association from the GestTopics class to the ChatRoom class.

bob : Chatter bobManager : TopicsManager playRoom : ChatRoom c : Chatter

getTopics

{“Java”, “UML” }

createTopics(“Games”)

getTopics

{“Java”, “UML”, “Games” }

joinTopics(“Games”)

playRoom

joinTopics(^Self) [getTopic==”Games”]

post(« Hello », ^Self)

receive(« Hello », bob )

Loop while bob is a group member

Loop for all the chatter in the chatroom

quite(^Self)

Page 18: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

18 / 36

Figure 3 specifies the class interfaces depicted in figure 4.

Figure 4 : detailed description of the classes from the package Chat.

<<interface>> Chatter

+receiveAMessage(msg: in String, c: in Chatter) +getAlias(alias: out String)

<<interface>> Chatroom

+post(msg: in String, c: in Chatter) +quit(c: in Chatter) +join(c: in Chatter)

+getTopic(topic: out String)

<<interface>> TopicsManager

+listTopics(out : Vector<String> topicsList) +joinTopic(topic: in String, cr: out Chatroom)

+createTopic(topic: out String)

Page 19: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

19 / 36

T o d o : A Chat package implementation : code the interfaces and a corresponding possible implementation (use the standard output the test your programs).

Use the following main to test your implementation. public static void main(String[] args) { Chatter bob = new TextChatter ("Bob"); Chatter joe = new TextChatter ("Joe"); TopicsManager gt = new TextGestTopics(); gt.createTopic("java"); gt.createTopic("UML"); gt.listTopics(); gt.createTopic("jeux"); gt.listTopics(); ChatRoom cr = gt.joinTopic("jeux"); cr.join(bob); cr.post("Je suis seul ou quoi ?",bob); cr.join(joe); cr.post("Tiens, salut Joe !",bob); cr.post("Toi aussi tu chat sur les forums de jeux pendant les TP, Bob?",joe); }

Your program should display the following messages on the screen :

Page 20: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

20 / 36

The openned topics are : UML java The openned topics are : UML java jeux (Message from Chatroom :jeux) Bob has join the room. (At Bob) : Bob $> Je suis seul ou quoi ? (Message from Chatroom :jeux) Joe has join the room. (At Bob) : Bob $> Tiens, salut Joe ! (At Joe) : Bob $> Tiens, salut Joe ! (At Bob) : Joe $> Toi aussi tu chat sur les forums de jeux pendant les TP, Bob? (At Joe) : Joe $> Toi aussi tu chat sur les forums de jeux pendant les TP, Bob?

Page 21: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

21 / 36

S e s s i o n 2 : s e r i a l i z a t i o n a n d c o m p a r i s o n i n t e r f a c e s , s o r t e d c o l l e c t i o n s a p p l i e d t o t h e A u t h e n t i f i c a t i o n p a c k a g e This package is in charge of the users authentification management. First of all, let's define the mains authentification functionalities in a Use Case diagram (figure 5). Two actors play a role during the authentification : Admin and User.

Figure 5 : the use cases for the authentification package. The figure 5 shows an association between the User and an authetification server (through the use case authentification). Furtheremore, the informations about an authentified user are persistents (saved in a file) (see the save and load use cases).

Admin

users management

users persistance

add a user

remove a user

save

load

User authentification

Authentification subsystem

Page 22: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

22 / 36

An interface supplies an Application Program Interface for these services. To specify this interface, the Facade design pattern is used (see figure 6) : the interface is the public access to the package.

Figure 6 : public part (interface) for the authentification package.

<<interface>> AuthentificationManager

void addUser( String login, String password ) throws UserExitsException void removeUser( String login ) throws UserUnknownException

void authentify( String login, String password ) throws UserExitsException, WrongPasswordException

static AuthentificationManagement load( String path ) throws IOException void save( String path ) throws IOException

java.lang.Exception

AuthentificationException

+login: String

UserUnknownException

WrongPasswordException

UserExitsException

Authentification

Page 23: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

23 / 36

The figure 7 shows a default implementation for this interface.

Figure 7 : a default implementation for the AuthentificationManager interface.

Authentification

java.io.Serializable

AuthentificationManagement

User

java.io.Serializable

java.lang.Comparable

- login: String - password : String

{ordered}

0...n

Page 24: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

24 / 36

To do Code the authentification package (its interface and the default implementation). Test your code with the following program : public static void main(String[] args) { AuthentificationManager am = new Authentification () ; // users management try { am.addUser("bob","123"); System.out.println("Bob has been added !"); am.removeUser("bob"); System.out.println("Bob has been removed !"); am.removeUser("bob"); System.out.println("Bob has been removes twice !"); } catch (UserUnknown e) { System.out.println(e.login +" : user unknown (enable to remove) !"); } catch (UserExists e) { System.out.println(e.login +" has already been added !"); } // authentification try { am.addUser("bob","123"); System.out.println("Bob has been added !"); am.authentify("bob","123"); System.out.println("Authentification OK !"); am.authentify("bob","456"); System.out.println("Invalid password !"); } catch (WrongPassword e) { System.out.println(e.login+" has provided an invalid password !"); } catch (UserExists e) { System.out.println(e.login +" has already been added !"); } catch (UserUnknown e) { System.out.println(e.login +" : user unknown (enable to remove) !"); } // persistance try { am.save("users.txt"); AuthentificationManager am1 = new Authentification(); am1.load("users.txt"); am1.authentify("bob","123"); System.out.println("Loading complete !");

Page 25: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

25 / 36

} catch (UserUnknown e) { System.out.println(e.login +" is unknown ! error during the saving/loading."); } catch (WrongPassword e) { System.out.println(e.login+" has provided an invalid password !error during the saving/loading ."); } catch (IOException e) { System.out.println(e); } }

Note : the package java.net provides a complete authentification mecanism (see the class java.net.Authenticator). But the above implementation is quite enough for our system. Optional work: give another implementation to serialize the authentification's datas in a relational data base. The add/remove operations can be done by using the « insert into » and « delete from » requests ; a commit operation allows to save the authentification datas. To implemants the data base access, you choose among two tehnologies : JDBC or Hibernate. JDBC is a low level API which requires to insert SQL requests within Java code. Nowadays, Hibernate is the most popular tool for persistance but, it is more complicated than JDBC.

Page 26: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

26 / 36

S e s s i o n 3 : t h e p a c k a g e N e t

This package is in charge of the network management. The underlying protocol is connected : TCP/IP. The proposed implementation is based on a message exchange. The messages are sent simultaneously by many clients (each client instance on the server is computed in one thread). A message is composed by a Header and a body (containing the datas). Note that, the read operation will block until an incoming message happens. Furthermore, try to write through a full piped stream will block your program.

T h e c o n n e c t e d p r o t o c o l T C P .

TCP avoids transmission errors and guaranty the packets ordering : an acknowledge message is automatically received for each transmitted packet (note that, the performance should be enhanced by using the UDP protocol, because no stream controls are performed). Using TCP, the connection is asymmetric :

- The server listens (waits for connection) on a particular socket (a socket is defined by a port number - for instance 80 is the port for a http server - and an IP address) ; when a client tries to connect to the server, the server accepts a new connection, and then a new socket (a dialog socket) is created. - The client creates a socket and then it sends a connection message to the server.

After the connection step, client and server can send or receive messages in a full duplex mode.

Page 27: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

27 / 36

Figure 8 : a TCP connection. Step 1 to 3 are computed by the constructor of the ServerSocket class.

(7) connexion établie Socket de comm. rendue par accept

(5) socket()

Serveur Client

(1)socket() (2)bind(port)bien connu

(3)listen()

(4)accept (bloquant)

(6)connect

Page 28: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

28 / 36

The figure 9 below shows many threads : a thread waits for clients (the main loop) while the other threads allows the communication between the server and each client.

Figure 9 : multiple clients communicate with the server in separated threads.

(5) socket()

Serveur Client

(1)socket() (2)bind(port)bien connu

(3)listen()

(4)accept (bloquant)

(6)connect

(7) connexion établie Socket de comm. rendue par accept

fork()

(8)

Page 29: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

29 / 36

P r o p o s e d i m p l e m e n t a t i o n

An abstract class ensure the net package reusability. The figure below shows our application skeleton .

activityTCPServer::run()

{abstract}gereClient(commSock)

{while}commSock = waitSock.accept();

[else]

myClone = clone()

myClone.mode = treatClient

new Thread(myClone).start()

(myClone)run()

Comportement du threadd’attente de connections

[doRun=true]

System.err.println(e);e.printStackTrace()

Exception e

{try}

{catch}

[mode = treatClient]

[else]

(Pour l’opération StopServer)Placer la variable booléennedoRun à false puis fermer lasocket waitSock pour interromprele serveur.

Figure 10 : an activity diagram for the server run’s method. Note that the diagram above use the clone method : thus, each cloned object holds the references towards the others objects (a reference towards the authentification manager for instance).

Page 30: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

30 / 36

The following diagram (figure 11) is the net package summery.

+startServer(int port)+stopServer()+run()+getPort(){abstract} +gereClient(Socket comm)

-commSocket : Socket-waitSock : ServerSocket

<<abstract>>TCPServer

Runnable Cloneable

+setServer(adr : InetAdress , port : int);+connect()

-sock : Socket

TCPClient

+Message getMessage() throws IOException+sendMessage(m : Message) throws IOException

<<interface>>MessageConnection

+String toString()

+Header head+Vector<String> data

Message

<<realize>>

<<realize>>

java.io.Serializable

Pour le champ type, logé dans le header:http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.htmlDisponibles depuis Java 1.5. les enum permettent une grande flexibilité.

Pour plus de flexibilité que ce qui est offert par Vector<String> pour véhiculerles données, on peut créer une classe Data, à part entière.

affichage lisibled’une trame (pourle debug)

permet de lire et ecrireles trames par le réseau

Pour implémenter cette interface avec une socket, on peut utiliser :out = new ObjectOutputStream(sock.getOutputStream());in = new ObjectInputStream(new BufferedInputStream(sock.getInputStream()));

Puis à l’aide de writeObjet et readObject dans les flux créés implémenter lesopérations send et get.

Figure 11 : the net package.

T o d o

Implement the net package. Optional work: use RMI rather than a low level implementation using sockets. RMI, Remote Method Invocation, allows to install objects into a server, and to perform remote object’s method calls.

Page 31: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

31 / 36

S e s s i o n 4 : c o m p o s a n t a s s e m b l y .

It is time to clarify the application architecture : which part is computed by the server, and which part resides in the client. The server side contains the topics manager and the chat room, while on the client side is the chatter. For the implementation, the Proxy design pattern is used : a local component plays the role of a remote component. Furthermore, we need to define the application protocol (note that the underlying protocol TCP/IP is in charge of the acknowledgements.

T h e s e r v e r p a c k a g e .

A distinct port to communicate is used for each chat room, while a well known port is used by the topics process. The topics manager needs 3 frames : to list the topics, to create a new topic and to join an existing topic. The gereClient method in the next figure returns one of theses 3 responses.

<<abstract>>net::TCPServer

gereClient()

ServerGestTopics

+creerTopic(String topic)

static int nextPort;

TCPGestTopics

Chat::TextGestTopics

concretGT

gereClient analyse les tramesreçues et s’appuie sur concretGTpour réaliser les actions associées

GestTopics

Cette interface estréalisée par délégationsur concretGT

Figure 12 : Topics manager instanciation for the network nanagement.

Page 32: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

32 / 36

The same model is used for the chat room management (figure 13) but the chat room server implements the Chatter interface.

<<abstract>>net::TCPServer

gereClient()recevoirMessage()

-string pseudo

ServerChatRoom

Chat::TextChatRoomconcretCR

gereClient analyse les tramesreçues (join, post, quit...) ets’appuie sur concretCR pourréaliser les actions associées

Chatter

l’appel à cette méthode (de l’interfaceChatter) provoque l’envoi d’une trameau client actuellement connecté

ChatRoom

Cette interface estréalisée par délégationsur concretCR

Figure 13 : server side ChatRoom instanciation

T h e C l i e n t p a c k a g e

On the client side, network clients implement the topics and chatrooms managers.

net::TCPClient

+ListerTopics(out : vector<String> topicList)+CreerTopic(topic : in String)+RejoindreTopic(topic: in String, cr : out ChatRoom)

ClientGestTopicsGestTopics

-leChatter : Chatter-topic : String

ClientChatRoomChatRoom

Runnable

Figure 14 : classes from the Client package.

Page 33: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

33 / 36

I n t e r a c t i o n d i a g r a m s u s i n g p r o x i e s

Figure 15 : using of a proxy to list the opened topics.

Figure 16 : chatroom creation through the topic manager proxy

Page 34: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

34 / 36

JoinTopic

bobClientproxy:ClientGestTopics

bob : Chatter

bobServ:ServerGestTopics

bobServ.concretGT:TCPGestTopics

javaCR:ServerChatRoom

JoinTopic(« java »)

JOIN_TOPIC(« java »)

joinTopic(« java »)

javaCR

JOIN_REPLY(1665)

javaCRproxy

gereClient : getMessage()

port =((ServerChatRoom) javaCR).getPort()typé ici en ChatRoompour respecterl’interface définie

On sait qu’en fait c’est ServerChatRoomqui implémente le ChatRoom fourni.Cette classe serveur est munie d’un port

javaCRproxy:ClientChatRoom

setServer(this.servAdr,1665)

1665

connect()connect()

Diagramme de séquence présentant l’interaction par proxy interposés pour rejoindreune chatroom

Figure 17 : client side chatroom creation through the topic manager proxy

Page 35: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

35 / 36

sdinteractions par proxy autour d’une chatroom

bob : Chatter

Le thread créé plushaut boucle :

while (doRun) {getMessage()

}

loop

javaCRproxy:ClientChatRoom

javaCR:ServerChatRoom

javaCR.concretCR:TextChatRoom

rejoindre(bob)

JOINCR(« bob »)

rejoindre(^Self)

gereClient : getMessage()

Diagramme de séquence présentant l’interaction par proxy interposés pour interactions avec une chat room distante

this.pseudo = « bob »

poster(« salut »,bob)

POST(« salut », « bob »)

this.leChatter = bob

poster(salut, ^Self)

quitter(bob)

QUITCR

quitter(^Self)

poster(« coucou »,joe)

recevoirMessage(« coucou »,joe)

RECV_MSG(« coucou », « joe »)

recevoirMessage(« coucou »,joe)

new Thread(this).start()

getMessage()

doRun = false

Figure 18 : network interactions with the chat room.

T o d o

Define and implement the network protocol for managing the communcations.

Page 36: Nicolas FLASQUE Benoît CHARROUX Java · 2016-11-10 · Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX Java _____ 3 / 36 1 . 1 . A p p l i c a t i o n a n a l y s i s Application

Nicolas FLASQUE Yann-Thierry MIEG Benoît CHARROUX

Java

______________________________________________________________________________________________

36 / 36

S e s s i o n 5 : G r a p h i c a l U s e r I n t e r f a c e

Code the application View Layer with the Swings library.