找回密码
 FreeOZ用户注册
查看: 3457|回复: 28

[论坛技术] 一个小程序:网络聊天室,Java版 - 从设计到实现

[复制链接]
发表于 20-6-2009 22:42:32 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?FreeOZ用户注册

x
简单说明:

这是一个用来做assessment的项目,项目其实不复杂,要求也不高。
不过是我自己画蛇添足地把它当实际项目做出来而已。

项目最后写成一个 2.5k 行的Java程序,我用了2.5天时间来写代码,
前面几天在写开发文档。因为时间问题,文档只完成了70%左右(文档不是要求),
也直接导致了后面的程序的质量。

总的来说,我最满意的地方是通过前期的分析,代码的重用性得到保证,扩展性也
还可以。基于这个项目,我可以整理出一个小的程序库,这也是我最想要的东西。

我自己不满意的地方主要是在设计的时候对线程的考虑过少,在实现的时候才发现
一些问题,但因为时间太紧,代码量不小,所以没有做得很好。

下面是我的一系列文档。有兴趣的朋友不妨自己玩一玩,不论是根据题目来实现,
还是根据我的文档来写代码,都可能是一件有趣的事。

如果你对于我的设计和实现有兴趣,不妨和我联系。

评分

参与人数 4威望 +100 收起 理由
MillerYang + 30 设计很详细!等代码学习。。
清风不写字 + 30 谢谢分享!
NoChoice + 10 谢谢分享!
ubuntuhk + 30 你太有才了!

查看全部评分

回复  

使用道具 举报

 楼主| 发表于 20-6-2009 22:54:00 | 显示全部楼层

Requirements

Server
The server application should have a window to show the names of the people currently using the application and the messages they send. Also, for convenience in testing, show the address of the server host. A button could be included to shut the server down.
server_window.PNG

Client
The client application should start up with a login window where the user enters the address of the server and a name.
client_login.PNG

After the client user logs on, the server window will show the user name and the client window will change to a different layout showing people and messages as well as a tet field for entering messages.

The following shows the windows for the server and two clients after two users have logged on and sending a few messages.
multi_client.png

Note that: all these windows usually should run on different computer.

New User
When a new user logs in, there will be an updated list of user names in the server window and all client windows. The new user will see only the messages sent after they join the chat session.

When a user exits their client application, their name should be removed from the lists of people in the server and all remaining client applications.

Dialog boxes
Dialog boxes should be used to show users any problems. Some examples are:

A user does not enter a name or address in the login window.
The client application cannot connect to the server.
There is already someone in the chat session with the same name.

If the server application shut down when there are still clients running, each client application should inform their user with a dialog box and then terminate.

[ 本帖最后由 key 于 20-6-2009 21:55 编辑 ]
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:05:43 | 显示全部楼层

Use Cases

Start Server
usecase-startserver.PNG
Process: The system administrator starts a chat room server.[Referring to requirement document Page 1, Server session.]
Pre-conditions:
    The host of the chat room server should have at least one IP address other than loop address (127.0.0.1). If there are more than one IP address, the first one will be chosen for simplicity reason.
       
    No other server listening on the same server port.

Post-conditions:
    A window of a chat room server is shown on the screen.

    The title of the server window will be: Server address: <hostname>/<ip address>

    A network server is listening on a CHAT PORT for the clients.

Alternatives:
    If there is no IP address other than the loop address(127.0.0.1), the server will still start with a warning message on the title bar. (For test)

    If there is another server listening on the same port and cause a server binding error, the NEW chat room server will refuse to start and give an error message.


Close Server
usecase-closeserver.PNG
Process: the System Administrator closes the chat room server, and the corresponding client will receive a message and close their window accordingly. [Referring to requirement document P3, Dialog boxes]
Pre-conditions:
    The server window exists.

Post-conditions:
    The server window closes.

    All chat clients get a server closing message.

    All chat clients close.

Alternatives:
None

User Login
usecase-login.PNG
Process: A chat room user login the chat room. The chat room user gives the server’s hostname or IP address, as well as his/her chat name, then logs into the chat room. When the chat user logs in, the server will greed with him/her. [Referring to requirement document Page 1, 2 and 3]
Pre-conditions:
    The chat user gives the server address and user name properly

    There is a chat room server in the Internet, which the client can access.

    The server’s hostname or IP address refers correctly to the target chat room server.

    There is not another user using the same name at the same chat room.

Post-conditions:
    The chat room user enters the chat room.

    The chat room user can chat in the room and receive messages from other users in the same room.

    The new chat user’s name appends to the chat room server’s People list, and so forth to the People list of the same chat room users.

    All users in the same chat room appear on the People list of

Alternatives:
    If the user doesn’t give a proper user name, he/her will be refused to login with an error message: “Invalid login attempt: you need to give a valid chat name”.

    If the user doesn’t give a proper address of the server, he/her will be refused to login with an error message: “Invalid login attempt: you need to give a valid host name or IP address of the chat room server”.

    If the connection to the chat room server fails, the user will get an error message: “Invalid login attempt: connection failed to the server”

    If there is another user using the same chat name in the same chat room, the user will be refused to login with an error message: “Invalid login attempt: chat name exists in the same chat room, please change your chat name before you login.”


Send message
usecase-sendmsg.PNG
Process: A chat room user send a message within a chatroom. This message displays on this user’s chat screen. The server gets the message and display on the server’s message screen. All the other clients get this message and display on their own screen.
Pre-conditions:
    The chat room server is alive and reachable.

Post-conditions:
    A message displays on all these windows, including the current user’s window, the server’s window and all the other clients’ windows.

Alternatives:
    If the message could not send correctly, a message will display on the client’s screen and force the client to exit.

    If a message could not reach one of the clients, this particular client will be deleted from the server’s People list and so forth other users’ People lists. When that client try to send a message, the server won’t accept it and then the client will close.

Exit Chat Room
usecase-exitchatroom.PNG
Process: A chatroom user exists the chatroom. The user name will remove from the server’s People list and so forth the other clients’ People lists.
Pre-conditions:
    The server exists and is reachable.

Post-conditions:
    The client’s name will be deleted from the server’s People lists and so forth the other clients People lists.

    The chat client window of that user will be closed and the user can not chat in the chat room anymore.

Alternatives:
    If the server is not reachable, the chat window will close anyway and leave the user name on the server’s People screen. The user’s chat name would be deleted from the server’s People list when a new message from one of the other client and the server just find out the chat user has gone.
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:08:47 | 显示全部楼层

Conceptual Model

conceptual-model.PNG

老实说,这个图也太concept了
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:12:48 | 显示全部楼层

System Architecture

login
systemarch-login.PNG
* ChatServDaemon represents a network server daemon, listening on a specific port and receiving message from the client side.
* NetworkController uses NetworkWriterThread and NetworkReaderThread to handle network events and operations. There are three types of NetworkController: ChatClientNetworkController, ChatServerNetworkController, ChatOnlineClientNetworkController.
** ChatClientNetworkController: to help the ChatClient to handle all network communication between client and server.
** ChatServerNetworkController: to help the ChatServerDaemon listening on a specific port, receiving connection message from the client.
** ChatOnlineClientNetworkController: to help the UserOnlineController to handle all back and forth message between chat client and chat server.
* ChatClient connects to ChatServDaemon via NetworkController, then composes a login message (ChatMessage(login)) with MessageController, and sends this login message via NetworkController to ChatServDaemon.
* ChatServDaemon receives the client login message and decodes with MessageController, then forwards to ChatServApp.
* ChatServApp is the centre of all server logic. ChatServApp validates the new comer with LoginController, and then checks whether there is a user name conflict with UserOnlinerController. If everything works fine, ChatServApp add a new user and a new ChatOnlineClientNetworkController to UserOnlineController, and lets the UserOnlinerController update other users with a NetworkController.

send message
systemarch-sendmsg.PNG
* ChatClient composes a new chat message by MessageController, and then send to chat server via NetworkController (ChatClientNetworkController).
* The chat message reaches the server on ChatOnlineClientNetworkController, which will notify UserOnlineController about the message.
* UserOnlineController receives the message and decodes with MessageController, then forward the message object to ChatServApp. ChatServApp updates ChatServWin.
* UserOnlineController updates all other clients with the new message.
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:18:27 | 显示全部楼层

Sequence Diagrams - Start Server

seq-startserver.PNG
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:19:30 | 显示全部楼层

Sequence Diagrams - Close Server

Server side
seq-closeserver-serverside.PNG

Client side
seq-closeserver-clientside.PNG
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:20:55 | 显示全部楼层

Sequence Diagrams - User Login

client side
seq-userlogin-clientside.PNG
server side
seq-userlogin-serverside.PNG
update others
seq-userlogin-updateothers.PNG
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:21:44 | 显示全部楼层

Sequence Diagrams - Send Message

client side
seq-sendmsg-clientside.PNG
server side
seq-sendmsg-serverside.PNG
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:24:15 | 显示全部楼层

Sequence Diagrams - Exit Chatroom

client side
seq-exitchatroom-clientside.PNG
server side
seq-exitchatroom-serverside.PNG
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:30:01 | 显示全部楼层

class diagram - server

classdiag-server.png
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:30:40 | 显示全部楼层

class diagram - gui

classdiag-gui.png
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:31:23 | 显示全部楼层

class diagram - Client Related Class Diagram

classdiag-client.png
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:31:56 | 显示全部楼层

class diagram - Message Class Diagram

classdiag-msg.png
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:32:26 | 显示全部楼层

class diagram - network

classdiag-network.png
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:35:03 | 显示全部楼层

有没有人希望我贴代码?

2.5k的代码,35个.java文件
回复  

使用道具 举报

 楼主| 发表于 20-6-2009 23:42:01 | 显示全部楼层

程序运行结果

netchat.png
回复  

使用道具 举报

发表于 21-6-2009 10:47:23 | 显示全部楼层
谢谢分享, 代码可以打个包放上来。

我以前上学的时候也做过一个路由器最短路经模拟也是用java写的,7个路由器在一个拓扑网络里面动态相互交换routing table,任何一个路由器上面的机器发信息到指定地址,路由器要负责找到最短路经,如果有路由器down了或者reset,其他路由器要更新routing table,并要求动态计算最短路经
回复  

使用道具 举报

 楼主| 发表于 21-6-2009 11:40:40 | 显示全部楼层
原帖由 NoChoice 于 21-6-2009 09:47 发表
谢谢分享, 代码可以打个包放上来。

我以前上学的时候也做过一个路由器最短路经模拟也是用java写的,7个路由器在一个拓扑网络里面动态相互交换routing table,任何一个路由器上面的机器发信息到指定地址,路由器要 ...


这道是算法题吧,摸拟RIP?
我打算把代码发到google code之类的地方去,然后链接过来。
等我写完现在这份垃圾报告再说。
回复  

使用道具 举报

发表于 21-6-2009 14:17:02 | 显示全部楼层
厉害呀
回复  

使用道具 举报

发表于 21-6-2009 17:34:47 | 显示全部楼层
学习一下。
回复  

使用道具 举报

发表于 21-6-2009 20:04:36 | 显示全部楼层
原帖由 key 于 21-6-2009 10:40 发表


这道是算法题吧,摸拟RIP?
我打算把代码发到google code之类的地方去,然后链接过来。
等我写完现在这份垃圾报告再说。


是rip模拟
期待lz连接
回复  

使用道具 举报

发表于 22-6-2009 15:13:25 | 显示全部楼层
key太强大!
本帖做国内本科论文足以。。。
回复  

使用道具 举报

 楼主| 发表于 22-6-2009 18:47:29 | 显示全部楼层

项目源代码已经上传Google Code

http://code.google.com/p/brave-netchat/

这套代码写得很匆忙,也没有时间做unit test,
所以多线程部分一定会有bug。
有兴趣的同学不妨帮我改一下。

评分

参与人数 1威望 +20 收起 理由
shenlh + 20 Key大侠,我在http://code.google.com/p/br ...

查看全部评分

回复  

使用道具 举报

 楼主| 发表于 22-6-2009 18:49:29 | 显示全部楼层
原帖由 procoder 于 21-6-2009 13:17 发表
厉害呀


pro同学过奖了,
你的uml文档给了不少启示给我,谢谢先。

另外,conceptual model那里我不是太满意,
理论上似乎还需要指出entity之间的relationship吧?
回复  

使用道具 举报

发表于 16-12-2009 23:31:00 | 显示全部楼层
你是用来做CDR的?
回复  

使用道具 举报

发表于 10-6-2010 20:54:34 | 显示全部楼层
程序应该是很简单的东西啊,这类型的作业基本属于本科第一个作业的内容,弄弄就好了呀? 楼主弄得文档这么全,厉害
回复  

使用道具 举报

发表于 10-6-2010 21:11:12 | 显示全部楼层
不过MVC 和抽象基类还有EXCEPTION HANDLER到是分得很清楚, 很好的习惯  如果没看错,楼主的SOCKET 写得好有CORBA的味道哦. 有BASE,HELPER

[ 本帖最后由 bthy 于 10-6-2010 20:16 编辑 ]
回复  

使用道具 举报

 楼主| 发表于 19-6-2010 08:56:24 | 显示全部楼层
这套东西的确有着眼MVC的分离,不过好象没有关于Exception handler部分吧?
我不会corba,哈哈。。。。想不到还有这个味着,就象黄蓉在压鬼岛做的羊腿一样。

我在这个课程设计中,最重要关注的是如何从sequence diagram变转到class diagram,一个是对应logical design,另一个则对应于physical design。
concept layer design用的是use cases diagram,还有一个叫system architecture的东西,是整体框架的考虑。
我喜欢activity diagram,在这里没有画出来。我通常从activity diagram开始画起,然后再考虑use cases。use cases的主要目的我觉得是系统模块的整理和定义,
当然是concept layer的定义了。

一直以来,如何平滑建立类图是我一直在思考的问题。我认为,拿起UML就直接类图,这和拿起电脑就直接coding没有质的区别。
我推崇的是传统的从概念到逻辑,再到物理实现的过程。就算是现在的OOA/OOD时代,这三步还是需要做的,只是这三个步骤之间的跨越间距比以前小很多而已。

Sequence diagram是一个很容易画出来的东西,我想做到的是,通过Sequence Diagram的进化,最后获得一个类的接口图,把主要的消息结构、类接口都定义出来,
然后再汇总在类图,以完成从逻辑设计到物理设计的进化。这个目的我想我在这套东西中已经做到了。

其实现在提介的DI/IoC概念又为这种逻辑设计到物理设计铺平了道路,我正在思考如何应用起来。
还有就是长期View页面实现的非程序员友好性(non-programmer-friendly)也让我们难以把OOA/OOD进行到底,
但GWT, Apache Click这类Web实现模型有可能帮我们解决这类问题。

对比Spring MVC,View层其实还有大量的代码无法让程序员来参与,或者参与得很痛苦。相比Apache Click,采用的是Page Component的概念来实现Web,
使得Web上的每一个东西都是程序员能控制的。而GWT走的路就更深远了,把整个界面实现都转化成程序语言。

有了这些系统工具的支撑,我们能做的事就更多了。

原帖由 bthy 于 10-6-2010 20:11 发表
不过MVC 和抽象基类还有EXCEPTION HANDLER到是分得很清楚, 很好的习惯  如果没看错,楼主的SOCKET 写得好有CORBA的味道哦. 有BASE,HELPER
回复  

使用道具 举报

您需要登录后才可以回帖 登录 | FreeOZ用户注册

本版积分规则

小黑屋|手机版|Archiver|FreeOZ论坛

GMT+11, 30-3-2024 01:22 , Processed in 0.044603 second(s), 50 queries , Gzip On, Redis On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表