mina-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Hieu Phan Thanh" <hie...@Cybersoft-VN.com>
Subject RE: Service Registry
Date Tue, 09 Jan 2007 15:24:57 GMT
It seems a bit hard to understand. I am sorry.:-(

I think you should separate the IoHandler and
MessageDecoder/MessageEncoder classes. In the [main] method, you just
simply as below:
-------------------------
SocketAcceptor acceptor = null;

acceptor = new SocketAcceptor();
		    
SocketAcceptorConfig socketConfig = new SocketAcceptorConfig();
socketConfig.setReuseAddress(true);

//add any filter you want here...
socketConfig.getFilterChain().addLast("codec", new
ProtocolCodecFilter(new YOURProtocolCodecFactory()));

acceptor.bind(new InetSocketAddress(port), new YOURIoHandler(),
socketConfig);
m_logger.info("Application is listening on port: " + port);
--------------------------

I think that if you want to be familiar with mina, you should develop
the simple application based on its examples (Sumup, EchoServer...),
even though just SumUp example is enough for yours. The architecture of
these example is very easy to understand and maintanance.

Following the Object Oriented methodology is better than building
everything in only one class like your code below.
And the SumUp example is a good enough for us to develop other better
application (both Server and Client applications).

If there is any mistake, I hope others would help to correct mine.

Cheers, Hieu Phan.

> -----Original Message-----
> From: peter ellis [mailto:petermarkellis@googlemail.com] 
> Sent: Tuesday, January 09, 2007 9:56 PM
> To: dev@mina.apache.org
> Subject: Re: Service Registry
> 
> If i can send you my class file.... can you help point out 
> what i need to change or a few hints!???..... ive taken on 
> this project from a senior and im struggling to get to grips 
> with mina?!
> 
> 
> 
> 
> I currently have...
> 
> 
> package com.myserverserver.xml;
> 
> import java.io.IOException;
> import java.nio.charset.Charset;
> import java.nio.charset.CharsetDecoder;
> import javax.imageio.spi.ServiceRegistry;
> import javax.mail.Service;
> import javax.net.ssl.SSLEngine;
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IdleStatus;
> import org.apache.mina.common.IoAcceptor;
> import org.apache.mina.common.IoFilter;
> import org.apache.mina.common.IoHandler; import 
> org.apache.mina.common.IoHandlerAdapter;
> import org.apache.mina.common.IoSession; import 
> org.apache.mina.common.TransportType;
> import org.apache.mina.filter.SSLFilter;
> 
> 
> 
> /*
> //old mine 0.8.2 imports
> 
> import org.apache.mina.common.ByteBuffer;
> import org.apache.mina.common.IdleStatus;
> import org.apache.mina.common.TransportType;
> import org.apache.mina.io.IoAcceptor;
> import org.apache.mina.io.IoFilter;
> import org.apache.mina.io.IoHandler;
> import org.apache.mina.io.IoSession;
> import org.apache.mina.io.filter.IoThreadPoolFilter;
> import org.apache.mina.io.filter.SSLFilter;
> import org.apache.mina.registry.Service; import 
> org.apache.mina.registry.ServiceRegistry;
> import org.apache.mina.registry.SimpleServiceRegistry;
> */
> 
> 
> /**
>  * The class which implements the XMLServer functionality 
> using Apache Mina lib  */ public class MinaXMLServer extends 
> AbstractXMLServer implements IoHandler, Runnable {
> 
>     private static final Log log = 
> LogFactory.getLog(MinaXMLServer.class);
> 
> 
> //these are deprecated????
>     private ServiceRegistry registry = null;
>     private Service xmlServerService;
> 
> 
>     private Thread minaXMLServerThread = null;
>     private volatile boolean isFinished = false;
>     private volatile boolean isRunning = false;
> 
>     /** Creates a new instance of MinaXMLServer */
>     public MinaXMLServer() {
>         // we should never call this
>         super();
>     }
> 
> 
> //I think this should be put in the messageRecieved(...) method???
>     public void dataRead(IoSession session, ByteBuffer buf) 
> throws Exception {
> 
>         // Explicitly expect UTF-8 strings
>         Charset charset = Charset.forName("UTF-8");
>         CharsetDecoder decoder = charset.newDecoder();
> 
>         String data = buf.getString(decoder);
> 
>         log.debug("Read the following request from the 
> client:\n" + data);
> 
>         // the rest of the code
>         MinaConnectionHandler handler = new 
> MinaConnectionHandler(this, data,
>                 session);
> 
>         // Mina uses a thread pool to call the handler, so we 
> do not need to
>         // worry about that. Just call the run method...
>         handler.run();
>     }
> 
>     /*public void start() throws IOException {
> 
>         xmlServerService = new Service("xmlserver", 
> TransportType.SOCKET, super.getPort());
>         registry.bind(xmlServerService, this);
> 
>         log.info("Server ready and listening on port " + 
> super.getPort());
>     }
>     */
> 
>     /*public void stop() throws Exception {
>         log.info("Stop request received for the server");
>         registry.unbind(xmlServerService);
>         getClusterManager().closeAllConnections();
>     }*/
> 
>     /*
>     protected void completeInitTasks(DaemonContext 
> daemonContext) throws Exception {
>         registry = new SimpleServiceRegistry();
> 
>         if (isUseSecureSockets()) {
>             addSSLSupport(registry);
>         }
> 
>         // Create the thread pool
>         IoThreadPoolFilter threadPool = new IoThreadPoolFilter();
>         threadPool.start();
>         addFilter("threadPoolFilter", threadPool);
>     }*/
> 
> 
> 
>     private void addFilter(String filterName, IoFilter filter) {
>         log.debug("Adding filter " + filterName);
> 
>         //IoAcceptor acceptor = 
> registry.getIoAcceptor(TransportType.SOCKET);
>         //acceptor.getFilterChain().addLast(filterName, filter);
>     }
> 
>     // TODO: remove the hack once we have a signed key
>     private void addSSLSupport(ServiceRegistry registry) 
> throws Exception {
> 
>         log.info("Adding SSL support");
> 
>         SSLFilter sslFilter = new SSLFilter(getSSLContext());
> 
>         // TODO: need to get a key signed by Verisign - then 
> remove this code
>         // A little hack for dev to get around the keys issue
>         SSLEngine engine = getSSLContext().createSSLEngine();
>         String[] supported = engine.getSupportedCipherSuites();
>         String[] anonCipherSuitesSupported = new 
> String[supported.length];
>         int numAnonCipherSuitesSupported = 0;
>         for (int i = 0; i < supported.length; i++) {
>             if (supported[i].contains("_anon_")) {
>                 log.debug(supported[i]);
> 
> anonCipherSuitesSupported[numAnonCipherSuitesSupported++] = 
> supported[i];
>             }
>         }
> 
>         String[] oldEnabled = engine.getEnabledCipherSuites();
>         String[] newEnabled = new String[oldEnabled.length
>                 + numAnonCipherSuitesSupported];
>         System.arraycopy(oldEnabled, 0, newEnabled, 0, 
> oldEnabled.length);
>         System.arraycopy(anonCipherSuitesSupported, 0, newEnabled,
>                 oldEnabled.length, numAnonCipherSuitesSupported);
> 
>         //server.setEnabledCipherSuites(newEnabled);
>         String[] noAuthRequiredSuites = new 
> String[numAnonCipherSuitesSupported];
>         System.arraycopy(anonCipherSuitesSupported, 0, 
> noAuthRequiredSuites,
>                 0, noAuthRequiredSuites.length);
> 
>         sslFilter.setEnabledCipherSuites(noAuthRequiredSuites);
> 
>         addFilter("sslFilter", sslFilter);
>     }
> 
> 
> 
> 
> 
>     public Integer start(String[] string) {
> 
>         if(minaXMLServerThread == null){
>             log.info("Mina XML Server starting..");
>             minaXMLServerThread = new Thread(this);
>             minaXMLServerThread.start();
> 
>         }else{
> 
>           log.info("Mina XML Server already running");
> 
>         }
> 
>         return null;
>     }
> 
> 
> 
> 
> 
>     public int stop(int exitCode) {
> 
>         log.info("Stop request received for the server");
> 
>         if (minaXMLServerThread != null)
>         {
> 
>             isFinished = false;
>             setRunning(false);
>                 while (!isFinished)
>                 {
>                     try {
>                          Thread.sleep(25);
>                     } catch (InterruptedException e) {}
>                 }
>             registry.unbind(xmlServerService);
>             getClusterManager().closeAllConnections();
> 
>             log.info("Calling stopMonitor() method in Monitor");
> 
>             minaXMLServerThread = null;
>         }
> 
>         return exitCode;
>     }
> 
> 
> 
> 
>     public void run() {
> 
>        setRunning(true);
> 
>        log.info("Starting Mina XML Server daemon");
> 
>        xmlServerService = new Service("xmlserver", 
> TransportType.SOCKET, super.getPort());
>         try {
>             registry.bind(xmlServerService, this);
>         } catch (IOException ex) {
>             ex.printStackTrace();
>         }
> 
>        log.info("Server ready and listening on port " + 
> super.getPort());
> 
>         //Main thread stuff goes in here...
>         while (isRunning()) {
>             try {
>                 //just keep looping so that this thread stays alive
>                 Thread.sleep(30);
>             } catch (InterruptedException ex) {
>                 ex.printStackTrace();
>             }
> 
>         }
> 
> 
>        log.info("Main connection listener stopped");
>        isFinished = true;
> 
>     }
> 
>     public synchronized boolean isRunning() {
>         return isRunning;
>     }
> 
>     private synchronized void setRunning(boolean running) {
>         this.isRunning = running;
>     }
> 
>     protected void completeInitTasks() throws Exception {
> 
>         registry = new SimpleServiceRegistry();
> 
>         if (isUseSecureSockets()) {
>             addSSLSupport(registry);
>         }
> 
>         // Create the thread pool
>         // I think the IOThreadPool is built into Mina by default
>         /*IoThreadPoolFilter threadPool = new IoThreadPoolFilter();
>         threadPool.start();
>         addFilter("threadPoolFilter", threadPool);
>          **/
>     }
> 
>     protected void completeDestroyTasks() throws Exception {
>     }
> 
>     public void sessionCreated(IoSession ioSession) throws Exception {
>     }
> 
>     public void sessionOpened(IoSession ioSession) throws Exception {
>     }
> 
>     public void sessionClosed(IoSession ioSession) throws Exception {
>     }
> 
>     public void sessionIdle(IoSession ioSession, IdleStatus
> idleStatus) throws Exception {
>     }
> 
>     public void exceptionCaught(IoSession ioSession, Throwable
> throwable) throws Exception {
>     }
> 
>     public void messageReceived(IoSession ioSession, Object 
> message) throws Exception {
> 
> 
>         // Explicitly expect UTF-8 strings
>         Charset charset = Charset.forName("UTF-8");
>         CharsetDecoder decoder = charset.newDecoder();
> 
>         ByteBuffer byteBuffer = ( ByteBuffer ) message;
> 
>         String data = byteBuffer.getString(decoder);
> 
>         log.debug("Read the following request from the 
> client:\n" + data);
> 
>         // the rest of the code
>         MinaConnectionHandler handler = new 
> MinaConnectionHandler(this, data, ioSession);
> 
>         // Mina uses a thread pool to call the handler, so we 
> do not need to
>         // worry about that. Just call the run method...
>         handler.run();
> 
> 
>     }
> 
>     public void messageSent(IoSession ioSession, Object 
> object) throws Exception {
>     }
> 
> 
> 
> This is the class im trying to fix (that is update to mina 
> 1.0.1) at present
> 
> 
> 
> 
> 
> On 1/9/07, Hieu Phan Thanh <hieupt@cybersoft-vn.com> wrote:
> > Hi Peter,
> >
> > I suggest you to take a look the Sumup ([Server] class) 
> example which 
> > is attached in the mina source code.
> > It will show you how to bind a port as well as to add a filter.
> > Hope it could help you.
> >
> > Thanks & best regards,
> > Hieu Phan.
> >
> > > -----Original Message-----
> > > From: peter ellis [mailto:petermarkellis@googlemail.com]
> > > Sent: Tuesday, January 09, 2007 9:00 PM
> > > To: dev@mina.apache.org
> > > Subject: Service Registry
> > >
> > > Hi Guys
> > >
> > > Ive been left to manage someones server code and I have 
> been asked 
> > > to update from mina 0.8.2 to the latest release.
> > >
> > > This has resulted in a few code updates. However...
> > >
> > > The previous coder had the following....
> > >
> > >  private ServiceRegistry registry = null;  private Service 
> > > xmlServerService;
> > >
> > > I also have the following statements:
> > >
> > > private void addFilter(String filterName, IoFilter filter) {
> > >         log.debug("Adding filter " + filterName);
> > >
> > >         IoAcceptor acceptor =
> > > registry.getIoAcceptor(TransportType.SOCKET);
> > >         acceptor.getFilterChain().addLast(filterName, filter);
> > >     }
> > >
> > > Of which it requires the ServiceRegistry as registry.
> > >
> > > Can anyone tell me what to use instead here (I assume the 
> Server and 
> > > ServiceRegistry have been deprecated) ???
> > >
> > > Peter
> > >
> > >
> >
> >
> 
> 
> --
> Peter Ellis
> Java Developer
> 
> 


Mime
View raw message