;;;	-*- package: MFS; mode: lisp; base: 10.; readtable: CL -*-


(defvar *ROOT* :unbound "Root of MOBY section")
(defvar *ARRAY* :unbound "Test array (car of *ROOT*)")

; (moby-smash-all)  simulates booting
; (initialize-root-on-disk) wipe out old partition
; (moby-boot-root) activate existing partition
; (define-moby-remote-host <hostname of remote> <net name of remote>)

;;; Run these FIRST before anything else in this file and also before
;;;  HINGE (ie, its toplevel function RUN-HINGE).  This sets up HOST/MACHINE
;;;  correspondences and connections.

(defun hinge-computer-setup ( )
  (setq *MY-MOBY-HOST* nil
	*COMPUTER-MOBY-HOST* nil
	*DISPLAYER-MOBY-HOST* nil
	*DISPLAYER-MOBY-HOST-NAME* nil)
  (let ((mfs:MLM-AUTOMATICALLY-CREATE-DIRECTORIES t)
	displayer-net-name)
 ;    (format t "~%MY MOBY host name:  ")
 ;    (setq *MY-MOBY-HOST-NAME* (string-upcase (read-line)))
    (setq *my-moby-host-name* (mfs:moby-select-partition-host-name))
    (format t "~%My moby host name is ~a" *my-moby-host-name*)
    (format t "~%MOBY host name for DISPLAYER:  ")
    (setq *DISPLAYER-MOBY-HOST-NAME* (string-upcase (read-line)))
    (when (y-or-n-p "Simulate booting (ie, MOBY-SMASH-ALL) ?")
      (mfs:moby-smash-all))
    (cond ((y-or-n-p "(Re)Initialize partition (ie, INITIALIZE-ROOT-ON-DISK) ?")
	   (mfs:moby-initialize-root-on-disk))
	  ((y-or-n-p "Activate existing partition (ie, MOBY-BOOT-ROOT) ?")
	   (mfs:moby-boot-root)))
    (when (y-or-n-p "Open comm file on local moby structure?")
      (open (string-append *MY-MOBY-HOST-NAME* ":BAR;HINGE-DATA.MBY")
	    :direction :output :moby-mapped t
	    :if-exists :append :if-does-not-exist :create)
      (format t "~%Displayer NET host name:  ")
      (setq displayer-net-name (string-upcase (read-line)))
      (y-or-n-p "~%Type Y when you get to here on OTHER machine:")
      (mfs:define-remote-moby-host *DISPLAYER-MOBY-HOST-NAME* displayer-net-name)
      (open (string-append *DISPLAYER-MOBY-HOST-NAME* ":BAR;HINGE-DATA.MBY")
	    :direction :output :moby-mapped t
	    :if-exists :append :if-does-not-exist :create))
    (setq *MY-MOBY-HOST* (fs:get-pathname-host *MY-MOBY-HOST-NAME*)
	  *DISPLAYER-MOBY-HOST* (fs:get-pathname-host *DISPLAYER-MOBY-HOST-NAME*)
	  *computer-moby-host* *my-moby-host*))
  'OK)


(defun hinge-displayer-setup ( )
  (setq *MY-MOBY-HOST* nil
	*COMPUTER-MOBY-HOST* nil
	*DISPLAYER-MOBY-HOST* nil
	*DISPLAYER-MOBY-HOST-NAME* nil)
  (let ((mfs:MLM-AUTOMATICALLY-CREATE-DIRECTORIES t)
	computer-moby-host-name computer-net-host-name)
 ;    (format t "~%MY MOBY host name:  ")
 ;    (setq *MY-MOBY-HOST-NAME* (string-upcase (read-line)))
    (setq *my-moby-host-name* (mfs:moby-select-partition-host-name))
    (format t "~%My moby host name is ~a" *my-moby-host-name*)
    (format t "~%MOBY host name for COMPUTER:  ")
    (setq computer-moby-host-name (string-upcase (read-line)))
    (when (y-or-n-p "Simulate booting (ie, MOBY-SMASH-ALL) ?")
      (mfs:moby-smash-all))
    (cond ((y-or-n-p "(Re)Initialize partition (ie, INITIALIZE-ROOT-ON-DISK) ?")
	   (mfs:moby-initialize-root-on-disk))
	  ((y-or-n-p "Activate existing partition (ie, MOBY-BOOT-ROOT) ?")
	   (mfs:moby-boot-root)))
    (when (y-or-n-p "Open comm file on local moby structure?")
      (open (string-append *MY-MOBY-HOST-NAME* ":BAR;HINGE-DATA.MBY")
	    :direction :output :moby-mapped t
	    :if-exists :append :if-does-not-exist :create
	    :create-directories t)
      (format t "~%Computer NET host name:  ")
      (setq computer-net-host-name (string-upcase (read-line)))
      (y-or-n-p "~%Type Y when you get to here on OTHER machine:")
      (mfs:define-remote-moby-host computer-moby-host-name computer-net-host-name)
      (open (string-append computer-moby-host-name ":BAR;HINGE-DATA.MBY")
	    :direction :output :moby-mapped t
	    :if-exists :append :if-does-not-exist :create
	    :create-directories t))
    (setq *MY-MOBY-HOST* (fs:get-pathname-host *MY-MOBY-HOST-NAME*))
    (setq *COMPUTER-MOBY-HOST* (fs:get-pathname-host computer-moby-host-name)
	  *displayer-moby-host* *my-moby-host*))
  'OK)


;;; Run this to initialize things for RUN-CHAOS-TEST.

(defun chaos-test-setup ( )
  (setq *ROOT* (open (string-append (send *computer-moby-host* :name)
				    ":BAR;HINGE-DATA.MBY")
		     :direction :output :moby-mapped t
		     :if-exists :append :if-does-not-exist :create
		     :create-directories t))
  (unless (arrayp (car *ROOT*))
    (rplaca *ROOT* (make-array 8. :area (%area-number *ROOT*))))
  (setq *ARRAY* (car *ROOT*))
  (dotimes (i (array-length *ARRAY*))
    (setf (aref *ARRAY* i) i))
  *ARRAY*)


;;; Run this on both machines to test whether packets are getting
;;;  across correctly.

(defun run-chaos-test ( )
  (let ((other-host
	  (car (delete *MY-MOBY-HOST*
		       (list *COMPUTER-MOBY-HOST* *DISPLAYER-MOBY-HOST*)))))
    (do-forever
      (mfs:moby-wait-for-data *ARRAY*)
      (print *ARRAY*)
      (dotimes (i (array-length *ARRAY*))
	(incf (aref *ARRAY* i)))
      (mfs:moby-give-data-to-host *ARRAY* other-host))))


;;; End.
