Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/act365/net/tcp/TCPJListener.java


1   /*
2     * JSocket Wrench
3     * 
4     * Copyright (C) act365.com October 2003
5     * 
6     * Web site: http://www.act365.com/wrench
7     * E-mail: developers@act365.com
8     * 
9     * The JSocket Wrench library adds support for low-level Internet protocols
10    * to the Java programming language.
11    * 
12    * This program is free software; you can redistribute it and/or modify it 
13    * under the terms of the GNU General Public License as published by the Free 
14    * Software Foundation; either version 2 of the License, or (at your option) 
15    * any later version.
16    *  
17    * This program is distributed in the hope that it will be useful, 
18    * but WITHOUT ANY WARRANTY; without even the implied warranty of 
19    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General 
20    * Public License for more details.
21    * 
22    * You should have received a copy of the GNU General Public License along with 
23    * this program; if not, write to the Free Software Foundation, Inc., 
24    * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25    */
26  
27  package com.act365.net.tcp ;
28  
29  import com.act365.net.*;
30  import com.act365.net.ip.*;
31  
32  import java.beans.*;
33  import java.io.*;
34  import java.net.*;
35  
36  /**
37   <code>TCPJListener</code> is a singleton class that polls for TCPJ messages.
38   It raises <code>PropertyChangeEvent</code> objects that, unconventionally,
39   contain an <code>IPMessage</code> object as the old value and a <code>TCPMessage</code> 
40   objects as the new.
41  */
42  
43  public class TCPJListener extends Thread {
44  
45    PropertyChangeSupport pcs ;
46  
47    boolean alive ;
48  
49    DatagramSocket socket ;
50  
51    static int protocol ;
52    
53    static TCPJListener listener = new TCPJListener();
54  
55    static {
56      if( ( protocol = SocketUtils.getProtocol() ) == 0 ){
57        protocol = SocketConstants.IPPROTO_TCPJ ;
58      }    
59    }
60    
61    TCPJListener(){
62      alive = true ;
63      pcs = new PropertyChangeSupport( this );
64   
65      try {
66        socket = new DatagramSocket();
67      } catch( SocketException e ){
68        System.err.println("TCPListener: " + e.getMessage() );
69      }
70    }
71  
72    /**
73     * Returns a reference to the single <code>TCPJListener</code> object.
74     */
75  
76    public static TCPJListener getInstance() {
77      return listener ;
78    }
79  
80    /**
81     * Starts the listener.
82     */
83    
84    public void run() {
85  
86      while( socket == null ); // The socket takes a while to start.
87  
88      byte[] buffer = new byte[ 2048 ];
89  
90      DatagramPacket packet ;
91  
92      IP4Message ipmessage ;
93  
94      TCPMessage tcpmessage ;
95  
96      try {
97  
98        while( alive ){
99  
100         packet = new DatagramPacket( buffer , buffer.length );
101 
102         socket.receive( packet );
103 
104         ipmessage = IP4Reader.read( packet.getData() , packet.getLength() , false );
105 
106         int mprotocol = ipmessage.protocol >= 0 ? ipmessage.protocol : 0xffffff00 ^ ipmessage.protocol ;
107 
108         if( mprotocol != protocol ){
109           continue ;
110         }
111 
112         tcpmessage = TCPReader.read( ipmessage.data , 0 , ipmessage.data.length );
113 
114         pcs.firePropertyChange( "TCPJ", ipmessage , tcpmessage );
115       }
116     } catch( IOException e ) {
117       System.err.println("TCPJListener: " + e.getMessage() );
118       return ;
119     }
120   }
121  
122   /**
123    * Terminates the listener.
124    */
125   
126   public void terminate() {
127     alive = false ;
128   }
129 
130   /**
131    * Registers an object to be notified if a message is received.
132    * @param l object to be registered
133    */
134   
135   public synchronized void addPropertyChangeListener( PropertyChangeListener l ){
136     pcs.addPropertyChangeListener( l );
137   }
138 
139   /**
140    * Deregisters an object.
141    * @param l object to be deregistered
142    */
143   
144   public synchronized void removePropertyChangeListener( PropertyChangeListener l ){
145     pcs.removePropertyChangeListener( l );
146   }
147 }
148