编写程序实现文件的分布式存储

系统的结构:

  1. 多个客户端程序,FileClient应用。客户端程序可以向文件服务节点的应用程序上传文,下载文件,删除文件。(不考虑文件夹与文件名重名)
  2. 一个FileServe应用,提供文件存储节点StorageNode的管理功能,提供文件的管理功能。
  3. 多个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]=filenameclientFileOperator.fileupload(args[1], ip, port);}else if(args[0].equals("download")){//args[1]=uuidclientFileOperator.filedownload(args[1], ip, port);}else if(args[0].equals("remove")){//args[1]=uuidclientFileOperator.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 stubSystem.out.println("Start download--------");MapdownlaodMap=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 map=(Map) 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 exceptiontry {socket=new Socket(backupip, backupport);} catch (Exception e2) {// TODO: handle exceptionSystem.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 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 exceptionSystem.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 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 exceptionSystem.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 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 exceptionSystem.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 stubSystem.out.println("Start remove--------");MapremoveMap=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 map=(Map) 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 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 exceptionSystem.out.println("鏈嶅姟鑺傜偣宸插穿婧�");}}}}catch(Exception e){e.printStackTrace();}}@Override
public void fileupload(String filename, String ip, int port)  {// TODO Auto-generated method stubSystem.out.println("Start upload------");Map 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 map=(Map)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 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 exceptionSystem.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 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}} 


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部