 "))
 
 (defconst ej-push-py (concat ej-header-py "
-file = sys.argv[1]
-
 m = hashlib.sha1()
 body = sys.stdin.read()
 m.update(body)
 
 request = {
-    'Hash': base64.b64encode(m.digest()),
-    'Data': body
+    'Key': file,
+    'Updated': {
+        'Hash': base64.b64encode(m.digest()),
+        'Data': body
+    }
 }
 
-requests.post(SERVER+\"/push\", data=json.dumps(request))
+print \"Push Response:\", requests.post(SERVER+\"/push\", data=json.dumps(request)).text
 "))
 
 (defvar ej-timer nil)

import sys

SERVER = \"http://localhost:8000\"
")

(defconst ej-unlock-py (concat ej-header-py "
file = sys.argv[1]
request = {'Key': file}

# Make a request to the unlock server, sending the key to unlock
requests.post(SERVER+\"/unlock\", data=json.dumps(request))
"))

(defconst ej-watch-py (concat ej-header-py "
file = sys.argv[1]

# Compute the hash
m = hashlib.sha1()
body = sys.stdin.read()
m.update(body)
hash = base64.b64encode(m.digest())


request = {'Key': file, 'Hash': hash}

res = json.loads(requests.post(SERVER+\"/watch\", data=json.dumps(request)).text)
if res['Locked']:

(defvar ej-locked-buffers nil)
(defvar ej-active-buffers nil)

(defun ej-invoke-py (outbuffer code)
  "Outputs to OUTBUFFER.  Invoke the given CODE in the python interpreter."
  (start-process "python" outbuffer "python" "-c" code (buffer-file-name)))

(defun ej-mode-after-save ()
  "Unlock the file."
  (ej-invoke-py "*ej-log*" ej-unlock-py)
  (setq ej-locked-buffers
        (remove (current-buffer) ej-locked-buffers)))

(defun ej-sync ()
  "Pushed change in buffers to the server & lock with push messages.
Read changes in unlocked buffers and update buffer."
  ; Deal with any changed buffers
  (dolist (buff ej-changed-list)
    (add-to-list 'ej-locked-buffers buff) ; The buffer is now locked
    (with-current-buffer buff
      (let ((proc (ej-invoke-py "*ej-log*" ej-push-py)))
        ; Send the entire buffer
        (process-send-region proc (point-min) (point-max))
        (process-send-eof proc))))

  ; Sync other buffers
  (dolist (buff ej-active-buffers)
    (unless (member buff ej-locked-buffers) ; TODO(michael): Don't update hidden buffers
      (with-current-buffer buff

        (let* ((tbuff (concat "*" (buffer-name buff) "-ej-swap*"))
               (proc (ej-invoke-py tbuff ej-watch-py)))
          ; Send the entire buffer
          (process-send-region proc (point-min) (point-max))
          (process-send-eof proc)

          ; Wait for the process to complete
          (when (accept-process-output proc 0.5)
            (save-excursion ; TODO(michael): Should move pointer back to position after chng
              (if (with-current-buffer tbuff
                    (goto-char (point-min))
                    (looking-at "RO\n"))
                  (progn ; Read only
                    ; Copy the temp buffer into the current buffer
                    (erase-buffer)
                    (goto-char (point-min))
                    (insert-buffer-substring tbuff 3) ; TODO(michael): This might not work

                    (unless buffer-read-only (read-only-mode)))
                (progn ; Read/Write
                  (when buffer-read-only (read-only-mode))))))))))

  (setq ej-changed-list nil))

