⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 friendslistmanager.java

📁 是一款国外的网络游戏平台的源码*不是类似浩方那种虚拟局域网技术
💻 JAVA
字号:
/*
 * Created on Nov 27, 2005
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.GTADS.usermanager;

import org.GTADS.server.*;
import java.util.*;
import org.GTADS.protocol.*;
import org.GTADS.messenger.*;
import java.net.*;
import java.io.*;
import org.GTADS.debug.*;

/**
 * @author sday
 * Class that manages buddylist states for client threads
 * Mechanism: Client thread sends user state to FriendsListManager
 * Class checks cache and sends users 
 */
public class FriendsListManager {
	private static FriendsListManager instance;
	private boolean readInMode = false;
	
	public static FriendsListManager getInstance(){
		synchronized(FriendsListManager.class) {
			if (instance != null){
				return instance;
			}
			else {
				instance = new FriendsListManager();
				return instance;
			}
		}
	}
	
	public static void clearInstance() {
		synchronized(FriendsListManager.class) {
			if (instance != null){
				instance = null;
			}
		}
	}
	
	public FriendsListManager () {
	}
	
	public void addUsertoFriendsList(String currentUser, String addedUser) throws IOException{
		FriendsList currentList;
		if (ServerCacheHandler.friendsListCache.containsKey(currentUser)){
			currentList = (FriendsList)ServerCacheHandler.friendsListCache.get(currentUser);
			currentList.addFriend(addedUser);
		}
		else {
			currentList = new FriendsList();
			currentList.addFriend(addedUser);
			ServerCacheHandler.friendsListCache.put(currentUser, currentList);
		}
		
		// Write to Disk
		if (!readInMode)
			writeFlistToDisk(currentUser);
			
		if (ServerCacheHandler.userSocketCache.containsKey(addedUser)){
			currentList = (FriendsList)ServerCacheHandler.friendsListCache.get(currentUser);
			currentList.modifyFriendState(addedUser, FriendsList.STATE_ONLINE);
		}
		sendWholeFriendsList(currentUser);
	}
	
	public void removeUserFromFriendsList(String currentUser, String removedUser){
		FriendsList currentList;
		if (ServerCacheHandler.friendsListCache.containsKey(currentUser)){
			currentList = (FriendsList)ServerCacheHandler.friendsListCache.get(currentUser);
			currentList.removeFriendFromList(removedUser);
		}
		else {
			currentList = new FriendsList();
			currentList.removeFriendFromList(removedUser);
			ServerCacheHandler.friendsListCache.put(currentUser, currentList);
		}
		
		// Write Changes to Disk
		if (!readInMode)
			writeFlistToDisk(currentUser);
	}
	
	public void announceFriendsList(String username, int state) throws IOException {
		Enumeration serverUsersEnum = ServerCacheHandler.friendsListCache.keys();
		
		while (serverUsersEnum.hasMoreElements()){
			String friendsListUser = (String)serverUsersEnum.nextElement();
			if (ServerCacheHandler.friendsListCache.containsKey(friendsListUser)){
				((FriendsList)ServerCacheHandler.friendsListCache.get(friendsListUser)).
				modifyFriendState(username, state);
				MetaData buddyUpdate = new MetaData(MetaData.FROM_SERVER,
						MetaData.FRIENDSLIST, state + "");
				if (((FriendsList)ServerCacheHandler.friendsListCache.get(friendsListUser)).getFriendsNamesList().contains(username)){
					if (ServerCacheHandler.userSocketCache.containsKey(friendsListUser)){
						Socket tempSocket = (Socket)ServerCacheHandler.userSocketCache.get(friendsListUser);
						if (!tempSocket.isClosed())
							MessageAdapter.sendData(null,(Socket)ServerCacheHandler.userSocketCache.get(friendsListUser),
									buddyUpdate, username);
					}
				}
			}
		}
	}
	
	public void announceFriendsSignOn(String username) throws IOException{
		announceFriendsList(username, FriendsList.STATE_ONLINE);
	}

	public void announceFriendsSignOff(String username) throws IOException {
		announceFriendsList(username, FriendsList.STATE_OFFLINE);
	}
	
	public void announceFriendsAway(String username) throws IOException{
		announceFriendsList(username, FriendsList.STATE_AWAY);
	}
	
	public void sendWholeFriendsList(String username) throws IOException {
		if (ServerCacheHandler.friendsListCache.containsKey(username)){
			FriendsList tempList = ((FriendsList)ServerCacheHandler.friendsListCache.get(username));
			Vector v = tempList.getFriendsNamesList();
			
			for (int i = 0; i < v.size(); i++){
				MetaData buddyState = new MetaData(MetaData.FROM_SERVER, MetaData.FRIENDSLIST,
						tempList.getFriendState((String)v.get(i)) + "");
				MessageAdapter.sendData(null,(Socket)ServerCacheHandler.userSocketCache.get(username),
						buddyState, (String)v.get(i));
			}
		}
	}
	
	public void readInFriendsList(){
		// read in Flist files from disk
		
		readInMode = true;
		String flistDirectory = ServerConfig.getInstance().getFriendsListDirectory();
		Enumeration Usernames = ServerCacheHandler.userPasswordCache.keys();
		ServerLogger.sendConsoleOutput("Loading FriendsLists...", this.getClass());
		
		if (flistDirectory == null) {
			ServerLogger.sendConsoleDebugMessage("No Friends List Directory Specified", this.getClass(), 5);
			return;
		}
		
		while (Usernames.hasMoreElements()) {
			String tempUserName = (String)Usernames.nextElement();
			String conditionalSlash = flistDirectory.endsWith("/") ? "" : "/";
			String flistFilename = flistDirectory + conditionalSlash + tempUserName + ".flist";
			
			writeNamesToCache(flistFilename, tempUserName);
		}
		
		ServerLogger.sendConsoleOutput("Loading FriendsLists Completed", this.getClass());
		readInMode = false;
	}
	
	private void writeNamesToCache(String filename, String username) {
		ServerLogger.sendConsoleDebugMessage("Reading in FriendsList for " + username, this.getClass(), 10);
		File flistFile = new File(filename);
		
		try {
			FileReader flistData = new FileReader(flistFile);
			BufferedReader inputBuffer = new BufferedReader(flistData);
			String tempLineData = new String();
			boolean endOfFile = false;
			
			while (!endOfFile){
				tempLineData = inputBuffer.readLine();
				if (tempLineData != null)
					this.addUsertoFriendsList(username, tempLineData);
				else
					endOfFile = true;
			}
		}
		catch (FileNotFoundException deadedFile){
			ServerLogger.sendConsoleDebugMessage("No existing FriendsList", this.getClass(), 10);
			return;
		}
		catch (IOException ioe){
			ServerLogger.sendConsoleDebugMessage("Error Reading FriendsListFile", this.getClass(), 10);
		}
	}
	
	private void writeFlistToDisk(String user){
		String flistDirectory = ServerConfig.getInstance().getFriendsListDirectory();
		Vector v = new Vector();
		
		if (flistDirectory == null) {
			return;
		}
		
		String conditionalSlash = flistDirectory.endsWith("/") ? "" : "/";
		String flistFilename = flistDirectory + conditionalSlash + user + ".flist";
		
		v = ((FriendsList)ServerCacheHandler.friendsListCache.get(user)).getFriendsNamesList();
		
		try {
			File flistFile = new File(flistFilename);
			if (!flistFile.exists())
				flistFile.createNewFile();
			
			FileWriter flistFileWriter = new FileWriter(flistFile);
			BufferedWriter buffer = new BufferedWriter(flistFileWriter);
			int friendsSize = v.size();
			
			for (int i = 0; i< friendsSize; i++){
				//flistFileWriter.write((String)v.get(i));
				buffer.flush();
				buffer.write((String)v.get((friendsSize - 1) - i));
				buffer.newLine();
			}
			buffer.close();
			flistFileWriter.close();
		}
		catch (IOException ioe) {
			return;
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -