package com.sudoku.comm;

import com.sudoku.comm.generated.Message;
import com.sudoku.comm.generated.NodeExplorer;
import com.sudoku.data.manager.UserManager;
import com.sudoku.data.model.User;
import org.apache.avro.AvroRemoteException;
import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.specific.SpecificRequestor;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

// Class representing a connection to a remote user
public class AvroConnectionManager extends ConnectionManager {
  private NettyTransceiver client;
  private NodeExplorer explorer;

  public AvroConnectionManager(String ip) {
    super(ip);
  }


  public void openConnection()
     throws OfflineUserException {
    System.out.println("CM - connect START");
    try {
      client = new NettyTransceiver(new InetSocketAddress(ipAddress, NODE_PORT));
      explorer = (NodeExplorer)
          SpecificRequestor.getClient(NodeExplorer.class, client);
    }
    catch(IOException exc) {throw new OfflineUserException();}
    System.out.println("CM - connect STOP");
    isConnected = true;
  }

  public List<String> getConnectedIps(ArrayList<String> newConnectedIps)
     throws ConnectionClosedException, OfflineUserException {
    System.out.println("CM - getConnectedIps START");
    super.getConnectedIps(newConnectedIps);
    User localUser = UserManager.getInstance().getLoggedUser();
    //Message userCredentials = new Message("wut", localUser.getPseudo(), newConnectedIps);
    Message userCredentials = new Message("wut", "toto", newConnectedIps);
    List<String> res;


    try {
      Message receivedMessage = explorer.discoverNode(userCredentials);
      res = receivedMessage.getListIps();
    }
    catch (AvroRemoteException exc) {throw new OfflineUserException();}
    System.out.println("CM - getConnectedIps STOP");
    return res;
  }

  public void closeConnection() throws OfflineUserException {
    System.out.println("CM - CLOSE CONNECTION");
    client.close();
    client = null;
    isConnected = false;
  }

  /*public ArrayList<Grid> getGrids() throws ConnectionClosedException {

  }
  
  public User getProfile() throws ConnectionClosedException {

  }

  public void pushComment(Comment c, Grid g) throws ConnectionClosedException {

  }

  public void publishComment(Comment c, Grid g) throws ConnectionClosedException {

  }
  */
}

@@ -356,24 +356,47 @@
 OException;%0A
+import java.lang.Long;%0A
 import java.
@@ -469,17 +469,16 @@
 l.List;%0A
-%0A
 // Class
@@ -584,16 +584,83 @@
 nager %7B%0A
+  private final static Long CONNECTION_TIME_OUT = new Long(1000);%0A%0A
   privat
@@ -855,54 +855,8 @@
 n %7B%0A
-    System.out.println(%22CM - connect START%22);%0A
     
@@ -940,16 +940,37 @@
 DE_PORT)
+, CONNECTION_TIME_OUT
 );%0A     
@@ -1136,53 +1136,8 @@
 );%7D%0A
-    System.out.println(%22CM - connect STOP%22);%0A
     
@@ -1300,62 +1300,8 @@
 n %7B%0A
-    System.out.println(%22CM - getConnectedIps START%22);%0A
     
@@ -1348,29 +1348,47 @@
     
-User localUser = User
+CommunicationManager cm = Communication
 Mana
@@ -1408,32 +1408,14 @@
 ce()
-.getLoggedUser()
 ;%0A    
-//
 Mess
@@ -1452,111 +1452,35 @@
 age(
-%22wut%22, localUser.getPseudo(), newConnectedIps);%0A    Message userCredentials = new Message(%22wut%22, %22toto%22
+cm.getUuid(), cm.getLogin()
 , ne
@@ -1518,17 +1518,16 @@
 %3E res;%0A%0A
-%0A
     try 
@@ -1724,61 +1724,8 @@
 );%7D%0A
-    System.out.println(%22CM - getConnectedIps STOP%22);%0A
     
@@ -1811,52 +1811,168 @@
     
-System.out.println(%22CM - CLOSE CONNECTION%22
+ try %7B%0A      explorer.disconnect(CommunicationManager.getInstance().getLocalIp());%0A    %7D%0A    catch (AvroRemoteException exc) %7Bthrow new OfflineUserException(
 );
+%7D
 %0A   

Set connection timeout for avro connection manager.