package endtoend;

import org.json.simple.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import org.zeromq.ZMQ.Socket;


/**
 * This is used to emulate an external to vismo event producer. It is assumed that the producers are running in the same (Linux)
 * host.
 */
public class FakeEventProducer {
    /***/
    private final String eventsEntryPoint;
    /***/
    private final Logger log = LoggerFactory.getLogger(FakeEventProducer.class);
    /***/
    private final int    noEventsToSend;
    /***/
    private final Socket sock;


    /**
     * Constructor.
     * 
     * @param ctx
     * @param eventsEntryPoint
     * @param noEventsToSend
     */
    public FakeEventProducer(final ZContext ctx, final String eventsEntryPoint, final int noEventsToSend) {
        this.eventsEntryPoint = eventsEntryPoint;
        this.sock = ctx.createSocket(ZMQ.PUSH);
        this.sock.setLinger(0);
        this.noEventsToSend = noEventsToSend;
    }


    /***/
    public void sendEvents() {
        for (int i = 0; i < noEventsToSend; ++i)
            sendEvent("foo");
    }


    /***/
    public void start() {
        log.debug("connecting to endpoint={}", eventsEntryPoint);
        sock.connect(eventsEntryPoint);
    }


    /***/
    public void stop() {
        sock.close();
    }


    /**
     * @param s
     */
    @SuppressWarnings("unchecked")
    private void sendEvent(final String s) {
        log.debug("sending: {}", s);
        final JSONObject o = new JSONObject();

        o.put("timestamp", System.currentTimeMillis());
        o.put("val", s);

        sock.send(o.toJSONString().getBytes(), 0);
    }
}

@@ -12,16 +12,40 @@
 toend;%0A%0A
+import java.util.UUID;%0A%0A
 import o
@@ -416,60 +416,14 @@
 ate 
-final String eventsEntryPoint;%0A    /***/%0A    private
+static
  fin
@@ -516,19 +516,86 @@
 e final 
-int
+String        eventsEntryPoint;%0A    /***/%0A    private final int       
     noEv
@@ -641,16 +641,23 @@
  Socket 
+       
 sock;%0A%0A%0A
@@ -1186,13 +1186,8 @@
 ent(
-%22foo%22
 );%0A 
@@ -1419,30 +1419,8 @@
  /**
-%0A     * @param s%0A     
 */%0A 
@@ -1484,62 +1484,11 @@
 ent(
-final String s) %7B%0A        log.debug(%22sending: %7B%7D%22, s);
+) %7B
 %0A   
@@ -1607,15 +1607,210 @@
 ut(%22
-val%22, s
+topic%22, %22my-topic%22);%0A        o.put(%22originating-service%22, %22fake-event-producer%22);%0A        o.put(%22originating-ip%22, %22localhost%22);%0A        o.put(%22id%22, UUID.randomUUID());%0A%0A        log.trace(%22sending %7B%7D%22, o
 );%0A%0A

use an object more suited to an event