Tip: Because the project is a little bit large for me personally, so may be in some aspects of unclear or logic is not strong enough, if there is a problem please timely @ me.
I said the outline design before. Now go into detailed design and implementation. This one says server first. The server is to handle various requests from the client, such as registering, retrieving passwords, adding friends, and so on. Of course, these requests are self-designed according to actual needs.
First, the overall architecture of the server, due to the use of a well-integrated framework Mina, so do not need to consider too many specific details, such as asynchronous processing, how to achieve high concurrency. If the traditional socket can also be achieved, but high concurrency requirements are difficult to achieve. has already sent an article about Mina, is the prototype of this server. With Mina, if you simply send a string message, you don't have to rewrite the codec yourself, it has a textlinecodefactory. To implement the multimedia message and to encrypt the session, you must write your own codec.
The most important of these is com.imomo_server. This is all about interacting with the client (including various business logic).
public class Imomoserver {
public static void Main (string[] args) {
Niosocketacceptor acceptor = new Niosock Etacceptor ();
Acceptor.getfilterchain (). AddLast ("Codec",
New Protocolcodecfilter (New Imomocodecfactory ()));
Acceptor.sethandler (New Imomoserverhandler ());
Acceptor.getsessionconfig (). Setidletime (Idlestatus.both_idle, 60);//One minute for call, enter idle state
try {
Acceptor.bind ( New Inetsocketaddress (Staticvalues.server_port));
} catch (IOException e) {
E.printstacktrace ();
}
}
}
This is the class that opens the Port listener service, so much more (once again, Mina's power). Then in one is the class that processes the business logic.
public class Minaserverhandler extends iohandleradapter{
@Overridepublic void Exceptioncaught (iosession session, Throwable cause) throws Exception {Super.exceptioncaugh T (session, cause); Cause.printstacktrace ();} @Overridepublic void Inputclosed (Iosession session) throws Exception {super.inputclosed (session); System.out.println ("inputclosed");} @Overridepublic void Messagereceived (iosession session, Object message) throws Exception {smsobject SMS = (smso bject) message; System.out.println (Sms.tostring ());} @Overridepublic void Messagesent (iosession session, Object message) throws Exception {System.out.println ("messagesent" );} @Overridepublic void Sessionclosed (Iosession session) throws Exception {System.out.println ("sessionclosed" + Session.getremoteaddress () + ": Is Closed");} @Overridepublic void Sessioncreated (Iosession session) throws Exception {System.out.println ("sessioncreated");} @Overridepublic void Sessionidle (iosession session, Idlestatus status) throws Exception {System.out.println ("sEssionidle ");} @Overridepublic void Sessionopened (Iosession session) throws Exception {System.out.println ("Open:::" + Session.getattribute (Session.getid ()));;}
}
In the Messagereceived method, the message sent by the client is received and processed accordingly.
Com.database and database operations-related packages are used when a user registers, modifies personal information, obtains a friend list, and sends offline messages.
? Com.imomo_codecfactory is the codec pack mentioned above
? com.imomo_msg This package contains the message classes that are passed by the server and the client, and some Java beans.
Com.managers session management, including session sessions, user location records, and group records for user-long connections
? com.security Secure Processing PBE symmetric encryption
? com.server_utils Some tool classes include two user distance calculations, file-related processing, sending messages, static variables
Deciphering Strangers (3) server overall architecture Brief