/tp1-ift585/src/ift585/client/Client.java

http://ift-585.googlecode.com/ · Java · 142 lines · 107 code · 32 blank · 3 comment · 4 complexity · 0c3be08d1197f8cc8234362b4ef90ede MD5 · raw file

  1. package ift585.client;
  2. import java.io.IOException;
  3. import java.net.DatagramPacket;
  4. import java.net.DatagramSocket;
  5. import java.net.InetAddress;
  6. import java.net.SocketException;
  7. import java.net.SocketTimeoutException;
  8. import java.net.UnknownHostException;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. public class Client {
  12. private static final int INIT_TIMEOUT = 10000;
  13. private static final int PACKET_LENGTH = 1024;
  14. private String serverIP;
  15. private int serverPort;
  16. private DatagramSocket serverInitComm = null;
  17. private InetAddress channelAddress;
  18. private int channelPort;
  19. private String fileToSend;
  20. private static int FAILED_TRANSACTION_THRESHOLD = 5;
  21. public Client(String serverAddress, int serverInitPort, String fileName) throws SocketException {
  22. serverIP = serverAddress;
  23. serverPort = serverInitPort;
  24. serverInitComm = new DatagramSocket();
  25. fileToSend = fileName;
  26. }
  27. public void execute(){
  28. try {
  29. System.out.println("Initialize communication");
  30. initComunication();
  31. System.out.println("Request accepted, beginning transfert");
  32. senFile();
  33. }
  34. catch (SocketTimeoutException e) {
  35. System.out.println("Server is not responding to initial request. Server may be too busy.");
  36. // e.printStackTrace();
  37. }
  38. catch (UnknownHostException e) {
  39. System.out.println("Specified host is unreachable. Try to validate host address.");
  40. // e.printStackTrace();
  41. } catch (IOException e) {
  42. System.out.println("Cannot create datagram socket.");
  43. // e.printStackTrace();
  44. }
  45. finally{
  46. System.out.println("Application will terminate.");
  47. }
  48. }
  49. private void initComunication() throws UnknownHostException, IOException {
  50. byte [] sendBuffer = new byte[PACKET_LENGTH];
  51. sendBuffer = new String(fileToSend).getBytes();
  52. serverInitComm.send(new DatagramPacket(sendBuffer, sendBuffer.length, InetAddress.getByName(serverIP), serverPort));
  53. byte [] receiveBuffer = new byte[PACKET_LENGTH];
  54. DatagramPacket receivedPacket = new DatagramPacket(receiveBuffer, PACKET_LENGTH);
  55. serverInitComm.setSoTimeout(INIT_TIMEOUT);
  56. serverInitComm.receive(receivedPacket);
  57. serverInitComm.setSoTimeout(0);
  58. channelAddress = receivedPacket.getAddress();
  59. channelPort = receivedPacket.getPort();
  60. }
  61. private void senFile() throws IOException {
  62. FenetreClient clientWindow = new FenetreClient(fileToSend);
  63. TransactionHandler transacHandler = new TransactionHandler();
  64. List<DatagramPacket> toSend = new ArrayList<DatagramPacket>();
  65. int failedTransaction = 0;
  66. boolean keepSending = true;
  67. List<byte[]> dataToSend = clientWindow.getProchainsPaquets();
  68. byte[] response = new byte[1];
  69. response[0] = 0;
  70. while(dataToSend.size() != 0 && keepSending){
  71. toSend = buildPacketToSend(dataToSend);
  72. try {
  73. response = transacHandler.doTransaction(toSend, serverInitComm);
  74. failedTransaction = 0;
  75. } catch (UnsuccessfulTransactionException e) {
  76. ++failedTransaction;
  77. System.out.println("Failed transaction detected");
  78. if(failedTransaction >= FAILED_TRANSACTION_THRESHOLD){
  79. keepSending = false;
  80. System.out.println("Too much failed transaction. Aborting transmission");
  81. }
  82. }
  83. dataToSend = clientWindow.getProchainsPaquets(response);
  84. toSend.clear();
  85. }
  86. sendEndOfTransmission();
  87. }
  88. private List<DatagramPacket> buildPacketToSend(List<byte[]> dataToSend) {
  89. List<DatagramPacket> toSend = new ArrayList<DatagramPacket>();
  90. for(int i = 0 ; i < dataToSend.size() ; ++i){
  91. toSend.add(new DatagramPacket(dataToSend.get(i), dataToSend.get(i).length, channelAddress, channelPort));
  92. }
  93. return toSend;
  94. }
  95. private void sendEndOfTransmission() throws IOException {
  96. System.out.println("Sending end of transmission");
  97. byte [] endOfTransmission = new byte [1];
  98. endOfTransmission[0] = FenetreClient.CLOSE_ACK;
  99. serverInitComm.send(new DatagramPacket(endOfTransmission, endOfTransmission.length, channelAddress, channelPort));
  100. }
  101. public static void main(String[] args) {
  102. try {
  103. System.out.println("Client initialization");
  104. Client oneClient = new Client(args[0], Integer.valueOf(args[1]), args[2]);
  105. oneClient.execute();
  106. } catch (Exception e) {
  107. System.out.println(e.getMessage());
  108. }
  109. }
  110. }