Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

ListenSocket.java

Go to the documentation of this file.
00001 /*
00002  * ListenSocket.java
00003  *
00004  * Created on den 25 oktober 2004, 14:21
00005  */
00006 /*
00007 Copyright (C) 2004  Anders Hedstrom
00008  
00009 This program is free software; you can redistribute it and/or
00010 modify it under the terms of the GNU General Public License
00011 as published by the Free Software Foundation; either version 2
00012 of the License, or (at your option) any later version.
00013  
00014 This program is distributed in the hope that it will be useful,
00015 but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 GNU General Public License for more details.
00018  
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
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     } // Create
00053     public void OnInitialOps()
00054     {
00055         GetKey().interestOps(SelectionKey.OP_ACCEPT);
00056     } // OnInitialOps
00057     
00058     public int Bind(int port)
00059     {
00060         // Instead of creating a ServerSocket,
00061         // create a ServerSocketChannel
00062         try
00063         {
00064             ServerSocketChannel ssc = ServerSocketChannel.open();
00065             
00066             // Set it to non-blocking, so we can use select
00067             ssc.configureBlocking( false );
00068             
00069             // Get the Socket connected to this channel, and bind it
00070             // to the listening port
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     } // Bind
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             // It's an incoming connection.
00091             // Register this socket with the Selector
00092             // so we can listen for input on it
00093             try
00094             {
00095                 java.net.Socket js = ss.accept();
00096                 System.out.println( "Got connection from " + js);
00097                 
00098                 // Make sure to make it non-blocking, so we can
00099                 // use a selector on it.
00100                 SocketChannel sc = js.getChannel();
00101                 sc.configureBlocking( false );
00102                 
00103                 // Register it with the selector, for reading
00104                 //        sc.register( selector, SelectionKey.OP_READ );
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     } // OnRead
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     } // OnDelete
00134     
00135     //
00136     private Socket m_creator;
00137 }

Generated on Fri Oct 29 14:11:17 2004 for Java Sockets by  doxygen 1.3.9.1