// This file is part of me.christopherdavis.beanstalkc
// Copyright: (c) 2014 Christopher Davis <http://christopherdavis.me>
// License: MIT http://opensource.org/licenses/MIT

package me.christopherdavis.beanstalkc.adapter;

import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import me.christopherdavis.beanstalkc.Adapter;
import me.christopherdavis.beanstalkc.Command;
import me.christopherdavis.beanstalkc.BeanstalkcException;
import me.christopherdavis.beanstalkc.exception.ConnectionException;

/**
 * Wraps up a native Socket with our adapter.
 *
 * @since   0.1
 * @author  Christopher Davis <http://christopehrdavis.me>
 */
public class SocketAdapter implements Adapter
{
    private Socket sock;

    public SocketAdapter(Socket sock)
    {
        this.sock = sock;
    }

    public SocketAdapter(String hostname, int port) throws BeanstalkcException
    {
        try {
            this.sock = new Socket(hostname, port);
        } catch (Exception e) {
            throw new ConnectionException(e.getMessage(), e);
        }
    }

    public <T> T perform(Command<T> cmd) throws BeanstalkcException
    {
        try {
            return cmd.execute(sock.getInputStream(), sock.getOutputStream());
        } catch (IOException e) {
            throw new ConnectionException(e.getMessage(), e);
        }
    }
}

@@ -1374,11 +1374,210 @@
  %7D%0A    %7D
+%0A%0A    public void close() throws IOException%0A    %7B%0A        if (!sock.isClosed()) %7B%0A            sock.close();%0A        %7D%0A    %7D%0A%0A    public boolean isClosed()%0A    %7B%0A        return sock.isClosed();%0A    %7D
 %0A%7D%0A

Implement Adapter#close and Adapter#isClosed