=begin
 ******************************************************************************
 * Copyright 1990-2009 MBARI
 * MBARI Proprietary Information. All rights reserved.
 ******************************************************************************
 * Summary  : Local communication classes LocalClient and LocalServer
 * Filename : local.rb
 * Author   : Henthorn
 * Project  : Benthic Rover
 * Version  : 1
 * Created  : Aug 2009
 * Modified :
 ******************************************************************************
=end

##
# Refer to Rover-code home directory as base
#
require "#{ENV['ROVER_HOME']}/utils/rover_environment"
#

require 'socket'
require 'utils/misc'
require 'utils/datalog'

##########
# LocalServer class - Server side of the communication interface between a
#                     local supervisor (client) and the executive (server).
#
#
class LocalServer

  attr_reader :port, :client

  def initialize(port='/tmp/exec')
    @port = port
    @server = nil
    @client = nil
    @accept_thread = nil
  end

  def init
    return nil unless (@server == nil || @client == nil)
    return nil if (File.exists?(port))

    @server = UNIXServer.new(port)
    @accept_thread = Thread.new do
      @client = @server.accept
      puts("Got a client")
    end
    true
  end

  def get_message
    return nil unless (@client)

    request = @client.recvfrom(100)
    puts(request)
    return request
  end


  def close
    @server.close if (@server)
    @server = nil
    File.delete(@port) if File.exists?(@port)
  end


  def disconnect
    if (@client)
      @accept_thread.join if (@accept_thread)

      @client.send("disconnect", 0)
      @client.close
      @client = nil
    end

    self.close()
  end

end
##########

##########
# LocalClient class - Server side of the communication interface between a
#                     local supervisor (client) and the executive (server).
#
#
class LocalClient

  attr_reader :port, :server

  def initialize(port='/tmp/exec')
    @port = port
    @server = nil
  end

  def init
    return nil unless (@server == nil || @client == nil)
    return nil unless (File.exists?(port))

    @server = UNIXSocket.new(port)
    puts("Connected")

    true
  end

  def get_message
    return nil unless (@client)

    request = @server.recvfrom(100)
    puts(request)
    return request
  end

  def close
    if (@server)
      @server.close
      @server = nil
    end
  end

  def disconnect
    if (@server)
      @server.send("disconnect", 0)
      self.close
    end
  end

end
##########


#####
# Standalone unit test (recommended)
# Include at the end of the file as a hook to run a unit test of
# the code from the command line including the command line options
# (e.g., "$ ruby my_class.rb --sim").
#
if __FILE__ == $0

  server = LocalServer.new("/tmp/testlocal")
  unless (server)
    puts("Server not instantiated")
    exit
  end
  client = LocalClient.new("/tmp/testlocal")
  server.init

  client.send("update",0)
  server.get_request
  server.send("ack",0)

  sleep(10)
  puts("shutdown")

  server.shutdown

end
