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

TcpSocket.java

Go to the documentation of this file.
00001 /*
00002  * TcpSocket.java
00003  *
00004  * Created on den 27 oktober 2004, 09:05
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.Byte;
00028 import java.lang.StringBuffer;
00029 import java.net.InetSocketAddress;
00030 import java.nio.ByteBuffer;
00031 import java.nio.channels.SelectionKey;
00032 import java.nio.channels.SocketChannel;
00033 
00038 public class TcpSocket extends Socket
00039 {
00040     
00042     public TcpSocket(SocketHandler h,int ilen,int olen)
00043     {
00044         super(h);
00045         m_ilen = ilen;
00046         m_ibuf = ByteBuffer.allocate(ilen);
00047         m_olen = olen;
00048         m_obuf = ByteBuffer.allocate(olen);
00049         m_line = new StringBuffer(4096);
00050     } // TcpSocket
00051     public TcpSocket(SocketHandler h)
00052     {
00053         this(h, 16384, 30000);
00054     }
00055     public Socket Create()
00056     {
00057         return new TcpSocket(Handler(), m_ilen, m_olen);
00058     } // Create
00059     public void OnInitialOps()
00060     {
00061         if (Connecting())
00062         {
00063             GetKey().interestOps(SelectionKey.OP_CONNECT);
00064         }
00065         else
00066         {
00067             GetKey().interestOps(SelectionKey.OP_READ);
00068         }
00069     } // OnInitialOps
00070     
00071     public void Open(String host,int port)
00072     {
00073         InetSocketAddress isa = new InetSocketAddress(host, port);
00074         try
00075         {
00076             SocketChannel sc = SocketChannel.open();
00077             //        SocketChannel sc = (SocketChannel)GetChannel();
00078             sc.configureBlocking(false);
00079             if (!sc.connect(isa))
00080             {
00081                 SetConnecting();
00082             }
00083             else
00084             {
00085                 // connection immediately ok
00086             }
00087             attach(sc);
00088         }
00089         catch (java.io.IOException e)
00090         {
00091             Handler().LogError(this, "Open", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
00092             SetCloseAndDelete();
00093         }
00094     } // Open
00095     
00096     public void OnRead()
00097     {
00098         m_ibuf.clear();
00099         try
00100         {
00101             SocketChannel sc = (SocketChannel)GetChannel();
00102             int n = sc.read(m_ibuf);
00103             m_ibuf.flip();
00104             if (n == -1)
00105             {
00106                 Handler().LogError(this, "OnRead", 0, "error while reading", SocketHandler.LOG_LEVEL_FATAL);
00107                 SetCloseAndDelete();
00108             }
00109             else
00110                 if (n == 0)
00111                 {
00112                 }
00113                 else
00114                     if (n > 0 && n == m_ibuf.limit() )
00115                     {
00116                         OnRawData(m_ibuf, n);
00117                     }
00118                     else
00119                     {
00120                         Handler().LogError(this, "OnRead", 0, "ibuf.limit != n (buffer cleared)", SocketHandler.LOG_LEVEL_ERROR);
00121                         m_ibuf.clear();
00122                     }
00123         }
00124         catch (Exception e)
00125         {
00126             Handler().LogError(this, "OnRead", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
00127             SetCloseAndDelete();
00128         }
00129     } // OnRead
00130     
00131     public void OnDelete()
00132     {
00133         SocketChannel sc = (SocketChannel)GetChannel();
00134         try
00135         {
00136             sc.close();
00137         }
00138         catch (IOException e)
00139         {
00140             Handler().LogError(this, "OnDelete", 0, e.toString(), SocketHandler.LOG_LEVEL_ERROR);
00141         }
00142     } // OnDelete
00143     
00144     public void ReadLine()
00145     {
00146         for (int i = 0; i < m_ibuf.limit(); ++i)
00147         {
00148             byte b = m_ibuf.get( i );
00149             String aChar = new Character((char)b).toString();
00150             switch (b)
00151             {
00152                 case 13: // ignore CR
00153                     break;
00154                 case 10: // LF
00155                     OnLine( m_line.toString() );
00156                     m_line.delete(0, m_line.length());
00157                     break;
00158                 default:
00159                     m_line.append(aChar);
00160             }
00161         } // while
00162     }
00163     
00164     public void Send(String str)
00165     {
00166         byte[] bbuf = str.getBytes();
00167         SendBuf(bbuf, bbuf.length);
00168     } // Send
00169     
00170     public void SendBuf(byte[] bbuf,int l)
00171     {
00172         int n = 0; // assume obuf empty
00173         m_obuf.clear(); // make assumption valid and break buffered output
00174         m_obuf.put(bbuf);
00175         if (n == 0)
00176         {
00177             OnWrite();
00178         }
00179     } // SendBuf
00180     
00181     public void OnWrite()
00182     {
00183         m_obuf.flip();
00184         try
00185         {
00186             SocketChannel sc = (SocketChannel)GetChannel();
00187             int n = sc.write(m_obuf);
00188             System.out.println(n + " bytes written");
00189         }
00190         catch (Exception e)
00191         {
00192             Handler().LogError(this, "OnWrite", 0, e.toString(), SocketHandler.LOG_LEVEL_FATAL);
00193             SetCloseAndDelete();
00194         }
00195     } // OnWrite
00196     
00197     //
00198     private int m_ilen;
00199     private int m_olen;
00200     protected ByteBuffer m_ibuf;
00201     protected ByteBuffer m_obuf;
00202     protected StringBuffer m_line;
00203 }

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