360 likes | 474 Views
Monkey Talk Design and Implementation of Netmeeting-Based Network Service Using Socket Programming. Speaker : 簡祥任 何政儒 Date : 2006.06.26 Est. Time : 20 minutes. UNIX Project (0936) 2006 Spring Department of CSIE, TungHai University. I NTRODUCTION. Server Daemon - MonkeyD MonkeyTalk Client
E N D
Monkey TalkDesign and Implementation of Netmeeting-Based Network Service Using Socket Programming Speaker : 簡祥任 何政儒 Date : 2006.06.26 Est. Time : 20 minutes UNIX Project (0936) 2006 Spring Department of CSIE, TungHai University
INTRODUCTION • Server Daemon - MonkeyD • MonkeyTalk Client • Command Line Interface (CLI) Version • Graphic User Interface (GUI) Version
INTRODUCTION • Server Daemon • Run in background (daemon mode) or foreground (verbose mode) • Log events • Provide chatting environment • Accept connection • Collect users’ data • Handle request from client • Broadcast messages
INTRODUCTION • Client – CLI • Interpret user commands • QUIT, HELP, etc. • Connect to host • Using TCP Socket and IPv4 Protocol • Resolve domain name (lookup DNS) • Open new chat room • Join a opened chat room • Chat in room
INTRODUCTION • Client – GUI • Provide the same functions as CLI version does • Construct graphic interfaces using Qt library
INTRODUCTION • Development Environment • Operating System • Fedora 3 (kernel 2.6.8) • Mandrake Linux 10.0 (kernel 2.6.3) • Window Environment • Provided by XFree86 via X11R6 protocol • Window Manager: KDE 3.2 • Compilation • GNU Compiler Collection (3.3.2) with thread support • Trolltech Qt Library (3.2.3) and Qmake utility (1.06c)
INTRODUCTION • Operating System Dependents • TCP Module • achieve the communication between server and clients • Qt Library / X11 Server • Construct GUI • Pthread Library
DESIGN • Key Methodologies • Application-Layer Protocol • Multi-Threading • States Transition and Control • Qt Programming
Application-Layer Protocol • Language that server and clients talks • The interactions between server and clients are achieved by sending messages • One message is a string of ASCII characters in variable length and ends up with a new-line character (ASCII=13) • Composed of two fields: OPCODE and ARGUMENT • OPCODE • fixed length with four characters • specify which command is in use • ARGUMENT • variable length • may be absent in some commands
Application-Layer Protocol • Three formats of message • No ARGUMENT • Single ARGUMENT • Multiple ARGUMENT • arguments are separated by a colon character “:”
Application-Layer Protocol • We’ve designed sixteen commands for different purposes
Application-Layer Protocol • Example • ULST: client request list of all users • UADD: server return information of online users Client: ULSTServer: UADD 0:John:1:Room#1Server: UADD 1:Tom:2:Room#2Server: UADD 2:Sam:2:Room#2Server: UADD
Multi-Threading • Server process… • Serve many clients via different connections simultaneously • How about using fork() to fork folks to serve each client? • Unfortunately, fork() is a slow system call and IPC (shared-memory) of spawned process consume much resource • Send messages while receiving message • Process got blocked after calling read() – blocked I/O system call • Can be solved by replacing with non-blocked I/O system calls or threading • Client process… • Receiving command from user and message from host at the same time
Multi-Threading • Threading Model • Client threads • BananaEater • Receive message • BananaFeeder • get input from user • send messages • Server threads • Main thread • accept connections • create new user • Pair of BananaEater and BananaFeeder for each client
Multi-Threading • Broadcast messages • Server main thread • create user profile when new connection has established • MesgIndex, one of the fields in user profile, contains the index of next message to be sent in queue • Server BananaEater thread • initialize MesgIndex when client has entered room • insert received message to message queue • Server BananaFeeder thread • check if the value of MesgIndex is equal to current index of message queue on a regular basis (busy polling) • send new arrival message to client and increase MesgIndex by one while MesgIndex is not equal to current index
Multi-Threading • Broadcast messages • User entered Room#2
Multi-Threading • Broadcast messages • New message has arrived
Multi-Threading • Broadcast messages • BananaFeeder send new message to user and update MesgIndex
Multi-Threading • Race Conditions • Occurs in server process • Threads write and read to shared data in race • Consistence of data could be damaged in such situation • Solution: Using mutual exclusive (MUTEX) lock to protect shared data • Call pthread_mutex_lock() before accessing shared data • Call pthread_mutex_unlock later • Threads never access to shared data in chorus
State Transition and Control • Flow Control • Server and client process should handle improper use of commands • Ex. Client send ULST before NICK • Ex. User use EXIT command before connecting to host • Fifteen states has been defined
Qt Programming • Client – GUI Version • Use Qt library to construct graphical interface • Developed by using Qt Designer • Each “window” has own class, derived from QWidget • Events are caught and handled by signal-slot mechanism • All events handling written in <main.cpp>
Qt概論 • 以C++為基礎所開發針對GUI設計的程式函式庫 • 能使用Qt Designer輕鬆設計出介面,或自己撰寫程式。
Qt Object Model • signals and slots • object properties • events and event filters • contextual string translation for internationalization • sophisticated interval driven timers • object trees • guarded pointers, QGuardedPtr
Object Properties • 分為Q_PROPERTY和Q_OVERRIDE,繼承至QObject • Q_PROPERTY : // QButton *b and QObject *o point to the same button b->setDown( TRUE ); o->setProperty( "down", TRUE ); • 提升compile time的效能
Events and Event Filters • events是一個物件繼承至QEvent, 例如:QPaintEvent、QMouseEvent、QKeyEvent等 • QObject::installEventFilter():用於外部event的參考
Signals and Slots • Signals:當object想改變內部狀態時所發出的一種訊息,就是signal, Ex:JPushButton中的clicked()。 • Slots: 只有當signal connected被發出時,才會被使用,是一種C++ functions。 • Ex: connect(&a, SIGNAL(clicked()), &b, SLOT(clear( ));
Qt 運作原理 • Qt 是屬於多平台型的函式庫 • Ex:X11
實做 • Qt Designer :
Qt Programming • References • Official online documentation • http://doc.trolltech.com/3.3/ • Qt 3.3 Whitepaper, Trolltech • C++ GUI Programming with Qt 3 • By Jasmin Blanchette, Mark Summerfield, Prentice Hall • Free to download now! • Programming with Qt • By Matthias Kalle Dalheimer 2nd edition, O'Reilly Media