├── .classpath ├── .gitignore ├── .project ├── LICENSE ├── README.md ├── bin └── kr │ └── re │ └── keti │ ├── DataProcess$UnitChunk.class │ ├── DataProcess$UnitEdge.class │ ├── DataProcess.class │ ├── EdgeDataAggregator$1.class │ ├── EdgeDataAggregator.class │ ├── EdgeDeviceInfoClient$TCP_ResopnseWaiter.class │ ├── EdgeDeviceInfoClient$UDP_ResponseWaiter.class │ ├── EdgeDeviceInfoClient.class │ ├── EdgeFinder$1.class │ ├── EdgeFinder.class │ ├── EdgeReceptor$1.class │ ├── EdgeReceptor$2.class │ ├── EdgeReceptor$ReceptionEvent.class │ ├── EdgeReceptor.class │ ├── MasterWorker.class │ ├── ReceiveWorker$PacketProcessorImpl$ChunkTransfer.class │ ├── ReceiveWorker$PacketProcessorImpl.class │ ├── ReceiveWorker$ReceiveWorkerEvent.class │ ├── ReceiveWorker.class │ └── SlaveWorker.class ├── edge_ipList.txt ├── info_device_ex.txt ├── lib ├── bcpkix-jdk15on-1.68.jar ├── bcprov-jdk15on-1.68.jar ├── commons-io-2.11.0.jar ├── kafka-clients-3.4.0.jar ├── kafka_2.13-3.5.1.jar ├── mysql-connector-java-8.0.22.jar ├── org.eclipse.paho.client.mqttv3-1.2.5.jar ├── slf4j-api-2.0.6.jar ├── slf4j-simple-2.0.6.jar └── sqlite-jdbc-3.27.2.1.jar ├── release ├── EdgeDataAggregator_v1217.jar ├── EdgeDataAggregator_v1220.jar ├── EdgeDataAggregator_v1221.jar ├── EdgeDataAggregator_v221219.jar ├── agent.jar ├── client.jar ├── execute.txt └── info_device_backup.txt └── src └── kr └── re └── keti ├── AddressUpdate.java ├── Command.java ├── DataProcess.java ├── FileMonitor.java ├── Main.java ├── RamDiskManager.java ├── RequestProcess.java ├── ResponseProcess.java ├── Ssl.java ├── agent ├── Agent.java ├── AgentPacket.java ├── EdgeDataAggregator.java ├── Kafka.java └── Mqtt.java ├── database ├── Database.java ├── FileManagementDto.java ├── FileUuidDto.java ├── MysqlDao.java └── SqliteDao.java ├── os ├── Azure.java ├── Broadcast.java ├── EdgeFinder.java ├── EdgeReceptor.java ├── Linux.java ├── OSProcess.java ├── TcpReceptor.java ├── TcpRequest.java └── UdpReceptor.java └── tcp ├── Client.java ├── Server.java ├── TcpPacket.java ├── UnitEdge.java └── UnitShared.java /.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /bin/ 2 | /.classpath 3 | .settings/ 4 | info_device.txt 5 | .project 6 | src/kr/re/keti/PortNum.java -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | edgedataaggregator 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.jdt.core.javabuilder 10 | 11 | 12 | 13 | 14 | 15 | org.eclipse.jdt.core.javanature 16 | 17 | 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 엣지컴퓨팅 데이터 공유 관리 S/W 2 | 엣지 컴퓨팅 시스템을 구성된 디바이스들간의 데이터 공유를 위한 프레임워크로서, 시스템을 자동으로 구성하고 디바이스간 데이터 공유와 보안등급 및 접근 정책 관리 등의 주요 기능을 수행함 3 | 4 | # info_device.txt 5 | info_device.txt는 디바이스의 전체적인 정보를 가지는 설정 파일입니다. 6 | ``` 7 | uuid // 디바이스 ID 8 | data path // 데이터 폴더 위치 9 | cert path // 인증서 폴더 위치 10 | DBMS // 사용하는 DBMS 11 | database,table,id,password // 데이터베이스, 테이블명, 사용자ID, 사용자PW ','로 구분 12 | mode // upnp or master or slave upnp일시 처음 접속하는 디바이스가 master 나머지는 slave 13 | device IP // 디바이스IP or auto auto 14 | ``` 15 | 16 | # 사용 17 | ``` 18 | 0 // 종료 19 | 1 // 디바이스 정보 읽기 20 | 2 // 전체 데이터 읽기 21 | 3 // 데이터 상세 정보 22 | 4 // 데이터 읽기 / 분산 공유 23 | 5 // 데이터 쓰기 24 | 6 // 데이터 제거 25 | 7 // 데이터 전송 26 | ``` 27 | -------------------------------------------------------------------------------- /bin/kr/re/keti/DataProcess$UnitChunk.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/DataProcess$UnitChunk.class -------------------------------------------------------------------------------- /bin/kr/re/keti/DataProcess$UnitEdge.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/DataProcess$UnitEdge.class -------------------------------------------------------------------------------- /bin/kr/re/keti/DataProcess.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/DataProcess.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeDataAggregator$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeDataAggregator$1.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeDataAggregator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeDataAggregator.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeDeviceInfoClient$TCP_ResopnseWaiter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeDeviceInfoClient$TCP_ResopnseWaiter.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeDeviceInfoClient$UDP_ResponseWaiter.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeDeviceInfoClient$UDP_ResponseWaiter.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeDeviceInfoClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeDeviceInfoClient.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeFinder$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeFinder$1.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeFinder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeFinder.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeReceptor$1.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeReceptor$1.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeReceptor$2.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeReceptor$2.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeReceptor$ReceptionEvent.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeReceptor$ReceptionEvent.class -------------------------------------------------------------------------------- /bin/kr/re/keti/EdgeReceptor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/EdgeReceptor.class -------------------------------------------------------------------------------- /bin/kr/re/keti/MasterWorker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/MasterWorker.class -------------------------------------------------------------------------------- /bin/kr/re/keti/ReceiveWorker$PacketProcessorImpl$ChunkTransfer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/ReceiveWorker$PacketProcessorImpl$ChunkTransfer.class -------------------------------------------------------------------------------- /bin/kr/re/keti/ReceiveWorker$PacketProcessorImpl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/ReceiveWorker$PacketProcessorImpl.class -------------------------------------------------------------------------------- /bin/kr/re/keti/ReceiveWorker$ReceiveWorkerEvent.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/ReceiveWorker$ReceiveWorkerEvent.class -------------------------------------------------------------------------------- /bin/kr/re/keti/ReceiveWorker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/ReceiveWorker.class -------------------------------------------------------------------------------- /bin/kr/re/keti/SlaveWorker.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/bin/kr/re/keti/SlaveWorker.class -------------------------------------------------------------------------------- /edge_ipList.txt: -------------------------------------------------------------------------------- 1 | slave 2 | 10.0.0.126 -------------------------------------------------------------------------------- /info_device_ex.txt: -------------------------------------------------------------------------------- 1 | 2ce81678-3c50-11ec-807b-3b8fa73906e7 2 | /home//keti/data/ 3 | /home/keti/cert/ 4 | MySQL 5 | db_name,table_name,db_id,db_pw 6 | upnp 7 | auto 8 | -------------------------------------------------------------------------------- /lib/bcpkix-jdk15on-1.68.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/bcpkix-jdk15on-1.68.jar -------------------------------------------------------------------------------- /lib/bcprov-jdk15on-1.68.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/bcprov-jdk15on-1.68.jar -------------------------------------------------------------------------------- /lib/commons-io-2.11.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/commons-io-2.11.0.jar -------------------------------------------------------------------------------- /lib/kafka-clients-3.4.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/kafka-clients-3.4.0.jar -------------------------------------------------------------------------------- /lib/kafka_2.13-3.5.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/kafka_2.13-3.5.1.jar -------------------------------------------------------------------------------- /lib/mysql-connector-java-8.0.22.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/mysql-connector-java-8.0.22.jar -------------------------------------------------------------------------------- /lib/org.eclipse.paho.client.mqttv3-1.2.5.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/org.eclipse.paho.client.mqttv3-1.2.5.jar -------------------------------------------------------------------------------- /lib/slf4j-api-2.0.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/slf4j-api-2.0.6.jar -------------------------------------------------------------------------------- /lib/slf4j-simple-2.0.6.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/slf4j-simple-2.0.6.jar -------------------------------------------------------------------------------- /lib/sqlite-jdbc-3.27.2.1.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/lib/sqlite-jdbc-3.27.2.1.jar -------------------------------------------------------------------------------- /release/EdgeDataAggregator_v1217.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/release/EdgeDataAggregator_v1217.jar -------------------------------------------------------------------------------- /release/EdgeDataAggregator_v1220.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/release/EdgeDataAggregator_v1220.jar -------------------------------------------------------------------------------- /release/EdgeDataAggregator_v1221.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/release/EdgeDataAggregator_v1221.jar -------------------------------------------------------------------------------- /release/EdgeDataAggregator_v221219.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/release/EdgeDataAggregator_v221219.jar -------------------------------------------------------------------------------- /release/agent.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/release/agent.jar -------------------------------------------------------------------------------- /release/client.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/eunae-park/EdgeComputing_DataAggregator/9693e1f674f3238735541e8dce4bc42a30e1e096/release/client.jar -------------------------------------------------------------------------------- /release/execute.txt: -------------------------------------------------------------------------------- 1 | java -jar EdgeDataAggregator.jar 2 | 3 | need to write the info_device.txt(guide : info_device_backup.txt) -------------------------------------------------------------------------------- /release/info_device_backup.txt: -------------------------------------------------------------------------------- 1 | Edge Node UUID 2 | Data Folder DIR/ 3 | Cert Folder DIR/ 4 | DBMS name 5 | DB information 6 | UPNP mode 7 | upnp information 8 | My IP Address 9 | 10 | 11 | 52.141.20.211 12 | keti.d-sharing.kr 13 | keties.iptime.org 14 | master 15 | 10.0.7.11 16 | 17 | 18 | //DB info 19 | MySQL : DB명,table명,userID,userPW 20 | (ex) mecTrace,file_management,mecTrace,penta&keti0415! 21 | SQLite : DB명,table명,userID,userPW,DB경로 22 | 23 | //upnp info 24 | 자동모드 : upnp 25 | 수동모드 : master 26 | 수동모드 : slave\nmaster_ip 27 | 28 | //My IP Address 29 | 자동으로 가져오기 : auto 30 | 수동설정 : ip 입력 31 | 32 | 33 | 수동모드 - keti.d-sharing.kr or keties.iptime.org 34 | vi /etc/hosts 35 | 127.0.0.1 keti.d-sharing.kr 36 | keti.d-sharing.kr 37 | keties.iptime.org 38 | -------------------------------------------------------------------------------- /src/kr/re/keti/AddressUpdate.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | import java.net.SocketException; 8 | import java.net.UnknownHostException; 9 | import java.util.Random; 10 | 11 | 12 | public class AddressUpdate { 13 | int port; 14 | String address; 15 | Thread thread; 16 | public String localAddressUpdate() { 17 | Random random = new Random(); 18 | 19 | port = random.nextInt(49151 - 1024 + 1)+1024; 20 | receive(); 21 | try { 22 | Thread.sleep(100); 23 | } catch (Exception e) { 24 | e.printStackTrace(); 25 | } 26 | send(); 27 | 28 | try { 29 | thread.join(); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | return address; 34 | 35 | } 36 | private void receive() { 37 | thread = new Thread(()->{ 38 | try { 39 | byte[] buf = new byte[1024]; 40 | DatagramSocket socket = new DatagramSocket(port); 41 | DatagramPacket packet = new DatagramPacket(buf, buf.length); 42 | socket.receive(packet); 43 | address = packet.getAddress().getHostAddress(); 44 | socket.close(); 45 | } catch (Exception e) { 46 | e.printStackTrace(); 47 | } 48 | 49 | }); 50 | thread.start(); 51 | 52 | } 53 | private void send() { 54 | Thread thread = new Thread(()->{ 55 | try { 56 | byte[] buf = "update".getBytes(); 57 | DatagramPacket packet = new DatagramPacket( 58 | buf, 59 | buf.length, 60 | InetAddress.getByName("255.255.255.255"), 61 | port 62 | ); 63 | DatagramSocket socket = new DatagramSocket(); 64 | socket.send(packet); 65 | socket.close(); 66 | } catch (Exception e) { 67 | e.printStackTrace(); 68 | } 69 | }); 70 | thread.start(); 71 | 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /src/kr/re/keti/Command.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.util.Scanner; 4 | 5 | import kr.re.keti.database.Database; 6 | import kr.re.keti.database.FileManagementDto; 7 | 8 | public class Command extends Thread{ 9 | Database database; 10 | RequestProcess requestProcess; 11 | Scanner scanner; 12 | 13 | public Command(Database database) { 14 | requestProcess = new RequestProcess(database); 15 | this.database = database; 16 | this.scanner = new Scanner(System.in); 17 | } 18 | @Override 19 | public void run() { 20 | String command; 21 | String type; 22 | 23 | while(!isInterrupted()) { 24 | try { 25 | System.out.println(); 26 | System.out.print(">>>"); 27 | command = scanner.nextLine(); 28 | type = command.split(" ")[0]; 29 | 30 | if(command.equals("exit")) break; 31 | 32 | 33 | if(type.matches("[0-9]+")) { 34 | switch(Integer.parseInt(type)) { 35 | case 1: deviceInformation(command); break; 36 | case 2: wholeDataInformation(command); break; 37 | case 3: individualMetaDataInformation(command);break; 38 | case 4: individualDataRead(command); break; 39 | case 5: requestProcess.newEdge("[10.0.0.126]"); 40 | } 41 | continue; 42 | } 43 | switch(type) { 44 | case "update": updateFile(); break; 45 | case "help": help(); break; 46 | case "deviceIP": System.out.println("device-IP : "+Main.deviceIP); break; 47 | case "masterIP": System.out.println("master-IP : "+Main.masterIP); break; 48 | case "mode" : System.out.println("device mode : "+Main.mode); break; 49 | case "edgeList": edgeList(); break; 50 | case "deviceInformation": deviceInformation(command); break; 51 | case "wholeDataInformation": wholeDataInformation(command); break; 52 | case "individualMetaDataInformation": individualMetaDataInformation(command);break; 53 | case "distributed": distributed(command); break; 54 | } 55 | 56 | // e.printStackTrace(); 57 | } catch (Exception e) { 58 | e.printStackTrace(); 59 | } 60 | } 61 | if(scanner != null) scanner.close(); 62 | } 63 | private void updateFile() { 64 | FileManagementDto dto = null; 65 | String dataId = ""; 66 | while(true) { 67 | System.out.println("File Name : "); 68 | dataId = scanner.nextLine(); 69 | if(dataId.equals("exit")) return; 70 | else { 71 | if(database.select(dataId) != null) { 72 | break; 73 | } 74 | else { 75 | System.out.println("not found data ["+dataId+"]"); 76 | } 77 | } 78 | } 79 | 80 | while(true) { 81 | System.out.println("Security Level(1 - 5) : "); 82 | try { 83 | int level = Integer.parseInt(scanner.nextLine()); 84 | if(level == -1) break; 85 | if(level<0 || level>5) throw new Exception(); 86 | 87 | dto.setSecurityLevel(level); 88 | database.update(dto); 89 | 90 | } catch (Exception e) { 91 | e.printStackTrace(); 92 | } 93 | break; 94 | } 95 | } 96 | private void help() { 97 | System.out.println("keyword : "); 98 | System.out.println("\thelp, deviceIP, masterIP, mode, edgeList"); 99 | System.out.println("\t1. deviceInformation [ip]"); 100 | System.out.println("\t2. wholeDataInformation [ip]"); 101 | System.out.println("\t3. individualMetaDataInformation [dataid]"); 102 | } 103 | private void edgeList() { 104 | String slaveList = requestProcess.requestEdgeList(PortNum.KETI_PORT); 105 | System.out.print("Edge List : "+Main.masterIP+"(master)\t["+slaveList+"]\n"); 106 | } 107 | private void deviceInformation(String command) { 108 | String[] commands = command.split(" "); 109 | if(commands.length == 1 || commands[1].equals(Main.deviceIP)) { 110 | System.out.println("request to mine"); 111 | String data = requestProcess.deviceInformation(); 112 | requestProcess.showDeviceInformation(data); 113 | } 114 | else { 115 | requestProcess.requestDeviceInformation(commands[1], PortNum.KETI_PORT); 116 | } 117 | } 118 | private void wholeDataInformation(String command) { 119 | String[] commands = command.split(" "); 120 | if(commands.length == 1 || commands[1].equals(Main.deviceIP)) { 121 | System.out.println("request to mine"); 122 | String data = requestProcess.wholeDataInformation(); 123 | requestProcess.wholeDataInformation(data); 124 | } 125 | else { 126 | System.out.println("request to "+commands[1]); 127 | requestProcess.requestWholeDataInformation(commands[1], PortNum.KETI_PORT);; 128 | } 129 | } 130 | private void individualMetaDataInformation(String command) { 131 | String[] commands = command.split(" "); 132 | if(commands.length <2) { 133 | System.out.println("NO DataID"); 134 | } 135 | else { 136 | requestProcess.requestIndividualMetaDataInformation(PortNum.KETI_PORT, commands[1]); 137 | } 138 | } 139 | private void individualDataRead(String command) { 140 | String[] commands = command.split(" "); 141 | if(commands.length <2) { 142 | System.out.println("not dataid"); 143 | } 144 | else { 145 | String dataid = commands[1]; 146 | requestProcess.requestIndividualDataRead(PortNum.KETI_PORT, dataid); 147 | 148 | } 149 | } 150 | private void distributed(String command) { 151 | String[] commands = command.split(" "); 152 | if(commands.length <2) { 153 | System.out.println("not dataid"); 154 | } 155 | else { 156 | String dataid = commands[1]; 157 | requestProcess.requestIndividualDataRead(PortNum.KETI_PORT, dataid); 158 | } 159 | 160 | } 161 | } 162 | 163 | -------------------------------------------------------------------------------- /src/kr/re/keti/FileMonitor.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.io.BufferedInputStream; 4 | import java.io.ByteArrayOutputStream; 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.IOException; 8 | import java.nio.file.Files; 9 | import java.nio.file.StandardCopyOption; 10 | import java.util.HashSet; 11 | import java.util.Set; 12 | import java.util.UUID; 13 | 14 | import org.apache.commons.io.monitor.FileAlterationListener; 15 | import org.apache.commons.io.monitor.FileAlterationMonitor; 16 | import org.apache.commons.io.monitor.FileAlterationObserver; 17 | 18 | import kr.re.keti.agent.Agent; 19 | import kr.re.keti.database.Database; 20 | import kr.re.keti.database.FileManagementDto; 21 | import kr.re.keti.database.FileUuidDto; 22 | 23 | public class FileMonitor{ 24 | private Agent agent; 25 | private String dir; 26 | private Database database; 27 | // private DataProcess dataProcess; 28 | private static Set ignoredFiles = new HashSet();; 29 | private FileAlterationMonitor monitor; 30 | private Ssl ssl; 31 | 32 | public FileMonitor(String dir, Agent agent, Database database, long interval) { 33 | this.dir = dir; 34 | this.agent = agent; 35 | this.database = database; 36 | this.monitor = new FileAlterationMonitor(interval); 37 | ssl = Ssl.getInstance(); 38 | } 39 | public void start() { 40 | File directory = new File(dir); 41 | FileAlterationObserver observer = new FileAlterationObserver(directory); 42 | FileAlterationListener listener = new FileAlterationListener() { 43 | @Override 44 | public void onStop(FileAlterationObserver arg0) { 45 | } 46 | @Override 47 | public void onStart(FileAlterationObserver arg0) { 48 | } 49 | @Override 50 | public void onFileCreate(File file) { 51 | System.out.println("File create : "+file); 52 | String fileName = file.getName(); 53 | if(fileName.endsWith(".swp")) return; 54 | if (ignoredFiles.contains(fileName)) return; 55 | 56 | int dotIndex = fileName.lastIndexOf("."); 57 | String dataid = (dotIndex > 0) ? fileName.substring(0, dotIndex) : fileName; 58 | 59 | String sign = ssl.sign(file.getPath()); 60 | String uuid = UUID.randomUUID().toString(); 61 | uuid = uuid.replace("-", ""); 62 | FileManagementDto dto = new FileManagementDto(file, sign, uuid); 63 | 64 | FileUuidDto uuidDto = new FileUuidDto(dataid, uuid); 65 | dto = new FileManagementDto(file, uuid, sign); 66 | dto.setLinkedEdge(Main.deviceIP); 67 | database.insert(uuidDto); 68 | database.insert(dto); 69 | 70 | String metaData = dto.toString(); 71 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "010", metaData); 72 | agent.send(message); 73 | 74 | File destFile = new File(Main.ramFolder + File.separator + fileName); 75 | try { 76 | Files.copy(file.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 77 | } catch (IOException e) { 78 | e.printStackTrace(); 79 | } 80 | 81 | byte[] data = readFile(file); 82 | String dataSize = data.length+""; 83 | message = DataProcess.messageCreate(data, "REQ", Main.deviceIP, "011", uuid, dataSize); 84 | agent.send(message); 85 | 86 | } 87 | @Override 88 | public void onFileChange(File file) { 89 | System.out.println("File change : "+file); 90 | String fileName = file.getName(); 91 | if(fileName.endsWith(".swp")) return; 92 | if (ignoredFiles.contains(fileName)) return; 93 | 94 | int dotIndex = fileName.lastIndexOf("."); 95 | String dataid = (dotIndex > 0) ? fileName.substring(0, dotIndex) : fileName; 96 | String sign = ssl.sign(fileName); 97 | 98 | FileManagementDto dto = new FileManagementDto(file, dataid, sign); 99 | database.update(dto); 100 | byte[] data = readFile(file); 101 | String dataSize = data.length+""; 102 | byte[] message = DataProcess.messageCreate(data, "REQ", Main.deviceIP, "012", dataid, dataSize); 103 | agent.send(message); 104 | } 105 | @Override 106 | public void onFileDelete(File file) { 107 | System.out.println("File delete : "+file); 108 | String fileName = file.getName(); 109 | if(fileName.endsWith(".swp")) return; 110 | if (ignoredFiles.contains(fileName)) return; 111 | 112 | int dotIndex = fileName.lastIndexOf("."); 113 | String dataid = (dotIndex > 0) ? fileName.substring(0, dotIndex) : fileName; 114 | 115 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "013", dataid); 116 | 117 | database.delete("file_uuid", fileName); 118 | database.delete("file_management", dataid); 119 | RamDiskManager.getInstance().deleteRamFile(fileName); 120 | agent.send(message); 121 | } 122 | @Override 123 | public void onDirectoryCreate(File directory) { 124 | } 125 | @Override 126 | public void onDirectoryChange(File directory) { 127 | } 128 | @Override 129 | public void onDirectoryDelete(File directory) { 130 | } 131 | }; 132 | 133 | observer.addListener(listener); 134 | monitor.addObserver(observer); 135 | try { 136 | monitor.start(); 137 | } catch (Exception e) { 138 | e.printStackTrace(); 139 | } 140 | } 141 | public void stop() { 142 | try { 143 | monitor.stop(); 144 | } catch (Exception e) { 145 | e.printStackTrace(); 146 | } 147 | } 148 | 149 | public static void ignoreFile(String fileName) { 150 | ignoredFiles.add(fileName); 151 | } 152 | 153 | public static void unignoreFile(String fileName) { 154 | ignoredFiles.remove(fileName); 155 | } 156 | public static Set getIgnoreFile() { 157 | return ignoredFiles; 158 | } 159 | 160 | 161 | private byte[] readFile(File file) { 162 | byte[] data = null; 163 | try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 164 | ByteArrayOutputStream bos = new ByteArrayOutputStream()) { 165 | byte[] buffer = new byte[1024]; 166 | int bytesRead; 167 | while ((bytesRead = bis.read(buffer)) != -1) { 168 | bos.write(buffer, 0, bytesRead); 169 | } 170 | data = bos.toByteArray(); 171 | } catch (IOException e) { 172 | e.printStackTrace(); 173 | } 174 | return data; 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/kr/re/keti/Main.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.File; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileReader; 7 | import java.io.FileWriter; 8 | import java.io.IOException; 9 | import java.net.InetAddress; 10 | import java.net.UnknownHostException; 11 | import java.nio.file.Files; 12 | import java.nio.file.Path; 13 | import java.time.LocalDateTime; 14 | import java.time.format.DateTimeFormatter; 15 | 16 | 17 | import kr.re.keti.agent.Agent; 18 | import kr.re.keti.database.Database; 19 | import kr.re.keti.database.MysqlDao; 20 | import kr.re.keti.database.SqliteDao; 21 | import kr.re.keti.os.Azure; 22 | import kr.re.keti.os.EdgeFinder; 23 | import kr.re.keti.os.Linux; 24 | import kr.re.keti.os.OSProcess; 25 | 26 | public class Main { 27 | public static String uuid; 28 | public static String deviceIP; 29 | public static String masterIP = "None"; 30 | public static String storageFolder; 31 | public static String certFolder; 32 | public static String ramFolder; 33 | public static String mode; 34 | public static String programStartTime = programStartTime(); 35 | public static OSProcess process; 36 | public static void main(String[] args){ 37 | // -----------------IP in args--------------------------------------------- 38 | if(args.length >0 ) { 39 | try { 40 | InetAddress addr = InetAddress.getByName(args[args.length - 1]); 41 | deviceIP = addr.getHostAddress(); 42 | } catch (Exception e) { 43 | e.printStackTrace(); 44 | } 45 | } 46 | 47 | Database database = null; 48 | 49 | //----------------------file read-------------------------- 50 | try { 51 | FileReader file = new FileReader("info_device.txt"); 52 | BufferedReader br = new BufferedReader(file); 53 | 54 | database = EdgeInformation(br); 55 | 56 | if(br!=null) br.close(); 57 | if(file!=null) file.close(); 58 | } catch (Exception e) { 59 | e.printStackTrace(); 60 | } 61 | 62 | //----------------OS----------------------------------- 63 | String osName = System.getProperty("os.name"); 64 | if(osName.equals("Linux")) { 65 | String osVersion = System.getProperty("os.version"); 66 | if(osVersion.indexOf("azure") != -1) { 67 | process = new Azure(); 68 | } 69 | else { 70 | process = new Linux(); 71 | } 72 | } 73 | else if(osName.equals("Windows")) { 74 | System.out.println("\t**System is Windows**"); 75 | } 76 | 77 | Ssl.selfSignedCertificate(certFolder, certFolder+"Private/", certFolder+"Private/private.key", 365); 78 | //--------------------Master Find--------------------------------- 79 | masterIP = process.getMaster(); 80 | if(masterIP.equals("none") || masterIP.equals(deviceIP)) { 81 | mode = "master"; 82 | masterIP = deviceIP; 83 | process.start(); 84 | } 85 | else { 86 | mode = "slave"; 87 | } 88 | 89 | 90 | DataProcess dataProcess = new DataProcess(database); 91 | dataProcess.initWholeDataInformation(); 92 | 93 | 94 | //------------------------master found---------------------------------- 95 | Agent agent = Agent.getInstance(); 96 | agent.setDatabase(database); 97 | agent.start(); 98 | 99 | 100 | if(mode.equals("master")) { 101 | System.out.println("Waiting for connections from slaves..."); 102 | } 103 | else { 104 | System.out.println("* Master found: " + masterIP + "\n"); 105 | agent.send(("{[{REQ::"+deviceIP+"::001::EDGE_LIST}]}").getBytes()); 106 | 107 | try { 108 | byte[] keyData = Files.readAllBytes(Path.of(certFolder+"Private/pub.key")); 109 | int keySize = keyData.length; 110 | byte[] start = ("{[{REQ::"+deviceIP+"::019::public_key::"+keySize+"::").getBytes(); 111 | byte[] end = "}]}".getBytes(); 112 | 113 | byte[] data = new byte[start.length + keyData.length + end.length]; 114 | System.arraycopy(start, 0, data, 0, start.length); 115 | System.arraycopy(keyData, 0, data, start.length, keyData.length); 116 | System.arraycopy(end, 0, data, start.length + keyData.length, end.length); 117 | 118 | agent.send(data); 119 | } catch (Exception e) { 120 | e.printStackTrace(); 121 | } 122 | } 123 | 124 | FileMonitor fileMonitor = new FileMonitor(storageFolder, agent, database, 500); 125 | try { 126 | fileMonitor.start(); 127 | } catch (Exception e) { 128 | e.printStackTrace(); 129 | } 130 | try { 131 | Thread.sleep(EdgeFinder.DEFAULT_WAITING_TIME+100); 132 | edgeIPList(); 133 | } catch (Exception e) { 134 | e.printStackTrace(); 135 | } 136 | 137 | //-----------------------command------------------------------------- 138 | Runtime.getRuntime().addShutdownHook(new Thread(() -> { 139 | Command.interrupted(); 140 | shutdown(); 141 | } 142 | try { 143 | Command command = new Command(database); 144 | command.setName("command"); 145 | command.start(); 146 | command.join(); 147 | } catch (Exception e) { 148 | e.printStackTrace(); 149 | } 150 | 151 | //-----------------------program exit--------------------------------- 152 | shutdown(); 153 | System.exit(0); 154 | } 155 | 156 | private static Database EdgeInformation(BufferedReader br) { 157 | Database database = null; 158 | System.out.println("=================================================================="); 159 | try { 160 | //------------------------uuid----------------------- 161 | uuid = br.readLine(); 162 | if(uuid == null) { 163 | System.out.println(" * Input the UUID of Edge Device."); 164 | System.exit(0); 165 | } 166 | else { 167 | System.out.println(" * UUID of Edge Device. : " + uuid); 168 | } 169 | 170 | //------------------------cert----------------------- 171 | 172 | //------------------------storage----------------------- 173 | storageFolder = br.readLine(); 174 | if(storageFolder == null) { 175 | System.out.println(" * Input the Name of Main Path with storage."); 176 | System.exit(0); 177 | } 178 | else { 179 | System.out.println(" * Name of Main Path with storage : " + storageFolder); 180 | File folder = new File(storageFolder); 181 | if(!folder.exists()) folder.mkdir(); 182 | } 183 | 184 | certFolder = br.readLine(); 185 | if(certFolder == null) { 186 | System.out.println(" * Input the Name of Main Path with cert."); 187 | System.exit(0); 188 | } 189 | else { 190 | System.out.println(" * Name of Main Path with cert : " + certFolder); 191 | File folder = new File(certFolder); 192 | if(!folder.exists()) folder.mkdir(); 193 | 194 | folder = new File(certFolder +"Private");//private key and original crt file 195 | if(!folder.exists()) folder.mkdir(); 196 | 197 | folder = new File(certFolder +"Vehicle"); // copy crt file 198 | if(!folder.exists()) folder.mkdir(); 199 | 200 | folder = new File(certFolder+"keys"); // public key file 201 | if(!folder.exists()) folder.mkdir(); 202 | } 203 | 204 | 205 | //------------------------ram----------------------- 206 | ramFolder = br.readLine(); 207 | if(ramFolder == null) { 208 | System.out.println(" * Input the Name of Main Path with ram."); 209 | System.exit(0); 210 | } 211 | else { 212 | System.out.println(" * Name of Main Path with ram : " + ramFolder); 213 | File folder = new File(ramFolder); 214 | if(!folder.exists()) folder.mkdir(); 215 | 216 | folder = new File(ramFolder+"chunk"); 217 | if(!folder.exists()) folder.mkdir(); 218 | 219 | folder = new File(ramFolder+"time"); 220 | if(!folder.exists()) folder.mkdir(); 221 | } 222 | 223 | //------------------------DB----------------------- 224 | String databaseType = br.readLine(); 225 | System.out.println(" * DBMS used by EdgeNode : " + databaseType); 226 | if(databaseType.equals("MySQL")) { 227 | String line = br.readLine(); 228 | String[] dbInfo = line.split(","); 229 | if(dbInfo.length != 3) { 230 | System.out.println(" * Input DB Infomation in info_device.txt(ex:DB name,table name,user ID,user PW)."); 231 | System.exit(0); 232 | } 233 | String databaseName = dbInfo[0]; 234 | String id = dbInfo[1]; 235 | String pw = dbInfo[2]; 236 | database = new MysqlDao(databaseName, id, pw); 237 | } 238 | 239 | else if(databaseType.equals("SQLite")) { 240 | String line = br.readLine(); 241 | String[] dbInfo = line.split(","); 242 | if(dbInfo.length != 2) { 243 | System.out.println(" * Input DB Infomation in info_device.txt(ex:DB path, DB name,table name)."); 244 | System.exit(0); 245 | } 246 | String path = dbInfo[0]; 247 | String databaseName = dbInfo[1]; 248 | database = new SqliteDao(path, databaseName); 249 | } 250 | else { 251 | System.out.println(" * Input DBMS used by EdgeNode(ex:MySQL, SQLite)."); 252 | System.exit(0); 253 | } 254 | 255 | //------------------------mode----------------------- 256 | mode = br.readLine(); 257 | if(!(mode.equals("master") || mode.equals("slave") || mode.equals("upnp"))) { 258 | System.out.println(" * Input UPnP mode(ex:upnp, master, slave&master_ip)."); 259 | System.exit(0); 260 | } 261 | 262 | //------------------------IP----------------------- 263 | deviceIP = br.readLine(); 264 | if(deviceIP.equals("auto")) { 265 | deviceIP = InetAddress.getLocalHost().getHostAddress(); 266 | } 267 | else if(deviceIP != null) { 268 | try { 269 | InetAddress inetAddress = InetAddress.getByName(deviceIP); 270 | deviceIP = inetAddress.getHostAddress(); 271 | } catch (Exception e) { 272 | e.printStackTrace(); 273 | } 274 | } 275 | else { 276 | System.out.println(" * Input IP(ex:DNS, Logical address, auto)."); 277 | System.exit(0); 278 | } 279 | 280 | if(deviceIP.startsWith("192.168") || deviceIP.startsWith("127.0")) { 281 | deviceIP = new AddressUpdate().localAddressUpdate(); 282 | } 283 | System.out.println(" * IP Address of Edge Device : " + deviceIP); 284 | 285 | //---------------------------------------------- 286 | return database; 287 | } catch (Exception e) { 288 | e.printStackTrace(); 289 | } 290 | return null; 291 | } 292 | private static void edgeIPList() { 293 | try { 294 | FileWriter writer = new FileWriter("edge_ipList.txt", false); 295 | if(mode.equals("master")) { 296 | writer.write("master\n"); 297 | writer.flush(); 298 | } 299 | else { 300 | writer.write("slave\n"); 301 | writer.flush(); 302 | writer.write(masterIP); 303 | writer.flush(); 304 | } 305 | if(writer != null) writer.close(); 306 | // TODO Auto-generated catch block 307 | } catch (Exception e) { 308 | e.printStackTrace(); 309 | } 310 | } 311 | private static String programStartTime() { 312 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); 313 | LocalDateTime currentDateTime = LocalDateTime.now(); 314 | String start = currentDateTime.format(formatter); 315 | return start; 316 | 317 | } 318 | private static void shutdown() { 319 | Agent agent = Agent.getInstance(); 320 | agent.send(("{[{REQ::"+deviceIP+"::-1}]}").getBytes()); 321 | 322 | process.stop(); 323 | 324 | String path = System.getProperty("user.dir"); 325 | System.out.println(path); 326 | File directory = new File(path); 327 | File[] files = directory.listFiles(); 328 | 329 | for(File file : files) { 330 | if(file.getName().startsWith("paho")) { 331 | delete(file); 332 | } 333 | } 334 | 335 | agent.stop(); 336 | } 337 | private static void delete(File file) { 338 | if(file.isDirectory()) { 339 | File[] files = file.listFiles(); 340 | if(files != null) { 341 | for(File subFile : files) { 342 | delete(subFile); 343 | } 344 | } 345 | } 346 | 347 | file.delete(); 348 | 349 | } 350 | } 351 | -------------------------------------------------------------------------------- /src/kr/re/keti/RamDiskManager.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | import java.io.File; 3 | import java.io.FileOutputStream; 4 | import java.io.IOException; 5 | import java.nio.file.Files; 6 | import java.nio.file.StandardCopyOption; 7 | import java.util.HashMap; 8 | import java.util.Map; 9 | 10 | public class RamDiskManager { 11 | public static RamDiskManager instance = null; 12 | private String ramDiskPath; 13 | private String diskPath; 14 | private long maxRamDiskSize; 15 | private Map fileSizes; 16 | 17 | public static RamDiskManager getInstance() { 18 | if(instance == null) { 19 | System.out.println("RamDiskManager is null"); 20 | } 21 | return instance; 22 | } 23 | public static RamDiskManager getInstance(String ramDiskPath, String diskPath, long maxRamDiskSize) { 24 | if(instance == null) { 25 | instance = new RamDiskManager(ramDiskPath, diskPath, maxRamDiskSize); 26 | } 27 | return instance; 28 | } 29 | private RamDiskManager(String ramDiskPath, String diskPath, long maxRamDiskSize) { 30 | this.ramDiskPath = ramDiskPath; 31 | this.diskPath = diskPath; 32 | this.maxRamDiskSize = maxRamDiskSize; 33 | this.fileSizes = new HashMap<>(); 34 | } 35 | 36 | public void createFile(String fileName, byte[] data) { 37 | FileMonitor.ignoreFile(fileName); 38 | // Check if there is enough space in the ramdisk 39 | long totalSize = 0; 40 | for (long size : fileSizes.values()) { 41 | totalSize += size; 42 | } 43 | if (totalSize + data.length > maxRamDiskSize) { 44 | // Not enough space, delete the oldest file 45 | String oldestFile = null; 46 | long oldestTime = Long.MAX_VALUE; 47 | for (Map.Entry entry : fileSizes.entrySet()) { 48 | File file = new File(ramDiskPath + File.separator + entry.getKey()); 49 | if (file.lastModified() < oldestTime) { 50 | oldestTime = file.lastModified(); 51 | oldestFile = entry.getKey(); 52 | } 53 | } 54 | if (oldestFile != null) { 55 | deleteRamFile(oldestFile); 56 | } 57 | } 58 | 59 | // Create the file in the ramdisk 60 | File file = new File(ramDiskPath + File.separator + fileName); 61 | try (FileOutputStream fos = new FileOutputStream(file)) { 62 | fos.write(data); 63 | fileSizes.put(fileName, (long) data.length); 64 | } catch (IOException e) { 65 | e.printStackTrace(); 66 | } 67 | 68 | // Copy the file to the disk 69 | copyFileToDisk(fileName); 70 | try { 71 | Thread.sleep(600); 72 | } catch (InterruptedException e) { 73 | e.printStackTrace(); 74 | } 75 | FileMonitor.unignoreFile(fileName); 76 | } 77 | 78 | public void deleteFile(String fileName) { 79 | FileMonitor.ignoreFile(fileName); 80 | File file = new File(diskPath + File.separator + fileName); 81 | if (file.exists()) { 82 | file.delete(); 83 | } 84 | deleteRamFile(fileName); 85 | try { 86 | Thread.sleep(600); 87 | } catch (InterruptedException e) { 88 | e.printStackTrace(); 89 | } 90 | FileMonitor.unignoreFile(fileName); 91 | } 92 | public void deleteRamFile(String fileName) { 93 | File file = new File(ramDiskPath + File.separator + fileName); 94 | if (file.exists()) { 95 | file.delete(); 96 | fileSizes.remove(fileName); 97 | } 98 | } 99 | 100 | private void copyFileToDisk(String fileName) { 101 | // Copy the file from the ramdisk to the disk 102 | File srcFile = new File(ramDiskPath + File.separator + fileName); 103 | File destFile = new File(diskPath + File.separator + fileName); 104 | try { 105 | Files.copy(srcFile.toPath(), destFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 106 | } catch (IOException e) { 107 | e.printStackTrace(); 108 | } 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /src/kr/re/keti/RequestProcess.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.OutputStream; 9 | import java.net.Socket; 10 | import java.sql.Timestamp; 11 | import java.util.ArrayList; 12 | import java.util.Arrays; 13 | import java.util.HashMap; 14 | import java.util.List; 15 | import java.util.Map; 16 | import java.util.concurrent.atomic.AtomicInteger; 17 | 18 | import kr.re.keti.agent.Agent; 19 | import kr.re.keti.agent.AgentPacket; 20 | import kr.re.keti.database.Database; 21 | import kr.re.keti.database.FileManagementDto; 22 | 23 | public class RequestProcess extends DataProcess{ 24 | Database database; 25 | 26 | public RequestProcess(Database database) { 27 | super(database); 28 | this.database = database; 29 | } 30 | // ------------------------------------------pub/sub-------------------------------------------------- 31 | public void newEdge(String data) { 32 | String request = "{[{REQ::"+Main.deviceIP+"::010::"+data+"}]}"; 33 | agent.send(request.getBytes()); 34 | } 35 | // ------------------------------------------TCP-------------------------------------------------- 36 | public void requestDeviceInformation(String address, int port) { 37 | if(ipCheck(address)) { 38 | System.out.println("request to "+address); 39 | byte[] request = ("{[{REQ::"+address+"::001::DEV_STATUS}]}").getBytes(); 40 | String response = new String(agent.send(address, request)); 41 | 42 | String[] array = response.substring(8, response.indexOf("}]}")).split("::"); 43 | String data = array[2]; 44 | showDeviceInformation(data); 45 | } 46 | else { 47 | System.out.println("not found: "+address); 48 | } 49 | } 50 | public FileManagementDto requestMetaDataInformation(String address, int port, String dataid) { 51 | String request = "{[{REQ::"+address+"::003::"+dataid+"}]}"; 52 | 53 | String response = new String(agent.send(address, request.getBytes())); 54 | System.out.println("requestProcess response : "+response); 55 | response = messageFormat(response); 56 | String data = response.split("::")[3]; 57 | if(data.equals("none")) return null; 58 | 59 | String datas[] = data.split("#"); 60 | FileManagementDto dto = new FileManagementDto(); 61 | dto.setDataId(dataid); 62 | dto.setTimestamp(Timestamp.valueOf(datas[1])); 63 | dto.setFileType(datas[2]); 64 | dto.setDataType(Integer.parseInt(datas[3])); 65 | dto.setSecurityLevel(Integer.parseInt(datas[4])); 66 | dto.setDataPriority(Integer.parseInt(datas[5])); 67 | dto.setDataSign(datas[6]); 68 | dto.setCert(datas[7]); 69 | dto.setDirectory(datas[8]); 70 | dto.setLinkedEdge(datas[9]); 71 | dto.setDataSize(Integer.parseInt(datas[10])); 72 | return dto; 73 | } 74 | public void requestWholeDataInformation(String address, int port) { 75 | if(ipCheck(address)) { 76 | byte[] request = ("{[{REQ::"+address+"::002::DATA_INFO}]}").getBytes(); 77 | String response = new String(agent.send(address, request)); 78 | response = messageFormat(response); 79 | String data = response.split("::")[3]; 80 | wholeDataInformation(data); 81 | } 82 | else { 83 | System.out.println("not found: "+address); 84 | } 85 | } 86 | public void requestIndividualMetaDataInformation(int port, String dataid) { 87 | ArrayList existsEdge = new ArrayList<>(); 88 | System.out.println("request to mine"); 89 | FileManagementDto dto = (FileManagementDto) database.select("file_management", dataid); 90 | String fileAddress = "none"; 91 | if(dto != null) { 92 | existsEdge.add(Main.deviceIP); 93 | fileAddress = "mine"; 94 | } 95 | 96 | if(Main.mode.equals("slave")) { 97 | System.out.println("request to "+Main.masterIP); 98 | FileManagementDto temp = requestMetaDataInformation(Main.masterIP, port, dataid); 99 | if(temp != null) { 100 | existsEdge.add(Main.masterIP); 101 | if(dto ==null) { 102 | dto = temp; 103 | fileAddress = Main.masterIP; 104 | } 105 | } 106 | } 107 | 108 | String slaves = requestEdgeList(port); 109 | String[] slaveList = slaves.split(", "); 110 | if(!slaves.equals("none")) { 111 | for(String address : slaveList) { 112 | if(!Main.deviceIP.equals(address)) { 113 | System.out.println("request to "+address); 114 | FileManagementDto temp = requestMetaDataInformation(address, port, dataid); 115 | if(temp != null) { 116 | existsEdge.add(address); 117 | if(dto ==null) { 118 | dto = temp; 119 | fileAddress = address; 120 | } 121 | } 122 | } 123 | } 124 | } 125 | if(dto ==null) { 126 | System.out.println("* Anyone doesn't have MetaData."); 127 | } 128 | else { 129 | System.out.print("* ["); 130 | for(int i=0; i edgeList = new ArrayList<>(); 159 | edgeList.add(Main.masterIP); 160 | edgeList.addAll(Arrays.asList(slaveList)); 161 | 162 | ArrayList existsEdge = new ArrayList<>(); 163 | Map> edgeChunkMap = new HashMap<>(); 164 | Map chunkSshMap = new HashMap<>(); 165 | 166 | FileManagementDto metaData = (FileManagementDto) database.select("file_management", dataid); 167 | 168 | //-------------------------existsEdge------------------------------------------- 169 | // if(metaData != null) return null; 170 | for(String edge : edgeList) { 171 | if(edge.equals(Main.deviceIP)) continue; 172 | 173 | FileManagementDto dto = requestMetaDataInformation(edge, port, dataid); 174 | if(dto != null) { 175 | if(metaData == null) metaData = dto; 176 | existsEdge.add(edge); 177 | } 178 | } 179 | String fileName = dataid+"."+metaData.getFileType(); 180 | 181 | int currentChunk = 1; 182 | int dataSize = (int) metaData.getDataSize(); 183 | 184 | int remainingChunks = dataSize / STANDARD_SIZE - currentChunk + 1; 185 | int chunksPerEdge = remainingChunks / existsEdge.size(); 186 | int extraChunks = remainingChunks % existsEdge.size(); 187 | 188 | for (String edge : existsEdge) { 189 | int edgeChunks = chunksPerEdge + (extraChunks > 0 ? 1 : 0); 190 | extraChunks--; 191 | List edgeChunksList = new ArrayList<>(); 192 | for (int i = 0; i < edgeChunks; i++) { 193 | edgeChunksList.add(fileName+"_" + currentChunk); 194 | currentChunk++; 195 | } 196 | edgeChunkMap.put(edge, edgeChunksList); 197 | } 198 | 199 | //----------------------------chunk 401 message-------------------------------------------- 200 | // [debug] chunk file list 201 | // for (Map.Entry> entry : edgeChunkMap.entrySet()) { 202 | // String edge = entry.getKey(); 203 | // List chunkList = entry.getValue(); 204 | // 205 | // System.out.println("Edge: " + edge); 206 | // System.out.println("Chunk List:"); 207 | // for (String chunk : chunkList) { 208 | // System.out.println(chunk); 209 | // } 210 | // System.out.println(); 211 | // } 212 | 213 | for (Map.Entry> entry : edgeChunkMap.entrySet()) { 214 | String edge = entry.getKey(); 215 | List chunkList = entry.getValue(); 216 | 217 | String chunk = chunkList.get(0); 218 | String[] stringItem = chunk.split("_"); 219 | int start = Integer.parseInt(stringItem[stringItem.length-1]); 220 | 221 | chunk = chunkList.get(chunkList.size() - 1); 222 | stringItem = chunk.split("_"); 223 | int finish = Integer.parseInt(stringItem[stringItem.length-1]); 224 | 225 | String request = "{[{REQ::"+edge+"::401::"+dataid+"::"+start+"::"+finish+"}]}"; 226 | String response = new String(agent.send(edge, request.getBytes())); 227 | response = messageFormat(response); 228 | String[] responseArray = response.split("::"); 229 | String result = responseArray[responseArray.length-1]; 230 | if(!result.equals("success")) { 231 | edgeChunkMap.remove(edge); 232 | continue; 233 | } 234 | } 235 | 236 | 237 | System.out.println("* Edge List with Data Separation Completed : "+edgeChunkMap.keySet()); 238 | for (Map.Entry> entry : edgeChunkMap.entrySet()) { 239 | String edge = entry.getKey(); 240 | List chunkList = entry.getValue(); 241 | 242 | String chunk = chunkList.get(0); 243 | String[] stringItem = chunk.split("_"); 244 | int start = Integer.parseInt(stringItem[stringItem.length-1]); 245 | 246 | chunk = chunkList.get(chunkList.size() - 1); 247 | stringItem = chunk.split("_"); 248 | int finish = Integer.parseInt(stringItem[stringItem.length-1]); 249 | 250 | 251 | System.out.println("Request to : "+edge+", chunk #"+start+" to #"+finish); 252 | } 253 | 254 | //----------------------------chunk 405 message-------------------------------------------- 255 | for(Map.Entry> entry : edgeChunkMap.entrySet()) { 256 | String edge = entry.getKey(); 257 | List chunkList = entry.getValue(); 258 | 259 | 260 | String startChunk = chunkList.get(0); 261 | String[] stringItem = startChunk.split("_"); 262 | int start = Integer.parseInt(stringItem[stringItem.length-1]); 263 | 264 | String finishChunk = chunkList.get(chunkList.size() - 1); 265 | stringItem = finishChunk.split("_"); 266 | int finish = Integer.parseInt(stringItem[stringItem.length-1]); 267 | 268 | String request = "{[{REQ::"+edge+"::405::"+dataid+"::"+start+"::"+finish+"}]}"; 269 | String response = new String(agent.send(edge, request.getBytes())); 270 | response = messageFormat(response); 271 | String[] responseArray = response.split("::"); 272 | String[] sha = Arrays.copyOfRange(responseArray, 4, responseArray.length); 273 | 274 | 275 | for (int i = 0; i < chunkList.size(); i++) { 276 | String chunk = chunkList.get(i); 277 | String sshInfo = sha[i]; // SSH 정보는 sha 배열에서 가져옴 278 | chunkSshMap.put(chunk, sshInfo); 279 | } 280 | } 281 | 282 | // [debug] ssh data 283 | // for (Map.Entry entry : chunkSshMap.entrySet()) { 284 | // String chunk = entry.getKey(); 285 | // String sshInfo = entry.getValue(); 286 | // System.out.println(chunk + " -> " + sshInfo); 287 | // } 288 | //-------------------------------chunk 406 message------------------------------ 289 | try { 290 | Thread.sleep(100); 291 | } catch (InterruptedException e) { 292 | e.printStackTrace(); 293 | } 294 | String filePath = Main.storageFolder+"chunk/"; 295 | File chunkPath = new File(filePath); 296 | if(!chunkPath.exists()) chunkPath.mkdir(); 297 | 298 | AtomicInteger downloadCompleteCount = new AtomicInteger(); 299 | int chunkSize = (int) Math.ceil((double)metaData.getDataSize()/STANDARD_SIZE); 300 | Object lock = new Object(); 301 | 302 | Thread downloadCompleteThread = new Thread(()->{ 303 | int errorCheck = 0; 304 | int beforeCount = -1; 305 | while(true) { 306 | synchronized(lock) { 307 | try { 308 | lock.wait(100); 309 | } catch (InterruptedException e) { 310 | e.printStackTrace(); 311 | } 312 | } 313 | int currentCount = downloadCompleteCount.get(); 314 | if(beforeCount != currentCount && currentCount 10) { 323 | System.out.println("\tReceive Chunk fail"); 324 | break; 325 | } 326 | } 327 | if(currentCount >= chunkSize) { 328 | System.out.println("\tReceive Rate of Chunk :"+currentCount+"/"+chunkSize+" = 100%"); 329 | break; 330 | } 331 | } 332 | }); 333 | downloadCompleteThread.setName("downloadCompleteThread"); 334 | downloadCompleteThread.start(); 335 | 336 | chunkSshMap.keySet().forEach(chunkName->{ 337 | Thread downloadThread = new Thread(()->{ 338 | String ssh = chunkSshMap.get(chunkName); 339 | AgentPacket packet = null; 340 | for (int i=0; i<10; i++) { 341 | try { 342 | Thread.sleep(500); 343 | packet = Agent.unitTable.get(chunkName); 344 | if(packet == null && i >8) { 345 | System.out.println("packet not data "+chunkName); 346 | return; 347 | } 348 | else if(packet == null) continue; 349 | Socket socket = packet.getSocket(); 350 | String address = socket.getInetAddress().getHostAddress(); 351 | OutputStream outputStream = socket.getOutputStream(); 352 | byte[] data = packet.getData(); 353 | if(ssh.equals(sha(data))) { 354 | try(FileOutputStream fileOutputStream = new FileOutputStream(filePath+chunkName)){ 355 | if(ssh.equals(sha(data))) { 356 | fileOutputStream.write(data); 357 | outputStream.write(("{[{ANS::"+address+"::406::success}]}").getBytes()); 358 | outputStream.flush(); 359 | int currentCount = downloadCompleteCount.incrementAndGet(); 360 | if(currentCount >= chunkSize) { 361 | synchronized (lock) { 362 | lock.notifyAll(); 363 | } 364 | } 365 | } 366 | else { 367 | outputStream.write(("{[{ANS::"+address+"::406::fail}]}").getBytes()); 368 | outputStream.flush(); 369 | } 370 | } catch (FileNotFoundException e) { 371 | e.printStackTrace(); 372 | } catch (IOException e) { 373 | e.printStackTrace(); 374 | } 375 | } 376 | else { 377 | continue; 378 | } 379 | 380 | socket.close(); 381 | return; 382 | } catch (InterruptedException e) { 383 | e.printStackTrace(); 384 | } catch (IOException e) { 385 | e.printStackTrace(); 386 | } 387 | } 388 | System.out.println("die "+chunkName); 389 | }); 390 | downloadThread.setName(chunkName+"DownLoadThread"); 391 | downloadThread.start(); 392 | }); 393 | 394 | try { 395 | downloadCompleteThread.join(); 396 | } catch (InterruptedException e) { 397 | e.printStackTrace(); 398 | } 399 | if(chunkSize != downloadCompleteCount.get()) return; 400 | //-------------------------------chunk 444 message------------------------------ 401 | Thread mergeThread = new Thread(()->{ 402 | try (FileOutputStream outputStream = new FileOutputStream(Main.storageFolder+fileName);){ 403 | for(int i=1; i<= chunkSize; i++) { 404 | String chunkName = fileName+"_"+i; 405 | FileInputStream inputStream = new FileInputStream(Main.storageFolder+"chunk/"+chunkName); 406 | byte[] buffer = new byte[1024]; 407 | int length; 408 | while((length = inputStream.read(buffer)) >0 ) { 409 | outputStream.write(buffer, 0, length); 410 | } 411 | inputStream.close(); 412 | } 413 | } catch (IOException e) { 414 | e.printStackTrace(); 415 | } 416 | }); 417 | mergeThread.setName(fileName+"MergeThread"); 418 | mergeThread.start(); 419 | 420 | try { 421 | mergeThread.join(); 422 | String fileSsh = sha(fileName); 423 | String responseSsh; 424 | int i=0; 425 | do { 426 | i++; 427 | Thread.sleep(100); 428 | String request = "{[{REQ::"+existsEdge.get(0)+"::444::"+fileName+"}]}"; 429 | byte[] response = agent.send(existsEdge.get(0), request.getBytes()); 430 | responseSsh = messageFormat(new String(response)).split("::")[3]; 431 | }while(i < 10 && !responseSsh.equals(fileSsh)); 432 | if(i>9) { 433 | System.out.println("file ssh not equals"); 434 | return; 435 | } 436 | else { 437 | System.out.println("* Recieved Data SHA code is th same as original."); 438 | System.out.println("\tOriginal Data SHA code :\t"+responseSsh); 439 | System.out.println("\tRecieved Data SHA code :\t"+fileSsh); 440 | System.out.println("* "+existsEdge+" : have Data."); 441 | } 442 | } catch (InterruptedException e) { 443 | e.printStackTrace(); 444 | } 445 | // return null; 446 | } 447 | 448 | } 449 | -------------------------------------------------------------------------------- /src/kr/re/keti/ResponseProcess.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.io.OutputStream; 10 | 11 | import kr.re.keti.database.Database; 12 | import kr.re.keti.database.FileManagementDto; 13 | import kr.re.keti.os.OSProcess; 14 | 15 | public class ResponseProcess extends DataProcess{ 16 | Database database; 17 | 18 | public ResponseProcess(Database database) { 19 | super(database); 20 | this.database = database; 21 | } 22 | 23 | 24 | public String responseInitEdgeList(String address) { 25 | String response = "{[{ANS::"+address+"::001::success}]}"; 26 | return response; 27 | } 28 | public String responseEdgeList(String address) { 29 | String response = "{[{ANS::"+address+"::001::"+OSProcess.getEdgeListAsString()+"}]}"; 30 | return response; 31 | } 32 | public String responseDeviceInformation(String address) { 33 | String response = "{[{ANS::"+address+"::001::"+deviceInformation()+"}]}"; 34 | return response; 35 | } 36 | public String responseWholeDataInformation(String address) { 37 | String response = "{[{ANS::"+address+"::002::"+wholeDataInformation()+"}]}"; 38 | System.out.println("response : "+response); 39 | return response; 40 | } 41 | public String responseMetaDataInformation(String address, String dataid) { 42 | String response = "{[{ANS::"+address+"::003::"; 43 | 44 | FileManagementDto dto = (FileManagementDto) database.select("file_management", dataid); 45 | if(dto != null) { 46 | String metaData = dto.getDataId()+"#"+dto.getTimestamp()+"#"+dto.getFileType() 47 | +"#"+dto.getSecurityLevel()+"#"+dto.getDataPriority()+"#"+dto.getAvailabilityPolicy() 48 | +"#"+dto.getDataSign()+"#"+dto.getCert()+"#"+dto.getDirectory() 49 | +"#"+dto.getLinkedEdge()+"#"+dto.getDataSize(); 50 | response += metaData+"}]}"; 51 | } 52 | else { 53 | response += "none}]}"; 54 | } 55 | return response; 56 | } 57 | public String responseIndividualDataRemove(String address, String dataid) { 58 | String response = "{[{ANS::"+address+"::006::"; 59 | boolean check = database.delete("file_management", dataid); 60 | if(check) { 61 | response += "permission}]}"; 62 | } 63 | else { 64 | response += "fail}]}"; 65 | } 66 | return response; 67 | } 68 | public String responseChunkCreate(String address, String dataid, int startIdx, int finishIdx) { 69 | final int CHUNK_SIZE = 1000; 70 | FileManagementDto dto =(FileManagementDto) database.select("file_management", dataid); 71 | String fileName = dataid + "." + dto.getFileType(); 72 | String filePath = Main.storageFolder + fileName; 73 | String response = "{[{ANS::" + address + "::401::fail}]}"; 74 | 75 | try (InputStream inputStream = new FileInputStream(filePath)) { 76 | File directory = new File(Main.storageFolder + "chunk/"); 77 | if (!directory.exists()) directory.mkdirs(); 78 | 79 | byte[] buffer = new byte[CHUNK_SIZE]; 80 | int chunkCount = 1; 81 | int bytesRead; 82 | while ((bytesRead = inputStream.read(buffer)) != -1) { 83 | if (chunkCount >= startIdx && chunkCount <= finishIdx) { // 청크 개수로 범위 비교 84 | String chunkFileName = fileName + "_" + chunkCount; 85 | String chunkFilePath = directory.getPath() + File.separator + chunkFileName; 86 | try (OutputStream outputStream = new FileOutputStream(chunkFilePath)) { 87 | outputStream.write(buffer, 0, bytesRead); 88 | } 89 | } 90 | chunkCount++; 91 | 92 | if (chunkCount > finishIdx) break; 93 | } 94 | 95 | response = "{[{ANS::" + address + "::401::success}]}"; 96 | } catch (FileNotFoundException e) { 97 | e.printStackTrace(); 98 | } catch (IOException e) { 99 | e.printStackTrace(); 100 | } 101 | 102 | return response; 103 | } 104 | 105 | 106 | 107 | public String responseSha(String address, String dataid) { 108 | String shaCode = sha(dataid); 109 | String response = "{[{ANS::"+address+"::444::"+shaCode+"}]}"; 110 | return response; 111 | } 112 | public String responseSha(String address, String dataid, int startIdx, int finishIdx) { 113 | String response = "{[{ANS::"+address+"::405::sha::"; 114 | FileManagementDto dto =(FileManagementDto) database.select("file_management", dataid); 115 | String fileName = dataid+"."+dto.getFileType(); 116 | for(int i=startIdx; i<=finishIdx;i++) { 117 | String chunkFileName = "chunk/"+fileName+"_"+i; 118 | String shaCode = sha(chunkFileName); 119 | response += (shaCode+"::"); 120 | } 121 | response+="}]}"; 122 | System.out.println(response); 123 | return response; 124 | } 125 | 126 | 127 | } 128 | -------------------------------------------------------------------------------- /src/kr/re/keti/Ssl.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti; 2 | 3 | import java.io.BufferedOutputStream; 4 | import java.io.ByteArrayInputStream; 5 | import java.io.File; 6 | import java.io.FileInputStream; 7 | import java.io.FileNotFoundException; 8 | import java.io.FileReader; 9 | import java.io.IOException; 10 | import java.io.InputStream; 11 | import java.io.OutputStream; 12 | import java.math.BigInteger; 13 | import java.nio.file.Files; 14 | import java.nio.file.Paths; 15 | import java.nio.file.StandardOpenOption; 16 | import java.security.InvalidKeyException; 17 | import java.security.KeyFactory; 18 | import java.security.KeyPair; 19 | import java.security.KeyPairGenerator; 20 | import java.security.NoSuchAlgorithmException; 21 | import java.security.PrivateKey; 22 | import java.security.PublicKey; 23 | import java.security.SecureRandom; 24 | import java.security.Security; 25 | import java.security.Signature; 26 | import java.security.SignatureException; 27 | import java.security.cert.CertificateException; 28 | import java.security.cert.X509Certificate; 29 | import java.security.spec.InvalidKeySpecException; 30 | import java.security.spec.X509EncodedKeySpec; 31 | import java.time.Instant; 32 | import java.time.LocalDate; 33 | import java.time.ZoneId; 34 | import java.util.ArrayList; 35 | import java.util.Base64; 36 | import java.util.Date; 37 | import java.util.Map; 38 | import java.util.concurrent.ConcurrentHashMap; 39 | 40 | import javax.security.auth.x500.X500Principal; 41 | 42 | import org.bouncycastle.asn1.pkcs.PrivateKeyInfo; 43 | import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter; 44 | import org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder; 45 | import org.bouncycastle.jce.provider.BouncyCastleProvider; 46 | import org.bouncycastle.openssl.PEMKeyPair; 47 | import org.bouncycastle.openssl.PEMParser; 48 | import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; 49 | import org.bouncycastle.operator.ContentSigner; 50 | import org.bouncycastle.operator.OperatorCreationException; 51 | import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder; 52 | import org.bouncycastle.util.io.pem.PemReader; 53 | 54 | public class Ssl { 55 | public static Ssl instance = new Ssl(); 56 | public static ConcurrentHashMap uuidMap = new ConcurrentHashMap<>(); 57 | public static ConcurrentHashMap pubKeyMap = new ConcurrentHashMap<>(); 58 | private static String certFolder; 59 | private static PrivateKey privateKey; 60 | private static String signatureAlgorithm = "SHA256withRSA"; 61 | 62 | public static Ssl getInstance() { 63 | return instance; 64 | } 65 | private Ssl() { 66 | 67 | } 68 | 69 | public static void setting(String path, String priKey, String pubKey) { 70 | certFolder = path; 71 | 72 | File keys = new File(path+"keys"); 73 | if(!keys.exists()) keys.mkdir(); 74 | 75 | path = path.endsWith("/") ? path : "/"; 76 | path += "Private/"; 77 | File Folder = new File(path); 78 | if(!Folder.exists()) Folder.mkdir(); 79 | String certPath = Folder+"/"+"cert.crt"; 80 | String priKeyPath = Folder+"/"+priKey; 81 | String pubKeyPath = Folder+"/"+pubKey; 82 | selfSignedCertificate(certPath, priKeyPath, pubKeyPath, 365); 83 | 84 | privateKey = generatePrivateKey(priKeyPath); 85 | } 86 | public static ArrayList getKeyList() { 87 | ArrayList keyList = new ArrayList<>(); 88 | 89 | for (Map.Entry entry : uuidMap.entrySet()) { 90 | String address = entry.getKey(); 91 | String uuid = entry.getValue(); 92 | PublicKey key = pubKeyMap.get(uuid); 93 | String data = Base64.getEncoder().encodeToString(key.getEncoded()); 94 | 95 | String keyData = address+":"+uuid+":"+data; 96 | keyList.add(keyData); 97 | } 98 | return keyList; 99 | } 100 | public static String getUuid(String address) { 101 | String uuid = uuidMap.get(address); 102 | return uuid; 103 | } 104 | public static void addKey(String address, String uuid, String keyData) { 105 | uuidMap.put(address, uuid); 106 | 107 | // for(Map.Entry entry : uuidMap.entrySet()) { 108 | // String key = entry.getKey(); 109 | // String value = entry.getValue(); 110 | // System.out.println(key+" : "+value); 111 | // } 112 | 113 | String path = certFolder+"keys/"+uuid+".key"; 114 | byte[] data = Base64.getDecoder().decode(keyData); 115 | try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(Paths.get(path), StandardOpenOption.CREATE))) { 116 | byte[] buffer = new byte[1024]; 117 | int lengthRead; 118 | InputStream in = new ByteArrayInputStream(data); 119 | while ((lengthRead = in.read(buffer)) > 0) { 120 | out.write(buffer, 0, lengthRead); 121 | out.flush(); 122 | } 123 | } catch (IOException e) { 124 | e.printStackTrace(); 125 | } 126 | PublicKey key = generatePublicKey(path); 127 | pubKeyMap.put(uuid, key); 128 | } 129 | public static String getKey(String address) { 130 | String keyData = "none"; 131 | String path = "none"; 132 | if(address.equals(Main.deviceIP)) { 133 | path = certFolder+"Private/pub.key"; 134 | } 135 | else { 136 | String uuid = uuidMap.get(address); 137 | if(uuid == null) { 138 | System.out.println("[SSL] "+address+"not found"); 139 | return keyData; 140 | } 141 | path = certFolder+"keys/"+uuid+".key"; 142 | } 143 | byte[] data = null; 144 | try { 145 | FileInputStream fis = new FileInputStream(path); 146 | data = new byte[fis.available()]; 147 | fis.read(data); 148 | fis.close(); 149 | } catch (IOException e) { 150 | e.printStackTrace(); 151 | } 152 | keyData = Base64.getEncoder().encodeToString(data); 153 | return keyData; 154 | } 155 | public static void deleteKey(String address) { 156 | pubKeyMap.remove(address); 157 | } 158 | public static String getPath() { 159 | return certFolder; 160 | } 161 | public void setAlgorithm(String algorithm) { 162 | signatureAlgorithm = algorithm; 163 | } 164 | public String getAlgorithm() { 165 | return signatureAlgorithm; 166 | } 167 | public String sign(String filePath) { 168 | String signatureData = "none"; 169 | try { 170 | Signature privateSignature = Signature.getInstance(signatureAlgorithm); 171 | privateSignature.initSign(privateKey); 172 | 173 | try (InputStream in = new FileInputStream(filePath)) { 174 | byte[] buffer = new byte[1024]; 175 | int read; 176 | while ((read = in.read(buffer)) != -1) { 177 | privateSignature.update(buffer, 0, read); 178 | } 179 | } 180 | 181 | byte[] signature = privateSignature.sign(); 182 | signatureData = Base64.getEncoder().encodeToString(signature); 183 | } catch (IOException | NoSuchAlgorithmException | InvalidKeyException | SignatureException e) { 184 | e.printStackTrace(); 185 | } 186 | return signatureData; 187 | } 188 | public static boolean verify(String address, String signature, String filePath) { 189 | boolean isVerified = false; 190 | 191 | // 검증할 파일을 로드합니다. 192 | try { 193 | 194 | System.out.println("uuidMap"); 195 | for(Map.Entry entry : uuidMap.entrySet()) { 196 | String key = entry.getKey(); 197 | String value = entry.getValue(); 198 | System.out.println(key+" : "+value); 199 | } 200 | System.out.println("pubkeyMap"); 201 | for(Map.Entry entry : pubKeyMap.entrySet()) { 202 | String key = entry.getKey(); 203 | PublicKey value = entry.getValue(); 204 | System.out.println(key+" : " + (value!=null)); 205 | } 206 | System.out.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="); 207 | String uuid = uuidMap.get(address); 208 | PublicKey key = pubKeyMap.get(uuid); 209 | if(key == null) { 210 | System.out.println("not find public key"); 211 | return isVerified; 212 | } 213 | byte[] signatureData = Base64.getDecoder().decode(signature); 214 | byte[] data = Files.readAllBytes(Paths.get(filePath)); 215 | Signature publicSignature = Signature.getInstance(signatureAlgorithm); 216 | publicSignature.initVerify(key); 217 | publicSignature.update(data); 218 | 219 | isVerified = publicSignature.verify(signatureData); 220 | } catch (IOException e) { 221 | e.printStackTrace(); 222 | } catch (InvalidKeyException e) { 223 | e.printStackTrace(); 224 | } catch (NoSuchAlgorithmException e) { 225 | e.printStackTrace(); 226 | } catch (SignatureException e) { 227 | e.printStackTrace(); 228 | } 229 | 230 | return isVerified; 231 | } 232 | public static PrivateKey generatePrivateKey(String priKeyPath) { 233 | PrivateKey privateKey = null; 234 | try { 235 | PemReader pemReader = new PemReader(new FileReader(priKeyPath)); 236 | PEMParser pemParser = new PEMParser(pemReader); 237 | Object object = pemParser.readObject(); 238 | JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); 239 | 240 | if (object instanceof PEMKeyPair) { 241 | KeyPair kp = converter.getKeyPair((PEMKeyPair) object); 242 | privateKey = kp.getPrivate(); 243 | } else if (object instanceof PrivateKeyInfo) { 244 | privateKey = converter.getPrivateKey((PrivateKeyInfo) object); 245 | } 246 | pemParser.close(); 247 | } catch (FileNotFoundException e) { 248 | e.printStackTrace(); 249 | } catch (IOException e) { 250 | e.printStackTrace(); 251 | } 252 | return privateKey; 253 | } 254 | public static PublicKey generatePublicKey(String pubKeyPath) { 255 | PublicKey pubKey = null; 256 | try { 257 | String publicKeyPEM = new String(Files.readAllBytes(Paths.get(pubKeyPath))); 258 | publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", ""); 259 | publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); 260 | publicKeyPEM = publicKeyPEM.replace("\n", ""); 261 | byte[] encodedPublicKey = Base64.getDecoder().decode(publicKeyPEM); 262 | 263 | X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedPublicKey); 264 | KeyFactory kf = KeyFactory.getInstance("RSA"); 265 | pubKey = kf.generatePublic(spec); 266 | } catch (IOException e) { 267 | e.printStackTrace(); 268 | } catch (NoSuchAlgorithmException e) { 269 | e.printStackTrace(); 270 | } catch (InvalidKeySpecException e) { 271 | e.printStackTrace(); 272 | } 273 | 274 | return pubKey; 275 | } 276 | public static void selfSignedCertificate(String crtPath, String priKeyPath, String pubKeyPath, long expirationDate) { 277 | selfSignedCertificate(crtPath, priKeyPath, pubKeyPath, expirationDate, "SHA256withRSA"); 278 | } 279 | public static void selfSignedCertificate(String crtPath, String priKeyPath, String pubKeyPath, long expirationDate, String signatureAlgorithm) { 280 | try { 281 | if(!new File(crtPath).exists() || ! new File(priKeyPath).exists() || new File(pubKeyPath).exists()) { 282 | Security.addProvider(new BouncyCastleProvider()); 283 | 284 | KeyPairGenerator keyPairGenerator; 285 | keyPairGenerator = KeyPairGenerator.getInstance("RSA"); 286 | keyPairGenerator.initialize(2048, new SecureRandom()); 287 | KeyPair keyPair = keyPairGenerator.generateKeyPair(); 288 | 289 | Date notBefore = new Date(); 290 | // Date notAfter = new Date(notBefore.getTime() + 3650L * 24 * 60 * 60 * 1000); // 10 years 291 | // 현재 날짜를 가져옵니다. 292 | LocalDate now = LocalDate.now(); 293 | 294 | // 10년 후의 날짜를 계산합니다. 295 | LocalDate tenYearsLater = now.plusDays(expirationDate); 296 | 297 | // LocalDate를 Date로 변환합니다. 298 | Instant instant = tenYearsLater.atStartOfDay(ZoneId.systemDefault()).toInstant(); 299 | 300 | Date notAfter = Date.from(instant); 301 | 302 | JcaX509v3CertificateBuilder certBuilder = new JcaX509v3CertificateBuilder( 303 | new X500Principal("CN=SelfSigned"), BigInteger.ONE, notBefore, notAfter, 304 | new X500Principal("CN=SelfSigned"), keyPair.getPublic()); 305 | 306 | ContentSigner signer = new JcaContentSignerBuilder(signatureAlgorithm).build(keyPair.getPrivate()); 307 | X509Certificate cert = new JcaX509CertificateConverter().getCertificate(certBuilder.build(signer)); 308 | 309 | // 인증서를 PEM 형식으로 변환 310 | String certPem = "-----BEGIN CERTIFICATE-----\n"; 311 | certPem += Base64.getEncoder().encodeToString(cert.getEncoded()).replaceAll("(.{64})", "$1\n"); 312 | certPem += "\n-----END CERTIFICATE-----\n"; 313 | 314 | // 개인 키를 PEM 형식으로 변환 315 | PrivateKey privateKey = keyPair.getPrivate(); 316 | String keyPem = "-----BEGIN PRIVATE KEY-----\n"; 317 | keyPem += Base64.getEncoder().encodeToString(privateKey.getEncoded()).replaceAll("(.{64})", "$1\n"); 318 | keyPem += "\n-----END PRIVATE KEY-----\n"; 319 | // PEM 형식의 인증서와 개인 키를 파일로 저장 320 | Files.write(Paths.get(crtPath), certPem.getBytes()); 321 | Files.write(Paths.get(priKeyPath), keyPem.getBytes()); 322 | // 인증서에서 공개 키를 추출합니다. 323 | PublicKey publicKey = cert.getPublicKey(); 324 | 325 | // 공개 키를 PEM 형식으로 변환합니다. 326 | String publicKeyPEM = "-----BEGIN PUBLIC KEY-----\n"; 327 | publicKeyPEM += Base64.getEncoder().encodeToString(publicKey.getEncoded()).replaceAll("(.{64})", "$1\n"); 328 | publicKeyPEM += "\n-----END PUBLIC KEY-----\n"; 329 | 330 | // PEM 형식의 공개 키를 파일로 저장합니다. 331 | Files.write(Paths.get(pubKeyPath), publicKeyPEM.getBytes()); 332 | } 333 | } catch (NoSuchAlgorithmException e) { 334 | // TODO Auto-generated catch block 335 | e.printStackTrace(); 336 | } catch (OperatorCreationException e) { 337 | // TODO Auto-generated catch block 338 | e.printStackTrace(); 339 | } catch (CertificateException e) { 340 | // TODO Auto-generated catch block 341 | e.printStackTrace(); 342 | } catch (IOException e) { 343 | // TODO Auto-generated catch block 344 | e.printStackTrace(); 345 | } 346 | } 347 | public static String sign(String priKeyPath, String filePath) { 348 | return sign(priKeyPath, filePath, "SHA256withRSA"); 349 | } 350 | public static String sign(String priKeyPath, String filePath, String signatureAlgorithm) { 351 | String signatureData = "none"; 352 | // 개인 키 파일을 로드합니다. 353 | try { 354 | PemReader pemReader = new PemReader(new FileReader(priKeyPath)); 355 | PEMParser pemParser = new PEMParser(pemReader); 356 | Object object = pemParser.readObject(); 357 | JcaPEMKeyConverter converter = new JcaPEMKeyConverter().setProvider("BC"); 358 | PrivateKey privateKey = null; 359 | 360 | if (object instanceof PEMKeyPair) { 361 | KeyPair kp = converter.getKeyPair((PEMKeyPair) object); 362 | privateKey = kp.getPrivate(); 363 | } else if (object instanceof PrivateKeyInfo) { 364 | privateKey = converter.getPrivateKey((PrivateKeyInfo) object); 365 | } 366 | // 텍스트 파일을 로드합니다. 367 | byte[] data = Files.readAllBytes(Paths.get(filePath)); 368 | 369 | // SHA-256 해시를 계산하고, 이를 개인 키로 서명합니다. 370 | Signature privateSignature = Signature.getInstance(signatureAlgorithm); 371 | privateSignature.initSign(privateKey); 372 | privateSignature.update(data); 373 | byte[] signature = privateSignature.sign(); 374 | signatureData = Base64.getEncoder().encodeToString(signature); 375 | 376 | pemParser.close(); 377 | //// 서명을 파일로 직접 저장합니다. 378 | // Files.write(Paths.get("/home/keti/cert/sign.crt"), signature); 379 | } catch (FileNotFoundException e) { 380 | e.printStackTrace(); 381 | } catch (IOException e) { 382 | e.printStackTrace(); 383 | } catch (NoSuchAlgorithmException e) { 384 | e.printStackTrace(); 385 | } catch (InvalidKeyException e) { 386 | e.printStackTrace(); 387 | } catch (SignatureException e) { 388 | e.printStackTrace(); 389 | } 390 | 391 | return signatureData; 392 | } 393 | public static boolean verify(byte[] signature, String pubKeyPath, String filePath) { 394 | return verify(signature, pubKeyPath, filePath, "SHA256withRSA"); 395 | } 396 | public static boolean verify(byte[] signature, String pubKeyPath, String filePath, String signatureAlgorithm) { 397 | boolean isVerified = false; 398 | 399 | // 공개 키 파일을 로드합니다. 400 | try { 401 | String publicKeyPEM = new String(Files.readAllBytes(Paths.get(pubKeyPath))); 402 | // PEM 형식에서 불필요한 헤더와 푸터를 제거합니다. 403 | publicKeyPEM = publicKeyPEM.replace("-----BEGIN PUBLIC KEY-----\n", ""); 404 | publicKeyPEM = publicKeyPEM.replace("-----END PUBLIC KEY-----", ""); 405 | publicKeyPEM = publicKeyPEM.replace("\n", ""); 406 | // Base64로 인코딩된 키를 디코딩합니다. 407 | byte[] encodedPublicKey = Base64.getDecoder().decode(publicKeyPEM); 408 | 409 | // 바이트 배열을 PublicKey 객체로 변환합니다. 410 | X509EncodedKeySpec spec = new X509EncodedKeySpec(encodedPublicKey); 411 | KeyFactory kf = KeyFactory.getInstance("RSA"); 412 | PublicKey publicKey = kf.generatePublic(spec); 413 | 414 | // 서명 파일을 로드합니다. 415 | // byte[] signature = Files.readAllBytes(Paths.get(signPath)); 416 | 417 | // 검증할 파일을 로드합니다. 418 | byte[] data = Files.readAllBytes(Paths.get(filePath)); // 실제 파일 경로로 대체하세요. 419 | 420 | // SHA-256 해시를 계산하고, 이를 공개 키로 검증합니다. 421 | Signature publicSignature = Signature.getInstance(signatureAlgorithm); 422 | publicSignature.initVerify(publicKey); 423 | publicSignature.update(data); 424 | 425 | isVerified = publicSignature.verify(signature); 426 | } catch (IOException e) { 427 | e.printStackTrace(); 428 | } catch (NoSuchAlgorithmException e) { 429 | e.printStackTrace(); 430 | } catch (InvalidKeySpecException e) { 431 | e.printStackTrace(); 432 | } catch (InvalidKeyException e) { 433 | e.printStackTrace(); 434 | } catch (SignatureException e) { 435 | e.printStackTrace(); 436 | } 437 | return isVerified; 438 | } 439 | } 440 | -------------------------------------------------------------------------------- /src/kr/re/keti/agent/Agent.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.agent; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.BufferedWriter; 5 | import java.io.File; 6 | import java.io.FileReader; 7 | import java.io.FileWriter; 8 | import java.io.IOException; 9 | import java.net.Socket; 10 | import java.nio.file.Files; 11 | import java.nio.file.Paths; 12 | import java.nio.file.StandardOpenOption; 13 | import java.text.DecimalFormat; 14 | import java.time.LocalTime; 15 | import java.time.format.DateTimeFormatter; 16 | import java.util.ArrayList; 17 | import java.util.Arrays; 18 | import java.util.Base64; 19 | import java.util.Date; 20 | import java.util.Hashtable; 21 | import java.util.List; 22 | 23 | import kr.re.keti.DataProcess; 24 | import kr.re.keti.FileHandler; 25 | import kr.re.keti.Main; 26 | import kr.re.keti.PortNum; 27 | import kr.re.keti.RamDiskManager; 28 | import kr.re.keti.ResponseProcess; 29 | import kr.re.keti.Ssl; 30 | import kr.re.keti.database.Database; 31 | import kr.re.keti.database.FileManagementDto; 32 | import kr.re.keti.database.FileUuidDto; 33 | import kr.re.keti.os.OSProcess; 34 | import kr.re.keti.tcp.UnitEdge; 35 | import kr.re.keti.tcp.UnitShared; 36 | 37 | public class Agent extends EdgeDataAggregator{ 38 | private static final Agent instance = new Agent(); 39 | public Database database; 40 | public static Hashtable unitTable; 41 | private ResponseProcess responseProcess; 42 | private List logFilters; 43 | private List responseFilters; 44 | private RamDiskManager ramDiskManager; 45 | private Agent() { 46 | logFilters = Arrays.asList(-1, 10, 200, 300, 399, 400, 405, 406, 444, 19, 20); 47 | responseFilters = Arrays.asList(); 48 | setStandard(1000); 49 | } 50 | 51 | public static Agent getInstance() { 52 | return instance; 53 | } 54 | public void setDatabase(Database database) { 55 | instance.database = database; 56 | responseProcess = new ResponseProcess(database); 57 | ramDiskManager = RamDiskManager.getInstance(Main.ramFolder, Main.storageFolder); 58 | } 59 | @Override 60 | void receive(AgentPacket packet) { 61 | byte[] originalData = packet.getData(); 62 | String dataString = new String(originalData); 63 | if(!(dataString.startsWith("{[{") && dataString.endsWith("}]}"))) return; 64 | 65 | String message = DataProcess.messageFormat(dataString); 66 | String datas[] = message.split("::"); 67 | String address = datas[1]; 68 | int code = Integer.parseInt(datas[2]); 69 | // if(!address.equals(Main.deviceIP)) { 70 | // if(code==400) { 71 | // if(dataString.length()>500) { 72 | // String[] temp = dataString.split("::"); 73 | // temp[temp.length-1] = "...}]}"; 74 | // System.out.println(String.join("::", temp)); 75 | // } 76 | // else { 77 | // System.out.println(dataString); 78 | // } 79 | // } 80 | // else { 81 | // System.out.println(dataString); 82 | // } 83 | // } 84 | 85 | // if(!address.equals(Main.deviceIP) && code == 400) { 86 | // System.out.println("1================================================================================================"); 87 | // System.out.println(address+"::"+datas[3]+": "+LocalTime.now().format(DateTimeFormatter.ofPattern("H:m:s.SSS"))); 88 | //// System.out.println(dataString); 89 | // System.out.println("2================================================================================================"); 90 | // System.out.println(); 91 | // 92 | // } 93 | //-------------------Accept Log-------------------- 94 | if(shouldLog(code)) { 95 | System.out.println(); 96 | log(message); 97 | } 98 | 99 | //------------------process---------------------- 100 | Socket socket = packet.getSocket(); 101 | if(socket == null) { 102 | messageProcess(originalData); 103 | } 104 | else { 105 | tcpProcess(packet); 106 | } 107 | 108 | //-----------------Complete Log------------------ 109 | if(shouldLog(code)) { 110 | logLine("complete", address); 111 | System.out.print(">>>"); 112 | } 113 | } 114 | 115 | private void tcpProcess(AgentPacket packet) { 116 | byte[] originalData = packet.getData(); 117 | String dataString = new String(originalData); 118 | String requestData = DataProcess.messageFormat(dataString); 119 | String[] datas = requestData.split("::"); 120 | String address = datas[1]; 121 | int requestCode = Integer.parseInt(datas[2]); 122 | String response = "none"; 123 | switch(requestCode) { 124 | case 1: 125 | String type = datas[3]; 126 | switch(type) { 127 | case "EDGE_LIST": 128 | response = responseProcess.responseInitEdgeList(address); break; 129 | case "SLAVE_LIST": 130 | response = responseProcess.responseEdgeList(address); break; 131 | case "DEV_STATUS": 132 | response = responseProcess.responseDeviceInformation(address); break; 133 | } 134 | break; 135 | case 2: 136 | response = responseProcess.responseWholeDataInformation(address); break; 137 | case 3: { 138 | String dataid = datas[3]; 139 | if(requestCode == 3) { 140 | response = responseProcess.responseMetaDataInformation(address, dataid); 141 | } 142 | break; 143 | } 144 | case 6:{ 145 | String dataid = datas[3]; 146 | response = responseProcess.responseIndividualDataRemove(address, dataid); 147 | break; 148 | } 149 | case 20:{ 150 | String uuid = datas[3].trim(); 151 | String data = datas[4]; 152 | String path = Ssl.getPath(); 153 | path = path+"keys/"+uuid+".key"; 154 | 155 | Ssl.addKey(address, uuid, data); 156 | response = "{[{ANS::"+address+"::020::success}]}"; 157 | break; 158 | } 159 | case 399:{ 160 | String fileName = datas[3]; 161 | String uuid = datas[4]; 162 | UnitShared unit = UnitShared.getInstance(fileName, uuid); 163 | unit.addReadyCount(); 164 | response = "{[{ANS::"+address+"::399::success}]}"; 165 | break; 166 | } 167 | case 400:{ 168 | String uuid = datas[3]; 169 | uuid = uuid.substring(0, uuid.indexOf("_")); 170 | final String chunkUuid = uuid; 171 | UnitShared unit = UnitShared.getInstanceUuid(chunkUuid); 172 | unit.countAdd(address, datas[3]); 173 | response = "{[{ANS::"+address+"::400::"+datas[3]+"::success}]}"; 174 | break; 175 | } 176 | case 401: 177 | case 405: 178 | String dataid = datas[3]; 179 | int startIdx = Integer.parseInt(datas[4]); 180 | int finishIdx = Integer.parseInt(datas[5]); 181 | if(requestCode == 401) { 182 | response = responseProcess.responseChunkCreate(address, dataid, startIdx, finishIdx); 183 | } 184 | else if(requestCode == 405) { 185 | response = responseProcess.responseSha(address, dataid, startIdx, finishIdx); 186 | UnitEdge unitEdge = new UnitEdge(database, address, dataid, startIdx, finishIdx); 187 | unitEdge.start(); 188 | } 189 | break; 190 | case 406: 191 | Socket socket = packet.getSocket(); 192 | if(socket == null) return; 193 | 194 | String chunkName = datas[3]; 195 | int chunkLength = Integer.parseInt(datas[4]); 196 | int chunkEndIdx = originalData.length - 3; 197 | int chunkStartIdx = chunkEndIdx - chunkLength; 198 | byte[] data = new byte[chunkLength]; 199 | System.arraycopy(originalData, chunkStartIdx, data, 0, chunkLength); 200 | AgentPacket unitPacket = new AgentPacket(socket, data); 201 | unitTable.put(chunkName, unitPacket); 202 | break; 203 | case 444: 204 | String fileName = datas[3]; 205 | response = responseProcess.responseSha(address, fileName); 206 | break; 207 | default: 208 | System.out.println("TCP ["+requestCode+"] is undefined "); 209 | } 210 | 211 | //----------------------------------------------------------------------------- 212 | if(packet.getSocket() != null) { 213 | send(packet.getSocket(), response.getBytes()); 214 | // System.out.println("receive: "+response+" : "+LocalTime.now().format(DateTimeFormatter.ofPattern("H:m:s.SSS"))); 215 | } 216 | if(shouldRespond(requestCode)) { 217 | send(response.getBytes()); 218 | } 219 | } 220 | private void messageProcess(byte[] originalMessage) { 221 | String message = DataProcess.messageFormat(new String(originalMessage)); 222 | String[] datas = message.split("::"); 223 | String address = datas[1]; 224 | int code = Integer.parseInt(datas[2]); 225 | 226 | if(address.equals(Main.deviceIP) && code != 200) { 227 | return; 228 | } 229 | switch(code) { 230 | case -1: 231 | break; 232 | case 1: 233 | break; 234 | case 10:{ 235 | String data = datas[3]; 236 | FileManagementDto dto = DataProcess.metaDataToDto(data); 237 | database.insert(dto); 238 | break; 239 | } 240 | case 11:{ 241 | String dataid = datas[3]; 242 | FileManagementDto dto = (FileManagementDto) database.select("file_management", dataid); 243 | String extension = dto.getFileType(); 244 | String fileName = dataid + "." +extension; 245 | byte[] data = getData(originalMessage); 246 | // ramDiskManager.createFile(fileName, data); 247 | ramDiskManager.download(fileName, data); 248 | 249 | // byte[] sign = Base64.getDecoder().decode(dto.getDataSign()); 250 | String sign = dto.getDataSign(); 251 | String ipAddress = dto.getLinkedEdge(); 252 | try { 253 | Thread.sleep(100); 254 | if(!Ssl.verify(ipAddress, sign, Main.storageFolder+fileName)) { 255 | System.out.println("sign verify fail"); 256 | } 257 | } catch (Exception e) { 258 | e.printStackTrace(); 259 | } 260 | break; 261 | } 262 | case 12:{ 263 | String dataid = datas[3]; 264 | FileManagementDto dto = (FileManagementDto) database.select("file_management", dataid); 265 | dto.setDataSize(Long.parseLong(datas[4])); 266 | String extension = dto.getFileType(); 267 | String fileName = dataid + "." +extension; 268 | byte[] data = getData(originalMessage); 269 | ramDiskManager.createFile(fileName, data); 270 | database.update(dto); 271 | break; 272 | } 273 | case 13:{ 274 | String dataid = datas[3]; 275 | FileManagementDto dto = (FileManagementDto) database.select("file_management", dataid); 276 | if(dto == null) break; 277 | database.delete("file_management", dataid); 278 | if(dto.getLinkedEdge().equals(Main.deviceIP)) { 279 | dataid = ((FileUuidDto) database.executeQuery("select * from file_uuid where fileUuid='"+dataid+"';").get(0)).getFileName(); 280 | } 281 | String extension = dto.getFileType(); 282 | String fileName = dataid+"."+extension; 283 | ramDiskManager.remove(fileName, ""); 284 | break; 285 | } 286 | case 14: 287 | break; 288 | case 15: 289 | break; 290 | case 16: 291 | break; 292 | case 19:{ 293 | String uuid = datas[3]; 294 | String data = datas[5]; 295 | Ssl.addKey(address, uuid, data); 296 | 297 | 298 | Agent agent = Agent.getInstance(); 299 | String keyData = Ssl.getKey(Main.deviceIP); 300 | agent.send(address, ("{[{REQ::"+Main.deviceIP+"::020::"+Main.uuid+"::"+keyData+"}]}").getBytes()); 301 | for(String ip : OSProcess.edgeList) { 302 | if(!ip.equals(address)) { 303 | keyData = Ssl.getKey(ip); 304 | String keyUuid = Ssl.getUuid(ip); 305 | agent.send(address, ("{[{REQ::"+ip+"::020::"+keyUuid+"::"+keyData+"}]}").getBytes()); 306 | } 307 | } 308 | break; 309 | } 310 | case 200:{ 311 | logSave(message); 312 | break; 313 | } 314 | case 500:{ 315 | String data = datas[3]; 316 | String[] logData = data.split("#"); 317 | FileHandler handler = FileHandler.getInstance(); 318 | if(handler.isExists(logData[1])) { 319 | handler.deleteRecord(logData[1]); 320 | } 321 | handler.addRecord(data); 322 | break; 323 | } 324 | case 399:{ 325 | String fileName = datas[3]; 326 | String uuid = datas[4]; 327 | int chunkLength = Integer.parseInt(datas[5]); 328 | 329 | UnitShared unit = UnitShared.getInstance(fileName, uuid); 330 | unit.setLength(chunkLength); 331 | 332 | Agent agent = Agent.getInstance(); 333 | byte[] requestMessage = DataProcess.messageCreate("REQ", Main.deviceIP, "399", fileName, "success"); 334 | agent.send(address, requestMessage); 335 | break; 336 | } 337 | case 400:{ 338 | String chunk = datas[3]; 339 | String dataid = chunk.substring(0, chunk.indexOf("_")); 340 | byte[] data = getData(originalMessage); 341 | 342 | UnitShared unit = UnitShared.getInstanceUuid(dataid); 343 | unit.receive(address, chunk, data); 344 | 345 | break; 346 | } 347 | default: 348 | System.out.println("message ["+code+"] is undefined "); 349 | } 350 | 351 | } 352 | private void log(String message) { 353 | String datas[] = message.split("::"); 354 | String address = datas[1]; 355 | int requestCode = Integer.parseInt(datas[2]); 356 | logLine("Accept",address); 357 | switch(requestCode) { 358 | case 1: 359 | String type = datas[3]; 360 | switch(type) { 361 | case "EDGE_LIST": logFun("Edge List Update"); break; 362 | case "SLAVE_LIST": logFun("Newest Edge List"); break; 363 | case "DEV_STATUS": logFun("Device Information"); break; 364 | } 365 | break; 366 | case 2: logFun("Whole Data Information"); break; 367 | case 3: { 368 | String dataid = datas[3]; 369 | logData("MetaData Information", dataid); 370 | break; 371 | } 372 | case 6: { 373 | String dataid = datas[3]; 374 | logData("Data Remove", dataid); 375 | break; 376 | } 377 | case 11: 378 | case 111: 379 | case 12: 380 | case 112: 381 | case 13: 382 | case 113: 383 | case 14: 384 | case 15: 385 | case 16:{ 386 | String dataid = datas[3]; 387 | String logMessage[] = { 388 | "create File", 389 | "Update File", 390 | "Delete File", 391 | "Create Directory", 392 | "Update Directory", 393 | "Delete Directory" 394 | }; 395 | int messageIdx = -1; 396 | if(requestCode>100) { 397 | messageIdx = requestCode -111; 398 | } 399 | else { 400 | messageIdx = requestCode -11; 401 | } 402 | logData(logMessage[messageIdx], dataid); 403 | break; 404 | } 405 | case 401:{ 406 | String dataid = datas[3]; 407 | int startIdx = Integer.parseInt(datas[4]); 408 | int finishIdx = Integer.parseInt(datas[5]); 409 | logChunk("Data read", dataid, startIdx, finishIdx); 410 | break; 411 | 412 | } 413 | } 414 | 415 | } 416 | private void logLine(String type, String address) { 417 | Date now = new Date(System.currentTimeMillis()); 418 | System.out.println(" <-- "+logFormat.format(now)+"\tData Processing Request from "+address+" : "+type+" -->"); 419 | 420 | } 421 | private void logFun(String type) { 422 | System.out.println("\tRequest Function : "+type); 423 | } 424 | private void logData(String type, String dataid) { 425 | logFun(type); 426 | System.out.println("\tDataID : "+dataid); 427 | } 428 | private void logChunk(String type, String dataid, int startIdx, int finishIdx) { 429 | logData(type, dataid); 430 | System.out.println("\tchunk : #"+startIdx+" to #"+finishIdx); 431 | } 432 | private boolean shouldLog(int code) { 433 | if(logFilters.contains(code)) { 434 | return false; 435 | } 436 | return true; 437 | } 438 | private boolean shouldRespond(int code) { 439 | if(responseFilters.contains(code)) { 440 | return true; 441 | } 442 | return false; 443 | 444 | } 445 | private byte[] getData(byte[] originalData) { 446 | String message = DataProcess.messageFormat(new String(originalData)); 447 | String[] datas = message.split("::"); 448 | 449 | // int fileSize = Integer.parseInt(datas[4]); 450 | String encodingData = datas[5]; 451 | byte[] data = Base64.getDecoder().decode(encodingData); 452 | // int dataEndIdx = originalData.length -3; 453 | // int dataStartIdx = dataEndIdx - fileSize; 454 | // byte[] data = new byte[fileSize]; 455 | // System.arraycopy(originalData, dataStartIdx, data, 0, fileSize); 456 | 457 | return data; 458 | } 459 | private void logSave(String message) { 460 | String[] datas = message.split("::"); 461 | 462 | String address = datas[1]; 463 | String status = ""; 464 | String metadata = datas[4]; 465 | switch(datas[3]) { 466 | case "011": 467 | case "111": 468 | status = "file create"; break; 469 | case "012": 470 | case "112": 471 | status = "file update"; break; 472 | case "013": status = "file delete"; break; 473 | case "014": status = "directory create"; break; 474 | case "015": status = "directory update"; break; 475 | case "016": status = "directory delete"; break; 476 | case "017": status = "Security Level update"; break; 477 | default: return; 478 | } 479 | FileManagementDto dto = new FileManagementDto(metadata); 480 | } 481 | } 482 | -------------------------------------------------------------------------------- /src/kr/re/keti/agent/AgentPacket.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.agent; 2 | 3 | import java.net.Socket; 4 | import java.util.function.Consumer; 5 | 6 | public class AgentPacket { 7 | private Socket socket; 8 | private String address; 9 | private int port; 10 | private byte[] data; 11 | private Consumer callback; 12 | 13 | public AgentPacket(byte[] data) { 14 | this.data = data; 15 | } 16 | 17 | public AgentPacket(Socket socket, byte[] data) { 18 | super(); 19 | this.socket = socket; 20 | this.data = data; 21 | } 22 | 23 | public AgentPacket(String address, int port, byte[] data) { 24 | super(); 25 | this.address = address; 26 | this.port = port; 27 | this.data = data; 28 | } 29 | 30 | public AgentPacket(Socket socket, String address, int port, byte[] data) { 31 | this.socket = socket; 32 | this.address = address; 33 | this.port = port; 34 | this.data = data; 35 | } 36 | 37 | public Socket getSocket() { 38 | return socket; 39 | } 40 | 41 | public void setSocket(Socket socket) { 42 | this.socket = socket; 43 | } 44 | 45 | public String getAddress() { 46 | return address; 47 | } 48 | 49 | public void setAddress(String address) { 50 | this.address = address; 51 | } 52 | 53 | public int getPort() { 54 | return port; 55 | } 56 | 57 | public void setPort(int port) { 58 | this.port = port; 59 | } 60 | 61 | public byte[] getData() { 62 | return data; 63 | } 64 | 65 | public void setData(byte[] data) { 66 | this.data = data; 67 | } 68 | 69 | public void setCallback(Consumer callback) { 70 | this.callback = callback; 71 | } 72 | 73 | public Consumer getCallback() { 74 | return callback; 75 | } 76 | 77 | } 78 | -------------------------------------------------------------------------------- /src/kr/re/keti/agent/EdgeDataAggregator.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.agent; 2 | 3 | import java.io.IOException; 4 | import java.io.OutputStream; 5 | import java.net.Socket; 6 | import java.text.SimpleDateFormat; 7 | import java.util.concurrent.ArrayBlockingQueue; 8 | import java.util.concurrent.CountDownLatch; 9 | import java.util.concurrent.atomic.AtomicReference; 10 | import java.util.function.Consumer; 11 | 12 | import kr.re.keti.PortNum; 13 | import kr.re.keti.database.Database; 14 | import kr.re.keti.tcp.Client; 15 | import kr.re.keti.tcp.Server; 16 | 17 | abstract class EdgeDataAggregator { 18 | protected final SimpleDateFormat logFormat = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss.SSS"); 19 | private int STANDARD; 20 | private final int CAPACITY = 5000; 21 | protected Database database; 22 | private ArrayBlockingQueue sendQueue; 23 | private ArrayBlockingQueue sendMqttQueue; 24 | private ArrayBlockingQueue sendTcpQueue; 25 | private ArrayBlockingQueue sendKafkaQueue; 26 | private ArrayBlockingQueue receiveQueue; 27 | private Mqtt mqtt; 28 | private Kafka kafka; 29 | private Server ketiServer; 30 | private Server pentaServer; 31 | private Thread sendThread; 32 | private Thread receiveThread; 33 | private Client client; 34 | 35 | public EdgeDataAggregator() { 36 | STANDARD = 5000; 37 | sendQueue = new ArrayBlockingQueue<>(CAPACITY); 38 | receiveQueue = new ArrayBlockingQueue<>(CAPACITY); 39 | sendMqttQueue = new ArrayBlockingQueue<>(CAPACITY); 40 | sendTcpQueue = new ArrayBlockingQueue<>(CAPACITY); 41 | sendKafkaQueue = new ArrayBlockingQueue<>(CAPACITY); 42 | ketiServer = new Server(PortNum.KETI_PORT, receiveQueue); 43 | pentaServer = new Server(PortNum.PENTA_PROT, receiveQueue); 44 | client = new Client(sendTcpQueue); 45 | } 46 | 47 | public void setStandard(int standard) { 48 | this.STANDARD = standard; 49 | } 50 | 51 | public int getStandard() { 52 | return this.STANDARD; 53 | } 54 | 55 | public void start() { 56 | if(sendThread == null || sendThread.getState() == Thread.State.TERMINATED) { 57 | initSendThread(); 58 | sendThread.start(); 59 | } 60 | if(receiveThread == null || receiveThread.getState() == Thread.State.TERMINATED) { 61 | initReceiveThread(); 62 | receiveThread.start(); 63 | } 64 | 65 | mqtt = new Mqtt("/", sendMqttQueue, receiveQueue); 66 | mqtt.start(); 67 | 68 | kafka = new Kafka("keti", sendQueue, receiveQueue); 69 | kafka.start(); 70 | 71 | client.start(); 72 | ketiServer.start(); 73 | pentaServer.start(); 74 | } 75 | 76 | public void stop() { 77 | if(mqtt != null) 78 | mqtt.stop(); 79 | if(kafka != null) 80 | kafka.stop(); 81 | 82 | if(sendThread != null) { 83 | sendThread.interrupt(); 84 | try { 85 | sendThread.join(); 86 | } catch (InterruptedException e) { 87 | e.printStackTrace(); 88 | } 89 | } 90 | if(receiveThread != null) { 91 | receiveThread.interrupt(); 92 | try { 93 | receiveThread.join(); 94 | } catch (InterruptedException e) { 95 | e.printStackTrace(); 96 | } 97 | } 98 | } 99 | 100 | private void initReceiveThread() { 101 | receiveThread = new Thread(() -> { 102 | try { 103 | while (!Thread.currentThread().isInterrupted()) { 104 | AgentPacket packet = receiveQueue.take(); 105 | receive(packet); 106 | } 107 | } catch (InterruptedException e) { 108 | return; 109 | } 110 | }); 111 | receiveThread.setName("sendThread"); 112 | } 113 | 114 | public void addReceive(AgentPacket packet) { 115 | try { 116 | receiveQueue.put(packet); 117 | } catch (InterruptedException e) { 118 | e.printStackTrace(); 119 | } 120 | } 121 | 122 | private void initSendThread() { 123 | sendThread = new Thread(() -> { 124 | try { 125 | while (!Thread.currentThread().isInterrupted()) { 126 | AgentPacket packet = sendQueue.take(); 127 | Socket socket = packet.getSocket(); 128 | String address = packet.getAddress(); 129 | byte[] data = packet.getData(); 130 | int len = data.length; 131 | if(socket != null || address != null) { //TCP 132 | sendTcpQueue.put(packet); 133 | } 134 | else if(len < STANDARD) { // MQTT 135 | sendMqttQueue.put(packet); 136 | } 137 | else if(len >= STANDARD) { // Kafka 138 | sendKafkaQueue.put(packet); 139 | } 140 | } 141 | } catch (InterruptedException e) { 142 | return; 143 | } 144 | }); 145 | sendThread.setName("sendKetiThread"); 146 | } 147 | 148 | public void send(byte[] data) { 149 | AgentPacket packet = new AgentPacket(data); 150 | try { 151 | sendQueue.put(packet); 152 | } catch (InterruptedException e) { 153 | e.printStackTrace(); 154 | } 155 | } 156 | 157 | public byte[] send(String address, byte[] data) { 158 | AtomicReference response = new AtomicReference<>(); 159 | try { 160 | if(address != null) { 161 | CountDownLatch latch = new CountDownLatch(1); 162 | Consumer callback = responseData -> { 163 | response.set(responseData); 164 | latch.countDown(); 165 | }; 166 | int port = portCategorization(new String(data)); 167 | 168 | AgentPacket packet = new AgentPacket(address, port, data); 169 | packet.setCallback(callback); 170 | sendTcpQueue.put(packet); 171 | latch.await(); 172 | } 173 | else { 174 | send(data); 175 | } 176 | 177 | } catch (InterruptedException e) { 178 | e.printStackTrace(); 179 | } 180 | return response.get(); 181 | } 182 | 183 | public void send(Socket socket, byte[] data) { 184 | AgentPacket packet = new AgentPacket(socket, data); 185 | if(socket != null) { 186 | String message = new String(data); 187 | if(message.indexOf("ANS") != -1) { 188 | try { 189 | OutputStream outputStream = socket.getOutputStream(); 190 | outputStream.write(data); 191 | outputStream.flush(); 192 | } catch (IOException e) { 193 | e.printStackTrace(); 194 | } 195 | } 196 | } 197 | else { 198 | try { 199 | sendTcpQueue.put(packet); 200 | } catch (InterruptedException e) { 201 | e.printStackTrace(); 202 | } 203 | } 204 | } 205 | 206 | public void sendThread() { 207 | 208 | } 209 | 210 | abstract int portCategorization(String massage); 211 | 212 | abstract void receive(AgentPacket packet); 213 | 214 | } 215 | -------------------------------------------------------------------------------- /src/kr/re/keti/agent/Kafka.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.agent; 2 | 3 | import java.time.Duration; 4 | import java.util.Arrays; 5 | import java.util.Collections; 6 | import java.util.Properties; 7 | import java.util.concurrent.ArrayBlockingQueue; 8 | 9 | import org.apache.kafka.clients.admin.AdminClient; 10 | import org.apache.kafka.clients.admin.NewTopic; 11 | import org.apache.kafka.clients.consumer.ConsumerConfig; 12 | import org.apache.kafka.clients.consumer.ConsumerRecord; 13 | import org.apache.kafka.clients.consumer.ConsumerRecords; 14 | import org.apache.kafka.clients.consumer.KafkaConsumer; 15 | import org.apache.kafka.clients.producer.Callback; 16 | import org.apache.kafka.clients.producer.KafkaProducer; 17 | import org.apache.kafka.clients.producer.ProducerConfig; 18 | import org.apache.kafka.clients.producer.ProducerRecord; 19 | import org.apache.kafka.clients.producer.RecordMetadata; 20 | import org.apache.kafka.common.serialization.ByteArrayDeserializer; 21 | import org.apache.kafka.common.serialization.ByteArraySerializer; 22 | import org.apache.kafka.common.serialization.StringDeserializer; 23 | import org.apache.kafka.common.serialization.StringSerializer; 24 | import org.slf4j.simple.SimpleLogger; 25 | 26 | import kr.re.keti.Main; 27 | import kr.re.keti.PortNum; 28 | 29 | public class Kafka { 30 | private String topic; 31 | private ArrayBlockingQueue sendQueue; 32 | private ArrayBlockingQueue receiveQueue; 33 | private KafkaProducer producer; 34 | private KafkaConsumer consumer; 35 | private Thread producerThread; 36 | private Thread consumerThread; 37 | private String serverIP; 38 | 39 | public Kafka(String topic, ArrayBlockingQueue sendQueue, ArrayBlockingQueue receiveQueue) { 40 | System.setProperty(SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "OFF"); 41 | 42 | serverIP = Main.masterIP + ":" + PortNum.DEFAULT_KAFKA_PORT; 43 | this.topic = topic; 44 | this.sendQueue = sendQueue; 45 | this.receiveQueue = receiveQueue; 46 | producer(); 47 | consumer(); 48 | } 49 | 50 | public void producer() { 51 | Properties properties = new Properties(); 52 | properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, serverIP); 53 | properties.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName()); 54 | properties.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class.getName()); 55 | 56 | producer = new KafkaProducer<>(properties); 57 | 58 | } 59 | 60 | public void consumer() { 61 | Properties properties = new Properties(); 62 | properties.setProperty(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, serverIP); 63 | properties.setProperty(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class.getName()); 64 | properties.setProperty(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, ByteArrayDeserializer.class.getName()); 65 | properties.setProperty(ConsumerConfig.GROUP_ID_CONFIG, Main.uuid); 66 | // properties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); 67 | properties.setProperty(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "latest"); 68 | properties.put(ConsumerConfig.CLIENT_ID_CONFIG, Main.uuid); 69 | properties.put(ConsumerConfig.GROUP_INSTANCE_ID_CONFIG, (Main.uuid)); 70 | 71 | consumer = new KafkaConsumer<>(properties); 72 | } 73 | 74 | public void admin() { 75 | Properties properties = new Properties(); 76 | properties.put("bootstrap.servers", serverIP); 77 | AdminClient adminClient = AdminClient.create(properties); 78 | 79 | int numPartitions = 3; 80 | short replicationFactor = 1; 81 | 82 | // NewTopic 객체 생성 83 | NewTopic newTopic = new NewTopic(topic, numPartitions, replicationFactor); 84 | 85 | // 토픽 생성 86 | adminClient.createTopics(Collections.singletonList(newTopic)); 87 | 88 | adminClient.close(); 89 | 90 | } 91 | 92 | public void start() { 93 | initThread(); 94 | producerThread.start(); 95 | consumerThread.start(); 96 | } 97 | 98 | public void stop() { 99 | if(producerThread != null) 100 | producerThread.interrupt(); 101 | if(consumerThread != null) 102 | consumerThread.interrupt(); 103 | producer.close(); 104 | consumer.close(); 105 | } 106 | 107 | public void initThread() { 108 | producerThread = new Thread(() -> { 109 | try { 110 | while (!Thread.currentThread().isInterrupted()) { 111 | AgentPacket packet = sendQueue.take(); 112 | byte[] data = packet.getData(); 113 | ProducerRecord producerRecord = new ProducerRecord<>(topic, data); 114 | 115 | producer.send(producerRecord, new Callback() { 116 | @Override 117 | public void onCompletion(RecordMetadata metadata, Exception exception) { 118 | if(exception != null) { 119 | // 데이터 전송이 실패한 경우 120 | // System.err.println("Failed to send data: " + exception.getMessage()); 121 | } 122 | else { 123 | // 데이터 전송이 성공한 경우 124 | // System.out.println("Data sent successfully: " + metadata); 125 | } 126 | } 127 | } 128 | } 129 | } catch (Exception e) { 130 | e.printStackTrace(); 131 | } 132 | } 133 | 134 | consumerThread = new Thread(() -> { 135 | try { 136 | consumer.subscribe(Arrays.asList(topic)); 137 | while (!Thread.currentThread().isInterrupted()) { 138 | ConsumerRecords records = consumer.poll(Duration.ofMillis(100)); 139 | for (ConsumerRecord record : records) { 140 | byte[] data = record.value(); 141 | AgentPacket packet = new AgentPacket(null, data); 142 | receiveQueue.put(packet); 143 | } 144 | } 145 | } catch (Exception e) { 146 | e.printStackTrace(); 147 | } 148 | } 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /src/kr/re/keti/agent/Mqtt.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.agent; 2 | 3 | import java.util.concurrent.ArrayBlockingQueue; 4 | 5 | import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken; 6 | import org.eclipse.paho.client.mqttv3.MqttCallback; 7 | import org.eclipse.paho.client.mqttv3.MqttClient; 8 | import org.eclipse.paho.client.mqttv3.MqttConnectOptions; 9 | import org.eclipse.paho.client.mqttv3.MqttException; 10 | import org.eclipse.paho.client.mqttv3.MqttMessage; 11 | 12 | import kr.re.keti.Main; 13 | import kr.re.keti.PortNum; 14 | 15 | public class Mqtt { 16 | private MqttClient client; 17 | private String topic; 18 | private String clientId = MqttClient.generateClientId(); 19 | private ArrayBlockingQueue sendQueue, receiveQueue; 20 | private Thread publishThread; 21 | private String address; 22 | 23 | public Mqtt(String topic, ArrayBlockingQueue sendQueue, ArrayBlockingQueue receiveQueue) { 24 | this.topic = topic; 25 | this.sendQueue = sendQueue; 26 | this.receiveQueue = receiveQueue; 27 | address = "tcp://" + Main.masterIP + ":" + PortNum.DEFAULT_MQTT_PORT; 28 | try { 29 | client = new MqttClient(address, clientId); 30 | } catch (Exception e) { 31 | e.printStackTrace(); 32 | } 33 | process(); 34 | 35 | } 36 | 37 | private void process() { 38 | client.setCallback(new MqttCallback() { 39 | @Override 40 | public void connectionLost(Throwable cause) { 41 | System.out.println("Connection lost"); 42 | } 43 | 44 | @Override 45 | public void messageArrived(String topic, MqttMessage message) throws Exception { 46 | String data = message + ""; 47 | AgentPacket packet = new AgentPacket(data.getBytes()); 48 | receiveQueue.put(packet); 49 | } 50 | 51 | @Override 52 | public void deliveryComplete(IMqttDeliveryToken token) { 53 | // System.out.println("Delivery complete"); 54 | } 55 | } 56 | } 57 | 58 | public void start() { 59 | try { 60 | MqttConnectOptions options = new MqttConnectOptions(); 61 | options.setCleanSession(true); 62 | options.setAutomaticReconnect(false); 63 | client.connect(options); 64 | client.subscribe(topic); 65 | } catch (Exception e) { 66 | e.printStackTrace(); 67 | } 68 | 69 | publishThread = new Thread(() -> { 70 | while (!Thread.currentThread().isInterrupted()) { 71 | try { 72 | AgentPacket packet = sendQueue.take(); 73 | byte[] data = packet.getData(); 74 | publish(topic, new String(data)); 75 | } catch (Exception e) { 76 | e.printStackTrace(); 77 | } 78 | } 79 | 80 | } 81 | publishThread.setName("MQTT_publish_Thread"); 82 | publishThread.start(); 83 | 84 | } 85 | 86 | public void stop() { 87 | publishThread.interrupt(); 88 | try { 89 | client.close(); 90 | } catch (Exception e) { 91 | e.printStackTrace(); 92 | } 93 | } 94 | 95 | public void publish(String topic, String message) throws MqttException { 96 | MqttMessage mqttMessage = new MqttMessage(); 97 | mqttMessage.setPayload(message.getBytes()); 98 | client.publish(topic, mqttMessage); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /src/kr/re/keti/database/Database.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.database; 2 | 3 | import java.sql.Connection; 4 | import java.util.ArrayList; 5 | 6 | public interface Database { 7 | public Connection getConnection(); 8 | public boolean exists(String table, String pk); 9 | public boolean update(Object ObjectDto); 10 | public boolean delete(String table, String pk); 11 | public boolean insert(Object objectDto); 12 | public ArrayList select(String table); 13 | public Object select(String table, String pk); 14 | public ArrayList executeQuery(String query); 15 | public boolean executeUpdate(String query); 16 | } 17 | -------------------------------------------------------------------------------- /src/kr/re/keti/database/FileManagementDto.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.database; 2 | 3 | import java.io.File; 4 | import java.sql.Timestamp; 5 | import java.time.LocalDateTime; 6 | 7 | import kr.re.keti.Main; 8 | 9 | public class FileManagementDto{ 10 | private String dataId; 11 | private int availabilityPolicy; 12 | private String cert; 13 | private int dataPriority; 14 | private String dataSign; 15 | private long dataSize; 16 | private int dataType; 17 | private String directory; 18 | private String fileType; 19 | private String linkedEdge; 20 | private int securityLevel; 21 | private Timestamp timestamp; 22 | 23 | public FileManagementDto() { 24 | } 25 | public FileManagementDto(String dataId, int availabilityPolicy, String cert, int dataPriority, String dataSign, 26 | long dataSize, int dataType, String directory, String fileType, String linkedEdge, int securityLevel, 27 | Timestamp timestamp) { 28 | super(); 29 | this.dataId = dataId; 30 | this.availabilityPolicy = availabilityPolicy; 31 | this.cert = cert; 32 | this.dataPriority = dataPriority; 33 | this.dataSign = dataSign; 34 | this.dataSize = dataSize; 35 | this.dataType = dataType; 36 | this.directory = directory; 37 | this.fileType = fileType; 38 | this.linkedEdge = linkedEdge; 39 | this.securityLevel = securityLevel; 40 | this.timestamp = timestamp; 41 | } 42 | public FileManagementDto(File file, String signature) { 43 | int dotIndex = file.getName().lastIndexOf("."); 44 | this.dataId = (dotIndex > 0) ? file.getName().substring(0, dotIndex) : file.getName(); 45 | this.availabilityPolicy = 1; 46 | this.cert = Main.certFolder; 47 | this.dataPriority = 0; 48 | this.dataSign = signature; 49 | this.dataSize = (long) Math.ceil((double) file.length() / 1000); 50 | this.dataType = 1; 51 | this.directory = file.getParent(); 52 | this.fileType = file.getName().substring(file.getName().lastIndexOf(".") + 1); 53 | this.linkedEdge = null; 54 | this.securityLevel = 1; 55 | this.timestamp = Timestamp.valueOf(LocalDateTime.now()); 56 | } 57 | public String toString() { 58 | String result = dataId+"#"+timestamp+"#"+fileType+"#"+dataType+"#"+securityLevel+"#"+dataPriority+"#" 59 | +availabilityPolicy+"#"+dataSign+"#"+cert+"#"+directory+"#"+linkedEdge+"#"+dataSize; 60 | return result; 61 | } 62 | 63 | public void setDataId(String dataId) { 64 | this.dataId = dataId; 65 | } 66 | public String getDataId() { 67 | return dataId; 68 | } 69 | 70 | public int getAvailabilityPolicy() { 71 | return availabilityPolicy; 72 | } 73 | 74 | public void setAvailabilityPolicy(int availabilityPolicy) { 75 | this.availabilityPolicy = availabilityPolicy; 76 | } 77 | 78 | public String getCert() { 79 | return cert; 80 | } 81 | 82 | public void setCert(String cert) { 83 | this.cert = cert; 84 | } 85 | 86 | public int getDataPriority() { 87 | return dataPriority; 88 | } 89 | 90 | public void setDataPriority(int dataPriority) { 91 | this.dataPriority = dataPriority; 92 | } 93 | 94 | public String getDataSign() { 95 | return dataSign; 96 | } 97 | 98 | public void setDataSign(String dataSign) { 99 | this.dataSign = dataSign; 100 | } 101 | 102 | public long getDataSize() { 103 | return dataSize; 104 | } 105 | 106 | public void setDataSize(long dataSize) { 107 | this.dataSize = dataSize; 108 | } 109 | 110 | public int getDataType() { 111 | return dataType; 112 | } 113 | 114 | public void setDataType(int dataType) { 115 | this.dataType = dataType; 116 | } 117 | 118 | public String getDirectory() { 119 | return directory; 120 | } 121 | 122 | public void setDirectory(String directory) { 123 | this.directory = directory; 124 | } 125 | 126 | public String getFileType() { 127 | return fileType; 128 | } 129 | 130 | public void setFileType(String fileType) { 131 | this.fileType = fileType; 132 | } 133 | 134 | public String getLinkedEdge() { 135 | return linkedEdge; 136 | } 137 | 138 | public void setLinkedEdge(String linkedEdge) { 139 | this.linkedEdge = linkedEdge; 140 | } 141 | 142 | public int getSecurityLevel() { 143 | return securityLevel; 144 | } 145 | 146 | public void setSecurityLevel(int securityLevel) { 147 | this.securityLevel = securityLevel; 148 | } 149 | 150 | public Timestamp getTimestamp() { 151 | return timestamp; 152 | } 153 | 154 | public void setTimestamp(Timestamp timestamp) { 155 | this.timestamp = timestamp; 156 | } 157 | 158 | } 159 | -------------------------------------------------------------------------------- /src/kr/re/keti/database/FileUuidDto.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.database; 2 | 3 | public class FileUuidDto { 4 | String fileName; 5 | String fileUuid; 6 | public FileUuidDto() { } 7 | public FileUuidDto(String fileName, String fileUuid) { 8 | super(); 9 | this.fileName = fileName; 10 | this.fileUuid = fileUuid; 11 | } 12 | public String getFileName() { 13 | return fileName; 14 | } 15 | public void setFileName(String fileName) { 16 | this.fileName = fileName; 17 | } 18 | public String getFileUuid() { 19 | return fileUuid; 20 | } 21 | public void setFileUuid(String fileUuid) { 22 | this.fileUuid = fileUuid; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/kr/re/keti/database/MysqlDao.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.database; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.SQLException; 8 | import java.sql.Statement; 9 | import java.util.ArrayList; 10 | 11 | public class MysqlDao implements Database { 12 | private String url; 13 | private String id; 14 | private String pw; 15 | 16 | public MysqlDao(String databaseName, String id, String pw) { 17 | this.url = "jdbc:mysql://localhost:3306/" + databaseName + "?serverTimezone=UTC&autoReconnect=true"; 18 | this.id = id; 19 | this.pw = pw; 20 | } 21 | 22 | @Override 23 | public Connection getConnection() { 24 | Connection connection = null; 25 | 26 | try { 27 | Class.forName("com.mysql.cj.jdbc.Driver"); 28 | connection = DriverManager.getConnection(url, id, pw); 29 | } catch (Exception e) { 30 | e.printStackTrace(); 31 | } 32 | 33 | return connection; 34 | } 35 | 36 | @Override 37 | public boolean exists(String table, String pk) { 38 | boolean result = false; 39 | 40 | if(table.equals("file_management")) { 41 | result = existsFileManagement(pk); 42 | } 43 | else if(table.equals("file_uuid")) { 44 | result = existsFileUuid(pk); 45 | } 46 | else { 47 | System.out.println("*** exists Invalid table name [" + table + "] ***"); 48 | } 49 | return result; 50 | } 51 | 52 | @Override 53 | public boolean insert(Object objectDto) { 54 | boolean result = false; 55 | if(objectDto instanceof FileManagementDto) { 56 | FileManagementDto dto = (FileManagementDto) objectDto; 57 | result = insertFileManagement(dto); 58 | } 59 | else if(objectDto instanceof FileUuidDto) { 60 | FileUuidDto dto = (FileUuidDto) objectDto; 61 | result = insertFileUuid(dto); 62 | } 63 | else { 64 | System.out.println("*** insert an unsupported DTO type into the database. ***"); 65 | } 66 | return result; 67 | } 68 | 69 | @Override 70 | public ArrayList select(String table) { 71 | ArrayList dtos = new ArrayList<>(); 72 | 73 | if(table.equals("file_management")) { 74 | dtos.addAll(selectFileManagement()); 75 | } 76 | else if(table.equals("file_uuid")) { 77 | dtos.addAll(selectFileUuid()); 78 | } 79 | else { 80 | System.out.println("*** select Invalid table name [" + table + "] ***"); 81 | } 82 | 83 | return dtos; 84 | 85 | } 86 | 87 | @Override 88 | public Object select(String table, String pk) { 89 | Object dto = null; 90 | if(table.equals("file_management")) { 91 | dto = selectFileManagement(pk); 92 | } 93 | else if(table.equals("file_uuid")) { 94 | dto = selectFileUuid(pk); 95 | } 96 | else { 97 | System.out.println("*** select Invalid table name [" + table + "] ***"); 98 | } 99 | return dto; 100 | } 101 | 102 | @Override 103 | public boolean update(Object objectDto) { 104 | boolean result = false; 105 | if(objectDto instanceof FileManagementDto) { 106 | result = updateFileManagement((FileManagementDto) objectDto); 107 | } 108 | else if(objectDto instanceof FileUuidDto) { 109 | result = updateFileUuid((FileUuidDto) objectDto); 110 | } 111 | else { 112 | System.out.println("*** update an unsupported DTO type into the database. ***"); 113 | } 114 | return result; 115 | } 116 | 117 | @Override 118 | public boolean delete(String table, String pk) { 119 | boolean result = false; 120 | 121 | if(table.equals("file_management")) { 122 | result = deleteFileManagement(pk); 123 | } 124 | else if(table.equals("file_uuid")) { 125 | result = deleteFileUuid(pk); 126 | } 127 | else { 128 | System.out.println("*** delete Invalid table name [" + table + "] ***"); 129 | } 130 | 131 | return result; 132 | } 133 | 134 | @Override 135 | public ArrayList executeQuery(String query) { 136 | ArrayList dtos = new ArrayList<>(); 137 | 138 | if(query.indexOf("from file_management") != -1) { 139 | dtos.addAll(excuteQueryFileManagement(query)); 140 | 141 | } 142 | else if(query.indexOf("from file_uuid") != -1) { 143 | dtos.addAll(excuteQueryFileUuid(query)); 144 | 145 | } 146 | else { 147 | System.out.println("*** query Invalid ***"); 148 | } 149 | return dtos; 150 | } 151 | 152 | @Override 153 | public boolean executeUpdate(String query) { 154 | try (Connection connection = getConnection(); Statement statement = connection.createStatement();) { 155 | 156 | int check = statement.executeUpdate(query); 157 | if(check == 0) { 158 | System.out.println("Database [" + query + "]: fail"); 159 | } 160 | else { 161 | return true; 162 | } 163 | } catch (Exception e) { 164 | e.printStackTrace(); 165 | } 166 | return false; 167 | } 168 | 169 | // ========================================================================================== 170 | 171 | private boolean existsFileManagement(String pk) { 172 | String query = "select dataid from file_management where dataid='" + pk + "'"; 173 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 174 | if(resultSet.next()) { 175 | return true; 176 | } 177 | } catch (Exception e) { 178 | e.printStackTrace(); 179 | } 180 | return false; 181 | } 182 | 183 | private boolean existsFileUuid(String pk) { 184 | String query = "select dataid from file_uuid whare fileName='" + pk + "'"; 185 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 186 | if(resultSet.next()) { 187 | return true; 188 | } 189 | } catch (Exception e) { 190 | e.printStackTrace(); 191 | } 192 | return false; 193 | } 194 | 195 | private boolean insertFileManagement(FileManagementDto dto) { 196 | String insert_sql = "insert into " + "file_management" + " (dataid, availability_policy, cert, data_priority, data_signature, data_size, data_type, directory, file_type, linked_edge, security_level, timestamp)" + " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 197 | try (Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement(insert_sql);) { 198 | pstmt.setString(1, dto.getDataId()); 199 | pstmt.setInt(2, dto.getAvailabilityPolicy()); 200 | pstmt.setString(3, dto.getCert()); 201 | pstmt.setInt(4, dto.getDataPriority()); 202 | pstmt.setString(5, dto.getDataSign()); 203 | pstmt.setLong(6, dto.getDataSize()); 204 | pstmt.setInt(7, dto.getDataType()); 205 | pstmt.setString(8, dto.getDirectory()); 206 | pstmt.setString(9, dto.getFileType()); 207 | pstmt.setString(10, dto.getLinkedEdge()); 208 | pstmt.setInt(11, dto.getSecurityLevel()); 209 | pstmt.setTimestamp(12, dto.getTimestamp()); 210 | int check = pstmt.executeUpdate(); 211 | if(check == 0) { 212 | System.out.println("Data '" + dto.getDataId() + "' insert fail"); 213 | } 214 | else { 215 | return true; 216 | } 217 | } catch (Exception e) { 218 | e.printStackTrace(); 219 | } 220 | return false; 221 | 222 | } 223 | 224 | private boolean insertFileUuid(FileUuidDto dto) { 225 | String insert_sql = "insert into " + "file_uuid" + " (fileName, fileUuid)" + " values(?, ?)"; 226 | try (Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement(insert_sql);) { 227 | pstmt.setString(1, dto.getFileName()); 228 | pstmt.setString(2, dto.getFileUuid()); 229 | int check = pstmt.executeUpdate(); 230 | if(check == 0) { 231 | System.out.println("Data '" + dto.getFileName() + "' insert fail"); 232 | } 233 | else { 234 | return true; 235 | } 236 | } catch (Exception e) { 237 | e.printStackTrace(); 238 | } 239 | return false; 240 | } 241 | 242 | private ArrayList selectFileManagement() { 243 | ArrayList dtos = new ArrayList<>(); 244 | String query = "select * from file_management"; 245 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 246 | while (resultSet.next()) { 247 | FileManagementDto dto = new FileManagementDto(); 248 | dto.setDataId(resultSet.getString("dataid")); 249 | dto.setAvailabilityPolicy(resultSet.getInt("availability_policy")); 250 | dto.setCert(resultSet.getString("cert")); 251 | dto.setDataPriority(resultSet.getInt("data_priority")); 252 | dto.setDataSign(resultSet.getString("data_signature")); 253 | dto.setDataSize(resultSet.getLong("data_size")); 254 | dto.setDataType(resultSet.getInt("data_type")); 255 | dto.setDirectory(resultSet.getString("directory")); 256 | dto.setFileType(resultSet.getString("file_type")); 257 | dto.setLinkedEdge(resultSet.getString("linked_edge")); 258 | dto.setSecurityLevel(resultSet.getInt("security_level")); 259 | dto.setTimestamp(resultSet.getTimestamp("timestamp")); 260 | dtos.add(dto); 261 | } 262 | } catch (Exception e) { 263 | e.printStackTrace(); 264 | } 265 | return dtos; 266 | } 267 | 268 | public FileManagementDto selectFileManagement(String pk) { 269 | String query = "select * from " + "file_management" + " where dataID='" + pk + "'"; 270 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 271 | if(resultSet.next()) { 272 | FileManagementDto dto = new FileManagementDto(); 273 | dto.setDataId(resultSet.getString("dataid")); 274 | dto.setAvailabilityPolicy(resultSet.getInt("availability_policy")); 275 | dto.setCert(resultSet.getString("cert")); 276 | dto.setDataPriority(resultSet.getInt("data_priority")); 277 | dto.setDataSign(resultSet.getString("data_signature")); 278 | dto.setDataSize(resultSet.getLong("data_size")); 279 | dto.setDataType(resultSet.getInt("data_type")); 280 | dto.setDirectory(resultSet.getString("directory")); 281 | dto.setFileType(resultSet.getString("file_type")); 282 | dto.setLinkedEdge(resultSet.getString("linked_edge")); 283 | dto.setSecurityLevel(resultSet.getInt("security_level")); 284 | dto.setTimestamp(resultSet.getTimestamp("timestamp")); 285 | return dto; 286 | } 287 | else { 288 | } 289 | } catch (Exception e) { 290 | e.printStackTrace(); 291 | } 292 | return null; 293 | } 294 | 295 | private ArrayList selectFileUuid() { 296 | ArrayList dtos = new ArrayList<>(); 297 | String query = "select * from file_uuid"; 298 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 299 | while (resultSet.next()) { 300 | FileUuidDto dto = new FileUuidDto(); 301 | dto.setFileName(resultSet.getString("fileName")); 302 | dto.setFileUuid(resultSet.getString("fileUuid")); 303 | dtos.add(dto); 304 | } 305 | } catch (Exception e) { 306 | e.printStackTrace(); 307 | } 308 | return dtos; 309 | } 310 | 311 | private FileUuidDto selectFileUuid(String pk) { 312 | String query = "select * from " + "file_uuid" + " where dataID='" + pk + "'"; 313 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 314 | if(resultSet.next()) { 315 | FileUuidDto dto = new FileUuidDto(); 316 | dto.setFileName(resultSet.getString("fileName")); 317 | dto.setFileUuid(resultSet.getString("fileUuid")); 318 | return dto; 319 | } 320 | else { 321 | } 322 | } catch (Exception e) { 323 | e.printStackTrace(); 324 | } 325 | return null; 326 | } 327 | 328 | private boolean updateFileManagement(FileManagementDto dto) { 329 | String query = "update file_management" + " set availability_policy=?, cert=?, data_priority=?, data_signature=?, data_size=?, data_type=?, " + "directory=?, file_type=?, linked_edge=?, security_level=?, timestamp=? " + "where dataId=?"; 330 | try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(query);) { 331 | statement.setInt(1, dto.getAvailabilityPolicy()); 332 | statement.setString(2, dto.getCert()); 333 | statement.setInt(3, dto.getDataPriority()); 334 | statement.setString(4, dto.getDataSign()); 335 | statement.setLong(5, dto.getDataSize()); 336 | statement.setInt(6, dto.getDataType()); 337 | statement.setString(7, dto.getDirectory()); 338 | statement.setString(8, dto.getFileType()); 339 | statement.setString(9, dto.getLinkedEdge()); 340 | statement.setInt(10, dto.getSecurityLevel()); 341 | statement.setTimestamp(11, dto.getTimestamp()); 342 | statement.setString(12, dto.getDataId()); 343 | 344 | int check = statement.executeUpdate(); 345 | if(check == 0) { 346 | System.out.println("Database '" + dto.getDataId() + "' update fail"); 347 | } 348 | else 349 | return true; 350 | } catch (Exception e) { 351 | e.printStackTrace(); 352 | } 353 | return false; 354 | 355 | } 356 | 357 | private boolean updateFileUuid(FileUuidDto dto) { 358 | String query = "update file_uuid set fileName=?, fileUuid=?"; 359 | try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(query);) { 360 | statement.setString(1, dto.getFileName()); 361 | statement.setString(2, dto.getFileUuid()); 362 | 363 | int check = statement.executeUpdate(); 364 | if(check == 0) { 365 | System.out.println("Database '" + dto.getFileName() + "' update fail"); 366 | } 367 | else 368 | return true; 369 | } catch (Exception e) { 370 | e.printStackTrace(); 371 | } 372 | return false; 373 | } 374 | 375 | private boolean deleteFileManagement(String pk) { 376 | String query = "delete from file_management where dataId='" + pk + "'"; 377 | try (Connection connection = getConnection(); Statement statement = connection.createStatement();) { 378 | int check = statement.executeUpdate(query); 379 | if(check == 0) { 380 | System.out.println("Database '" + pk + "' delete fail"); 381 | } 382 | else { 383 | return true; 384 | } 385 | } catch (Exception e) { 386 | e.printStackTrace(); 387 | } 388 | return false; 389 | } 390 | 391 | private boolean deleteFileUuid(String pk) { 392 | String query = "delete from file_Uuid where dataId='" + pk + "'"; 393 | try (Connection connection = getConnection(); Statement statement = connection.createStatement();) { 394 | int check = statement.executeUpdate(query); 395 | if(check == 0) { 396 | System.out.println("Database '" + pk + "' delete fail"); 397 | } 398 | else { 399 | return true; 400 | } 401 | } catch (Exception e) { 402 | e.printStackTrace(); 403 | } 404 | return false; 405 | } 406 | 407 | private ArrayList excuteQueryFileManagement(String query) { 408 | ArrayList dtos = new ArrayList<>(); 409 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 410 | while (resultSet.next()) { 411 | FileManagementDto dto = new FileManagementDto(); 412 | dto.setDataId(resultSet.getString("dataid")); 413 | dto.setAvailabilityPolicy(resultSet.getInt("availability_policy")); 414 | dto.setCert(resultSet.getString("cert")); 415 | dto.setDataPriority(resultSet.getInt("data_priority")); 416 | dto.setDataSign(resultSet.getString("data_signature")); 417 | dto.setDataSize(resultSet.getLong("data_size")); 418 | dto.setDataType(resultSet.getInt("data_type")); 419 | dto.setDirectory(resultSet.getString("directory")); 420 | dto.setFileType(resultSet.getString("file_type")); 421 | dto.setLinkedEdge(resultSet.getString("linked_edge")); 422 | dto.setSecurityLevel(resultSet.getInt("security_level")); 423 | dto.setTimestamp(resultSet.getTimestamp("timestamp")); 424 | dtos.add(dto); 425 | } 426 | } catch (Exception e) { 427 | e.printStackTrace(); 428 | } 429 | return dtos; 430 | } 431 | 432 | private ArrayList excuteQueryFileUuid(String query) { 433 | ArrayList dtos = new ArrayList<>(); 434 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 435 | while (resultSet.next()) { 436 | FileUuidDto dto = new FileUuidDto(); 437 | dto.setFileName(resultSet.getString("fileName")); 438 | dto.setFileUuid(resultSet.getString("fileUuid")); 439 | dtos.add(dto); 440 | } 441 | } catch (Exception e) { 442 | e.printStackTrace(); 443 | } 444 | return dtos; 445 | } 446 | } 447 | -------------------------------------------------------------------------------- /src/kr/re/keti/database/SqliteDao.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.database; 2 | 3 | import java.sql.Connection; 4 | import java.sql.DriverManager; 5 | import java.sql.PreparedStatement; 6 | import java.sql.ResultSet; 7 | import java.sql.Statement; 8 | import java.util.ArrayList; 9 | 10 | public class SqliteDao implements Database { 11 | private String path; 12 | 13 | public SqliteDao(String path, String databaseName) { 14 | this.path = "jdbc:sqlite:" + path + databaseName + ".db"; 15 | } 16 | 17 | @Override 18 | public Connection getConnection() { 19 | Connection connection = null; 20 | try { 21 | Class.forName("org.sqlite.JDBC"); 22 | connection = DriverManager.getConnection(path); 23 | } catch (Exception e) { 24 | e.printStackTrace(); 25 | } 26 | return connection; 27 | } 28 | 29 | @Override 30 | public boolean exists(String table, String pk) { 31 | boolean result = false; 32 | 33 | if(table.equals("file_management")) { 34 | result = existsFileManagement(pk); 35 | } 36 | else if(table.equals("file_uuid")) { 37 | result = existsFileUuid(pk); 38 | } 39 | else { 40 | System.out.println("*** exists Invalid table name [" + table + "] ***"); 41 | } 42 | return result; 43 | } 44 | 45 | @Override 46 | public boolean insert(Object objectDto) { 47 | boolean result = false; 48 | if(objectDto instanceof FileManagementDto) { 49 | FileManagementDto dto = (FileManagementDto) objectDto; 50 | result = insertFileManagement(dto); 51 | } 52 | else if(objectDto instanceof FileUuidDto) { 53 | FileUuidDto dto = (FileUuidDto) objectDto; 54 | result = insertFileUuid(dto); 55 | } 56 | else { 57 | System.out.println("*** insert an unsupported DTO type into the database. ***"); 58 | } 59 | return result; 60 | } 61 | 62 | @Override 63 | public ArrayList select(String table) { 64 | ArrayList dtos = new ArrayList<>(); 65 | 66 | if(table.equals("file_management")) { 67 | dtos.addAll(selectFileManagement()); 68 | } 69 | else if(table.equals("file_uuid")) { 70 | dtos.addAll(selectFileUuid()); 71 | } 72 | else { 73 | System.out.println("*** select Invalid table name [" + table + "] ***"); 74 | } 75 | 76 | return dtos; 77 | 78 | } 79 | 80 | @Override 81 | public Object select(String table, String pk) { 82 | Object dto = null; 83 | if(table.equals("file_management")) { 84 | dto = selectFileManagement(pk); 85 | } 86 | else if(table.equals("file_uuid")) { 87 | dto = selectFileUuid(pk); 88 | } 89 | else { 90 | System.out.println("*** select Invalid table name [" + table + "] ***"); 91 | } 92 | return dto; 93 | } 94 | 95 | @Override 96 | public boolean update(Object objectDto) { 97 | boolean result = false; 98 | if(objectDto instanceof FileManagementDto) { 99 | result = updateFileManagement((FileManagementDto) objectDto); 100 | } 101 | else if(objectDto instanceof FileUuidDto) { 102 | result = updateFileUuid((FileUuidDto) objectDto); 103 | } 104 | else { 105 | System.out.println("*** update an unsupported DTO type into the database. ***"); 106 | } 107 | return result; 108 | } 109 | 110 | @Override 111 | public boolean delete(String table, String pk) { 112 | boolean result = false; 113 | 114 | if(table.equals("file_management")) { 115 | result = deleteFileManagement(pk); 116 | } 117 | else if(table.equals("file_uuid")) { 118 | result = deleteFileUuid(pk); 119 | } 120 | else { 121 | System.out.println("*** delete Invalid table name [" + table + "] ***"); 122 | } 123 | 124 | return result; 125 | } 126 | 127 | @Override 128 | public ArrayList executeQuery(String query) { 129 | ArrayList dtos = new ArrayList<>(); 130 | 131 | if(query.indexOf("from file_management") != -1) { 132 | dtos.addAll(excuteQueryFileManagement(query)); 133 | 134 | } 135 | else if(query.indexOf("from file_uuid") != -1) { 136 | dtos.addAll(excuteQueryFileUuid(query)); 137 | 138 | } 139 | else { 140 | System.out.println("*** query Invalid ***"); 141 | } 142 | return dtos; 143 | } 144 | 145 | @Override 146 | public boolean executeUpdate(String query) { 147 | try (Connection connection = getConnection(); Statement statement = connection.createStatement();) { 148 | 149 | int check = statement.executeUpdate(query); 150 | if(check == 0) { 151 | System.out.println("Database [" + query + "]: fail"); 152 | } 153 | else { 154 | return true; 155 | } 156 | } catch (Exception e) { 157 | e.printStackTrace(); 158 | } 159 | return false; 160 | } 161 | 162 | //========================================================================================== 163 | 164 | private boolean existsFileManagement(String pk) { 165 | String query = "select dataid from file_management where dataid='" + pk + "'"; 166 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 167 | if(resultSet.next()) { 168 | return true; 169 | } 170 | } catch (Exception e) { 171 | e.printStackTrace(); 172 | } 173 | return false; 174 | } 175 | 176 | private boolean existsFileUuid(String pk) { 177 | String query = "select dataid from file_uuid whare fileName='" + pk + "'"; 178 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 179 | if(resultSet.next()) { 180 | return true; 181 | } 182 | } catch (Exception e) { 183 | e.printStackTrace(); 184 | } 185 | return false; 186 | } 187 | 188 | private boolean insertFileManagement(FileManagementDto dto) { 189 | 190 | String insert_sql = "insert into " + "file_management" + " (dataid, availability_policy, cert, data_priority, data_signature, data_size, data_type, directory, file_type, linked_edge, security_level, timestamp)" + " values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; 191 | try (Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement(insert_sql);) { 192 | pstmt.setString(1, dto.getDataId()); 193 | pstmt.setInt(2, dto.getAvailabilityPolicy()); 194 | pstmt.setString(3, dto.getCert()); 195 | pstmt.setInt(4, dto.getDataPriority()); 196 | pstmt.setString(5, dto.getDataSign()); 197 | pstmt.setLong(6, dto.getDataSize()); 198 | pstmt.setInt(7, dto.getDataType()); 199 | pstmt.setString(8, dto.getDirectory()); 200 | pstmt.setString(9, dto.getFileType()); 201 | pstmt.setString(10, dto.getLinkedEdge()); 202 | pstmt.setInt(11, dto.getSecurityLevel()); 203 | pstmt.setTimestamp(12, dto.getTimestamp()); 204 | int check = pstmt.executeUpdate(); 205 | if(check == 0) { 206 | System.out.println("Data '" + dto.getDataId() + "' insert fail"); 207 | } 208 | else { 209 | return true; 210 | } 211 | } catch (Exception e) { 212 | e.printStackTrace(); 213 | } 214 | return false; 215 | 216 | } 217 | 218 | private boolean insertFileUuid(FileUuidDto dto) { 219 | String insert_sql = "insert into " + "file_uuid" + " (fileName, fileUuid)" + " values(?, ?)"; 220 | try (Connection connection = getConnection(); PreparedStatement pstmt = connection.prepareStatement(insert_sql);) { 221 | pstmt.setString(1, dto.getFileName()); 222 | pstmt.setString(2, dto.getFileUuid()); 223 | int check = pstmt.executeUpdate(); 224 | if(check == 0) { 225 | System.out.println("Data '" + dto.getFileName() + "' insert fail"); 226 | } 227 | else { 228 | return true; 229 | } 230 | } catch (Exception e) { 231 | e.printStackTrace(); 232 | } 233 | return false; 234 | } 235 | 236 | private ArrayList selectFileManagement() { 237 | ArrayList dtos = new ArrayList<>(); 238 | String query = "select * from file_management"; 239 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 240 | while (resultSet.next()) { 241 | FileManagementDto dto = new FileManagementDto(); 242 | dto.setDataId(resultSet.getString("dataid")); 243 | dto.setAvailabilityPolicy(resultSet.getInt("availability_policy")); 244 | dto.setCert(resultSet.getString("cert")); 245 | dto.setDataPriority(resultSet.getInt("data_priority")); 246 | dto.setDataSign(resultSet.getString("data_signature")); 247 | dto.setDataSize(resultSet.getLong("data_size")); 248 | dto.setDataType(resultSet.getInt("data_type")); 249 | dto.setDirectory(resultSet.getString("directory")); 250 | dto.setFileType(resultSet.getString("file_type")); 251 | dto.setLinkedEdge(resultSet.getString("linked_edge")); 252 | dto.setSecurityLevel(resultSet.getInt("security_level")); 253 | dto.setTimestamp(resultSet.getTimestamp("timestamp")); 254 | dtos.add(dto); 255 | } 256 | } catch (Exception e) { 257 | e.printStackTrace(); 258 | } 259 | return dtos; 260 | } 261 | 262 | public FileManagementDto selectFileManagement(String pk) { 263 | String query = "select * from " + "file_management" + " where dataID='" + pk + "'"; 264 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 265 | if(resultSet.next()) { 266 | FileManagementDto dto = new FileManagementDto(); 267 | dto.setDataId(resultSet.getString("dataid")); 268 | dto.setAvailabilityPolicy(resultSet.getInt("availability_policy")); 269 | dto.setCert(resultSet.getString("cert")); 270 | dto.setDataPriority(resultSet.getInt("data_priority")); 271 | dto.setDataSign(resultSet.getString("data_signature")); 272 | dto.setDataSize(resultSet.getLong("data_size")); 273 | dto.setDataType(resultSet.getInt("data_type")); 274 | dto.setDirectory(resultSet.getString("directory")); 275 | dto.setFileType(resultSet.getString("file_type")); 276 | dto.setLinkedEdge(resultSet.getString("linked_edge")); 277 | dto.setSecurityLevel(resultSet.getInt("security_level")); 278 | dto.setTimestamp(resultSet.getTimestamp("timestamp")); 279 | return dto; 280 | } 281 | else { 282 | } 283 | } catch (Exception e) { 284 | e.printStackTrace(); 285 | } 286 | return null; 287 | } 288 | 289 | private ArrayList selectFileUuid() { 290 | ArrayList dtos = new ArrayList<>(); 291 | String query = "select * from file_uuid"; 292 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 293 | while (resultSet.next()) { 294 | FileUuidDto dto = new FileUuidDto(); 295 | dto.setFileName(resultSet.getString("fileName")); 296 | dto.setFileUuid(resultSet.getString("fileUuid")); 297 | dtos.add(dto); 298 | } 299 | } catch (Exception e) { 300 | e.printStackTrace(); 301 | } 302 | return dtos; 303 | } 304 | 305 | private FileUuidDto selectFileUuid(String pk) { 306 | String query = "select * from " + "file_uuid" + " where dataID='" + pk + "'"; 307 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 308 | if(resultSet.next()) { 309 | FileUuidDto dto = new FileUuidDto(); 310 | dto.setFileName(resultSet.getString("fileName")); 311 | dto.setFileUuid(resultSet.getString("fileUuid")); 312 | return dto; 313 | } 314 | else { 315 | } 316 | } catch (Exception e) { 317 | e.printStackTrace(); 318 | } 319 | return null; 320 | } 321 | 322 | private boolean updateFileManagement(FileManagementDto dto) { 323 | String query = "update file_management" + " set availability_policy=?, cert=?, data_priority=?, data_signature=?, data_size=?, data_type=?, " + "directory=?, file_type=?, linked_edge=?, security_level=?, timestamp=? " + "where dataId=?"; 324 | try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(query);) { 325 | statement.setInt(1, dto.getAvailabilityPolicy()); 326 | statement.setString(2, dto.getCert()); 327 | statement.setInt(3, dto.getDataPriority()); 328 | statement.setString(4, dto.getDataSign()); 329 | statement.setLong(5, dto.getDataSize()); 330 | statement.setInt(6, dto.getDataType()); 331 | statement.setString(7, dto.getDirectory()); 332 | statement.setString(8, dto.getFileType()); 333 | statement.setString(9, dto.getLinkedEdge()); 334 | statement.setInt(10, dto.getSecurityLevel()); 335 | statement.setTimestamp(11, dto.getTimestamp()); 336 | statement.setString(12, dto.getDataId()); 337 | 338 | int check = statement.executeUpdate(); 339 | if(check == 0) { 340 | System.out.println("Database '" + dto.getDataId() + "' update fail"); 341 | } 342 | else 343 | return true; 344 | } catch (Exception e) { 345 | e.printStackTrace(); 346 | } 347 | return false; 348 | 349 | } 350 | 351 | private boolean updateFileUuid(FileUuidDto dto) { 352 | String query = "update file_uuid set fileName=?, fileUuid=?"; 353 | try (Connection connection = getConnection(); PreparedStatement statement = connection.prepareStatement(query);) { 354 | statement.setString(1, dto.getFileName()); 355 | statement.setString(2, dto.getFileUuid()); 356 | 357 | int check = statement.executeUpdate(); 358 | if(check == 0) { 359 | System.out.println("Database '" + dto.getFileName() + "' update fail"); 360 | } 361 | else 362 | return true; 363 | } catch (Exception e) { 364 | e.printStackTrace(); 365 | } 366 | return false; 367 | } 368 | 369 | private boolean deleteFileManagement(String pk) { 370 | String query = "delete from file_management where dataId='" + pk + "'"; 371 | try (Connection connection = getConnection(); Statement statement = connection.createStatement();) { 372 | int check = statement.executeUpdate(query); 373 | if(check == 0) { 374 | System.out.println("Database '" + pk + "' delete fail"); 375 | } 376 | else { 377 | return true; 378 | } 379 | } catch (Exception e) { 380 | e.printStackTrace(); 381 | } 382 | return false; 383 | } 384 | 385 | private boolean deleteFileUuid(String pk) { 386 | String query = "delete from file_Uuid where dataId='" + pk + "'"; 387 | try (Connection connection = getConnection(); Statement statement = connection.createStatement();) { 388 | int check = statement.executeUpdate(query); 389 | if(check == 0) { 390 | System.out.println("Database '" + pk + "' delete fail"); 391 | } 392 | else { 393 | return true; 394 | } 395 | } catch (Exception e) { 396 | e.printStackTrace(); 397 | } 398 | return false; 399 | } 400 | 401 | private ArrayList excuteQueryFileManagement(String query) { 402 | ArrayList dtos = new ArrayList<>(); 403 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 404 | while (resultSet.next()) { 405 | FileManagementDto dto = new FileManagementDto(); 406 | dto.setDataId(resultSet.getString("dataid")); 407 | dto.setAvailabilityPolicy(resultSet.getInt("availability_policy")); 408 | dto.setCert(resultSet.getString("cert")); 409 | dto.setDataPriority(resultSet.getInt("data_priority")); 410 | dto.setDataSign(resultSet.getString("data_signature")); 411 | dto.setDataSize(resultSet.getLong("data_size")); 412 | dto.setDataType(resultSet.getInt("data_type")); 413 | dto.setDirectory(resultSet.getString("directory")); 414 | dto.setFileType(resultSet.getString("file_type")); 415 | dto.setLinkedEdge(resultSet.getString("linked_edge")); 416 | dto.setSecurityLevel(resultSet.getInt("security_level")); 417 | dto.setTimestamp(resultSet.getTimestamp("timestamp")); 418 | dtos.add(dto); 419 | } 420 | } catch (Exception e) { 421 | e.printStackTrace(); 422 | } 423 | return dtos; 424 | } 425 | 426 | private ArrayList excuteQueryFileUuid(String query) { 427 | ArrayList dtos = new ArrayList<>(); 428 | try (Connection connection = getConnection(); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(query);) { 429 | while (resultSet.next()) { 430 | FileUuidDto dto = new FileUuidDto(); 431 | dto.setFileName(resultSet.getString("fileName")); 432 | dto.setFileUuid(resultSet.getString("fileUuid")); 433 | dtos.add(dto); 434 | } 435 | } catch (Exception e) { 436 | e.printStackTrace(); 437 | } 438 | return dtos; 439 | } 440 | } 441 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/Azure.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.io.BufferedReader; 4 | import java.io.BufferedWriter; 5 | import java.io.File; 6 | import java.io.FileNotFoundException; 7 | import java.io.FileReader; 8 | import java.io.FileWriter; 9 | import java.io.IOException; 10 | import java.io.InputStreamReader; 11 | import java.net.InetAddress; 12 | import java.util.Arrays; 13 | 14 | public class Azure implements OSProcess{ 15 | private final String mountPoint = "/media/azure/"; 16 | private final String checkFile = "check.txt"; 17 | private String mountPath; 18 | private TcpReceptor receptor; 19 | @Override 20 | public String getMaster() { 21 | receptor = new TcpReceptor(); 22 | String masterIP = "none"; 23 | mountPath = getSharedDiskPath(); 24 | mount(mountPath); 25 | masterIP = getMasterAddress(); 26 | if(!receptor.check(masterIP)) { 27 | checkFileCreate(); 28 | masterIP = getMasterAddress(); 29 | } 30 | umount(); 31 | return masterIP; 32 | } 33 | 34 | @Override 35 | public void start() { 36 | receptor.start(); 37 | } 38 | 39 | @Override 40 | public void stop() { 41 | receptor.stop(); 42 | mount(mountPath); 43 | checkFileDelete(); 44 | umount(); 45 | } 46 | 47 | public boolean checkFileDelete() { 48 | boolean result = false; 49 | File file = new File(mountPoint+checkFile); 50 | if(file.exists()) { 51 | file.delete(); 52 | result = true; 53 | } 54 | return result; 55 | } 56 | private String getMasterAddress() { 57 | String masterIP = "none"; 58 | File checkPath = new File(mountPoint+checkFile); 59 | if(checkPath.exists()) { 60 | try { 61 | BufferedReader reader = new BufferedReader(new FileReader(checkPath)); 62 | masterIP = reader.readLine(); 63 | reader.close(); 64 | } catch (Exception e) { 65 | e.printStackTrace(); 66 | } 67 | } 68 | else { 69 | masterIP = checkFileCreate(); 70 | } 71 | return masterIP; 72 | } 73 | private String checkFileCreate() { 74 | String address = "none"; 75 | File checkPath = new File(mountPoint+checkFile); 76 | try { 77 | address = InetAddress.getLocalHost().getHostAddress(); 78 | BufferedWriter writer = new BufferedWriter(new FileWriter(checkPath)); 79 | writer.write(address); 80 | writer.close(); 81 | } catch (Exception e) { 82 | e.printStackTrace(); 83 | } 84 | return address; 85 | 86 | } 87 | private boolean mount(String path) { 88 | File mountFolder = new File(mountPoint); 89 | if(!mountFolder.exists()) { 90 | mountFolder.mkdir(); 91 | } 92 | 93 | try { 94 | Process process = Runtime.getRuntime().exec("sudo mount "+path+" "+mountPoint); 95 | process.waitFor(); 96 | return true; 97 | } catch (Exception e) { 98 | e.printStackTrace(); 99 | } 100 | return false; 101 | } 102 | private boolean umount() { 103 | try { 104 | Process process = Runtime.getRuntime().exec("sudo umount "+mountPoint); 105 | process.waitFor(); 106 | return true; 107 | 108 | } catch (Exception e) { 109 | e.printStackTrace(); 110 | } 111 | return false; 112 | } 113 | private String getSharedDiskPath() { 114 | String path = "none"; 115 | double topSize = -1; 116 | try { 117 | Process process = new ProcessBuilder("lsblk", "--noheadings", "--output", "NAME,TYPE,SIZE").start(); 118 | BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream())); 119 | String line; 120 | while((line = reader.readLine()) != null) { 121 | String[] parts = line.trim().split("\\s+"); 122 | double size = convertToBytes(parts[2]); 123 | if(size > topSize) { 124 | path = parts[0]; 125 | topSize = size; 126 | } 127 | 128 | } 129 | return "/dev/"+path; 130 | } catch (Exception e) { 131 | e.printStackTrace(); 132 | } 133 | 134 | return path; 135 | } 136 | private double convertToBytes(String size) { 137 | String[] units = {"B", "K", "M", "G", "T"}; 138 | String unit = size.substring(size.length()-1); 139 | double value = Double.parseDouble(size.substring(0, size.length()-1)); 140 | int square = Arrays.binarySearch(units, unit); 141 | value = (double) (value * Math.pow(1024, square)); 142 | return value; 143 | } 144 | } 145 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/Broadcast.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.io.IOException; 4 | import java.net.DatagramPacket; 5 | import java.net.DatagramSocket; 6 | import java.net.InetAddress; 7 | import java.net.SocketException; 8 | import java.net.UnknownHostException; 9 | 10 | import kr.re.keti.Main; 11 | import kr.re.keti.PortNum; 12 | 13 | public class Broadcast { 14 | private static final Broadcast instance = new Broadcast(); 15 | private final int DEFAULT_BUF_LENGTH = 64; 16 | private final String DEFAULT_BROADCAST_ADDRESS = "255.255.255.255"; 17 | 18 | private byte[] addr; 19 | private DatagramSocket receiverSocket; 20 | private DatagramSocket sendSocket; 21 | 22 | public static Broadcast getInstance() { 23 | return instance; 24 | } 25 | private Broadcast() { 26 | setting(); 27 | } 28 | public void setting() { 29 | try { 30 | receiverSocket = new DatagramSocket(PortNum.DEFAULT_RECEIVE_PORT); 31 | receiverSocket.setReuseAddress(true); 32 | 33 | sendSocket = new DatagramSocket(PortNum.DEFAULT_SEND_PORT); 34 | sendSocket.setReuseAddress(true); 35 | sendSocket.setBroadcast(true); 36 | } catch (Exception e) { 37 | e.printStackTrace(); 38 | } 39 | } 40 | public void close() { 41 | if(receiverSocket != null) receiverSocket.close(); 42 | if(sendSocket != null) sendSocket.close(); 43 | } 44 | //--------------------------send packet-------------------------------------------------- 45 | public void send() { 46 | byte[] buf = new byte[DEFAULT_BUF_LENGTH]; 47 | send(buf); 48 | } 49 | public void send(String data) { 50 | byte[] buf = new byte[DEFAULT_BUF_LENGTH]; 51 | byte[] b = data.getBytes(); 52 | int len = Math.min(b.length, buf.length); 53 | System.arraycopy(b, 0, buf, 0, len); 54 | send(buf); 55 | } 56 | public void send(byte[] data) { 57 | DatagramPacket packet = null; 58 | try { 59 | addr = Main.deviceIP.getBytes(); 60 | packet = new DatagramPacket(addr, addr.length, InetAddress.getByName(DEFAULT_BROADCAST_ADDRESS),PortNum.DEFAULT_RECEIVE_PORT); 61 | packet.setData(data); 62 | send(packet); 63 | } catch (Exception e) { 64 | e.printStackTrace(); 65 | } 66 | 67 | } 68 | public void send(DatagramPacket packet) { 69 | try { 70 | sendSocket.send(packet); 71 | } catch (Exception e) { 72 | e.printStackTrace(); 73 | } 74 | } 75 | //------------------------receive packet----------------------------------------- 76 | public DatagramPacket receive() { 77 | DatagramPacket packet = null; 78 | try { 79 | byte[] buf = new byte[DEFAULT_BUF_LENGTH]; 80 | packet = new DatagramPacket(buf, DEFAULT_BUF_LENGTH); 81 | receiverSocket.receive(packet); 82 | } catch (Exception e) { 83 | e.printStackTrace(); 84 | } 85 | return packet; 86 | } 87 | 88 | //----------------------get local IP-------------------------------------- 89 | public void selfSend() { 90 | try { 91 | addr = Main.deviceIP.getBytes(); 92 | DatagramPacket packet = new DatagramPacket(addr, addr.length, InetAddress.getByName(DEFAULT_BROADCAST_ADDRESS), PortNum.DEFAULT_SELF_PORT); 93 | DatagramSocket socket = new DatagramSocket(); 94 | socket.send(packet); 95 | socket.close(); 96 | } catch (Exception e) { 97 | e.printStackTrace(); 98 | } 99 | 100 | 101 | } 102 | public String selfReceive() { 103 | try { 104 | byte[] buf = new byte[DEFAULT_BUF_LENGTH]; 105 | DatagramSocket socket = new DatagramSocket(PortNum.DEFAULT_SELF_PORT); 106 | DatagramPacket packet = new DatagramPacket(buf, DEFAULT_BUF_LENGTH); 107 | socket.receive(packet); 108 | String addr = packet.getAddress().getHostAddress(); 109 | socket.close(); 110 | return addr; 111 | // TODO Auto-generated catch block 112 | // TODO Auto-generated catch block 113 | } catch (Exception e) { 114 | e.printStackTrace(); 115 | } 116 | return "none"; 117 | } 118 | } 119 | 120 | 121 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/EdgeFinder.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.net.DatagramPacket; 4 | import java.time.LocalDateTime; 5 | import java.time.format.DateTimeFormatter; 6 | import java.util.concurrent.ArrayBlockingQueue; 7 | 8 | import kr.re.keti.Main; 9 | 10 | public class EdgeFinder { 11 | private static EdgeFinder instance = new EdgeFinder(); 12 | private Broadcast broadcast; 13 | private ArrayBlockingQueue queue; 14 | private final int DEFAULT_CAPACITY_SIZE = 50000; 15 | public static final int DEFAULT_WAITING_TIME = 100; 16 | 17 | public static EdgeFinder getInstance() { 18 | return instance; 19 | } 20 | 21 | private EdgeFinder() { 22 | broadcast = Broadcast.getInstance(); 23 | queue = new ArrayBlockingQueue<>(DEFAULT_CAPACITY_SIZE); 24 | } 25 | 26 | public void discoverMaster() { 27 | DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); 28 | 29 | try { 30 | Thread.sleep(DEFAULT_WAITING_TIME); 31 | } catch (Exception e) { 32 | e.printStackTrace(); 33 | } 34 | 35 | LocalDateTime masterTime = LocalDateTime.parse(Main.programStartTime, formatter); 36 | String masterIP = Main.deviceIP; 37 | 38 | for (DatagramPacket packet : queue) { 39 | String packetData = new String(packet.getData()).trim(); 40 | String[] datas = packetData.split("::"); 41 | String packetIP = packet.getAddress().getHostAddress(); 42 | 43 | //------------master answer-------------- 44 | if(datas[1].equals("master")) { 45 | masterIP = packetIP; 46 | break; 47 | } 48 | 49 | //-----------------all device answer--------------------- 50 | else { 51 | String packetTime = datas[2]; 52 | LocalDateTime time = LocalDateTime.parse(packetTime, formatter); 53 | if(time.isBefore(masterTime)) { 54 | masterTime = time; 55 | masterIP = packetIP; 56 | } 57 | } 58 | } 59 | 60 | //----------------------master found log-------------------------- 61 | if(Main.masterIP.equals("None")) { 62 | if(masterIP.equals(Main.deviceIP)) { 63 | System.out.println("Waiting for connections from slaves..."); 64 | } 65 | else { 66 | System.out.println("* Master found: " + masterIP + "\n"); 67 | } 68 | } 69 | else { 70 | if(!Main.masterIP.equals(masterIP)) { 71 | System.out.println("* Master(" + Main.masterIP + ") die"); 72 | System.out.println("* Master change to " + masterIP); 73 | } 74 | } 75 | 76 | Main.masterIP = masterIP; 77 | //--------------------------mode setting------------------------ 78 | if(Main.masterIP.equals(Main.deviceIP)) { 79 | Main.mode = "master"; 80 | } 81 | else { 82 | Main.mode = "slave"; 83 | } 84 | clearQueue(); 85 | } 86 | 87 | public void addPacket(DatagramPacket packet) { 88 | queue.add(packet); 89 | } 90 | 91 | public void clearQueue() { 92 | queue.clear(); 93 | } 94 | 95 | public void response() { 96 | String data = "ANS::" + Main.mode + "::" + Main.programStartTime; 97 | broadcast.send(data); 98 | } 99 | 100 | public void request() { 101 | String data = "REQ::" + Main.mode + "::" + Main.programStartTime; 102 | broadcast.send(data); 103 | } 104 | 105 | public void getLocalIPAddress() { 106 | Thread sendThread = new Thread() { 107 | public void run() { 108 | while (!Thread.interrupted()) { 109 | broadcast.selfSend(); 110 | } 111 | } 112 | } 113 | Thread receiveThread = new Thread() { 114 | public void run() { 115 | Main.deviceIP = broadcast.selfReceive(); 116 | } 117 | } 118 | receiveThread.start(); 119 | sendThread.start(); 120 | try { 121 | receiveThread.join(); 122 | sendThread.interrupt(); 123 | sendThread.join(); 124 | System.out.println("device adress update : " + Main.deviceIP); 125 | } catch (Exception e) { 126 | e.printStackTrace(); 127 | } 128 | 129 | } 130 | 131 | } 132 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/EdgeReceptor.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.io.FileWriter; 4 | import java.net.DatagramPacket; 5 | import java.net.InetAddress; 6 | import java.net.UnknownHostException; 7 | import java.text.SimpleDateFormat; 8 | import java.util.ArrayList; 9 | import java.util.Date; 10 | import java.util.HashMap; 11 | import java.util.concurrent.ArrayBlockingQueue; 12 | 13 | import kr.re.keti.Main; 14 | 15 | public class EdgeReceptor { 16 | public static final EdgeReceptor instance = new EdgeReceptor(); 17 | public static ArrayList slaveList = new ArrayList<>(); 18 | private HashMap slaveMap = new HashMap<>(); 19 | private final int DEFAULT_WAITING_QUEUE_CAPACITY = 256; 20 | private Thread listenerThread; 21 | private Thread responseThread; 22 | private byte[] deviceIP; 23 | private Broadcast broadcast; 24 | // private Agent agent; 25 | 26 | private ArrayBlockingQueue waitingAddressQeue; 27 | 28 | public static EdgeReceptor getInstance() { 29 | return instance; 30 | } 31 | 32 | private EdgeReceptor() { 33 | // agent = Agent.getInstance(); 34 | broadcast = Broadcast.getInstance(); 35 | waitingAddressQeue = new ArrayBlockingQueue<>(DEFAULT_WAITING_QUEUE_CAPACITY); 36 | try { 37 | deviceIP = InetAddress.getLocalHost().getHostAddress().getBytes(); 38 | } catch (Exception e) { 39 | e.printStackTrace(); 40 | } 41 | initThreads(); 42 | } 43 | 44 | public void listenerStart() { 45 | listenerThread.start(); 46 | } 47 | 48 | public void listenerStop() { 49 | listenerThread.interrupt(); 50 | ; 51 | } 52 | 53 | public void responseStart() { 54 | responseThread.start(); 55 | } 56 | 57 | public void responseStop() { 58 | responseThread.interrupt(); 59 | } 60 | 61 | public void close() { 62 | if(listenerThread != null) 63 | listenerStop(); 64 | if(responseThread != null) 65 | responseStop(); 66 | waitingAddressQeue.clear(); 67 | } 68 | 69 | public void newEdgeListShow() { 70 | if(slaveList.size() == 0) 71 | return; 72 | System.out.println("\n* Slave List"); 73 | for (String slave : slaveList) { 74 | if(slave.equals(slaveList.get(slaveList.size() - 1))) 75 | // last slave == new slave 76 | System.out.println("\t" + slaveMap.get(slave) + " : " + slave + " : new"); 77 | else 78 | System.out.println("\t" + slaveMap.get(slave) + " : " + slave); 79 | } 80 | } 81 | 82 | public void delEdgeListShow(String address) { 83 | for (String slave : slaveList) { 84 | if(slave.equals(address)) { 85 | System.out.println("\t" + slaveMap.get(slave) + " : " + slave + " : delete"); 86 | slaveList.remove(address); 87 | slaveMap.remove(address); 88 | } 89 | else { 90 | System.out.println("\t" + slaveMap.get(slave) + " : " + slave); 91 | } 92 | } 93 | } 94 | 95 | public void initThreads() { 96 | 97 | listenerThread = new Thread(() -> { 98 | try { 99 | EdgeFinder finder = EdgeFinder.getInstance(); 100 | while (!Thread.currentThread().isInterrupted()) { 101 | DatagramPacket packet = broadcast.receive(); 102 | String packetData = new String(packet.getData()); 103 | 104 | //-------------------packet save------------------------ 105 | if(packetData.indexOf("ANS") != -1) { 106 | finder.addPacket(packet); 107 | } 108 | if(packetData.indexOf("ANS") != -1 || packetData.indexOf("REQ") != -1) { 109 | waitingAddressQeue.put(packet); 110 | } 111 | } 112 | } catch (Exception e) { 113 | e.printStackTrace(); 114 | } 115 | } 116 | 117 | responseThread = new Thread(() -> { 118 | try { 119 | String addr; 120 | DatagramPacket packet; 121 | while (!Thread.currentThread().isInterrupted()) { 122 | packet = waitingAddressQeue.take(); 123 | addr = packet.getAddress().getHostAddress(); 124 | if(deviceIP != null && addr != null) { 125 | String packetData = new String(packet.getData()).trim(); 126 | EdgeFinder finder = EdgeFinder.getInstance(); 127 | 128 | String[] datas = packetData.split("::"); 129 | 130 | String type = datas[0]; 131 | // String mode = datas[1]; 132 | // String time = datas[2]; 133 | 134 | if(type.equals("REQ")) { 135 | finder.response(); 136 | finder.discoverMaster(); 137 | } 138 | // System.out.println("DUP Data : "+packetData); 139 | if(Main.mode.equals("master") && !addr.equals(Main.deviceIP)) { 140 | if(logWrite(addr)) { 141 | newEdgeListShow(); 142 | // DataProcess.initEdgeList(PortNum.KETI_PORT); 143 | String edgeList = slaveList + ""; 144 | edgeList.replace(Main.masterIP, ""); 145 | // String request = "{[{REQ::"+Main.masterIP+"::001::EDGE_LIST::"+edgeList+"}]}"; 146 | // agent.send(request.getBytes()); 147 | 148 | // String request = "{[{REQ::"+Main.masterIP+"::001::EDGE_LIST::"+edgeList+"}]}"; 149 | // agent.send(request.getBytes()); 150 | } 151 | } 152 | } 153 | } 154 | } catch (Exception e) { 155 | e.printStackTrace(); 156 | } 157 | } 158 | 159 | listenerThread.setName("UDP_lisener"); 160 | responseThread.setName("UDP_response"); 161 | 162 | } 163 | 164 | public boolean logWrite(String slaveAddr) { 165 | SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 166 | Date nowTime = new Date(); 167 | String logTime = timeFormat.format(nowTime); 168 | 169 | if(slaveList.contains(slaveAddr)) 170 | return false; 171 | 172 | slaveList.add(slaveAddr); 173 | slaveMap.put(slaveAddr, logTime); 174 | 175 | try { 176 | FileWriter writer = new FileWriter("edge_ipList.txt", false); 177 | writer.write("master\n"); 178 | writer.flush(); 179 | 180 | for (int i = 0; i < slaveList.size(); i++) { 181 | writer.write(slaveList.get(i) + "\n"); 182 | writer.flush(); 183 | } 184 | 185 | if(writer != null) 186 | writer.close(); 187 | 188 | } catch (Exception e) { 189 | e.printStackTrace(); 190 | } 191 | return true; 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/Linux.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | public class Linux implements OSProcess{ 4 | UdpReceptor receptor; 5 | 6 | @Override 7 | public String getMaster() { 8 | receptor = new UdpReceptor(); 9 | String masterIP = "none"; 10 | masterIP = receptor.getMaster(); 11 | return masterIP; 12 | } 13 | @Override 14 | public void start() { 15 | receptor.start(); 16 | } 17 | 18 | @Override 19 | public void stop() { 20 | receptor.stop(); 21 | } 22 | 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/OSProcess.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | public interface OSProcess { 4 | public String getMaster(); 5 | public void start(); 6 | public void stop(); 7 | } 8 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/TcpReceptor.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | import java.net.InetAddress; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | import java.net.UnknownHostException; 9 | import java.text.SimpleDateFormat; 10 | import java.util.ArrayList; 11 | import java.util.Date; 12 | import java.util.HashMap; 13 | import java.util.concurrent.ArrayBlockingQueue; 14 | 15 | import kr.re.keti.PortNum; 16 | 17 | public class TcpReceptor { 18 | public static ArrayList slaveList = new ArrayList<>(); 19 | private HashMap slaveMap = new HashMap<>(); 20 | private final int CAPACITY = 5000; 21 | private ServerSocket serverSocket; 22 | private ArrayBlockingQueue acceptQueue; 23 | private Thread acceptThread; 24 | private Thread receiveThread; 25 | private final int PORT = PortNum.DEFAULT_TCP_RECEPTOR_PORT; 26 | 27 | public TcpReceptor() { 28 | try { 29 | serverSocket = new ServerSocket(PortNum.DEFAULT_TCP_RECEPTOR_PORT); 30 | acceptQueue = new ArrayBlockingQueue<>(CAPACITY); 31 | initThread(); 32 | } catch (Exception e) { 33 | e.printStackTrace(); 34 | } 35 | } 36 | 37 | public void start() { 38 | acceptThread.start(); 39 | receiveThread.start(); 40 | } 41 | 42 | public void stop() { 43 | acceptThread.interrupt(); 44 | ; 45 | receiveThread.interrupt(); 46 | ; 47 | acceptQueue.clear(); 48 | 49 | } 50 | 51 | private void initThread() { 52 | receiveThread = new Thread(() -> { 53 | try { 54 | while (!Thread.currentThread().isInterrupted()) { 55 | Socket socket = acceptQueue.take(); 56 | String address = socket.getInetAddress().getHostAddress(); 57 | if(!slaveList.contains(address)) { 58 | SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 59 | Date nowTime = new Date(); 60 | String logTime = timeFormat.format(nowTime); 61 | slaveList.add(address); 62 | slaveMap.put(address, logTime); 63 | newEdgeListShow(); 64 | 65 | try { 66 | FileWriter writer = new FileWriter("edge_ipList.txt", false); 67 | writer.write("master\n"); 68 | writer.flush(); 69 | 70 | for (int i = 0; i < slaveList.size(); i++) { 71 | writer.write(slaveList.get(i) + "\n"); 72 | writer.flush(); 73 | } 74 | 75 | if(writer != null) 76 | writer.close(); 77 | 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | } 81 | } 82 | } 83 | // e.printStackTrace(); 84 | } catch (Exception e) { 85 | e.printStackTrace(); 86 | } 87 | } 88 | acceptThread = new Thread(() -> { 89 | while (!Thread.currentThread().isInterrupted()) { 90 | try { 91 | Socket clientSocket = serverSocket.accept(); 92 | acceptQueue.put(clientSocket); 93 | // e.printStackTrace(); 94 | } catch (Exception e) { 95 | e.printStackTrace(); 96 | } 97 | } 98 | } 99 | 100 | acceptThread.setName("TCP_AcceptThread"); 101 | receiveThread.setName("TCP_ReceiveThread"); 102 | } 103 | 104 | public boolean check(String masterIP) { 105 | try { 106 | String address = InetAddress.getLocalHost().getHostAddress(); 107 | if(address.equals(masterIP)) 108 | return true; 109 | } catch (Exception e) { 110 | e.printStackTrace(); 111 | } 112 | try (Socket socket = new Socket(masterIP, PORT);) { 113 | socket.setSoTimeout(1000); 114 | } catch (Exception e) { 115 | e.printStackTrace(); 116 | } 117 | return true; 118 | } 119 | 120 | public void newEdgeListShow() { 121 | if(slaveList.size() == 0) 122 | return; 123 | System.out.println("\n* Slave List"); 124 | for (String slave : slaveList) { 125 | if(slave.equals(slaveList.get(slaveList.size() - 1))) 126 | // last slave == new slave 127 | System.out.println("\t" + slaveMap.get(slave) + " : " + slave + " : new"); 128 | else 129 | System.out.println("\t" + slaveMap.get(slave) + " : " + slave); 130 | } 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/TcpRequest.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.io.PrintWriter; 4 | import java.net.Socket; 5 | 6 | import kr.re.keti.Main; 7 | import kr.re.keti.PortNum; 8 | 9 | public class TcpRequest { 10 | public static void send() { 11 | try ( Socket socket = new Socket(Main.masterIP, PortNum.DEFAULT_TCP_RECEPTOR_PORT); 12 | ){ 13 | PrintWriter out = new PrintWriter(socket.getOutputStream()); 14 | out.println(Main.deviceIP); 15 | 16 | } catch (Exception e) { 17 | e.printStackTrace(); 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /src/kr/re/keti/os/UdpReceptor.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.os; 2 | 3 | import java.io.FileWriter; 4 | import java.io.IOException; 5 | import java.net.DatagramPacket; 6 | import java.net.DatagramSocket; 7 | import java.net.InetAddress; 8 | import java.net.SocketException; 9 | import java.net.UnknownHostException; 10 | import java.nio.file.Files; 11 | import java.nio.file.Path; 12 | import java.text.SimpleDateFormat; 13 | import java.util.ArrayList; 14 | import java.util.Date; 15 | import java.util.HashMap; 16 | import java.util.concurrent.ArrayBlockingQueue; 17 | 18 | import kr.re.keti.Main; 19 | import kr.re.keti.PortNum; 20 | import kr.re.keti.agent.Agent; 21 | import kr.re.keti.Ssl; 22 | 23 | public class UdpReceptor { 24 | public static ArrayList edgeList = new ArrayList<>(); 25 | private HashMap edgeMap = new HashMap<>(); 26 | 27 | public final int DEFAULT_RECEIVE_PORT = PortNum.DEFAULT_RECEIVE_PORT; 28 | public final int DEFAULT_SEND_PORT = PortNum.DEFAULT_SEND_PORT; 29 | private final int DEFAULT_BUF_LENGTH = 64; 30 | private final String DEFAULT_BROADCAST_ADDRESS = "255.255.255.255"; 31 | 32 | private ArrayBlockingQueue queue; 33 | private DatagramSocket receiveSocket, sendSocket; 34 | private Thread receiveThread, sendThread; 35 | private Agent agent; 36 | 37 | public UdpReceptor() { 38 | try { 39 | queue = new ArrayBlockingQueue<>(5000); 40 | receiveSocket = new DatagramSocket(DEFAULT_RECEIVE_PORT); 41 | receiveSocket.setReuseAddress(true); 42 | 43 | sendSocket = new DatagramSocket(DEFAULT_SEND_PORT); 44 | sendSocket.setReuseAddress(true); 45 | sendSocket.setBroadcast(true); 46 | sendSocket.setSoTimeout(1000); 47 | } catch (Exception e) { 48 | e.printStackTrace(); 49 | } 50 | } 51 | public String getMaster() { 52 | String masterIP = "none"; 53 | 54 | try { 55 | byte[] buf = new byte[DEFAULT_BUF_LENGTH]; 56 | send(DEFAULT_RECEIVE_PORT, Main.uuid.getBytes()); 57 | DatagramPacket packet = new DatagramPacket(buf, DEFAULT_BUF_LENGTH); 58 | sendSocket.receive(packet); 59 | masterIP= packet.getAddress().getHostAddress(); 60 | 61 | 62 | byte[] request = ("{[{REQ::"+masterIP+"::020::EDGE_KEYS}]}").getBytes(); 63 | String response = new String(agent.send(masterIP, request)); 64 | String responseData = response.split("::")[3]; 65 | String[] datas = response.split(":"); 66 | for(String data : datas) { 67 | String uuid = data.substring(0, 36); 68 | String key = data.substring(36, data.length()); 69 | byte[] keyData = key.getBytes(); 70 | Ssl.addKey(responseData, uuid, keyData); 71 | } 72 | String path = Ssl.getPath()+"private/private.key"; 73 | byte[] keyData = Files.readAllBytes(Path.of(path)); 74 | String key = new String(keyData); 75 | agent.send(("{[{REQ::"+Main.deviceIP+"::019::"+Main.uuid+"::"+key+"}]}").getBytes()); 76 | // e.printStackTrace(); 77 | // e.printStackTrace(); 78 | } catch (Exception e) { 79 | e.printStackTrace(); 80 | } 81 | return masterIP; 82 | } 83 | public void start() { 84 | agent = Agent.getInstance(); 85 | queue = new ArrayBlockingQueue<>(5000); 86 | receiveThread = new Thread(()->{ 87 | while(!Thread.currentThread().isInterrupted()) { 88 | byte[] buf = new byte[DEFAULT_BUF_LENGTH]; 89 | DatagramPacket packet = new DatagramPacket(buf, DEFAULT_BUF_LENGTH); 90 | try { 91 | receiveSocket.receive(packet); 92 | queue.put(packet); 93 | } catch (Exception e) { 94 | e.printStackTrace(); 95 | } 96 | } 97 | }); 98 | 99 | sendThread = new Thread(()->{ 100 | while(!Thread.currentThread().isInterrupted()) { 101 | try { 102 | DatagramPacket packet = queue.take(); 103 | String address = packet.getAddress().getHostAddress(); 104 | String uuid = new String(packet.getData()); 105 | if(address.equals(Main.deviceIP)) continue; 106 | send(packet.getPort(), Main.uuid.getBytes()); 107 | newEdge(address, uuid); 108 | } catch (Exception e) { 109 | e.printStackTrace(); 110 | } 111 | } 112 | }); 113 | 114 | receiveThread.setName("UDP_Listner"); 115 | receiveThread.start(); 116 | 117 | sendThread.setName("UDP_Send"); 118 | sendThread.start(); 119 | } 120 | public void stop() { 121 | queue.clear(); 122 | if(sendThread != null) sendThread.interrupt(); 123 | if(receiveThread != null) receiveThread.interrupt(); 124 | } 125 | private void send(int targetPort, byte[] data) { 126 | try { 127 | DatagramPacket packet = createPacket(targetPort, data); 128 | sendSocket.send(packet); 129 | } catch (Exception e) { 130 | e.printStackTrace(); 131 | } 132 | } 133 | private DatagramPacket createPacket(int port, byte[] data) { 134 | try { 135 | DatagramPacket packet = new DatagramPacket( 136 | data, 137 | data.length, 138 | InetAddress.getByName(DEFAULT_BROADCAST_ADDRESS), 139 | port 140 | ); 141 | return packet; 142 | } catch (Exception e) { 143 | e.printStackTrace(); 144 | } 145 | return null; 146 | } 147 | private void newEdge(String address, String uuid) { 148 | SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); 149 | Date nowTime = new Date(); 150 | String logTime = timeFormat.format(nowTime); 151 | 152 | if(edgeList.contains(address)) { 153 | System.out.println("\t"+edgeMap.get(address)+" : "+address +" : delete"); 154 | edgeList.remove(address); 155 | edgeMap.remove(address); 156 | } 157 | 158 | edgeList.add(address); 159 | edgeMap.put(address, logTime); 160 | 161 | newEdgeLog(); 162 | if(!logWrite()) { 163 | System.out.println("edge_ipList write error"); 164 | } 165 | String list = getEdgeList(); 166 | agent.send(("{[{REQ::"+address+"::001::EDGE_LIST::"+list+"}]}").getBytes()); 167 | 168 | 169 | } 170 | public void newEdgeLog() { 171 | if(edgeList.size() == 0) return; 172 | System.out.println("\n* Slave List"); 173 | for(String slave : edgeList) { 174 | if(slave.equals(edgeList.get(edgeList.size()-1))) 175 | // last slave == new slave 176 | System.out.println("\t"+edgeMap.get(slave)+" : "+slave+" : new"); 177 | else 178 | System.out.println("\t"+edgeMap.get(slave)+" : "+slave); 179 | } 180 | 181 | } 182 | public boolean logWrite() { 183 | 184 | try { 185 | FileWriter writer = new FileWriter("edge_ipList.txt", false); 186 | writer.write("master\n"); 187 | writer.flush(); 188 | 189 | for(int i=0; i queue; 20 | 21 | public Client(ArrayBlockingQueue queue) { 22 | this.queue = queue; 23 | } 24 | public boolean send(Socket socket, byte[] data, Consumer callback) { 25 | AtomicBoolean check = new AtomicBoolean(false); 26 | Thread thread = new Thread(()->{ 27 | try { 28 | int currReteries = 0; 29 | InputStream inputStream = socket.getInputStream(); 30 | OutputStream outputStream = socket.getOutputStream(); 31 | while(!Thread.currentThread().isInterrupted()) { 32 | try { 33 | outputStream.write(data); 34 | outputStream.flush(); 35 | 36 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 37 | byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 38 | int len; 39 | while ((len = inputStream.read(buffer)) != -1) { 40 | baos.write(buffer, 0, len); 41 | if(inputStream.available() < 1) break; 42 | } 43 | byte[] responseData = baos.toByteArray(); 44 | String response = new String(responseData); 45 | if(response.indexOf("fail") != -1) { 46 | currReteries++; 47 | if(currReteries>MAX_RETRIES) { 48 | break; 49 | } 50 | continue; 51 | } 52 | else { 53 | check.set(true); 54 | if (callback != null) { 55 | callback.accept(responseData); 56 | } 57 | break; 58 | } 59 | 60 | } catch (Exception e) { 61 | e.printStackTrace(); 62 | } 63 | } 64 | } catch (Exception e) { 65 | e.printStackTrace(); 66 | } 67 | }); 68 | thread.setName(socket.getInetAddress().getHostAddress()+"retryTCPThread"); 69 | thread.start(); 70 | try { 71 | thread.join(); 72 | return check.get(); 73 | } catch (Exception e) { 74 | e.printStackTrace(); 75 | } 76 | return check.get(); 77 | } 78 | @Override 79 | public void run() { 80 | try { 81 | while(!Thread.currentThread().isInterrupted()) { 82 | AgentPacket packet = queue.take(); 83 | Thread thread = new Thread(()->{ 84 | try { 85 | Socket socket = packet.getSocket(); 86 | String address = packet.getAddress(); 87 | int port = packet.getPort(); 88 | byte[] requestData = packet.getData(); 89 | if(socket == null) { 90 | socket = new Socket(address, port); 91 | } 92 | socket.setSoTimeout(DEFAULT_TIMEOUT); 93 | 94 | InputStream inputStream = socket.getInputStream(); 95 | OutputStream outputStream = socket.getOutputStream(); 96 | 97 | outputStream.write(requestData); 98 | outputStream.flush(); 99 | 100 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 101 | byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 102 | int len; 103 | while ((len = inputStream.read(buffer)) != -1) { 104 | baos.write(buffer, 0, len); 105 | if(inputStream.available() < 1) break; 106 | } 107 | byte[] responseData = baos.toByteArray(); 108 | boolean check = true; 109 | if(new String(responseData).indexOf("fail") != -1) { 110 | final Socket finalSocket = socket; 111 | Thread retryThread = new Thread(()->{ 112 | if(check) { 113 | Consumer callback = packet.getCallback(); 114 | send(finalSocket, requestData, callback); 115 | } 116 | } catch (Exception e) { 117 | retryThread.start(); 118 | e.printStackTrace(); 119 | else { 120 | Consumer callback = packet.getCallback(); 121 | if (callback != null) { 122 | callback.accept(responseData); 123 | } 124 | } 125 | } 126 | } 127 | } catch (Exception e) { 128 | 129 | e.printStackTrace(); 130 | thread.setName(packet.getAddress()+"TCPThread"); 131 | thread.start(); 132 | } 133 | }); 134 | } 135 | } catch (Exception e) { 136 | e.printStackTrace(); 137 | } 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/kr/re/keti/tcp/Server.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.tcp; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.IOException; 5 | import java.io.InputStream; 6 | import java.net.ServerSocket; 7 | import java.net.Socket; 8 | import java.util.concurrent.ArrayBlockingQueue; 9 | 10 | import kr.re.keti.agent.AgentPacket; 11 | 12 | public class Server{ 13 | private final int CAPACITY = 5000; 14 | private final int DEFAULT_BUFFER_SIZE = 5000; 15 | private ServerSocket serverSocket; 16 | private ArrayBlockingQueue acceptQueue; 17 | private ArrayBlockingQueue receivQueue; 18 | private Thread acceptThread; 19 | private Thread receiveThread; 20 | 21 | 22 | public Server(int port, ArrayBlockingQueue receivQueue) { 23 | try { 24 | serverSocket = new ServerSocket(port); 25 | this.receivQueue = receivQueue; 26 | acceptQueue = new ArrayBlockingQueue<>(CAPACITY); 27 | initThread(); 28 | } catch (Exception e) { 29 | e.printStackTrace(); 30 | } 31 | } 32 | public void start() { 33 | acceptThread.start(); 34 | receiveThread.start(); 35 | } 36 | private void initThread() { 37 | receiveThread = new Thread(()->{ 38 | try { 39 | while(!Thread.currentThread().isInterrupted()) { 40 | Socket socket = acceptQueue.take(); 41 | try { 42 | InputStream inputStream = socket.getInputStream(); 43 | 44 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 45 | byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; 46 | int len; 47 | while ((len = inputStream.read(buffer)) != -1) { 48 | baos.write(buffer, 0, len); 49 | if(inputStream.available() < 1) break; 50 | } 51 | byte[] data = baos.toByteArray(); 52 | 53 | AgentPacket packet = new AgentPacket(socket, data); 54 | receivQueue.put(packet); 55 | 56 | } catch (Exception e) { 57 | e.printStackTrace(); 58 | } 59 | 60 | } 61 | } catch (Exception e) { 62 | e.printStackTrace(); 63 | } 64 | }); 65 | acceptThread = new Thread(()->{ 66 | while (!Thread.currentThread().isInterrupted()) { 67 | try { 68 | Socket clientSocket = serverSocket.accept(); 69 | acceptQueue.put(clientSocket); 70 | } catch (Exception e) { 71 | e.printStackTrace(); 72 | } 73 | } 74 | }); 75 | 76 | acceptThread.setName("TCP_AcceptThread"); 77 | receiveThread.setName("TCP_ReceiveThread"); 78 | } 79 | 80 | } 81 | -------------------------------------------------------------------------------- /src/kr/re/keti/tcp/TcpPacket.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.tcp; 2 | 3 | import java.net.Socket; 4 | 5 | public class TcpPacket { 6 | private Socket socket; 7 | private int port; 8 | private int length; 9 | private byte[] data; 10 | 11 | public TcpPacket(Socket socket, byte[] data, int length) { 12 | this.socket = socket; 13 | this.data = data; 14 | this.length = length; 15 | } 16 | public TcpPacket(int port, byte[] data, int length) { 17 | this.port = port; 18 | this.data = data; 19 | this.length = length; 20 | } 21 | public Socket getSocket() { 22 | return socket; 23 | } 24 | public void setSocket(Socket socket) { 25 | this.socket = socket; 26 | } 27 | public byte[] getData() { 28 | return data; 29 | } 30 | public void setData(byte[] data) { 31 | this.data = data; 32 | } 33 | public int getLength() { 34 | return length; 35 | } 36 | public void setLength(int length) { 37 | this.length = length; 38 | } 39 | public int getPort() { 40 | return port; 41 | } 42 | public void setPort(int port) { 43 | this.port = port; 44 | } 45 | 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/kr/re/keti/tcp/UnitEdge.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.tcp; 2 | 3 | import java.io.ByteArrayOutputStream; 4 | import java.io.File; 5 | import java.io.IOException; 6 | import java.io.InputStream; 7 | import java.io.OutputStream; 8 | import java.net.Socket; 9 | import java.nio.file.Files; 10 | import java.nio.file.Paths; 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import kr.re.keti.Main; 15 | import kr.re.keti.PortNum; 16 | import kr.re.keti.database.Database; 17 | import kr.re.keti.database.FileManagementDto; 18 | 19 | public class UnitEdge{ 20 | private String address; 21 | private List chunkList; 22 | 23 | public UnitEdge(Database database, String address, String dataid, int startIdx, int finishIdx) { 24 | chunkList = new ArrayList<>(); 25 | FileManagementDto dto = (FileManagementDto) database.select("file_management", dataid); 26 | this.address = address; 27 | String fileName = dataid+"."+dto.getFileType(); 28 | for(int i=startIdx; i<=finishIdx;i++) { 29 | chunkList.add(fileName+"_"+i); 30 | } 31 | } 32 | 33 | public void start() { 34 | for (String fileName : chunkList) { 35 | Thread thread = new Thread(new UnitEdgeRunnable(fileName)); 36 | thread.setName(fileName+"UnitEdgeThread"); 37 | thread.start(); 38 | } 39 | } 40 | 41 | private class UnitEdgeRunnable implements Runnable { 42 | private String fileName; 43 | 44 | public UnitEdgeRunnable(String fileName) { 45 | this.fileName = fileName; 46 | } 47 | 48 | @Override 49 | public void run() { 50 | while(true) { 51 | try { 52 | Thread.sleep(100); 53 | } catch (Exception e) { 54 | e.printStackTrace(); 55 | } 56 | try (Socket socket = new Socket(address, PortNum.KETI_PORT);){ 57 | socket.setSoTimeout(3000); 58 | 59 | String path = Main.storageFolder+"chunk/"; 60 | String filePath = path+fileName; 61 | 62 | File file = new File(filePath); 63 | byte[] data = Files.readAllBytes(Paths.get(file.toURI())); 64 | 65 | byte[] request = message(address, fileName, data); 66 | OutputStream outputStream = socket.getOutputStream(); 67 | outputStream.write(request); 68 | outputStream.flush(); 69 | 70 | //---------------------------response---------------------------------- 71 | InputStream inputStream = socket.getInputStream(); 72 | byte[] buffer = new byte[1024]; 73 | int length = inputStream.read(buffer); 74 | String response = new String(buffer, 0, length); 75 | if(response.indexOf("fail") !=-1 ) { 76 | continue; 77 | } 78 | else if(response.indexOf("success") != -1) { 79 | socket.close(); 80 | // System.out.println("File transfer completed for: " + filePath); 81 | return; 82 | } 83 | // Close the connections 84 | // e.printStackTrace(); 85 | } catch (Exception e) { 86 | e.printStackTrace(); 87 | } 88 | } 89 | } 90 | public static byte[] message(String address, String fileName, byte[] data) throws IOException { 91 | int fileSize = data.length; 92 | ByteArrayOutputStream baos = new ByteArrayOutputStream(); 93 | baos.write(("{[{REQ::" + address + "::406::" + fileName + "::" + fileSize + "::").getBytes("UTF-8")); 94 | baos.write(data); 95 | baos.write("}]}".getBytes("UTF-8")); 96 | return baos.toByteArray(); 97 | } 98 | 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /src/kr/re/keti/tcp/UnitShared.java: -------------------------------------------------------------------------------- 1 | package kr.re.keti.tcp; 2 | 3 | import java.io.File; 4 | import java.io.FileInputStream; 5 | import java.io.FileNotFoundException; 6 | import java.io.FileOutputStream; 7 | import java.io.IOException; 8 | import java.io.InputStream; 9 | import java.io.OutputStream; 10 | import java.time.LocalTime; 11 | import java.time.format.DateTimeFormatter; 12 | import java.util.Arrays; 13 | import java.util.Base64; 14 | import java.util.HashMap; 15 | import java.util.Map; 16 | import java.util.concurrent.ArrayBlockingQueue; 17 | import java.util.concurrent.BlockingQueue; 18 | import java.util.concurrent.ExecutorService; 19 | import java.util.concurrent.Executors; 20 | import java.util.concurrent.atomic.AtomicInteger; 21 | 22 | import kr.re.keti.DataProcess; 23 | import kr.re.keti.FileHandler; 24 | import kr.re.keti.FileMonitor; 25 | import kr.re.keti.Main; 26 | import kr.re.keti.PortNum; 27 | import kr.re.keti.agent.Agent; 28 | 29 | public class UnitShared { 30 | private static int port = PortNum.KETI_PORT; 31 | private static Map instances = new HashMap(); 32 | private static Map uuidMap = new HashMap(); 33 | private static BlockingQueue queue = new ArrayBlockingQueue<>(500); 34 | private static Thread processThread = processThread(); 35 | private Agent agent; 36 | private File file; 37 | private String fileName; 38 | private String uuid; 39 | private int securityLevel; 40 | private int chunkLength; 41 | private AtomicInteger chunkShareStatus; 42 | private AtomicInteger chunkCreateStatus; 43 | private int progress; 44 | private AtomicInteger progressCount; 45 | private int standard; 46 | private int edgeCount; 47 | private AtomicInteger readyCount; 48 | 49 | private UnitShared() {} 50 | public static UnitShared getInstance(String fileName, String uuid) { 51 | if(!instances.containsKey(fileName)) { 52 | UnitShared unit = new UnitShared(); 53 | unit.agent = Agent.getInstance(); 54 | unit.chunkShareStatus = new AtomicInteger(0); 55 | unit.chunkCreateStatus = new AtomicInteger(0); 56 | unit.readyCount = new AtomicInteger(0); 57 | unit.setFileName(fileName); 58 | unit.setUuid(uuid); 59 | instances.put(fileName, unit); 60 | uuidMap.put(uuid, fileName); 61 | } 62 | return instances.get(fileName); 63 | } 64 | public static void delInstance(String uuid) { 65 | if(uuidMap.containsKey(uuid)) { 66 | String fileName = uuidMap.get(uuid); 67 | uuidMap.remove(uuid); 68 | instances.remove(fileName); 69 | } 70 | else { 71 | String fileName = uuidMap.get(uuid); 72 | System.out.println("instance delete fail"); 73 | System.out.println("uuid: "+uuid+"\tfileName: "+fileName); 74 | } 75 | } 76 | public static UnitShared getInstanceUuid(String uuid) { 77 | if(uuidMap.containsKey(uuid)) { 78 | return getInstance(uuidMap.get(uuid), uuid); 79 | } 80 | return null; 81 | } 82 | public void setStandard(int standard) { 83 | this.standard = standard; 84 | } 85 | public void setFileName(String fileName) { 86 | this.fileName = fileName; 87 | } 88 | public String getUuid() { 89 | return this.uuid; 90 | } 91 | public void setUuid(String uuid) { 92 | this.uuid = uuid; 93 | } 94 | public void setLength(int length) { 95 | this.chunkLength = length; 96 | } 97 | public void setSecurityLevel(int securityLevel) { 98 | this.securityLevel = securityLevel; 99 | } 100 | public void setFile(File file) { 101 | this.file = file; 102 | } 103 | public void countAdd(String address, String fileName) { 104 | int progressCount = this.progressCount.incrementAndGet(); 105 | float status = 100; 106 | if(this.progress>0) { 107 | status = ((float)progressCount/this.progress)*100; 108 | } 109 | 110 | FileHandler handler = FileHandler.getInstance(); 111 | handler.modifyRecord(this.uuid, status+""); 112 | handler.modifyRecord(this.uuid, "sharing("+status+"%)", 4); 113 | // System.out.println(progressCount+"/"+this.progress+"\t"+status); 114 | if(status>=100) { 115 | String log = Main.deviceIP+"#"+this.uuid+"#"+this.fileName+"#"+this.securityLevel+"#complte"+100; 116 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "500", log); 117 | this.agent.send(message); 118 | 119 | handler.modifyRecord(this.uuid, "complte", 4); 120 | FileMonitor.unignoreFile(this.fileName); 121 | delInstance(this.uuid); 122 | } 123 | } 124 | // public void countAdd() { 125 | // this.progressCount.incrementAndGet(); 126 | // float status = 100; 127 | // if(this.progress>0) { 128 | // status = ((float)this.progressCount.incrementAndGet()/this.progress)*100; 129 | // } 130 | // String log = Main.deviceIP+"#"+this.uuid+"#"+this.fileName+"#"+this.securityLevel+"#sharing("+status+"%)#"+status; 131 | // byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "300", log); 132 | // FileHandler handler = FileHandler.getInstance(); 133 | // handler.modifyRecord(this.uuid, status+""); 134 | // handler.modifyRecord(this.uuid, "sharing("+status+"%)", 4); 135 | // 136 | // if(status>=100) { 137 | // handler.modifyRecord(this.uuid, "complte", 4); 138 | // FileMonitor.unignoreFile(this.fileName); 139 | // delInstance(this.uuid); 140 | // 141 | // } 142 | // } 143 | public static void send(UnitShared unit) { 144 | try { 145 | queue.put(unit); 146 | } catch (Exception e) { 147 | e.printStackTrace(); 148 | } 149 | } 150 | private static Thread processThread() { 151 | processThread = new Thread(()->{ 152 | while(!Thread.currentThread().isInterrupted()) { 153 | try { 154 | UnitShared unit = queue.take(); 155 | unit.preparData(); 156 | for(int i=0; i<30; i++) { 157 | if(unit.readyCount.get() == unit.edgeCount) { 158 | unit.share(); 159 | while(true) { 160 | int progressCount = unit.progressCount.get(); 161 | unit.loadingBar("progress", progressCount, unit.progress); 162 | if(progressCount >= unit.progress) { 163 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "111", unit.uuid); 164 | unit.agent.send(message); 165 | break; 166 | } 167 | Thread.sleep(100); 168 | } 169 | break; 170 | } 171 | Thread.sleep(100); 172 | } 173 | } catch (Exception e) { 174 | e.printStackTrace(); 175 | } 176 | } 177 | }); 178 | processThread.setName("UnitProcessThread"); 179 | processThread.start(); 180 | return processThread; 181 | } 182 | private void preparData() { 183 | this.edgeCount = DataProcess.requestEdgeList(port).split(", ").length; 184 | this.chunkLength = (int) Math.ceil((double) this.file.length() / standard); 185 | this.chunkCreate(); 186 | this.progress = chunkLength*this.edgeCount; 187 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "399", this.fileName, this.uuid, chunkLength+""); 188 | this.agent.send(message); 189 | } 190 | public void addReadyCount() { 191 | this.readyCount.incrementAndGet(); 192 | // if(this.readyCount.incrementAndGet() == this.edgeCount) { 193 | // this.share(); 194 | // System.out.println(this.uuid+"\tshare start"); 195 | // } 196 | // System.out.println(this.uuid+"\t"+this.readyCount.get()+"/"+this.edgeCount+"\tedge"); 197 | } 198 | private void share() { 199 | // 스레드 풀 생성 200 | ExecutorService executor = Executors.newFixedThreadPool(10); // 10개의 스레드를 가진 스레드 풀 201 | 202 | this.progressCount = new AtomicInteger(0); 203 | for(int i=0; i { // 스레드 풀에 작업 제출 206 | String fileName = file.getName()+"_"+fileCount; 207 | String filePath = Main.storageFolder+"chunk/"+fileName; 208 | File chunkFile = new File(filePath); 209 | byte[] data = DataProcess.readFileToByteArray(chunkFile); 210 | String encodingData = Base64.getEncoder().encodeToString(data); 211 | fileName = this.uuid+"_"+fileCount; 212 | byte[] chunkMessage = DataProcess.messageCreate("REQ", Main.deviceIP, "400", fileName, data.length+"", encodingData); 213 | this.agent.send(chunkMessage); 214 | // System.out.println("send data: "+LocalTime.now().format(DateTimeFormatter.ofPattern("H:m:s.SSS"))); 215 | } 216 | } 217 | 218 | executor.shutdown(); // 모든 작업이 완료되면 스레드 풀 종료 219 | } 220 | public int chunkCreate() { 221 | FileHandler handler = FileHandler.getInstance(); 222 | if(handler.isExists(this.uuid)) { 223 | handler.deleteRecord(this.uuid); 224 | } 225 | StringBuilder logBuilder = new StringBuilder(); 226 | logBuilder.append(Main.deviceIP).append("#").append(this.uuid).append("#").append(this.fileName).append("#").append(this.securityLevel).append("#loding(0%)#").append(0); 227 | 228 | handler.addRecord(logBuilder.toString()); 229 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "300", logBuilder.toString()); 230 | // this.agent.send(message); 231 | 232 | int maxCount = (int) Math.ceil((double) this.file.length() / standard); 233 | 234 | final int CHUNK_SIZE = this.standard; 235 | ExecutorService executor = Executors.newFixedThreadPool(10); // Thread pool with 10 threads 236 | 237 | loadingBar("loading", 0, this.chunkLength); 238 | new Thread(()->{ 239 | while(true) { 240 | loadingBar("loading", chunkCreateStatus.get(), this.chunkLength); 241 | try { 242 | Thread.sleep(100); 243 | } catch (Exception e) { 244 | e.printStackTrace(); 245 | } 246 | if(chunkCreateStatus.get() == chunkLength || chunkCreateStatus.get() == -1) { 247 | loadingBar("loading", chunkCreateStatus.get(), this.chunkLength); 248 | System.out.println(); 249 | break; 250 | } 251 | } 252 | }); 253 | try (InputStream inputStream = new FileInputStream(this.file)) { 254 | File directory = new File(Main.storageFolder + "chunk/"); 255 | if (!directory.exists()) directory.mkdirs(); 256 | 257 | byte[] buffer = new byte[CHUNK_SIZE]; 258 | int bytesRead; 259 | while ((bytesRead = inputStream.read(buffer)) != -1) { 260 | final byte[] bufferCopy = Arrays.copyOf(buffer, bytesRead); 261 | final int currentChunkCount = chunkCreateStatus.getAndIncrement(); 262 | executor.submit(() -> { 263 | String chunkFileName = this.file.getName() + "_" + currentChunkCount; 264 | String chunkFilePath = directory.getPath() + File.separator + chunkFileName; 265 | try (OutputStream outputStream = new FileOutputStream(chunkFilePath)) { 266 | outputStream.write(bufferCopy); 267 | } catch (Exception e) { 268 | e.printStackTrace(); 269 | } 270 | float chunkStatus = ((currentChunkCount)/(float) maxCount)*100; 271 | StringBuilder threadLogBuilder = new StringBuilder(); 272 | threadLogBuilder.append(Main.deviceIP).append("#").append(this.uuid).append("#").append(this.fileName).append("#").append(this.securityLevel).append("#loding(").append(chunkStatus).append("%)#").append(chunkStatus); 273 | String threadLog = threadLogBuilder.toString(); 274 | 275 | byte[] threadMessage = DataProcess.messageCreate("REQ", Main.deviceIP, "300", threadLog); 276 | handler.modifyRecord(this.uuid, String.valueOf(chunkStatus)); 277 | handler.modifyRecord(this.uuid, "loding("+chunkStatus+"%)", 4); 278 | // this.agent.send(threadMessage); 279 | } 280 | } 281 | executor.shutdown(); // Shutdown the executor 282 | while (!executor.isTerminated()) {} // Wait until all tasks are finished 283 | 284 | float chunkStatus = ((chunkCreateStatus.get())/(float) maxCount)*100; 285 | StringBuilder threadLogBuilder = new StringBuilder(); 286 | threadLogBuilder.append(Main.deviceIP).append("#").append(this.uuid).append("#").append(this.fileName).append("#").append(this.securityLevel).append("#loding(").append(chunkStatus).append("%)#").append(chunkStatus); 287 | String threadLog = threadLogBuilder.toString(); 288 | byte[] threadMessage = DataProcess.messageCreate("REQ", Main.deviceIP, "300", threadLog); 289 | handler.modifyRecord(this.uuid, String.valueOf(chunkStatus)); 290 | handler.modifyRecord(this.uuid, "loding("+chunkStatus+"%)", 4); 291 | this.agent.send(threadMessage); 292 | 293 | return chunkCreateStatus.get(); 294 | } catch (Exception e) { 295 | e.printStackTrace(); 296 | } 297 | return -1; 298 | } 299 | 300 | 301 | 302 | 303 | public void receive(String address, String fileName, byte[] data) { 304 | String filePath = Main.storageFolder+"chunk/"+fileName; 305 | if(!this.fileWrite(filePath, data)) { 306 | return; 307 | } 308 | 309 | String number = fileName.substring(fileName.indexOf("_"), fileName.length()); 310 | 311 | if(chunkLength == chunkShareStatus.incrementAndGet()) { 312 | // System.out.println(fileName+"\t"+chunkShareStatus.get()+"/"+chunkLength); 313 | new Thread(()->{ 314 | try { 315 | FileMonitor.ignoreFile(this.uuid); 316 | mergeFiles(); 317 | Thread.sleep(Main.INTERVAL*3); 318 | FileMonitor.unignoreFile(this.uuid); 319 | } catch (Exception e) { 320 | e.printStackTrace(); 321 | } 322 | }); 323 | delInstance(this.uuid); 324 | } 325 | // System.out.println(fileName+"\t"+chunkShareStatus.get()+"/"+chunkLength); 326 | byte[] message = DataProcess.messageCreate("REQ", Main.deviceIP, "400", fileName, "success"); 327 | agent.send(address, message); 328 | } 329 | private Boolean fileWrite(String filePath, byte[] data) { 330 | try (FileOutputStream fos = new FileOutputStream(filePath)) { 331 | fos.write(data); 332 | } catch (Exception e) { 333 | e.printStackTrace(); 334 | } 335 | return true; 336 | } 337 | private void mergeFiles() { 338 | String filePath = Main.storageFolder + this.uuid; 339 | try (FileOutputStream fos = new FileOutputStream(filePath)) { 340 | for(int i = 0; i < this.chunkLength; i++) { 341 | String chunkPath = Main.storageFolder + "chunk/" + this.uuid + "_" + i; 342 | File file = new File(chunkPath); 343 | byte[] fileBytes = new byte[(int) file.length()]; 344 | try (FileInputStream fis = new FileInputStream(file)) { 345 | int bytesRead = fis.read(fileBytes); 346 | assert(bytesRead == fileBytes.length); 347 | assert(bytesRead == (int) file.length()); 348 | fos.write(fileBytes); 349 | fos.flush(); 350 | } catch (Exception e) { 351 | e.printStackTrace(); 352 | } 353 | } 354 | } catch (Exception e) { 355 | e.printStackTrace(); 356 | private void loadingBar(String type, int count, int total) { 357 | int barLength = 30; 358 | double percent = (double) count / total; 359 | String format = "%"+(total+"").length()+"d"; 360 | System.out.print("\r"+this.uuid+" ["); 361 | int j = 0; 362 | for (; j < (int) (percent * barLength); j++) { 363 | switch(type) { 364 | case "loading": 365 | System.out.print("-"); 366 | break; 367 | case "progress": 368 | System.out.print("="); 369 | break; 370 | } 371 | } 372 | for (; j < barLength; j++) { 373 | System.out.print(" "); 374 | } 375 | System.out.print("] " + String.format(format, count) + "/" + total); 376 | } 377 | } 378 | } 379 | --------------------------------------------------------------------------------