BroadCast


SUBMITTED BY: Guest

DATE: Nov. 6, 2013, 12:34 p.m.

FORMAT: Java

SIZE: 11.7 kB

HITS: 960

  1. package Main;
  2. /*
  3. * To change this template, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. import java.net.*;
  7. import java.util.ArrayList;
  8. import java.io.*;
  9. /**
  10. *
  11. * @author phihai
  12. */
  13. public class BroadCast {
  14. DatagramSocket utpSocket;
  15. ArrayList<Networks> arr_nw = new ArrayList<Networks>();
  16. public BroadCast(){}
  17. public BroadCast(int Port)
  18. {
  19. try
  20. {
  21. utpSocket = new DatagramSocket(Port);
  22. Readmap();
  23. }
  24. catch (Exception e)
  25. {
  26. e.printStackTrace();
  27. }
  28. }
  29. private void send(BC_Packet pk,Networks nw ) // sned thông tin cho tất cả các client
  30. {
  31. try
  32. {
  33. byte[] buf = serialize(pk);
  34. DatagramPacket packet = new DatagramPacket(buf, buf.length, nw.GetAddress(), nw.GetPort());
  35. utpSocket.send(packet);
  36. }
  37. catch (Exception e)
  38. {
  39. e.printStackTrace();
  40. }
  41. }
  42. public void sendBroadCast(BC_Packet pk) // hàm dùng để gửi thông tin pk cho các clien trong node.map
  43. {
  44. for(int i=0;i<arr_nw.size();i++)
  45. {
  46. send(pk,arr_nw.get(i));
  47. }
  48. }
  49. private void Readmap() // hàm đọc thông tin node.map
  50. {
  51. try
  52. {
  53. String str ;
  54. String [] array;
  55. int port;
  56. BufferedReader br = new BufferedReader(new FileReader("nodes.map"));
  57. int count = Integer.parseInt(br.readLine());
  58. for(int i=0;i<count;i++)
  59. {
  60. str = br.readLine();
  61. array = str.split(" ");
  62. port = Integer.parseInt(array[2]);
  63. arr_nw.add(new Networks(InetAddress.getByName(array[1]),port)); // arr_nw lấy thông tin IP + port
  64. }
  65. while ((str = br.readLine()) != null)
  66. {
  67. array = str.split(" "); // lấy thông tin cost -> chưa làm
  68. //System.out.println("Id: "+array[0] + " IP: "+array[1] +" Port: "+array[2]);
  69. }
  70. }
  71. catch (Exception e)
  72. {
  73. e.printStackTrace();
  74. }
  75. }
  76. protected void startListenerThread() // tạo cổng lắng nghe
  77. {
  78. ReceiveRP thread = new ReceiveRP(utpSocket);
  79. thread.start();
  80. }
  81. public byte[] serialize(Object obj) throws IOException // chuyển các object thành dạng byte để tiến hành gửi thông tin
  82. {
  83. ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
  84. ObjectOutputStream objectStream = new ObjectOutputStream(byteStream);
  85. objectStream.writeObject(obj);
  86. objectStream.flush();
  87. return byteStream.toByteArray();
  88. }
  89. }
  90. class BC_Packet implements Serializable // lưu thông tin broadcast cần thiết cho việc gửi gói tin
  91. {
  92. private int Flag;
  93. private String msg;
  94. private int part;
  95. private String filename;
  96. private byte[] data;
  97. private int fileSize;
  98. private long headerlength;
  99. private int Sequence;
  100. private int Acknowledge;
  101. public BC_Packet(){}
  102. public BC_Packet(int Flag,int part,String msg)
  103. {
  104. this.Flag = Flag;
  105. this.part = part;
  106. this.msg = msg;
  107. }
  108. public int getFlag() {
  109. return Flag;
  110. }
  111. public void setFlag(int Flag) {
  112. this.Flag = Flag;
  113. }
  114. public String getMsg() {
  115. return msg;
  116. }
  117. public void setMsg(String msg) {
  118. this.msg = msg;
  119. }
  120. public int getPart() {
  121. return part;
  122. }
  123. public void setPart(int part) {
  124. this.part = part;
  125. }
  126. public String getFilename() {
  127. return filename;
  128. }
  129. public void setFilename(String filename) {
  130. this.filename = filename;
  131. }
  132. public byte[] getData() {
  133. return data;
  134. }
  135. public void setData(byte[] data) {
  136. this.data = data;
  137. }
  138. public long getHeaderlength() {
  139. return headerlength;
  140. }
  141. public void setHeaderlength(long headerlength) {
  142. this.headerlength = headerlength;
  143. }
  144. public int getSequence() {
  145. return Sequence;
  146. }
  147. public void setSequence(int Sequence) {
  148. this.Sequence = Sequence;
  149. }
  150. public int getAcknowledge() {
  151. return Acknowledge;
  152. }
  153. public void setAcknowledge(int Acknowledge) {
  154. this.Acknowledge = Acknowledge;
  155. }
  156. public int getFileSize() {
  157. return fileSize;
  158. }
  159. public void setFileSize(int fileSize) {
  160. this.fileSize = fileSize;
  161. }
  162. }
  163. class ReceiveRP extends Thread // nhận thông tin broadcast
  164. {
  165. DatagramSocket socket;
  166. BroadCast bc = new BroadCast();
  167. BC_Packet pk;
  168. Chunks ck = new Chunks();
  169. byte[] Buf = new byte[1500];
  170. DatagramPacket Packet;
  171. byte [] Chunks = new byte[1024*512];
  172. public ReceiveRP(DatagramSocket socket)
  173. {
  174. this.socket = socket;
  175. }
  176. public void run()
  177. {
  178. while(true)
  179. {
  180. try
  181. {
  182. Packet = new DatagramPacket(Buf, Buf.length);
  183. socket.receive(Packet);
  184. pk = (BC_Packet) deserialize(Packet.getData()); // tiến hành phân giải byte ra dạng object ( cụ thể là cho ra object thuộc class BC_Packet)
  185. if(pk.getFlag() == 0)// tìm chunks
  186. {
  187. String name = ck.Check_Chunks(pk.getMsg());
  188. pk.setFlag(1);
  189. if(name.equals("")) // nếu không tìm thấy chunks
  190. {
  191. pk.setMsg(" ");
  192. }
  193. else
  194. {
  195. pk.setMsg(name);
  196. }
  197. SendPacket();
  198. }
  199. else if(pk.getFlag() == 1)
  200. {
  201. if(!pk.getMsg().equals(" "))
  202. {
  203. pk.setFlag(2);
  204. pk.setFilename(pk.getMsg());
  205. }
  206. SendPacket();
  207. }
  208. else if(pk.getFlag() == 2)// read file
  209. {
  210. File file = new File("Share\\"+pk.getFilename());
  211. BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
  212. int size = (int) file.length();
  213. byte[] buffer = new byte[1024];
  214. int n;
  215. int downloaded = 0;
  216. while((n = bis.read(buffer,0,buffer.length)) != -1)
  217. {
  218. downloaded += n; // số lượng byte đọc được
  219. pk.setSequence(pk.getAcknowledge());
  220. pk.setAcknowledge(downloaded);
  221. pk.setFileSize(size);
  222. pk.setData(buffer);
  223. pk.setFlag(3);
  224. System.out.println(buffer);
  225. SendPacket(); // gửi packet
  226. }
  227. bis.close();
  228. pk.setFlag(-1); // thoát khỏi quá trình truyền file
  229. }
  230. else if(pk.getFlag() == 3)// tìm chunks
  231. {
  232. System.out.println(pk.getFilename()+" "+pk.getSequence()+" "+pk.getAcknowledge()+" "+pk.getFileSize());
  233. System.arraycopy(pk.getData(),0,Chunks,pk.getSequence(),pk.getData().length); // ghép mảng pk.getdata(1024 bytes) nhận được vào mảng Chunks (512*1024)
  234. if(pk.getAcknowledge() == pk.getFileSize())
  235. {
  236. File file = new File("Share\\"+pk.getFilename());
  237. BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
  238. //out.write(Chunks,0,pk.getAcknowledge()); // tiến hành ghi file
  239. out.write(Chunks,0,pk.getFileSize()); //sửa lại
  240. out.flush();
  241. out.close();
  242. }
  243. }
  244. }
  245. catch (Exception e)
  246. {
  247. e.printStackTrace();
  248. }
  249. }
  250. }
  251. private void SendPacket()throws IOException
  252. {
  253. byte[] buf = bc.serialize(pk); // chuyển object (pk)-> packet sang dạng byte
  254. Packet = new DatagramPacket(buf, buf.length,Packet.getAddress(), Packet.getPort());
  255. socket.send(Packet);
  256. }
  257. private Object deserialize(byte[] bytes) throws IOException, ClassNotFoundException // hàm chuyển đổi thông tin byte nhận được sang object
  258. {
  259. ByteArrayInputStream byteStream = new ByteArrayInputStream(bytes);
  260. ObjectInputStream objectStream = new ObjectInputStream(byteStream);
  261. return objectStream.readObject();
  262. }
  263. }
  264. class Networks{ // các thông tin của mạng trong file node.map
  265. private InetAddress address;
  266. private int port;
  267. private int cost; // chưa lấy thông tin cost ở đây
  268. public Networks(InetAddress address,int port)
  269. {
  270. this.address = address;
  271. this.port = port;
  272. }
  273. public InetAddress GetAddress()
  274. {
  275. return address;
  276. }
  277. public void SetAddress(InetAddress address)
  278. {
  279. this.address = address;
  280. }
  281. public int GetPort()
  282. {
  283. return port;
  284. }
  285. public void SetPort(int port)
  286. {
  287. this.port = port;
  288. }
  289. public int GetCost()
  290. {
  291. return cost;
  292. }
  293. public void SetCost(int cost)
  294. {
  295. this.cost = cost;
  296. }
  297. }

comments powered by Disqus