package org.fedoraproject.fedmsg;

import org.zeromq.ZMQ;

import java.io.IOException;

import fj.F;
import fj.Unit;
import fj.data.Either;
import fj.data.IO;

/**
 * Connect to a fedmsg bus.
 *
 * @author Ricky Elrod
 * @version 1.0.0
 */
public final class FedmsgConnection {
    private final String endpoint;
    private final int linger;
    private ZMQ.Socket sock;
    private ZMQ.Context context;

    public FedmsgConnection(
        String endpoint,
        int linger
    ) {
        this.endpoint = endpoint;
        this.linger = linger;
    }

    public String getEndpoint() {
        return this.endpoint;
    }

    public int getLinger() {
        return this.linger;
    }

    /**
     * @return a FedmsgConnection wrapped in an (impure) IO monad.
     */
    public IO<FedmsgConnection> connect() {
        return new IO<FedmsgConnection>() {
            public FedmsgConnection run() {
                FedmsgConnection.this.context = ZMQ.context(1);
                FedmsgConnection.this.sock = context.socket(ZMQ.PUB);
                FedmsgConnection.this.sock.setLinger(FedmsgConnection.this.linger);
                FedmsgConnection.this.sock.connect(FedmsgConnection.this.endpoint);
                return FedmsgConnection.this;
            }
        };
    }

    public IO<Either<Exception, Unit>> send(final FedmsgMessage msg) {
        return new IO<Either<Exception, Unit>>() {
            public Either<Exception, Unit> run() {
                try {
                    FedmsgConnection.this.sock.send(msg.getTopic(), ZMQ.SNDMORE | ZMQ.NOBLOCK);
                    FedmsgConnection.this.sock.send(msg.toJson().toString(), ZMQ.NOBLOCK);
                    return Either.right(Unit.unit());
                } catch (Exception e) {
                    return Either.left(e);
                }
            }
        };
    }
}

@@ -85,80 +85,8 @@
 n;%0A%0A
-import fj.F;%0Aimport fj.Unit;%0Aimport fj.data.Either;%0Aimport fj.data.IO;%0A%0A
 /**%0A
@@ -272,24 +272,30 @@
     private 
+final 
 ZMQ.Socket s
@@ -311,16 +311,22 @@
 private 
+final 
 ZMQ.Cont
@@ -630,91 +630,8 @@
  %7D%0A%0A
-    /**%0A     * @return a FedmsgConnection wrapped in an (impure) IO monad.%0A     */%0A
     
@@ -637,19 +637,16 @@
  public 
-IO%3C
 FedmsgCo
@@ -653,17 +653,16 @@
 nnection
-%3E
  connect
@@ -678,121 +678,8 @@
     
-return new IO%3CFedmsgConnection%3E() %7B%0A            public FedmsgConnection run() %7B%0A                FedmsgConnection.
 this
@@ -717,33 +717,8 @@
     
-        FedmsgConnection.
 this
@@ -762,33 +762,8 @@
     
-        FedmsgConnection.
 this
@@ -778,33 +778,16 @@
 tLinger(
-FedmsgConnection.
 this.lin
@@ -804,33 +804,8 @@
     
-        FedmsgConnection.
 this
@@ -818,33 +818,16 @@
 connect(
-FedmsgConnection.
 this.end
@@ -846,310 +846,97 @@
     
-     
-   
 return 
-FedmsgConnection.this;%0A            %7D%0A        %7D;%0A    %7D%0A%0A    public IO%3CEither%3CException, Unit%3E%3E send(final FedmsgMessage msg) %7B%0A        return new IO%3CEither%3CException, Unit%3E%3E() %7B%0A            public Either%3CException, Unit%3E run() %7B%0A                try %7B%0A                    FedmsgConnection.
+this;%0A    %7D%0A%0A    public void send(FedmsgMessage msg) throws IOException %7B%0A        
 this
@@ -1002,37 +1002,8 @@
     
-            FedmsgConnection.
 this
@@ -1056,188 +1056,8 @@
 K);%0A
-                    return Either.right(Unit.unit());%0A                %7D catch (Exception e) %7B%0A                    return Either.left(e);%0A                %7D%0A            %7D%0A        %7D;%0A
     

Revert "Wrap FedmsgConnection#send and FedmsgConnection#connect in an (impure) IO monad, because it is still useful"