📄 serverthread.java
字号:
}
private void addFriends(Communicate d){
ServerClientData scd=(ServerClientData)d;
List<Long> temp=(LinkedList<Long>)scd.getFriends();
long id=scd.getID();
//if id doesn't exit--maybe a online client has been deled
if(!Server.getServerDatabase().contains(id) || temp==null){
processing=false;
return;
}
//check vilidation
for(int i=0;i<temp.size();i++){
if(!Server.getServerDatabase().contains(temp.get(i).longValue()) //if id doesn't in database
//if friend has already in friend-list
|| Server.getServerDatabase().findClient(id).getFriends().contains(temp.get(i))
//if add self as friend
|| temp.get(i).longValue()==id){
temp.remove(i);
i=0;
}
}
if(temp==null){
processing=false;
return;
}
//System.out.println("test: add friends,client ID:"+id);
Server.getServerDatabase().addClientFriends(id,temp);
//System.out.println("to checkOnlineFriends()"); //**
checkOnlineFriends(id); //start another task
}
private void delFriends(Communicate d) throws IOException{
//String tf,info;
ServerClientData scd=(ServerClientData)d;
List<Long> temp=scd.getFriends(); //to-del friends' ids
long id=scd.getID(); //client's id
//if id doesn't exit--maybe a online client has been deled
if(!Server.getServerDatabase().contains(id) || temp==null){
processing=false;
return;
}
//del this client's friends list
System.out.println("to del friend:"+temp); //**test
List<Long> tempList=Server.getServerDatabase().delClientFriends(id,temp);
//del this client's id from deled friends' friend-list
if(tempList!=null){ //if some friend id are not validate then del them from temp
System.out.println("invalide friend:"+tempList); //**test
temp.removeAll(tempList);
}
//System.out.println("after remove,temp:"+temp);
if(temp.size()==0){
processing=false;
return;
}
//del this client's id from deled friends' friend-list
long tempFriendID;
List<Long> aFriend=new LinkedList<Long>();
aFriend.add(new Long(id));
for(int i=0;i<temp.size();i++){
tempFriendID=temp.get(i).longValue(); //get friend's id
Server.getServerDatabase().delClientFriends(tempFriendID,aFriend);
}
processing=false;
}
/*check if any of this clients friend online,if any info both friends and client
called by addFriends() or dealOnline()*/
private void checkOnlineFriends(long id){ //id:serach key,client' id
//friends list
LinkedList<Long> tempFriends=(LinkedList<Long>)Server.getServerDatabase().findClient(id).getFriends();
//online clients list
LinkedList<Long> tempOnlineClients=(LinkedList<Long>)Server.getServerDatabase().getOnlineClientsList();
//if any friend online,put its id into this list,at the end the list is sent to client
List<Long> tempOnlineFriendsID=new LinkedList<Long>();
//if any friend online,put its ip into this list,at the end the list is sent to client
List<String> tempOnlineFriendsIP=new LinkedList<String>();
//if any friend online,put its name into this list,at the end the list is sent to client
List<String> tempOnlineFriendsName=new LinkedList<String>();
System.out.println("friends:"+tempFriends); //**test
System.out.println("online clients"+tempOnlineClients);
Socket toFriendSocket=null;
Socket toClientSocket=null;
InetAddress tempInetAddress=tempSocket.getInetAddress();//get client's ip
long tempID; //to store friend's id
String tempIP=null; //to store friend's ip
String tempName=null; //to store friend's name
String clientName=Server.getServerDatabase().findClient(id).getName();
String clientIP=tempInetAddress.toString().substring(1);
ByteArrayOutputStream tempBaos;
ObjectOutputStream tempOos;
DataOutputStream tempDos;
byte[] tempByteArray;
String idIPName=null; //client's id+ip
if(tempFriends.size()==0){ //have no friend
processing=false;
System.out.println("Have no friends");
return;
}
if(tempOnlineClients.size()<=0){ //only the client online
processing=false;
System.out.println("No other clients online");
return;
}
//string: id+ip+name
idIPName=String.valueOf(id)+","+clientIP+","+clientName;
for(int i=0;i<tempFriends.size();i++){ //check begin
if(tempOnlineClients.contains(tempFriends.get(i))) {//if one friends online
/*inform one online friend that this client(its friend) has gone online*/
tempID=tempFriends.get(i).longValue(); //get search key(friend's id)
tempIP=Server.getServerDatabase().findClient(tempID).getIP(); //get friend's ip
tempName=Server.getServerDatabase().findClient(tempID).getName(); //get friend's name
try{
toFriendSocket=new Socket(tempIP,4445);
//tempOos=new ObjectOutputStream(/*new BufferedOutputStream(*/
//toFriendSocket.getOutputStream());
//write info to the online friends
//System.out.println("oneFriendOnline:"+idIPName); //**test
//toFriendSocket.setSoLinger(true,2);
tempBaos=new ByteArrayOutputStream(6000);
tempOos=new ObjectOutputStream(tempBaos);
tempOos.flush();
tempOos.writeObject(new ServerClientData("oneFriendOnline",idIPName));
tempOos.flush();
tempByteArray=tempBaos.toByteArray();
tempDos=new DataOutputStream(toFriendSocket.getOutputStream());
tempDos.write(tempByteArray,0,tempByteArray.length);
//for(int j=0;j<10000000;j++); //**test
//close temporary socket and stream
tempOos.close();
tempBaos.close();
tempDos.close();
toFriendSocket.close();
}catch(ConnectException e){
continue;
}
catch(UnknownHostException e){
System.out.println(e+" ServerThread "+e.getMessage());
}catch(IOException e){
System.out.println(e+" ServerThread "+e.getMessage());
}
//add id and ip to lists
tempOnlineFriendsID.add(new Long(tempID));
tempOnlineFriendsIP.add(tempIP);
tempOnlineFriendsName.add(tempName);
}
}
if(tempOnlineFriendsID.size()==0){ //got no info to send
processing=false;
System.out.println("Get no valide friends!!"); //**test
return;
}
//System.out.println(tempOnlineFriendsID); //**test
//System.out.println(tempOnlineFriendsIP); //**test
//send this client its online friends ids and ips
try{
toClientSocket=new Socket(tempInetAddress,4445);
tempDos=new DataOutputStream(toClientSocket.getOutputStream());
//tempOos=new ObjectOutputStream(/*new BufferedOutputStream(*/
//toClientSocket.getOutputStream());
//System.out.println("onlineFriendsList:"+tempOnlineFriendsID); //**test
//toClientSocket.setSoLinger(true,2);
tempBaos=new ByteArrayOutputStream(6000);
tempOos=new ObjectOutputStream(tempBaos);
tempOos.flush();
tempOos.writeObject(new ServerClientData("onlineFriendsList",tempOnlineFriendsID,tempOnlineFriendsIP,tempOnlineFriendsName));
tempOos.flush();
//tempByteArray=new byte[6000];
tempByteArray=tempBaos.toByteArray();
tempDos.write(tempByteArray,0,tempByteArray.length);
//for(int j=0;j<1000000;j++); //**test
tempOos.close();
tempBaos.close();
tempDos.close();
toClientSocket.close();
}catch(UnknownHostException e){
System.out.println(e+" ServerThread "+e.getMessage());
}catch(IOException e){
System.out.println(e+" ServerThread "+e.getMessage());
}
processing=false;
}
/*when a client request to quit ,check if any this client's friend online,
if any,inform them; called by dealQuit()*/
private void checkOfflineFriends(long id){ //client's id
//System.out.println("checkOfflineFriends was called"); //**test
//friends list
LinkedList<Long> tempFriends=(LinkedList<Long>)Server.getServerDatabase().findClient(id).getFriends();
//online clients list
LinkedList<Long> tempOnlineClients=(LinkedList<Long>)Server.getServerDatabase().getOnlineClientsList();
if(tempFriends.size()==0){ //have no friend
processing=false;
return;
}
String tempIP;
long tempID;
Socket toFriendSocket;
ByteArrayOutputStream tempBaos;
ObjectOutputStream tempOos;
DataOutputStream tempDos;
byte[] tempByteArray;
for(int i=0;i<tempFriends.size();i++){
if(tempOnlineClients.contains(tempFriends.get(i))){ //if one friend online
tempID=tempFriends.get(i).longValue(); //get search key(friend's id)
tempIP=Server.getServerDatabase().findClient(tempID).getIP(); //get friend's ip
try{
toFriendSocket=new Socket(tempIP,4445);
tempDos=new DataOutputStream(toFriendSocket.getOutputStream());
//tempOos=new ObjectOutputStream(/*new BufferedOutputStream(*/
//toFriendSocket.getOutputStream());
//write info to the online friends
//toFriendSocket.setSoLinger(true,2);
tempBaos=new ByteArrayOutputStream(6000);
tempOos=new ObjectOutputStream(tempBaos);
tempOos.flush();
tempOos.writeObject(new ServerClientData("oneFriendQuit",id));
tempOos.flush();
tempByteArray=new byte[6000];
tempByteArray=tempBaos.toByteArray();
//for(int j=0;j<1000000;j++); //**test
//System.out.println("friend-quit info is sent to ID:"+tempID+" IP:"+tempIP); //**test
//close temporary socket and stream
tempDos.write(tempByteArray,0,tempByteArray.length);
tempOos.close();
tempBaos.close();
tempDos.close();
toFriendSocket.close();
}catch(ConnectException e){
continue;
}
catch(UnknownHostException e){
System.out.println(e+" ServerThread "+e.getMessage());
}catch(IOException e){
System.out.println(e+" ServerThread "+e.getMessage());
}
}
}
processing=false;
}
private void infoBack(Communicate d) {
try{
oos.flush();
oos.writeObject(d);
oos.flush();
byteArray=new byte[6000];
byteArray=baos.toByteArray();
dos.write(byteArray,0,byteArray.length);
}catch(IOException e){
System.out.println(e+" ServerThread "+e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -