package no.ntnu.okse.protocol.amqp091;
import fr.dyade.aaa.agent.AgentServer;
import no.ntnu.okse.core.messaging.Message;
import no.ntnu.okse.protocol.AbstractProtocolServer;
import org.apache.log4j.Logger;
import org.ow2.joram.mom.amqp.AMQPService;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Created by Andreas the time lord on 30/03/2516
 */
public class AMQP091ProtocolServer extends AbstractProtocolServer {


    protected static final String SERVERTYPE = "amqp091";


    public AMQP091ProtocolServer(String host, int port) throws IOException {
        this.port = port;
        this.host = host;
        log = Logger.getLogger(AMQP091ProtocolServer.class.getName());
    }

    @Override
    public void boot() {

        //_serverThread

        if (!_running) {
            _running = true;
            _serverThread = new Thread(() -> this.run());
            _serverThread.setName("AMQ091ProtocolServer");
            _serverThread.start();
            log.info("AMQ091ProtocolServer successfully");
        }
    }

    @Override
    public void run() {
        try {
            AgentServer.init((short) 0, "./s0", null);
            AMQPService.init("" + port, true);
            AMQPService.addMessageListener(new AMQP091MessageListener(this));
            AMQPService.setPublishing(false);
        } catch (Exception e) {
            // TODO: Properly handle exception
            e.printStackTrace();
        }
    }

    @Override
    public void stopServer() {
        _running = false;
    }

    @Override
    public String getProtocolServerType() {
        return SERVERTYPE;
    }

    @Override
    public void sendMessage(Message message) {
        AMQPService.internalPublish(message.getTopic(), "", message.getMessage().getBytes(StandardCharsets.UTF_8));
    }
}

@@ -244,16 +244,37 @@
 rvice;%0A%0A
+import java.io.File;%0A
 import j
@@ -293,16 +293,16 @@
 eption;%0A
-
 import j
@@ -334,16 +334,99 @@
 harsets;
+%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;
 %0A%0A/**%0A *
@@ -1128,16 +1128,23 @@
 lServer 
+booted 
 successf
@@ -1220,16 +1220,106 @@
   try %7B%0A
+            log.debug(String.format(%22Starting AMQP 0.9.1 service on %25s:%25d%22, host, port));%0A
         
@@ -1354,14 +1354,27 @@
  0, 
-%22./s0%22
+createAgentFolder()
 , nu
@@ -1416,16 +1416,29 @@
 %22 + port
+ + %22 %22 + host
 , true);
@@ -1683,32 +1683,587 @@
        %7D%0A    %7D%0A%0A
+    private String createAgentFolder() %7B%0A        String directoryName = %22agent-server%22;%0A        try %7B%0A            Path agentFolder = Files.createTempDirectory(directoryName);%0A            return agentFolder.toString();%0A        %7D catch (IOException e) %7B%0A            log.warn(%22Failed to create temporary folder, creating folder in local directory%22);%0A            File localDirectory = new File(directoryName);%0A            if(!localDirectory.exists()) %7B%0A                localDirectory.mkdir();g%0A            %7D%0A            return directoryName;%0A        %7D%0A    %7D%0A%0A
     @Override%0A  
@@ -2601,8 +2601,9 @@
     %7D%0A%7D%0A
+;

Create tmp folder for AgentServer