Erori fixate, imbunatatiri threading, debug output optional.
[ChatApp.git] / ChatApp / appServerThread.java
blobdef197c57a3436aef836201faa3849ab58e53181
1 /*
2 * © 2010 ROBO Design
3 * http://www.robodesign.ro
5 * $Date: 2010-05-19 18:44:30 +0300 $
6 */
8 package ChatApp;
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) {
29 server = srv;
30 client = sock;
31 numeUtilizator = nume;
32 server.myApp.mesajDebug("appServerThread initializare " + nume);
34 if (!server.getServerPornit()) {
35 System.err.println("appServerThread: serverul nu este pornit");
36 eroare = true;
37 return;
40 try {
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);
47 ex.printStackTrace();
49 try {
50 if (out != null) {
51 out.close();
52 out = null;
54 if (!client.isClosed()) {
55 client.close();
57 } catch (Exception ex2) {
58 System.err.println("appServerThread: esec total. " + ex2);
59 ex2.printStackTrace();
62 client = null;
63 eroare = true;
67 // metoda care ruleaza in thread separat
68 public void run () {
69 if (!server.getServerPornit() || client == null || client.isClosed() || out == null || out.checkError() || in == null) {
70 System.err.println("appServerThread.run esec " + numeUtilizator);
71 oprire();
72 return;
75 server.myApp.mesajDebug("appServerThread.run " + numeUtilizator);
77 // primire comenzi de la client
78 String cmd = null;
79 while (server.getServerPornit() && in != null && client != null && !client.isClosed()) {
80 try {
81 cmd = in.readLine();
82 } catch (Exception ex) {
83 System.err.println("appServerThread.run readLine() a esuat. " + ex);
84 ex.printStackTrace();
85 break;
88 if (cmd == null || cmd.trim().isEmpty()) {
89 break;
92 server.procesareComanda(cmd, this);
93 cmd = null;
96 server.myApp.mesajDebug("appServerThread.run end " + numeUtilizator);
98 oprire();
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.");
105 return false;
108 server.myApp.mesajDebug("appServerThread.trimiteComanda " + numeUtilizator + " " + cmd);
110 try {
111 out.println(cmd);
112 } catch (Exception ex) {
113 System.err.println("appServerThread.trimiteComanda " + numeUtilizator + " a esuat. " + ex);
114 ex.printStackTrace();
115 return false;
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);
128 return false;
129 } else {
130 numeUtilizator = nume;
131 return true;
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;
142 return true;
143 } else {
144 return false;
148 public boolean getEroare () {
149 if (!eroare && (numeUtilizator == null || out == null || in == null || client == null || client.isClosed())) {
150 eroare = true;
153 return eroare;
156 // oprire conexiune cu client
157 public void oprire () {
158 server.myApp.mesajDebug("appServerThread.oprire " + numeUtilizator);
160 try {
161 if (in != null) {
162 in.close();
163 in = null;
166 if (out != null) {
167 out.close();
168 out = null;
171 if (client != null && !client.isClosed()) {
172 client.close();
174 client = null;
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: