3 * http://www.robodesign.ro
5 * $Date: 2010-05-19 18:44:30 +0300 $
10 import java
.io
.BufferedReader
;
11 import java
.io
.InputStreamReader
;
12 import java
.io
.PrintStream
;
13 import java
.net
.Socket
;
15 // clasa de tip thread ce gestioneaza o conexiune cu un client. pentru fiecare
16 // client un obiect nou este alocat si executat intr-un thread separat.
18 public class appServerThread
extends Thread
{
19 private Socket client
= null;
20 private appServer server
;
21 private PrintStream out
= null;
22 private BufferedReader in
= null;
23 private volatile String numeUtilizator
= null;
24 private volatile boolean clientInitializat
= false;
25 private volatile boolean eroare
= false;
27 // initializare thread
28 public appServerThread (String nume
, Socket sock
, appServer srv
) {
31 numeUtilizator
= nume
;
32 server
.myApp
.mesajDebug("appServerThread initializare " + nume
);
34 if (!server
.getServerPornit()) {
35 System
.err
.println("appServerThread: serverul nu este pornit");
41 out
= new PrintStream(client
.getOutputStream(), true, "utf-8");
42 in
= new BufferedReader(new InputStreamReader(client
.getInputStream(), "utf-8"));
43 server
.myApp
.mesajDebug("appServerThread initializare reusita " + numeUtilizator
);
45 } catch (Exception ex
) {
46 System
.err
.println("appServerThread: esec initializare. " + ex
);
54 if (!client
.isClosed()) {
57 } catch (Exception ex2
) {
58 System
.err
.println("appServerThread: esec total. " + ex2
);
59 ex2
.printStackTrace();
67 // metoda care ruleaza in thread separat
69 if (!server
.getServerPornit() || client
== null || client
.isClosed() || out
== null || out
.checkError() || in
== null) {
70 System
.err
.println("appServerThread.run esec " + numeUtilizator
);
75 server
.myApp
.mesajDebug("appServerThread.run " + numeUtilizator
);
77 // primire comenzi de la client
79 while (server
.getServerPornit() && in
!= null && client
!= null && !client
.isClosed()) {
82 } catch (Exception ex
) {
83 System
.err
.println("appServerThread.run readLine() a esuat. " + ex
);
88 if (cmd
== null || cmd
.trim().isEmpty()) {
92 server
.procesareComanda(cmd
, this);
96 server
.myApp
.mesajDebug("appServerThread.run end " + numeUtilizator
);
101 // metoda ce permite trimiterea de mesaje la client
102 public synchronized boolean trimiteComanda (String cmd
) {
103 if (out
== null || out
.checkError() || !server
.getServerPornit() || client
== null || client
.isClosed() || eroare
|| cmd
== null || cmd
.trim().isEmpty()) {
104 System
.err
.println("appServerThread.trimiteComanda " + numeUtilizator
+ " esec. Starea conexiunii este invalida.");
108 server
.myApp
.mesajDebug("appServerThread.trimiteComanda " + numeUtilizator
+ " " + cmd
);
112 } catch (Exception ex
) {
113 System
.err
.println("appServerThread.trimiteComanda " + numeUtilizator
+ " a esuat. " + ex
);
114 ex
.printStackTrace();
118 return !out
.checkError();
121 public String
getNumeUtilizator () {
122 return numeUtilizator
;
125 public synchronized boolean setNumeUtilizator (String nume
) {
126 if (nume
== null || nume
.isEmpty() || nume
.trim().isEmpty() || nume
.indexOf(" ") != -1 || nume
.equals("#server")) {
127 System
.err
.println("appServerThread.setNumeUtilizator nume invalid: " + nume
);
130 numeUtilizator
= nume
;
135 public boolean getClientInitializat () {
136 return clientInitializat
;
139 public synchronized boolean setClientInitializat (boolean val
) {
140 if (numeUtilizator
!= null && !eroare
&& client
!= null && !client
.isClosed()) {
141 clientInitializat
= val
;
148 public boolean getEroare () {
149 if (!eroare
&& (numeUtilizator
== null || out
== null || in
== null || client
== null || client
.isClosed())) {
156 // oprire conexiune cu client
157 public void oprire () {
158 server
.myApp
.mesajDebug("appServerThread.oprire " + numeUtilizator
);
171 if (client
!= null && !client
.isClosed()) {
175 } catch (Exception ex
) {
176 System
.err
.println("appServerThread.oprire cu erori. " + ex
);
177 ex
.printStackTrace();
180 server
.clientIesire(this);
181 clientInitializat
= false;
185 // vim:set fo=wancroql tw=80 ts=2 sw=2 sts=2 sta et ai cin ff=unix: