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

Quick Search    Search Deep

Source code: cxtable/core_comm/xClientConn.java


1   package cxtable.core_comm;
2   import cxtable.plugin.*;
3   import cxtable.peer.*;
4   
5   
6   
7   /*
8   This class manages the Socket streams and provides a front end for
9   the rest of the application to write to and read from the sockets.  
10  When a class calls the send(String) method, it passes that to xSockWriter
11  for writing.
12  This class maintains an xListenerRegistry with all of the objects
13  that implement xListener who've registered with this xClientConn.
14  
15  When xSockReader reads, it passes line by line to this class, which
16  then dispatches a xListenerDispatch thread.  This thread handles passing
17  the String to the xListener(s) for processing.
18  
19  Currently, each peer-to-peer connection uses -2- of these... as the
20  connection is "client-end" initiated... 
21  */
22  
23     import java.io.*;
24     import java.net.*;
25  
26  
27      public class xClientConn extends Thread implements xOutListen,xListener,xRemovable{
28     
29        private Socket sock;
30    private static int total_xcc=0;
31    private int me;
32    private static final String[] key = new String[0];
33        private boolean enabled=false;
34        private xMessageConverter xmc=new xMessageConverter();
35        private boolean chatty=false;
36        private xRemoveListen xreggy;
37        private boolean listening=false;
38        private xSockReader xsr;
39        private xSockWriter xsw;
40        private String name,port,itad;
41        private xListenerRegistry listeners;
42        private String alkey="";
43        private String server_id=""; private String client_id="";
44        private String reg_id ="";
45        private boolean control;
46       
47        public xClientConn(Socket _s)
48        {
49    total_xcc++;
50     me=total_xcc;
51             sock=_s;
52           xsr = new xSockReader(sock,this);
53           xsw = new xSockWriter(sock);
54           listeners=new xListenerRegistry();
55        }
56     
57        public void run()
58        {
59           if (xsr.create() == true) {new Thread(xsr).start();}
60        
61           if (xsw.create() == true) 
62           {
63              try{xsw.write("<OK>"+sock.getInetAddress()+"[port]:"+sock.getLocalPort()+"</OK>");}
64                 catch(Exception e) {
65                    System.out.println("Error writing 'OK'");
66                 /*error here*/
67                 }
68           }  
69        }
70        public void send(String s)
71        {
72           /*System.out.println("in_send");*/
73           boolean b;
74           int x=0;
75           do{x++; b=xsw.write(s);
76              if (x>10) {b=true;}} while(!b);      
77        }
78        public String who(){
79           return "xClientConn:"+reg_id+"(("+me+" of "+total_xcc+"))" ;}
80  
81        public boolean readAll()
82    {return true;}
83  
84        public String[] readKeys()
85    {return key;}
86  
87  
88        
89        public void process(String s)
90        {
91           
92           if (s==null)
93       {System.out.println("xCC("+name+"):Received null string");
94       return;}
95  
96           new Thread(new xListenerDispatch(listeners,xmc,s)).start();
97        
98        
99        /*end process*/
100       }
101    
102       public void set_Name(String n)
103       {name=n;}
104       public String get_Name()
105       {return name;}
106 
107       public void set_IP(String s)
108       {itad=s;}
109       public String get_IP()
110       {return itad;}
111       
112       public void set_Port(String s)
113       {port=s;}
114       public String get_Port()
115       {return port;}
116    
117       public void setListen(xListener x)
118       { 
119       
120          listeners.add(x);
121          listening = true;
122       }
123      
124      /*The next two methods are for the SocketReader*/
125       public void removeListeners()
126       {
127          listeners.removeAll();listening=false;
128       }
129    
130       public void remove()
131       {
132          if (xreggy !=null)
133          {xreggy.remove(this);}
134          xsw.dump(who());
135       
136       }
137      /*The following method is for the xRegistry to add itself*/
138    
139       public void setReg(xRemoveListen x)
140       {
141          xreggy=x;
142       }
143    
144       public void offListen(xListener x)
145       {
146          listeners.remove(x);
147       }
148 
149       public void read(String s)
150       {send(s);}
151    
152       public void setRegID(String s)
153       {reg_id = s;}
154    
155       public void setClientID(String s)
156       {client_id=s;
157          if (chatty == true){System.out.println("xCC setClientID to:"+client_id);}
158       }
159    
160       public void setServerID(String s)
161       {server_id=s;}
162    
163       public String getRegID()
164       {
165          return reg_id;}
166    
167       public String getClientID()
168       {
169          return client_id;}
170    
171       public String getServerID()
172       {
173          return server_id;}
174       public void setControl(boolean b)
175       {control=b;}
176       public boolean getControl()
177       {
178          return control;}
179    
180    
181    /*newest methods for peer-to-peer enable... need to check the instance
182    of xCC to make sure that it has not been accomplished by reg-server 
183    enable, or else attempting it peer-to-peer is futile*/
184    
185       public void setEnableBit(boolean b)
186       {enabled=b;}
187    
188       public boolean getEnableBit()
189       {
190          return enabled;}
191    
192       public void kill()
193       {
194          send(who()+":"+name+":-> closing");
195       
196          try{Thread.sleep(1000);}
197             catch(Exception e){
198             }
199          try{sock.close();}
200             catch(Exception e){
201                System.out.println("Could not close socket");}
202       }
203    
204    
205    /*end class*/
206    
207    /*static compare method*/
208    
209       public static boolean equals_(xClientConn one, xClientConn two)
210       {
211          if (one == two) {
212             return true;}
213          String or=one.getRegID();String oc=one.getClientID(); String os=one.getServerID();
214          String tr=two.getRegID();String tc=two.getClientID(); String ts=two.getServerID();
215       /*comment out later*/
216          System.out.println(or+"::"+oc+"::"+os+"\n"+tr+"::"+tc+"::"+ts);
217       
218       /*this compares RegID, ClientID and ServerID.. if ALL three are the same, this 
219       is the same ClientConn {even if it is not the same EXACT object}*/
220          if ((or.equals(tr)) & (oc.equals(tc))&(os.equals(ts)))
221          {
222             return true;}
223       
224          return false;
225       }
226    }