00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 package net.alhem.jsockets;
00025 
00026 import java.io.IOException;
00027 import java.lang.Class;
00028 import java.net.InetSocketAddress;
00029 import java.net.ServerSocket;
00030 import java.nio.channels.SelectionKey;
00031 import java.nio.channels.ServerSocketChannel;
00032 import java.nio.channels.SocketChannel;
00033 
00034 
00035 
00040 public class ListenSocket extends Socket
00041 {
00042     
00044     public ListenSocket(SocketHandler h,Socket creator)
00045     {
00046         super(h);
00047         m_creator = creator;
00048     } 
00049     public Socket Create()
00050     {
00051         return new ListenSocket(Handler(), m_creator); 
00052     } 
00053     public void OnInitialOps()
00054     {
00055         GetKey().interestOps(SelectionKey.OP_ACCEPT);
00056     } 
00057     
00058     public int Bind(int port)
00059     {
00060         
00061         
00062         try
00063         {
00064             ServerSocketChannel ssc = ServerSocketChannel.open();
00065             
00066             
00067             ssc.configureBlocking( false );
00068             
00069             
00070             
00071             ServerSocket ss = ssc.socket();
00072             InetSocketAddress isa = new InetSocketAddress( port );
00073             ss.bind( isa );
00074             
00075             attach(ssc);
00076             return 0;
00077         } catch (java.io.IOException e)
00078         {
00079             Handler().LogError(this, "Bind", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
00080         }
00081         return -1;
00082     } 
00083     
00084     public void OnRead()
00085     {
00086         try
00087         {
00088             ServerSocketChannel ssc = (ServerSocketChannel)GetChannel();
00089             java.net.ServerSocket ss = (java.net.ServerSocket)ssc.socket();
00090             
00091             
00092             
00093             try
00094             {
00095                 java.net.Socket js = ss.accept();
00096                 System.out.println( "Got connection from " + js);
00097                 
00098                 
00099                 
00100                 SocketChannel sc = js.getChannel();
00101                 sc.configureBlocking( false );
00102                 
00103                 
00104                 
00105                 
00106                 Socket s = m_creator.Create();
00107                 System.out.println("New Socket object: " + s.toString() );
00108                 s.attach(sc);
00109                 Handler().Add(s);
00110                 s.OnAccept();
00111             }
00112             catch (Exception e)
00113             {
00114                 Handler().LogError(this, "OnRead", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
00115             }
00116         } catch (java.lang.NullPointerException e)
00117         {
00118             Handler().LogError(this, "OnRead", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
00119         }
00120     } 
00121 
00122     public void OnDelete()
00123     {
00124         ServerSocketChannel sc = (ServerSocketChannel)GetChannel();
00125         try
00126         {
00127             sc.close();
00128         }
00129         catch (IOException e)
00130         {
00131             Handler().LogError(this, "OnDelete", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
00132         }
00133     } 
00134     
00135     
00136     private Socket m_creator;
00137 }