package myServer;
import java.util.*;
import java.security.*;
import java.io.*;
public class FileDataSource implements DataSource {
String[] us = {"no connected users"}; // An array of all users.
boolean userListChanged = true;
boolean debug = false;
LinkedList users = new LinkedList();
LinkedList infoList = new LinkedList();
String pathSep = System.getProperty("file.separator");
String fsroot = System.getProperty("user.home") + pathSep
+ ".mihalychat" + pathSep;
File userFile, infoFile;
boolean basicWrite = false;
boolean allWrite = false;
private final String[] infoComments = {"# Info file for Chat Server",
"# Used when the server is running in FileDataSource mode",
"# <username> <field> <data>",
"# username is the name of the user which the info belongs to"};
private final String[] userComments = {"# User file for Mihaly Chat Server",
"# <username> <password>",
"# usernames are in clear text",
"# passwords are MD5 hashes"};
public FileDataSource() {
System.out.println("FileDataSource init");
initFileSystem();
if(allWrite) {
readFiles();
}
}
public void printAll() {
if(debug)System.out.println("\nStaring to print");
Iterator it = users.iterator();
while(it.hasNext())
((User)(it.next())).printUser();
it = infoList.iterator();
while(it.hasNext())
((Info)(it.next())).printInfo();
if(debug)System.out.println("Ending print\n");
}
private void initFileSystem() {
File file = new File(fsroot);
if(file.exists() && file.isDirectory()) {
if(debug)System.out.println("FileDataSource home " + fsroot);
basicWrite = true;
} else {
System.out.println("FileDataSource is unable to access home" +
"\nAttempting to create home");
file.mkdir();
if( !(file.exists() && file.isDirectory()) )
System.out.println("FileDataSource is unable to create home @ "
+ fsroot + " ex " + file.exists() + " isDir "
+ file.isDirectory());
else {
System.out.println("FileDataSource home created " + fsroot);
basicWrite = true;
}
}
if(basicWrite) {
userFile = new File(fsroot + "users");
infoFile = new File(fsroot + "info");
if(userFile.exists() && infoFile.exists()) {
if(userFile.canRead() && userFile.canWrite()
&& infoFile.canRead() && infoFile.canWrite()) {
if(debug) System.out.println("FileDataSource userfile " + userFile +
"\nFileDataSource infofile " + infoFile);
allWrite = true;
} else {
System.out.println("FileDataSource files exist but" +
"userFile.read " + userFile.canRead() +
"userFile.write " + userFile.canWrite() +
"infoFile.read " + infoFile.canRead() +
"userFile.write " + userFile.canWrite());
}
} else {
try {
userFile.createNewFile();
infoFile.createNewFile();
if(userFile.exists() && infoFile.exists()) {
if(userFile.canWrite() && infoFile.canWrite()) {
if(debug) System.out.println("FileDataSource userfile "
+ userFile + "\nFileDataSource infofile "
+ infoFile);
createFiles();
allWrite = true;
} else {
System.out.println("FileDataSource user/info files " +
"we just created are not writeable :(");
}
} else {
System.out.println("FileDataSource you shouldn't get " +
"here but you did. The user/info file that we just" +
"created are gone missing");
}
} catch(IOException ioe) {
System.out.println("FileDataSource " + ioe.getMessage());
}
}
}
}
private void createFiles() {
for(int i=0;i<userComments.length;i++)
appendToUserFile(userComments[i]);
for(int i=0;i<infoComments.length;i++)
appendToInfoFile(infoComments[i]);
}
private void readFiles() {
Object[] ObjUsers = readLines(userFile);
Object[] ObjInfo = readLines(infoFile);
users = new LinkedList();
infoList = new LinkedList();
try {
for(int i = 0;i<ObjUsers.length;i++) {
String str = "" + ObjUsers[i];
String name = str.substring(0, str.indexOf(" "));
String pass = str.substring(str.indexOf(" ")+1);
users.add(new User(name,pass));
}
for(int i = 0;i<ObjInfo.length;i++) {
String str = "" + ObjInfo[i];
StringTokenizer strTok = new StringTokenizer(str);
infoList.add(new Info(strTok.nextToken(),
strTok.nextToken(),
strTok.nextToken())
);
}
} catch(Exception e) {
errorInFiles(e);
}
}
private void appendToUserFile(String str) {
appendToFile(userFile, str);
}
private void appendToInfoFile(String str) {
appendToFile(infoFile, str);
}
private void appendToFile(File e, String str) {
try {
FileOutputStream fileOut = new FileOutputStream(e,true);
BufferedOutputStream buffOut = new BufferedOutputStream(fileOut);
DataOutputStream dataOut = new DataOutputStream(buffOut);
///////////////////////////////////////////////////////////
dataOut.writeBytes(str + "\n");
dataOut.close();
buffOut.close();
fileOut.close();
} catch(IOException ioe) {
System.out.println("FileDataSource alot of checks was done when initing " +
" " + e + " still it got messed up! This time no checks!");
ioe.printStackTrace();
}
}
private void removeUserFromFile(String user) {
try {
if(allWrite) {
File f = new File(fsroot + "newUser");
FileOutputStream fo = new FileOutputStream(f);
BufferedOutputStream buffOut = new BufferedOutputStream(fo);
DataOutputStream dataOut = new DataOutputStream(buffOut);
for(int i=0;i<userComments.length;i++)
//////////////////////////////////////////////////////////////////
dataOut.writeBytes(userComments[i] + "\n");
Iterator it = users.iterator();
while(it.hasNext()) {
User s = (User)it.next();
/////////////////////////////////////////////////////////
dataOut.writeBytes("" + s.getName() + " " + s.getPass() + "\n");
}
dataOut.close();
buffOut.close();
fo.close();
网络课设局域网实时通信系统java设计
4星 · 超过85%的资源 需积分: 11 171 浏览量
2010-10-16
21:54:59
上传
评论 2
收藏 74KB RAR 举报
jennifermo
- 粉丝: 0
- 资源: 8
最新资源
- 数据库标识码BSM重排序工具、重构标识码工具
- 基于C语言的校园导航系统报告.doc
- __init__.py
- tensorflow-gpu-2.6.5-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-rocm-2.13.1.600-cp38-cp38-manylinux2014-x86-64.whl
- tensorflow-rocm-2.13.0.570-cp311-cp311-manylinux2014-x86-64.whl
- 3ds_1.0_3.6.ipa
- 卷积神经网络(Convolutional Neural Network,CNN
- 网页设计HTML、CSS和JS网页设计HTML、CSS和JS
- 利用Python构建作物病虫害识别分类系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈