------------------------------------------------------------------------------
--- Knowledge-based Autonomic Heterogeneous Networks (KAHN)
--- @author  Rajesh Krishnan, Cosocket LLC
--- @version September 2014
------------------------------------------------------------------------------
{-
Copyright (c) 2014, Cosocket LLC
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
  list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.

* Neither the name of Cosocket LLC nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
-}
{-# LANGUAGE DeriveGeneric #-}
module KAHN.SysInit where
import System.IO
import System.ZMQ4.Monadic
import System.Posix.IO     (createPipe, fdToHandle)
import Control.Monad       (when,forever)
import Control.Concurrent  (forkIO)
import KAHN.BB             (bb)
import KAHN.KBI            (toKB, fromKB, cmdKB)
import KAHN.MIMI           (mimi)
import KAHN.PKTPROC        (pktproc)
import KAHN.Config         (configOpts,Config(cfgKBInitStr),saveConfigFile)

makePipePair :: IO (Handle,Handle,Handle,Handle)
makePipePair = do
  (f1,f2) <- createPipe
  (f3,f4) <- createPipe
  h1      <- fdToHandle f1
  h2      <- fdToHandle f2
  h3      <- fdToHandle f3
  h4      <- fdToHandle f4
  return(h1,h4,h3,h2)

initSystemFromKB :: IO (Handle,Handle,Handle,Handle,String)
initSystemFromKB = do
  (pSIn,pSOut,cSIn,cSOut) <- makePipePair
  (pAIn,pAOut,cAIn,cAOut) <- makePipePair
  kbInitstr <- startComponents (kbi cSIn cSOut cAIn cAOut) -- use kbshort for testing
  return (pSIn,pSOut,pAIn,pAOut,kbInitstr)

startComponents :: (Config -> IO ()) -> IO (String)
startComponents kbCurry = do
  cfg <- configOpts
  --saveConfigFile "/tmp/kahn.conf" cfg
  forkIO $ kbCurry cfg 
  return (cfgKBInitStr cfg)

kbi :: Handle -> Handle -> Handle -> Handle -> Config -> IO ()
kbi sI sO aI aO cfg = runZMQ $ do
  async  (bb      cfg) 
  async  (toKB    aO cfg) 
  async  (fromKB  aI cfg) 
  async  (cmdKB   sI sO cfg) 
  async  (mimi    cfg) 
  async  (pktproc cfg)
  return ()

kbe :: Config -> IO ()
kbe cfg = runZMQ $ do
  async  (bb      cfg)
  async  (mimi    cfg)
  async  (pktproc cfg)
  return ()

kbshort :: Handle -> Handle -> Handle -> Handle -> Config -> IO ()
kbshort sI sO aI aO cfg = (forkIO $ echo aI aO) >> echo sI sO 

stdecho :: IO ()
stdecho = echo stdin stdout

echo :: Handle -> Handle -> IO ()
echo i o = hGetLine i >>= hPutStrLn o >> hFlush o >> echo i o
