// Path: gnikrap-core/src/main/java/org/gnikrap/GnikrapAppContext.java
// public interface GnikrapAppContext {
// 
//   /**
//    * Returns the {@link GnikrapApp} object (the application entry point).
//    */
//   GnikrapApp getGnikrapApp();
// 
//   /**
//    * Returns the {@link EV3ActionProcessor} object (process asynchronously all the actions/events from the GUI).
//    */
//   EV3ActionProcessor getEV3ActionProcessor();
// 
//   /**
//    * Returns the {@link ScriptExecutionManager} object (manage the script that run on Gnikrap).
//    */
//   ScriptExecutionManager getScriptExecutionManager();
// 
//   /**
//    * Returns {@link Configuration} object (Application configuration configuration loaded from the config file).
//    */
//   Configuration getConfiguration();
// 
//   /**
//    * Returns the {@link EV3SriptCommandSocketConnectionCallback} object (Enable to send back information to the GUI).
//    */
//   EV3SriptCommandSocketConnectionCallback getEV3SriptCommandSocketConnectionCallback();
// }
// 
// Path: gnikrap-core/src/main/java/org/gnikrap/utils/LoggerUtils.java
// public class LoggerUtils {
// 
//   private LoggerUtils() { // Avoid instantiation
//   }
// 
//   /**
//    * Default initialization of the Logging fwk - Replace default formatter.
//    */
//   public static void initializeLogging() {
//     Logger.getLogger("");
// 
//     for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames(); e.hasMoreElements();) {
//       for (Handler h : Logger.getLogger(e.nextElement()).getHandlers()) {
//         h.setFormatter(new OneLineFormatter());
//         h.setLevel(Level.ALL); // Only filter Logger level
//       }
//     }
//   }
// 
//   /**
//    * 
//    */
//   public static void setDefaultLogLevel(String level) {
//     Level l = Level.parse(level);
//     initializeLogging();
// 
//     for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames(); e.hasMoreElements();) {
//       Logger.getLogger(e.nextElement()).setLevel(l);
//     }
//   }
// 
//   /**
//    * @return the {@see Logger} for this class
//    */
//   public static Logger getLogger(Class<?> clazz) {
//     return Logger.getLogger(clazz.getName());
//   }
// 
//   /**
//    * Print a log on a simple line
//    */
//   public static final class OneLineFormatter extends Formatter {
//     private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
//     private static final String FIELD_SEPARATOR = " - ";
// 
//     private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS");;
//     private final Date tempDate = new Date();
// 
//     /**
//      * Format the given LogRecord.
//      * 
//      * @param record the log record to be formatted.
//      * @return a formatted log record
//      */
//     @Override
//     public synchronized String format(final LogRecord record) {
//       StringBuilder sb = new StringBuilder(256);
// 
//       // Date
//       tempDate.setTime(record.getMillis());
//       sb.append(dateFormat.format(tempDate)).append(FIELD_SEPARATOR);
// 
//       // Level
//       sb.append(record.getLevel().getName()).append(FIELD_SEPARATOR);
// 
//       // Source
//       if (record.getSourceClassName() != null) {
//         sb.append(record.getSourceClassName());
//       } else {
//         sb.append(record.getLoggerName());
//       }
//       if (record.getSourceMethodName() != null) {
//         sb.append("/").append(record.getSourceMethodName());
//       }
//       sb.append(FIELD_SEPARATOR);
// 
//       // Message
//       sb.append(formatMessage(record)).append(LINE_SEPARATOR);
// 
//       // Exceptions (if any)
//       if (record.getThrown() != null) {
//         StringWriter sw = new StringWriter(256);
//         PrintWriter pw = new PrintWriter(sw);
//         record.getThrown().printStackTrace(pw);
//         sb.append(sw);
//       }
// 
//       return sb.toString();
//     }
//   }
// }


import java.util.logging.Level;
import java.util.logging.Logger;
import org.gnikrap.GnikrapAppContext;
import org.gnikrap.utils.LoggerUtils;
import org.xnio.ChannelListener;
import io.undertow.websockets.WebSocketConnectionCallback;
import io.undertow.websockets.core.AbstractReceiveListener;
import io.undertow.websockets.core.BufferedTextMessage;
import io.undertow.websockets.core.StreamSourceFrameChannel;
import io.undertow.websockets.core.WebSocketCallback;
import io.undertow.websockets.core.WebSocketChannel;
import io.undertow.websockets.core.WebSockets;
import io.undertow.websockets.spi.WebSocketHttpExchange;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
/*
 * Gnikrap is a simple scripting environment for the Lego Mindstrom EV3
 * Copyright (C) 2014-2015 Jean BENECH
 * 
 * Gnikrap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Gnikrap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Gnikrap.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.gnikrap.script;




/**
 * Handle the WebSocket connections for the Gnikrap web application.
 */
final public class EV3SriptCommandSocketConnectionCallback implements WebSocketConnectionCallback {
  private static final Logger LOGGER = LoggerUtils.getLogger(EV3SriptCommandSocketConnectionCallback.class);

  /** List of currently active WebSocket connections */
  private final List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();

  private final EV3ActionProcessor ev3ActionProcessor;

// Path: gnikrap-core/src/main/java/org/gnikrap/GnikrapAppContext.java
// public interface GnikrapAppContext {
// 
//   /**
//    * Returns the {@link GnikrapApp} object (the application entry point).
//    */
//   GnikrapApp getGnikrapApp();
// 
//   /**
//    * Returns the {@link EV3ActionProcessor} object (process asynchronously all the actions/events from the GUI).
//    */
//   EV3ActionProcessor getEV3ActionProcessor();
// 
//   /**
//    * Returns the {@link ScriptExecutionManager} object (manage the script that run on Gnikrap).
//    */
//   ScriptExecutionManager getScriptExecutionManager();
// 
//   /**
//    * Returns {@link Configuration} object (Application configuration configuration loaded from the config file).
//    */
//   Configuration getConfiguration();
// 
//   /**
//    * Returns the {@link EV3SriptCommandSocketConnectionCallback} object (Enable to send back information to the GUI).
//    */
//   EV3SriptCommandSocketConnectionCallback getEV3SriptCommandSocketConnectionCallback();
// }
// 
// Path: gnikrap-core/src/main/java/org/gnikrap/utils/LoggerUtils.java
// public class LoggerUtils {
// 
//   private LoggerUtils() { // Avoid instantiation
//   }
// 
//   /**
//    * Default initialization of the Logging fwk - Replace default formatter.
//    */
//   public static void initializeLogging() {
//     Logger.getLogger("");
// 
//     for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames(); e.hasMoreElements();) {
//       for (Handler h : Logger.getLogger(e.nextElement()).getHandlers()) {
//         h.setFormatter(new OneLineFormatter());
//         h.setLevel(Level.ALL); // Only filter Logger level
//       }
//     }
//   }
// 
//   /**
//    * 
//    */
//   public static void setDefaultLogLevel(String level) {
//     Level l = Level.parse(level);
//     initializeLogging();
// 
//     for (Enumeration<String> e = LogManager.getLogManager().getLoggerNames(); e.hasMoreElements();) {
//       Logger.getLogger(e.nextElement()).setLevel(l);
//     }
//   }
// 
//   /**
//    * @return the {@see Logger} for this class
//    */
//   public static Logger getLogger(Class<?> clazz) {
//     return Logger.getLogger(clazz.getName());
//   }
// 
//   /**
//    * Print a log on a simple line
//    */
//   public static final class OneLineFormatter extends Formatter {
//     private static final String LINE_SEPARATOR = System.getProperty("line.separator", "\n");
//     private static final String FIELD_SEPARATOR = " - ";
// 
//     private final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd HH:mm:ss.SSS");;
//     private final Date tempDate = new Date();
// 
//     /**
//      * Format the given LogRecord.
//      * 
//      * @param record the log record to be formatted.
//      * @return a formatted log record
//      */
//     @Override
//     public synchronized String format(final LogRecord record) {
//       StringBuilder sb = new StringBuilder(256);
// 
//       // Date
//       tempDate.setTime(record.getMillis());
//       sb.append(dateFormat.format(tempDate)).append(FIELD_SEPARATOR);
// 
//       // Level
//       sb.append(record.getLevel().getName()).append(FIELD_SEPARATOR);
// 
//       // Source
//       if (record.getSourceClassName() != null) {
//         sb.append(record.getSourceClassName());
//       } else {
//         sb.append(record.getLoggerName());
//       }
//       if (record.getSourceMethodName() != null) {
//         sb.append("/").append(record.getSourceMethodName());
//       }
//       sb.append(FIELD_SEPARATOR);
// 
//       // Message
//       sb.append(formatMessage(record)).append(LINE_SEPARATOR);
// 
//       // Exceptions (if any)
//       if (record.getThrown() != null) {
//         StringWriter sw = new StringWriter(256);
//         PrintWriter pw = new PrintWriter(sw);
//         record.getThrown().printStackTrace(pw);
//         sb.append(sw);
//       }
// 
//       return sb.toString();
//     }
//   }
// }


// Path: gnikrap-core/src/main/java/org/gnikrap/script/EV3SriptCommandSocketConnectionCallback.java
import java.util.logging.Level;
import java.util.logging.Logger;
import org.gnikrap.GnikrapAppContext;
import org.gnikrap.utils.LoggerUtils;
import org.xnio.ChannelListener;
import io.undertow.websockets.WebSocketConnectionCallback;
import io.undertow.websockets.core.AbstractReceiveListener;
import io.undertow.websockets.core.BufferedTextMessage;
import io.undertow.websockets.core.StreamSourceFrameChannel;
import io.undertow.websockets.core.WebSocketCallback;
import io.undertow.websockets.core.WebSocketChannel;
import io.undertow.websockets.core.WebSockets;
import io.undertow.websockets.spi.WebSocketHttpExchange;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/*
 * Gnikrap is a simple scripting environment for the Lego Mindstrom EV3
 * Copyright (C) 2014-2015 Jean BENECH
 * 
 * Gnikrap is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Gnikrap is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Gnikrap.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.gnikrap.script;




/**
 * Handle the WebSocket connections for the Gnikrap web application.
 */
final public class EV3SriptCommandSocketConnectionCallback implements WebSocketConnectionCallback {
  private static final Logger LOGGER = LoggerUtils.getLogger(EV3SriptCommandSocketConnectionCallback.class);

  /** List of currently active WebSocket connections */
  private final List<WebSocketSession> sessions = new ArrayList<WebSocketSession>();

  private final EV3ActionProcessor ev3ActionProcessor;

  public EV3SriptCommandSocketConnectionCallback(GnikrapAppContext context) {