package ch.sheremet.dasfoo.rover.android.client.grpc.task;

import android.util.Log;

import java.util.concurrent.TimeUnit;

import ch.sheremet.dasfoo.rover.android.client.BuildConfig;
import dasfoo.grpc.roverserver.nano.RoverServiceGrpc;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import io.grpc.Metadata;
import io.grpc.stub.MetadataUtils;


/**
 * Created by Katarina Sheremet on 5/24/16 5:09 PM.
 */
public class GrpcConnection {
    private static final String TAG = GrpcConnection.class.getName();

    private final String mHost;

    private final int mPort;

    private final String mPassword;

    private ManagedChannel mChannel;

    private RoverServiceGrpc.RoverServiceBlockingStub mStub;

    private static Metadata.Key<String> authKey =
            Metadata.Key.of("authentication", Metadata.ASCII_STRING_MARSHALLER);



    public GrpcConnection(final String host, final int port, final String password) {
        this.mHost = host;
        this.mPort = port;
        this.mPassword = password;
        establishConnection();
    }

    public final int getPort() {
        return mPort;
    }

    public final String getHost() {
        return mHost;
    }

    public final String getPassword() {
        return mPassword;
    }

    private void establishConnection() {
        // Create header for stub
        Metadata headers = new Metadata();
        headers.put(authKey, mPassword);
        mChannel = ManagedChannelBuilder.forAddress(mHost, mPort).build();
        mStub = RoverServiceGrpc.newBlockingStub(mChannel);
        // Attach header to stub
        mStub = MetadataUtils.attachHeaders(mStub, headers);
    }

    public final RoverServiceGrpc.RoverServiceBlockingStub getStub() {
        return mStub;
    }

    public final boolean shutDownConnection() {
        try {
            mChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);
            return Boolean.TRUE;
        } catch (InterruptedException e) {
            if (BuildConfig.DEBUG) {
                Log.v(TAG, e.toString());
            }
            return Boolean.FALSE;
        }
    }
}

@@ -532,337 +532,636 @@
     
-private final String mHost;%0A%0A    private final int mPort;%0A%0A    private final String mPassword;%0A%0A    private ManagedChannel mChannel;%0A%0A    private RoverServiceGrpc.RoverServiceBlockingStub mStub;%0A%0A    private static Metadata.Key%3CString%3E authKey =%0A            Metadata.Key.of(%22authentication%22, Metadata.ASCII_STRING_MARSHALLER);%0A%0A%0A
+/**%0A     * Forms header.%0A     */%0A    private static Metadata.Key%3CString%3E authKey =%0A            Metadata.Key.of(%22authentication%22, Metadata.ASCII_STRING_MARSHALLER);%0A%0A    private final String mHost;%0A%0A    private final int mPort;%0A%0A    /**%0A     * Attaches to header for validation user on server.%0A     */%0A    private final String mPassword;%0A%0A    private ManagedChannel mChannel;%0A%0A    private RoverServiceGrpc.RoverServiceBlockingStub mStub;%0A%0A    /**%0A     * Constructor.%0A     *%0A     * @param host is for connection to the server%0A     * @param port is for connection to the server%0A     * @param password is for validating user%0A     */
 %0A   
@@ -1489,32 +1489,125 @@
 n mHost;%0A    %7D%0A%0A
+    /**%0A     * Getters method for returning password.%0A     *%0A     * @return password%0A     */%0A
     public final

Add comments