//==================================================================================================
// S i m u l a t o r                                                                      Interface
// U r b a n _ b u s
// P l u g i n
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library 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 Library General Public License for more details (http://www.gnu.org).

/*DESCRIPTION*/
/* This module provides a Java plugin for the simulation model, so it can be loaded and executed
   by the <CODE>SimulatorFrame</CODE> Java component. */

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "simulator/urban_bus/plugin.hpp"

// Guardian //--------------------------------------------------------------------------------------
#ifndef guSimulatorUrbanBusPlugin
#define guSimulatorUrbanBusPlugin

// Headers //---------------------------------------------------------------------------------------
#include <bpp/graphic/console.hpp> /*INCLUDE*/
#include <bpp/simulator/urban_bus/model.hpp> /*INCLUDE*/

namespace bpp {

// Importation/Exportation //-----------------------------------------------------------------------
#ifdef SIMULATOR_URBAN_BUS_DLL
 #define dll_export DLL_EXPORT
#else
 #define dll_export DLL_IMPORT
#endif

// Namespaces //------------------------------------------------------------------------------------
#define public_area  simulatorUrbanBusPlugin
#define private_area simulatorUrbanBusPlugin_private

namespace public_area  { /*NAMESPACE*/ using namespace simulatorUrbanBusModel; }
namespace private_area { using namespace public_area; }

extern_module_name;

// Initialization //--------------------------------------------------------------------------------

// Macrocommands //---------------------------------------------------------------------------------

// Types & Classes //-------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Interface //---------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Errors //----------------------------------------------------------------------------------------
namespace public_area {}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Interface //------------------------------------------------------------------------------
namespace {}

// Functions Inline //------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Inline //---------------------------------------------------------------------------------
namespace {}

// End //-------------------------------------------------------------------------------------------
}
#undef dll_export
#undef public_area
#undef private_area
#endif
 
//==================================================================================================
// S i m u l a t o r                                                                 Implementation
// U r b a n _ b u s
// P l u g i n
//                                                                                By Bruno Bachelet
//==================================================================================================
// Copyright (c) 1999-2016
// Bruno Bachelet - bruno@nawouak.net - http://www.nawouak.net
//
// This file is part of the B++ Library. This library is free software; you can redistribute it
// and/or modify it under the terms of the GNU Library General Public License as published by the
// Free Software Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This library 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 Library General Public License for more details (http://www.gnu.org).

// File Name //-------------------------------------------------------------------------------------
#line __LINE__ "simulator/urban_bus/plugin.cpp"

// DLL Belonging //---------------------------------------------------------------------------------
#define SIMULATOR_URBAN_BUS_DLL

// Headers //---------------------------------------------------------------------------------------
#include <bpp/simulator/urban_bus/plugin.hpp> /*INTERFACE*/
#include <jirk/java/lang/thread.hpp> /*NEED*/
#include <jirk/java/lang/string.hpp> /*NEED*/

namespace bpp {

// Namespaces //------------------------------------------------------------------------------------
#define public_area  simulatorUrbanBusPlugin
#define private_area simulatorUrbanBusPlugin_private
#define dll_export   DLL_EXPORT

namespace public_area  {}
namespace private_area {}

static_module_name("Simulator/Urban_bus/Plugin");

// Initialization //--------------------------------------------------------------------------------

// Errors //----------------------------------------------------------------------------------------
namespace public_area {}

// Constants & Variables //-------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Static Members //--------------------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// Functions Implementation //----------------------------------------------------------------------
namespace public_area  {}
namespace private_area {}

// X X X  Implementation //-------------------------------------------------------------------------
namespace {}

// Java Zone //-------------------------------------------------------------------------------------
java_zone {
 //---------------------------------------------------------------------------------------Namespaces
 using namespace java;
 using namespace jirk::bpp::simulation;
 using namespace jirk::java::lang;
 using namespace simulatorUrbanBusModel;
 //------------------------------------------------------------------------------------------Display
 static graphicConsole::clDisplay * goDisplay = nil;
 //--------------------------------------------------------------------------------------StartModule
 /*FUNCTION*/
 /* Loads and starts the simulation model. The two first arguments are the JNI context and the
    frame class. Then, the frame object and its output stream (where the messages will be written)
    are provided. The last argument is a string that contains the location of the experiment file.
    Java native method. */
 java_method(void,bpp_simulation_SimulatorFrame,startModule) (jyContext agContext,jyClass,
                                                              jyObject agFrame,jyObject agStream,
                                                              jyObject agExperimentFile) {
  if (goDisplay==nil) {
   goDisplay=new graphicConsole::clDisplay();
   goDisplay->setStream(agContext,agStream);
   standard::openLibrary(*goDisplay);
   environment::nextLine();
   environment::inform("B++ Library Environment Initialized.");
  }

  try {
   getVirtualMachine(agContext);

   jaSimulatorFrame lcFrame(agFrame);
   jaString         lcExperimentFile(agExperimentFile);
   clModel          lcModel(lcFrame.jniPointer(),lcExperimentFile.native().data(),true);

   do {
    lcFrame.j_setRestarted(java_false);
    lcModel.run();
    while (not lcFrame.j_closing() and not lcFrame.j_restarted()) jaThread::j_sleep(100);
   }
   while (lcFrame.j_restarted()==java_true);
  }

  simulator_catch;
 }
 //----------------------------------------------------------------------------------TerminateModule
 /*FUNCTION*/
 /* Unloads the simulation model. The arguments are the JNI context, the frame class and
    the frame object. Java native method. */
 java_method(void,bpp_simulation_SimulatorFrame,terminateModule) (jyContext,jyClass,jyObject) {
  if (goDisplay!=nil) {
   standard::closeLibrary();
   delete goDisplay;
   goDisplay=nil;
  }
 }
}

// End //-------------------------------------------------------------------------------------------
}