黑马程序员技术交流社区
标题:
聊天系统,服务器端Server( 四)
[打印本页]
作者:
王鑫宇
时间:
2011-8-4 18:28
标题:
聊天系统,服务器端Server( 四)
5.ServerThread类[code=java]package userlogin;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.*;
import util.ServerChat;
import dao.UserDAO;
import dao.UserFriendsDAO;
public class ServerThread implements Runnable{
private String userName;
private String secondName;
private String msg;
String states;
Socket sc;
Vector ve;
Vector allFriends;
public static HashMap<String, String> user;
DataInputStream s_input;
DataOutputStream s_output;
private boolean on=true;
private String text;
public ServerThread(Socket sc,Vector vec){
this.sc=sc;
this.ve=vec;
try {
s_input=new DataInputStream(sc.getInputStream());
s_output=new DataOutputStream(sc.getOutputStream());
user = new HashMap<String,String>();
boolean flag1=true;
while(flag1){
msg=s_input.readUTF();
if(msg.equals("")){
break;
}
String name = msg.substring(0,msg.indexOf(";"));
states = msg.substring(msg.indexOf(";")+1,msg.indexOf(";")+3);
String password = msg.substring(msg.indexOf(";")+3);
userName = name;
boolean b = UserDAO.login(name, password,states);
if(b){
secondName = UserDAO.getSecondName(name);
write("#"+secondName);
user.put(userName, secondName);
String str = s_input.readUTF();
if(!str.equals("收到")){exit();}
allFriends = UserFriendsDAO.getFriend(userName);
if(allFriends.size()==0){
write("end");
break;
}
for(int j = 0 ; j < allFriends.size();j++){
HashMap hashMap = (HashMap)allFriends.get(j);
String friendName = hashMap.get("friendName").toString();
String nickName = hashMap.get("nickName").toString();
String friendSetVisible = hashMap.get("friendSetVisible").toString();
String friendState = hashMap.get("friendstate").toString();
String stopful = hashMap.get("stopful").toString();
// String state = hashMap.get("state").toString();
// if(state.equals("隐身")&&setVisible.equals("否")) state = "离线";
// if(state.equals("隐身")&&setVisible.equals("是")) state = "在线";
if(friendState.equals("隐身")&&friendSetVisible.equals("否")) friendState="离线";
if(friendState.equals("隐身")&&friendSetVisible.equals("是")) friendState="在线";
write("friend"+friendName.length()+";"+friendName+nickName.length()+";"+nickName+friendState.length()+";"+friendState+stopful.length()+";"+stopful);
}
write("end");
for(int i = 0 ; i < ve.size() ; i ++){
if(ServerChat.vec_Threads.get(i).getUserName().equals(userName)){
ServerChat.vec_Threads.get(i).write("r");
ServerChat.vec_Threads.get(i).exit();
continue;
}
// if(allFriends.size()==0){
// write("end");
// break;
// }
ServerThread serverThread=(ServerThread)ve.get(i);
for(int j = 0 ; j < allFriends.size();j++){
HashMap hashMap = (HashMap)allFriends.get(j);
String friendName = hashMap.get("friendName").toString();
String setVisible = hashMap.get("setVisible").toString();
String state = hashMap.get("state").toString();
// String friendState = hashMap.get("friendstate").toString();
// if(state.equals("隐身")&&setVisible.equals("否")) state = "离线";
// if(state.equals("隐身")&&setVisible.equals("是")) state = "在线";
if(friendName.equals(serverThread.getUserName())&&(!(state.equals("隐身")&&setVisible.equals("否"))||(state.equals("隐身")&&setVisible.equals("是")))){
serverThread.write("l"+userName);
}
}
// s_output.writeUTF("o"+serverThread.getSecondName());
// s_output.writeUTF("u"+serverThread.getUserName());
}
break;
}else if(!b){
s_output.writeUTF("*");
sc.close();
return;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static HashMap putUser(){
return user;
}
public void write(String msg){
synchronized(s_output){
try{
s_output.writeUTF(msg);
}catch(Exception e){
}
}
}
public Socket getSocket(){
return sc;
}
public String getUserName(){
return this.userName;
}
public String getSecondName(){
return this.secondName;
}
public void exit(){
try {
s_output.writeUTF("q");
UserDAO.exit(userName);
UserFriendsDAO.setLogOut(userName);
for(int i = 0 ; i < allFriends.size();i++){
HashMap h = (HashMap)allFriends.get(i);
String name = h.get("friendName").toString();
for(int j = 0 ; j < ServerChat.vec_Threads.size() ; j++){
if(ServerChat.vec_Threads.get(j).getUserName().equals(name)){
ServerChat.vec_Threads.get(j).write("t"+userName);
break;
}
}
}
this.sc.close();
ve.removeElement(this);
} catch (IOException e) {
e.printStackTrace();
}
for(int i=0;i<ve.size();i++){
ServerThread st1=(ServerThread)ve.get(i);
if(st1.getUserName().equals(this.getUserName())){
ServerChat.vec_Users.remove(i);
break;
}
}
//ServerChat.exit(getUserName());
}
public void run() {
while(on){
try {
text = s_input.readUTF();
if(text.equals("EXIT")){
exit();
return;
}
if(text.charAt(0)=='m'){
s_output.writeUTF("m"+UserDAO.getFriendInfo(userName));
continue;
}
if(text.charAt(0)=='g'){
int num = UserDAO.updateInfo(userName,text.substring(1));
s_output.writeUTF("g"+num);
continue;
}
if(text.charAt(0)=='s'){
s_output.writeUTF("f"+UserDAO.getFriendInfo(text.substring(1)));
continue;
}
if(text.charAt(0)=='[' && text.substring(text.indexOf("[")+1,text.indexOf("]")).equals("公聊")){
ServerChat.say(2,text,getSecondName());
continue;
}
if(text.charAt(0)=='[' && text.substring(text.indexOf("[")+1,text.indexOf("]")).equals("私聊")){
ServerChat.say(3,text,getSecondName());
continue;
}
if(text.charAt(0)=='a'){
int num = UserFriendsDAO.addFriend(userName, text.substring(1),states);
if(num==1){
HashMap h = UserFriendsDAO.getFriendInfo(text.substring(1),true);
allFriends.add(h);
String friendName = h.get("friendName").toString();
String nickName = h.get("nickName").toString();
String state = h.get("friendstate").toString();
String stopful = "否";
write("friend"+friendName.length()+";"+friendName+nickName.length()+";"+nickName+state.length()+";"+state+stopful.length()+";"+stopful);
for(int i = 0 ;i <ServerChat.vec_Threads.size();i++){
if(ServerChat.vec_Threads.get(i).getUserName().equals(friendName)){
ServerChat.vec_Threads.get(i).write("p"+userName.length()+";"+userName+secondName);
}else{
}
}
}
s_output.writeUTF("a"+num);
continue;
}
if(text.charAt(0)=='c'){
int num = Integer.parseInt(text.substring(1,text.indexOf(";")));
int len = String.valueOf(num).length();
String old = text.substring(len+2,len+2+num);
String news = text.substring(len+2+num);
int temp = UserDAO.modifiPassword(userName, old, news);
write("c"+temp);
}
if(text.charAt(0)=='5'){
String friendName = text.substring(text.indexOf("(")+1,text.indexOf(")"));
for(int i = 0 ; i <ServerChat.vec_Threads.size();i++){
if(ServerChat.vec_Threads.get(i).getUserName().equals(friendName)){
ServerChat.vec_Threads.get(i).write(text);
}
}
}
if(text.charAt(0)=='k'){
int num = UserFriendsDAO.setVisible(userName,text.substring(1));
if(num==1){
for(int i = 0 ; i<ServerChat.vec_Threads.size();i++){
if(ServerChat.vec_Threads.get(i).getUserName().equals(text.substring(1))){
ServerChat.vec_Threads.get(i).write("l"+userName);
}
}
}
write("k"+num);
}
if(text.charAt(0)=='d'){
int num = UserFriendsDAO.setDisvisible(userName,text.substring(1));
if(num==1){
for(int i = 0 ; i<ServerChat.vec_Threads.size();i++){
if(ServerChat.vec_Threads.get(i).getUserName().equals(text.substring(1))){
ServerChat.vec_Threads.get(i).write("t"+userName);
}
}
}
write("d"+num);
}
if(text.charAt(0)=='b'){
int num = UserFriendsDAO.setStopful(userName,text.substring(1));
// if(num==1){
// for(int i = 0 ; i<ServerChat.vec_Threads.size();i++){
// if(ServerChat.vec_Threads.get(i).getUserName().equals(text.substring(1))&&states.equals("在线")){
// ServerChat.vec_Threads.get(i).write("t"+userName);
// }
// }
// }
write("b"+num);
}
if(text.charAt(0)=='v'){
int num = UserFriendsDAO.setStopless(userName,text.substring(1));
if(num==1){
for(int i = 0 ; i<ServerChat.vec_Threads.size();i++){
if(ServerChat.vec_Threads.get(i).getUserName().equals(text.substring(1))&&states.equals("在线")){
ServerChat.vec_Threads.get(i).write("l"+userName);
}
}
}
write("v"+num);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}[/code]
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2