Changeset 3426

Show
Ignore:
Timestamp:
07/14/08 10:38:03 (4 months ago)
Author:
ksprotte
Message:

contract-tree-image-handler now serves its images from independently computed store-images

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/projects/bos/web/contract-tree.lisp

    r3425 r3426  
    66   (timestamp :accessor timestamp :initform (get-universal-time)) 
    77   (placemark-contracts :initform nil :accessor placemark-contracts) 
     8   (image :initform nil :accessor image) 
    89   (kml-req-count :initform 0 :accessor kml-req-count) 
    910   (image-req-count :initform 0 :accessor image-req-count))) 
     
    194195 
    195196 
     197;;; image 
     198 
     199;; contract-images are stored as store-images. The image slot of 
     200;; contract-node points to the current store-image. 
     201 
     202(defun contract-node-store-image-name (node) 
     203  (format nil "contract-node~{~D~}" (node-path node))) 
     204 
     205(defun contract-node-update-image (node) 
     206  (labels ((find-contract-color (contract) 
     207             (destructuring-bind (r g b) 
     208                 (contract-color contract) 
     209               (cl-gd:find-color r g b :alpha 40)))) 
     210    (let ((box (geo-box node)) 
     211          (image-size *contract-tree-images-size*)) 
     212      (cl-gd:with-image (cl-gd:*default-image* image-size image-size t) 
     213        (setf (cl-gd:save-alpha-p) t 
     214              (cl-gd:alpha-blending-p) nil) 
     215        ;; (cl-gd:draw-rectangle* 0 0 127 127 :filled nil :color (cl-gd:find-color 255 0 0)) 
     216        (let ((transparent (cl-gd:find-color 255 255 255 :alpha 127)) 
     217              (subbox (make-geo-box 0d0 0d0 0d0 0d0))) 
     218          (cl-gd:do-rows (y) 
     219            (cl-gd:do-pixels-in-row (x) 
     220              (let ((subbox (geo-subbox box x y image-size subbox))) 
     221                (multiple-value-bind (m2x m2y) 
     222                    (geo-box-middle-m2coord subbox) 
     223                  (setf (cl-gd:raw-pixel) 
     224                        (let* ((m2 (ignore-errors (get-m2 m2x m2y))) 
     225                               (contract (and m2 (m2-contract m2)))) 
     226                          (if (and contract (contract-paidp contract)) 
     227                              (find-contract-color contract) 
     228                              transparent)))))))) 
     229        (let* ((image-name (contract-node-store-image-name node)) 
     230               (old-store-image (store-image-with-name image-name))) 
     231          (when old-store-image (delete-object old-store-image)) 
     232          (make-store-image :name image-name 
     233                            :type :png)))))) 
     234 
     235(defun contract-node-update-image-if-needed (node) 
     236  (when (or (null (image node)) 
     237            (> (timestamp node) (blob-timestamp (image node)))) 
     238    (contract-node-update-image node))) 
     239 
    196240;;; image handler 
    197241(defclass contract-tree-image-handler (page-handler) 
    198242  ()) 
    199243 
    200 (defmethod handle ((handler contract-tree-image-handler))   
     244(defmethod handle ((handler contract-tree-image-handler)) 
    201245  (with-query-params (path) 
    202     (handle-if-node-modified 
    203       (incf (image-req-count node)) 
    204       (let ((box (geo-box node)) 
    205             (image-size *contract-tree-images-size*))         
    206         (cl-gd:with-image (cl-gd:*default-image* image-size image-size t) 
    207           (setf (cl-gd:save-alpha-p) t 
    208                 (cl-gd:alpha-blending-p) nil) 
    209           ;; (cl-gd:draw-rectangle* 0 0 127 127 :filled nil :color (cl-gd:find-color 255 0 0)) 
    210           (let ((white (cl-gd:find-color 255 255 255 :alpha 127)) 
    211                 (subbox (make-geo-box 0d0 0d0 0d0 0d0))) 
    212             (cl-gd:do-rows (y) 
    213               (cl-gd:do-pixels-in-row (x) 
    214                 (let ((subbox (geo-subbox box x y image-size subbox))) 
    215                   (multiple-value-bind (m2x m2y) 
    216                       (geo-box-middle-m2coord subbox) 
    217                     (setf (cl-gd:raw-pixel) 
    218                           (let* ((m2 (ignore-errors (get-m2 m2x m2y))) 
    219                                  (%contract (m2-contract m2)) 
    220                                  (contract (and m2 
    221                                                 %contract 
    222                                                 (contract-paidp %contract) 
    223                                                 %contract))) 
    224                             (if contract 
    225                                 (destructuring-bind (r g b) 
    226                                     (contract-color contract) 
    227                                   (cl-gd:find-color r g b :alpha 40)) 
    228                                 white)))))))) 
    229           (emit-image-to-browser cl-gd:*default-image* :png :date (timestamp node))))))) 
     246    (let* ((path (parse-path path)) 
     247           (node (find-node-with-path *contract-tree* path)) 
     248           (image (image node))) 
     249      (hunchentoot:handle-if-modified-since (timestamp image)) 
     250      (with-store-image* (image) 
     251        (emit-image-to-browser cl-gd:*default-image* :png :date (timestamp image)))))) 
    230252 
    231253;;; make-contract-tree-from-m2 
     
    239261    (when (contract-published-p contract) 
    240262      (insert-contract *contract-tree* contract))) 
     263  (format t "~&rendering contract-tree images...") 
     264  (map-nodes #'contract-node-update-image-if-needed *contract-tree*) 
     265  (format t "done.~%") 
     266  (bknr.datastore::delete-orphaned-blob-files nil) 
    241267  (geometry:register-rect-subscriber geometry:*rect-publisher* *contract-tree* 
    242268                                     (list 0 0 +width+ +width+)