changeset 69:34ac82ad8ca8

*** empty log message ***
author gongo
date Tue, 28 Oct 2008 19:39:19 +0900
parents bb40c73f7af3
children e8814b2db649
files ChangeLog redit-client-sm.el
diffstat 2 files changed, 124 insertions(+), 116 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Fri Sep 19 18:57:40 2008 +0900
+++ b/ChangeLog	Tue Oct 28 19:39:19 2008 +0900
@@ -1,3 +1,14 @@
+2008-10-28  Wataru MIYAGUNI  <gongo@cr.ie.u-ryukyu.ac.jp>
+
+	* thinking:
+	一つのセッションで一つのソケットを開く方がいい。
+	今は Sesssion Manager が一つしかないので問題ないが、
+	複数の Session Manager からくるコマンドを見ると、
+	来たコマンドがどこの Session Manager からきたかというのを
+	コマンドから判別できない。
+	emacs だと、filter で持ってるので、見れない事はない。。。が
+	やっぱり分けた方がいいだろうね。
+
 2008-09-12  Wataru MIYAGUNI  <gongo@cr.ie.u-ryukyu.ac.jp>
 
 	* cvs-rm: line-merge.el translate.el redit_client.pl
--- a/redit-client-sm.el	Fri Sep 19 18:57:40 2008 +0900
+++ b/redit-client-sm.el	Tue Oct 28 19:39:19 2008 +0900
@@ -74,7 +74,7 @@
   "End edited line of redit-client-after-change-function")
 
 
-;; REP command header size
+;; REP command header size (byte)
 (defconst redit-command-header-size 24)
 
 (defconst redit-command-offset-cmd   0)
@@ -85,48 +85,39 @@
 (defconst redit-command-offset-siz  20)
 (defconst redit-command-offset-txt  24)
 
+;; Remote editor Command
+(defconst repcmd-insert      6)
+(defconst repcmd-insert-ack  7)
+(defconst repcmd-delete      9)
+(defconst repcmd-delete-ack 10)
+(defconst repcmd-close      11)
+(defconst repcmd-close2     12)
+(defconst repcmd-nop        15)
+
+;; Session Manager Command
+(defconst smcmd-join        41)
+(defconst smcmd-join-ack    42)
+(defconst smcmd-put         45)
+(defconst smcmd-put-ack     46)
+(defconst smcmd-select      47)
+(defconst smcmd-select-ack  48)
+(defconst smcmd-select0     49)
+(defconst smcmd-quit        53)
+(defconst smcmd-quit-ack    54)
+(defconst smcmd-quit2       67)
+(defconst smcmd-quit2-ack   68)
+(defconst smcmd-sync        82)
+(defconst smcmd-sync-ack    83)
+
+(defconst smeditor-sm    -1)
+(defconst smeditor-merge -2)
 
 
-(defconst redit-open-command           1)
-(defconst redit-open-ack               2)
-(defconst redit-read-command           3)
-(defconst redit-read-ack               4)
-(defconst redit-read-finish-ack        5) ;; obsolete
-(defconst redit-insert-command         6)
-(defconst redit-insert-ack             7)
-;;(defconst redit-write-still-ack        "7")
-;;(defconst redit-write-finish-ack       "8")
-(defconst redit-delete-line-command    9)
-(defconst redit-delete-line-ack       10)
-(defconst redit-close-command         11)
-(defconst redit-close-ack             12)
-(defconst redit-replace-command       13)
-(defconst redit-replace-ack           14)
-(defconst redit-nop-command           15)
-
-;; REP Session
-(defconst redit-join-command           41)
-(defconst redit-join-ack-command       42)
-(defconst redit-get-command            43) ;; obsolete
-(defconst redit-get-ack-command        44) ;; obsolete
-(defconst redit-put-command            45)
-(defconst redit-put-ack-command        46)
-(defconst redit-select-command         47) ;; obsolete
-(defconst redit-select-ack-command     48)
-(defconst redit-register-command       49) ;; obsolete
-(defconst redit-register-ack-command   50) ;; obsolete
-(defconst redit-deregister-command     51) ;; obsolete
-(defconst redit-deregister-ack-command 52) ;; obsolete
-(defconst redit-quit-command           53)
-(defconst redit-quit-ack-command       54)
-
-(defconst redit-sync-command    82)
-(defconst redit-sync-ackcommand 83)
 
 (defconst redit-debug-flag t)
-(defun redit-debug-message (string)
+(defun redit-debug-message (string &rest args)
   (if (eq redit-debug-flag t)
-      (message string)))
+      (message (apply 'format string args))))
 
 ;;;;;;;;;;;;;;;;;;
 ;; Queue struct ;;
@@ -173,8 +164,6 @@
 (defun redit-get-text-from-queue (queue)
   (nth 3 (Queue-front queue)))
 
-(defvar redit-client-send-command-queue (make-Queue))
-
 ;;;;;;;;;;;;;;;;;;;;;;;;;
 ;; Session Info struct ;;
 ;;;;;;;;;;;;;;;;;;;;;;;;;
@@ -205,10 +194,6 @@
   (SessionInfo-recv_cmdlist sinfo))
 
 ;; hash table of SessionInfo
-
-(defvar htable-sid2bufname (make-hash-table))
-(defvar htable-bufname2sinfo (make-hash-table))
-
 (defun redit-sinfo-put-sid2buf (sid bufname)
   ""
   (setf (gethash sid htable-sid2bufname) bufname))
@@ -278,11 +263,18 @@
 	(setq text (substring pkt offset (+ offset size)))
 	(decode-coding-string text redit-string-coding)))))
 
+
+
+(defvar redit-client-send-command-queue (make-Queue))
+
+(defvar htable-sid2bufname (make-hash-table))
+(defvar htable-bufname2sinfo (make-hash-table))
+
 ;;;;;;;;;;;;;;;;;;
 ;; User Command ;;
 ;;;;;;;;;;;;;;;;;;
 
-(defun redit-join-command ()
+(defun redit-join ()
   "Allow this Emacs process to be a remote-edit session manager for client processes."
   (interactive)
   (let (bufname)
@@ -321,23 +313,21 @@
 	  ;; JOIN if buffer-name is "*scratch*"
 	  (process-send-string redit-client-process
 			       (redit-make-packet
-				redit-join-command 0 0 0 0 0 ""))
-	  (redit-debug-message
-	   (format "join-command: %d 0 0 0 0 0" redit-join-command))
+				smcmd-join 0 0 0 0 0 ""))
+	  (redit-debug-message "join-command: %d 0 0 0 0 0" smcmd-join)
 	  )
       ;; PUT if buffer-name is not "*scratch*"
       (progn
 	(process-send-string redit-client-process
 			     (redit-make-packet
-			      redit-put-command 0 0 0 0
+			      smcmd-put 0 0 0 0
 			      (string-bytes bufname) bufname))
-	(redit-debug-message
-	 (format "put-command: %d 0 0 0 0 %d %s"
-		 redit-put-command (string-bytes bufname) (buffer-name)))
+	(redit-debug-message "put-command: %d 0 0 0 0 %d %s"
+			     smcmd-put (string-bytes bufname) (buffer-name))
 	)
       )))
 
-(defun redit-select-command ()
+(defun redit-select()
   "Allow this Emacs process to be a remote-edit session manager for client processes."
   (interactive)
   (let (sid)
@@ -356,13 +346,12 @@
     (setq sid (read-minibuffer "Input session id: "))
     (process-send-string redit-client-process
 			 (redit-make-packet
-			  redit-select-command sid 0 0 0 0 ""))
+			  smcmd-select sid 0 0 0 0 ""))
 
-    (redit-debug-message
-     (format "select-command: %d %d 0 0 0 0 0" redit-select-command sid))
+    (redit-debug-message "select-command: %d %d 0 0 0 0 0" smcmd-select sid)
   ))
 
-(defun redit-quit-command ()
+(defun redit-quit ()
   "quit"
   (interactive)
   ;; process kill とかしよう
@@ -373,15 +362,15 @@
 ;; バッファ(sid)番号、行番号、行の内容を送り、 Ack を受け取る
 ;; _text が指定されている場合、バッファのlinenumの文字列ではなく
 ;; _text で指定された文字列を送信する
-(defun redit-client-insert-line (linenum &optional isnewline _text)
-  "Send \"redit-insert-command\" to SessionManager.
+(defun redit-client-insert (linenum &optional isnewline _text)
+  "Send \"repcmd-insert\" to SessionManager.
 
 If current buffer is not connected with SessionManager,
 nothing is done (see `redit-client-exec-join', `redit-client-exec-put').
 
 * REP command header
 
-cmd : 6 (redit-insert-command).
+cmd : 6 (repcmd-insert).
 sid : Session ID corresponding to current buffer.
 eid : Editor ID corresponding to current buffer.
       (sid & eid are acquired from hash table `SessionInfo'.)
@@ -410,30 +399,30 @@
 		(setq text-size (string-bytes text))
 
 		(setq packet (redit-make-packet
-			      redit-insert-command sid eid
+			      repcmd-insert sid eid
 			      (gen-seqno) linenum text-size text))
 		
 		;;(process-send-string redit-client-process packet)
 		(enqueue redit-client-send-command-queue packet)
 		
 		(redit-debug-message
-		 (format "[send] insert-command: %d %d %d %d %d %d %s"
-			 redit-insert-command sid eid (gen-seqno)
-			 linenum text-size text))
+		 "[send] insert-command: %d %d %d %d %d %d %s"
+		 repcmd-insert sid eid (gen-seqno)
+		 linenum text-size text)
 		)
 	    )))
     (error "redit-client is not running.")))
 
 ;; linenum で指定した行の削除命令を redit-client-process に送信する
-(defun redit-client-delete-line (linenum)
-  "Send \"redit-delete-line-command\" to SessionManager.
+(defun redit-client-delete (linenum)
+  "Send \"repcmd-delete\" to SessionManager.
 
 If current buffer is not connected with SessionManager,
 nothing is done (see `redit-client-exec-join', `redit-client-exec-put').
 
 * REP command header
 
-cmd : 9 (redit-delete-line-command).
+cmd : 9 (repcmd-delete).
 sid : Session ID corresponding to current buffer.
 eid : Editor ID corresponding to current buffer.
       (sid & eid are acquired from hash table `SessionInfo'.)
@@ -456,27 +445,27 @@
 		(setq text-size (string-bytes text))
 
 		(setq packet (redit-make-packet
-			      redit-delete-line-command sid eid
+			      repcmd-delete sid eid
 			      (gen-seqno) linenum text-size text))
 	    
 		;;(process-send-string redit-client-process packet)
 		(enqueue redit-client-send-command-queue packet)
 
 		(redit-debug-message
-		 (format "[send] delete-command: %d %d %d %d %d %d %s"
-			 redit-delete-line-command sid eid (gen-seqno)
-			 linenum text-size text))
+		 "[send] delete-command: %d %d %d %d %d %d %s"
+		 repcmd-delete sid eid (gen-seqno)
+		 linenum text-size text)
 		)
 	    )))
     (error "redit-client is not running.")))
 
-(defun redit-client-replace-line (linenum &optional text)
-  "Send \"redit-delete-line-command\" and \"redit-insert-line-command
+(defun redit-client-replace (linenum &optional text)
+  "Send \"repcmd-delete\" and \"redit-insert-line-command
 to SessionManager.
 
 `redit-replace-command' is obsolete command.
-Now, replace command is done by using `redit-client-delete-line' and
-`redit-client-insert-line'.
+Now, replace command is done by using `redit-client-delete' and
+`redit-client-insert'.
 
 For example, now buffer
 
@@ -492,8 +481,8 @@
 2: redit-client
 3: elisp
 "
-  (redit-client-delete-line linenum)
-  (redit-client-insert-line linenum text))
+  (redit-client-delete linenum)
+  (redit-client-insert linenum text))
 
 (defun redit-client-send-rep-command ()
   (let (command)
@@ -540,17 +529,17 @@
 	
 	;; command がどの命令かを判断し、対応した処理をする。case みたい
 	(cond
-	 ((if (= cmd redit-insert-command)
-	      (redit-client-exec-insert-line sid lno txt)))
-	 
-	 ((if (= cmd redit-join-ack-command)
+	 ((if (= cmd repcmd-insert)
+	      (redit-client-exec-insert sid lno txt)
+	    ))
+	 ((if (= cmd repcmd-delete)
+	      (redit-client-exec-delete sid lno)
+	    ))
+	 ((if (= cmd smcmd-join-ack)
 	      (redit-client-exec-join txt sid eid)))
 	   
-	 ((if (= cmd redit-put-ack-command)
+	 ((if (= cmd smcmd-put-ack)
 	      (redit-client-exec-put sid eid)))
-
-	 ((if (= cmd redit-delete-line-command)
-	      (redit-client-exec-delete-line sid lno)))
 	 )
 
 	(setq allsize (- allsize cursize))
@@ -598,18 +587,18 @@
 	    (setq prev-edit-line (SessionInfo-prevline sinfo))
 	    (if (/= prev-edit-line redit-client-before-begin-line)
 		(progn
-		  (redit-client-replace-line prev-edit-line))))
+		  (redit-client-replace prev-edit-line))))
 
 	  ;; delete-line
 	  (let (editl)
 	    (setq editl redit-client-before-end-line)
 	    (if (> editl redit-client-before-begin-line)
 		(while (>= editl redit-client-before-begin-line)
-		  (redit-client-delete-line editl)
+		  (redit-client-delete editl)
 		  (setq editl (1- editl)))))
 	  )
       )
-    )
+    )  
   )
 
 (defun redit-client-after-change-function (begin end length)
@@ -639,21 +628,21 @@
 	      ;; case delete
 	      (if (/= redit-client-before-begin-line
 		      redit-client-before-end-line)
-		  (redit-client-insert-line redit-client-before-begin-line))
+		  (redit-client-insert redit-client-before-begin-line))
 
 	    (progn
 	      ;; case insert or replace
 	      (setq editl redit-client-before-begin-line)
 	      (while (< editl redit-client-after-end-line)
-		(redit-client-insert-line editl)
+		(redit-client-insert editl)
 		(setq editl (1+ editl)))
 
 	      ;; case insert
 	      (if (= redit-client-before-begin-line
 		     redit-client-before-end-line)
-		  (redit-client-delete-line redit-client-after-end-line))
+		  (redit-client-delete redit-client-after-end-line))
 
-	      (redit-client-insert-line redit-client-after-end-line)
+	      (redit-client-insert redit-client-after-end-line)
 	      )
 	    )
 
@@ -661,8 +650,8 @@
 	  (setf (SessionInfo-prevline sinfo) redit-client-after-end-line)
 ))))
 
-(defun redit-client-exec-insert-line (sid linenum text)
-  "Call when received \"redit-insert-command\" from SessionManager.
+(defun redit-client-exec-insert (sid linenum text)
+  "Call when received \"repcmd-insert\" from SessionManager.
 see. `redit-client-process-filter'.
 
 TEXT is inserted in line LINENUM of buffer corresponding to SID.
@@ -682,18 +671,17 @@
 3: void
 4: elisp"
 
-  (redit-debug-message
-   (format "[recv] insert-line: Session[%d] linenum = %d text = %s"
-	   sid linenum text))
+  (redit-debug-message "[recv] insert-line: Session[%d] linenum = %d text = %s"
+		       sid linenum text)
 
-  (let (curlineno bufname sinfo)
+  (let (curlineno maxlineno bufname sinfo)
     (setq bufname (redit-sinfo-get-sid2buf sid))
 
     (save-excursion
       (set-buffer bufname)
-
       (goto-line linenum)
       (setq curlineno (real-count-lines (point)))
+      (setq maxlineno (real-count-lines (point-max)))
 
       (if (> linenum curlineno)
 	  ;; buffer の 最後の行番号が、linenum に足りない場合、
@@ -701,13 +689,19 @@
 	  (progn (end-of-line)
 		 (newline (- linenum curlineno))
 		 (goto-line linenum)))
- 
-      (beginning-of-line)
-      (insert (concat text "\n"))
-)))
 
-(defun redit-client-exec-delete-line (sid linenum)
-  "Call when received \"redit-delete-line-command\" from SessionManager.
+      (if (= curlineno 1)
+	  (progn
+	    (beginning-of-line)
+	    (insert (concat text (if (/= maxlineno 1) "\n" ""))))
+	(progn
+	  (goto-line (1- curlineno))
+	  (end-of-line)
+	  (insert (concat "\n" text))))
+      )))
+
+(defun redit-client-exec-delete (sid linenum)
+  "Call when received \"repcmd-delete\" from SessionManager.
 see. `redit-client-process-filter'.
 
 Line LINENUM of buffer corresponding to SID is deleted.
@@ -725,14 +719,17 @@
 1: hoge
 2: elisp
 "
-  (redit-debug-message
-   (format "[recv] delete-line: Session[%d] linenum = %d" sid linenum))
+  (redit-debug-message "[recv] delete-line: Session[%d] linenum = %d"
+		       sid linenum)
 
-  (let ((diff) (bufname (redit-sinfo-get-sid2buf sid)))
+  (let ((bufname (redit-sinfo-get-sid2buf sid))
+	(maxlineno (real-count-lines (point-max)))
+	diff)
     (save-excursion
       (set-buffer bufname)
       (setq diff (goto-line linenum))
-      (if (= diff 0)
+      ;;(if (= diff 0)
+      (if (is-value-in-range 1 linenum maxlineno)
 	  (progn
 	    (delete-region (progn (beginning-of-line) (point))
 			   (progn (end-of-line) (point)))
@@ -744,7 +741,7 @@
       )))
 
 (defun redit-client-exec-join (filename sid eid)
-  "Call when received \"redit-join-ack-command\" from SessionManager.
+  "Call when received \"smcmd-join-ack-command\" from SessionManager.
 see. `redit-client-process-filter'.
 
 SID is id of session which client participation.
@@ -767,8 +764,8 @@
 
     (switch-to-buffer bufname)
 
-    (redit-debug-message (format "join-ack: Session [%d] %s" sid bufname))
-    (redit-debug-message (format "Your editor id = %d" eid))
+    (redit-debug-message "join-ack: Session [%d] %s" sid bufname)
+    (redit-debug-message "Your editor id = %d" eid)
 
 ))
 
@@ -790,16 +787,16 @@
 
     (switch-to-buffer bufname)
     
-    (redit-debug-message (format "put-ack Session [%d] %s" sid bufname))
-    (redit-debug-message (format "Your editor id = %d" eid))
+    (redit-debug-message "put-ack Session [%d] %s" sid bufname)
+    (redit-debug-message "Your editor id = %d" eid)
 ))
 
 (defun redit-client-exec-sync (sid)
   (let ((max-line (real-count-lines (point-max)))
 	(curline 1))
     (while (<= curline max-line)
-      (redit-client-delete-line curline)
-      (redit-client-insert-line curline)
+      (redit-client-delete curline)
+      (redit-client-insert curline)
 
       (accept-process-output redit-client-process)