系統的結構:
- 多個客戶端程序,FileClient應用。客戶端程序可以向文件服務節點的應用程序上傳文,下載文件,刪除文件。(不考慮文件夾與文件名重名)
- 一個FileServe應用,提供文件存儲節點StorageNode的管理功能,提供文件的管理功能。
- 多個StorageNode應用,提供文件的存儲能力。提供向備份節點服務器進行文件備份功能。 一個監控程序
系統功能細節
NodeName=aName
NodeIP=ip address
NodePort=aPort
RootFolder=aFolder
Volume=100GB
FileServerIP=file server ip address
FileServerPort=file server port
其中NodeName屬性是存儲節點的名稱,要求唯一,在作業檢查時,至少要啓動4個存儲節點進程,在運行過程中,還需要動態進行存儲擴展,新啓動兩個進程。還需要動態將一個節點關閉。
NodeIp地址,由於沒有多臺計算機,所以NodeIp都設置爲127.0.0.1,將每個節點的端口NodePort設置爲獨立的即可。
RootFolder即爲每個存儲節點的根文件夾,volume是該存儲節點提供的最大存儲能力。
StorageNode需要知道FileServer服務器的地址信息,FileServerIp和FileServerPort就是UDP協議使用的地址和端口
———————————————————————————————
幾點
1.UDP
2.TCP
3.文件操作
三個核心
——————————————————————
操作代碼見:
http://59.110.243.127/svn/repos/src/dfs/
客戶端
“`
package dfs.client;
import dfs.utils.Config;
public class FileClient {
public static void main(String[]args)
{
ClientFileOperator clientFileOperator=new ClientFileOperator();
Config config=new Config("FileServer.properties");
String ip=config.getValue("ip");
int port=Integer.parseInt(config.getValue("port").trim());
if(args[0].equals("upload"))
{
//args[1]=filename
clientFileOperator.fileupload(args[1], ip, port);
}
else if(args[0].equals("download"))
{
//args[1]=uuid
clientFileOperator.filedownload(args[1], ip, port);
}
else if(args[0].equals("remove"))
{
//args[1]=uuid
clientFileOperator.removefile(args[1], ip, port);
}
}
}“`
CLIENT_OPERATOR:
public class ClientFileOperator implements FileOperation {
//榪欓噷鏄鎴風搴斿~鐨勪唬鐮併€傚疄鐜板畬鏁寸殑閫昏緫錛岃奼傛槸瀹炵幇涓巗erver浜や簰錛屼笌Node鑺傜偣浜や簰銆�
@Override
public void filedownload(String uuid, String ip, int port) {
// TODO Auto-generated method stub
System.out.println("Start download--------");
Map<String, Object>downlaodMap=new HashMap<>();
downlaodMap.put("uuid", uuid);
//1.鍚戞湇鍔″櫒璇㈤棶鍦ㄥ摢
Socket socket=null;
try {
socket = new Socket(ip, port);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeInt(2);//琛ㄧず涓嬭澆
objectOutputStream.writeObject(downlaodMap);
objectOutputStream.flush();
// objectOutputStream.close();
//鎺ュ彈娑堟伅
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
Map<String, Object> map=(Map<String, Object>) ois.readObject();
if(map!=null)
{
if((boolean)map.get("isuseful")!=true)
{
System.err.println("Wrong uuid or the file doesn't exist");
}
else
{//2.鍚戝瓨鍌ㄨ妭鐐規潵瑕�
//鎺ュ彈淇℃伅錛屽悜涓誨瓨鍌ㄨ妭鐐逛笅杞芥枃浠�,娉ㄦ剰鎶涘嚭寮傚父鍜岃В鍘嬬緝錛岃В瀵�
DataInputStream oins=null;
String nodeip=(String) map.get("nodeip");
int nodeport=(int)map.get("nodeport");
String filename=(String)map.get("filename");
String router=(String)map.get("router");
String backupip=(String)map.get("backupip");
int backupport=(int)map.get("backupport");
String backuprputer=(String)map.get("backuprouter");
String prename=(String)map.get("prename");
socket.close();
DataOutputStream obj;
try {
socket=new Socket(nodeip, nodeport);
System.out.println(nodeip+","+nodeport);
//涓庝富鑺傜偣榪涜閫氫俊
obj=new DataOutputStream(socket.getOutputStream());
} catch (Exception e) {
// TODO: handle exception
try {
socket=new Socket(backupip, backupport);
} catch (Exception e2) {
// TODO: handle exception
System.out.println("鏈嶅姟鑺傜偣宸插穿婧�");
throw new Exception();
}
//涓庡浠借妭鐐硅繘琛岄€氫俊
obj=new DataOutputStream(socket.getOutputStream());
obj.writeInt(2);
System.out.println("Client try to attach"+backuprputer+"/"+filename);
String filepath1=router+"/"+filename;
Map<String, Object> mapp1=new HashMap<>();
mapp1.put("filepath", filepath1);
byte[]bytearray1=ConvertMapArray.convertMapToByteArray(mapp1);
obj.writeInt(bytearray1.length);
obj.write(bytearray1);
obj.flush();
try {
oins=new DataInputStream(socket.getInputStream());
} catch (Exception e2) {
// TODO: handle exception
System.out.println("鏈嶅姟鑺傜偣宸插穿婧�");
throw new Exception();
}
FileOutputStream foStream=new FileOutputStream(new File(prename));
DataOutputStream doStream=new DataOutputStream(foStream);
System.out.println("Create "+prename);
int length=oins.readInt();
System.out.println("lenth:"+length);
byte []array=new byte[length];
/*byte key=MD5Utils.getKey();
while((readbyte=bis.read())!=-1){
//array=EDS.Encrytor(array);//鍔犲瘑
dos.write((readbyte^key));
}*/
oins.read(array);
array=Tool.quickDecrypt(Tool.decompress(array));
doStream.write(array);
socket.close();
System.out.println("浠庡浠借妭鐐硅幏鍙栧埌浜嗘枃浠�");
throw new Exception();//緇撴潫
}
obj.writeInt(2);
System.out.println("Client try to attach"+router+"/"+filename);
String filepath=router+"/"+filename;
Map<String, Object> mapp=new HashMap<>();
mapp.put("filepath", filepath);
byte[]bytearray=ConvertMapArray.convertMapToByteArray(mapp);
obj.writeInt(bytearray.length);
obj.write(bytearray);
obj.flush();
System.out.println("_________SEND__REQUEST");
try {
oins=new DataInputStream(socket.getInputStream());
} catch (Exception e) {
//鑻ヤ腑鏂摼鎺�
try {
socket=new Socket(backupip, backupport);
} catch (Exception e2) {
// TODO: handle exception
System.out.println("鏈嶅姟鑺傜偣宸插穿婧�");
throw new Exception();
}
//涓庡浠借妭鐐硅繘琛岄€氫俊
obj=new DataOutputStream(socket.getOutputStream());
obj.writeInt(2);
System.out.println("Client try to attach"+backuprputer+"/"+filename);
String filepath1=router+"/"+filename;
Map<String, Object> mapp1=new HashMap<>();
mapp.put("filepath", filepath1);
byte[]bytearray1=ConvertMapArray.convertMapToByteArray(mapp1);
obj.writeInt(bytearray.length);
obj.write(bytearray1);
obj.flush();
try {
oins=new DataInputStream(socket.getInputStream());
} catch (Exception e2) {
// TODO: handle exception
System.out.println("鏈嶅姟鑺傜偣宸插穿婧�");
}
FileOutputStream foStream=new FileOutputStream(new File(prename));
DataOutputStream doStream=new DataOutputStream(foStream);
System.out.println("Create "+prename);
int length=oins.readInt();
System.out.println("lenth:"+length);
byte []array=new byte[length];
/*byte key=MD5Utils.getKey();
while((readbyte=bis.read())!=-1){
//array=EDS.Encrytor(array);//鍔犲瘑
dos.write((readbyte^key));
}*/
oins.read(array);
array=Tool.quickDecrypt(Tool.decompress(array));
doStream.write(array);
socket.close();
System.out.println("浠庡浠借妭鐐硅幏鍙栧埌浜嗘枃浠�");
throw new Exception();
}
FileOutputStream foStream=new FileOutputStream(new File("a/"+prename));
DataOutputStream doStream=new DataOutputStream(foStream);
System.out.println("Create "+prename);
int length=oins.readInt();
System.out.println("lenth:"+length);
byte []array=new byte[length];
/*byte key=MD5Utils.getKey();
while((readbyte=bis.read())!=-1){
//array=EDS.Encrytor(array);//鍔犲瘑
dos.write((readbyte^key));
}*/
oins.read(array);
array=Tool.quickDecrypt(Tool.decompress(array));
doStream.write(array);
socket.close();
System.out.println("浠庝富瀛樺偍鑺傜偣鑾峯彇鍒頒簡鏂囦歡");
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
}
}
@Override
public void removefile(String uuid, String ip, int port) {
// TODO Auto-generated method stub
System.out.println("Start remove--------");
Map<String, Object>removeMap=new HashMap<>();
removeMap.put("uuid", uuid);
//1.鍚戞湇鍔″櫒璇㈤棶鍦ㄥ摢
Socket socket=null;
try {
socket = new Socket(ip, port);
ObjectOutputStream objectOutputStream=new ObjectOutputStream(socket.getOutputStream());
objectOutputStream.writeInt(3);//琛ㄧず鍒犻櫎
objectOutputStream.writeObject(removeMap);
objectOutputStream.flush();
// objectOutputStream.close();
//鎺ュ彈娑堟伅
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
Map<String, Object> map=(Map<String, Object>) ois.readObject();
if(map!=null)
{
if((boolean)map.get("isuseful")!=true)
{
System.err.println("Wrong uuid or the file doesn't exist");
}
else
{//2.鍦ㄥ垹闄や富瀛樿妭鐐圭殑鍚屾椂鍒犻櫎澶囦喚鑺傜偣鎵€瀛樻枃浠�
DataInputStream oins=null;
String nodeip=(String) map.get("nodeip");
int nodeport=(int)map.get("nodeport");
String filename=(String)map.get("filename");
String router=(String)map.get("router");
String backupip=(String)map.get("backupip");
int backupport=(int)map.get("backupport");
String backuprputer=(String)map.get("backuprouter");
String prename=(String)map.get("prename");
socket.close();
DataOutputStream obj;
try {
socket=new Socket(nodeip, nodeport);
System.out.println(nodeip+","+nodeport);
//涓庝富鑺傜偣榪涜閫氫俊
obj=new DataOutputStream(socket.getOutputStream());
obj.writeInt(3);
System.out.println("Client try to attach"+router+"/"+filename);
String filepath1=router+"/"+filename;
Map<String, Object> mapp1=new HashMap<>();
mapp1.put("filepath", filepath1);
mapp1.put("backupip", backupip);
mapp1.put("backupport", backupport);
mapp1.put("backfilename",filename);
mapp1.put("backuprouter", backuprputer);
byte[]bytearray1=ConvertMapArray.convertMapToByteArray(mapp1);;
obj.writeInt(bytearray1.length);
obj.write(bytearray1);
obj.flush();
} catch (Exception e) {
// TODO: handle exception
System.out.println("鏈嶅姟鑺傜偣宸插穿婧�");
}
}
}
}
catch(Exception e){e.printStackTrace();}
}
@Override
public void fileupload(String filename, String ip, int port) {
// TODO Auto-generated method stub
System.out.println("Start upload------");
Map<String, Object> fileinformap=new HashMap<>();
File file=new File(filename);
fileinformap.put("filename", filename);
fileinformap.put("filesize",file.length());
Socket socket = null;
InputStream inputStream = null;
OutputStream outputStream=null;
try {
try {
socket = new Socket(ip, port);
ObjectOutputStream ooStream=new ObjectOutputStream(socket.getOutputStream());
System.out.println("--------Get Stream-------");
ooStream.writeInt(1);;//琛ㄧず涓婁紶
ooStream.writeObject(fileinformap);
ooStream.writeChar('e');;//琛ㄧず緇撴潫
ooStream.flush();
inputStream=socket.getInputStream();
} catch (Exception e) {
// TODO: handle exception
}
ObjectInputStream ois=new ObjectInputStream(inputStream);
Map<String,Object> map=(Map<String, Object>)ois.readObject();
System.out.println("Connect to Server sucessfully");
if(map.get("uuid")!=null)
{ System.out.println("File information");
System.out.println("Fileid "+map.get("uuid"));
System.out.println("Main node ip,port"+(String)map.get("nodeip")+" ,"+(int)map.get("nodeport"));
System.out.println("Folderroot:"+(String)map.get("router"));
System.out.println("__NEW_FILE_NAME"+(String)map.get("uuid")+filename.substring(filename.lastIndexOf(".")));
System.out.println("______________BACKUP_NODE__________");
System.out.println("Main node ip,port"+(String)map.get("ip")+" ,"+(int)map.get("port"));
System.out.println("Folderroot:"+(String)map.get("brouter"));
}
socket.close();
try {
socket=new Socket((String)map.get("nodeip"),(int)map.get("nodeport"));
String router=(String)map.get("router");
String uuid=(String)map.get("uuid");
String fileName=uuid+filename.substring(filename.lastIndexOf("."));
String Ip=(String)map.get("ip");
int Port=(int)map.get("port");
Map<String, Object> storeInformap=new HashMap<>();
storeInformap.put("router", router);
storeInformap.put("fileName", fileName);
storeInformap.put("Ip", Ip);
storeInformap.put("Port",Port);
storeInformap.put("backuprouter", (String)map.get("brouter"));
byte[] byteArray=ConvertMapArray.convertMapToByteArray(storeInformap);
int length=byteArray.length;
InputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.write(1);
( dos).writeInt(byteArray.length);
System.out.println("length: "+length);
dos.write(byteArray);
byte []array=new byte[1024*100];
/*byte key=MD5Utils.getKey();
while((readbyte=bis.read())!=-1){
//array=EDS.Encrytor(array);//鍔犲瘑
dos.write((readbyte^key));
}*/
int i=0;
while(bis.read(array)!=-1)
{
array=Tool.compress(Tool.quickEncrypt(array));//鍔犲瘑//鍘嬬緝
dos.write(array);
}
System.out.println("鏂囦歡璇誨靉鎴愬姛");
dos.flush();
socket.close();
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
System.err.println("涓誨瓨鍌ㄨ妭鐐瑰瓨鍌ㄥけ璐�");
System.out.println("鍚戝浠借妭鐐瑰彂閫佹暟鎹�");
String Ip=(String)map.get("ip");
int Port=(int)map.get("port");
System.out.println(Ip+","+Port);
socket=new Socket(Ip,Port);
String router=(String)map.get("brouter");
String uuid=(String)map.get("uuid");
String fileName=uuid+filename.substring(filename.lastIndexOf("."));
Map<String, Object> storeInformap=new HashMap<>();
storeInformap.put("router", router);
storeInformap.put("fileName", fileName);
storeInformap.put("Ip", (String)map.get("nodeip"));
storeInformap.put("Port",(int)map.get("nodeport"));
storeInformap.put("backuprouter",router );
byte[] byteArray=ConvertMapArray.convertMapToByteArray(storeInformap);
int length=byteArray.length;
InputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
DataOutputStream dos=new DataOutputStream(socket.getOutputStream());
dos.write(1);
( dos).writeInt(byteArray.length);
System.out.println("length: "+length);
dos.write(byteArray);
byte []array=new byte[1024*100];
/*byte key=MD5Utils.getKey();
while((readbyte=bis.read())!=-1){
//array=EDS.Encrytor(array);
dos.write((readbyte^key));
}*/
int i=0;
while(bis.read(array)!=-1)
{
array=Tool.compress(Tool.quickEncrypt(array));
dos.write(array);
}
dos.flush();
socket.close();
}
} catch ( Exception e) {
// TODO Auto-generated catch block
}
}