2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5 % DDDD IIIII SSSSS TTTTT RRRR IIIII BBBB U U TTTTT EEEEE %
6 % D D I SS T R R I B B U U T E %
7 % D D I SSS T RRRR I BBBB U U T EEE %
8 % D D I SS T R R I B B U U T E %
9 % DDDDA IIIII SSSSS T R R IIIII BBBB UUU T EEEEE %
11 % CCCC AAA CCCC H H EEEEE %
13 % C AAAAA C HHHHH EEE %
15 % CCCC A A CCCC H H EEEEE %
18 % MagickCore Distributed Pixel Cache Methods %
25 % Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
26 % dedicated to making software imaging solutions freely available. %
28 % You may not use this file except in compliance with the License. You may %
29 % obtain a copy of the License at %
31 % http://www.imagemagick.org/script/license.php %
33 % Unless required by applicable law or agreed to in writing, software %
34 % distributed under the License is distributed on an "AS IS" BASIS, %
35 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
36 % See the License for the specific language governing permissions and %
37 % limitations under the License. %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 % A distributed pixel cache is an extension of the traditional pixel cache
42 % available on a single host. The distributed pixel cache may span multiple
43 % servers so that it can grow in size and transactional capacity to support
44 % very large images. Start up the pixel cache server on one or more machines.
45 % When you read or operate on an image and the local pixel cache resources are
46 % exhausted, ImageMagick contacts one or more of these remote pixel servers to
47 % store or retrieve pixels.
54 #include "MagickCore/studio.h"
55 #include "MagickCore/cache.h"
56 #include "MagickCore/cache-private.h"
57 #include "MagickCore/distribute-cache.h"
58 #include "MagickCore/distribute-cache-private.h"
59 #include "MagickCore/exception.h"
60 #include "MagickCore/exception-private.h"
61 #include "MagickCore/geometry.h"
62 #include "MagickCore/image.h"
63 #include "MagickCore/locale_.h"
64 #include "MagickCore/memory_.h"
65 #include "MagickCore/policy.h"
66 #include "MagickCore/random_.h"
67 #include "MagickCore/registry.h"
68 #include "MagickCore/splay-tree.h"
69 #include "MagickCore/string_.h"
70 #include "MagickCore/string-private.h"
71 #if defined(MAGICKCORE_HAVE_SOCKET)
72 #include <netinet/in.h>
74 #include <sys/socket.h>
75 #include <arpa/inet.h>
81 #define DPCHostname "127.0.0.1"
83 #define DPCSessionKeyLength 8
86 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90 + A c q u i r e D i s t r i b u t e C a c h e I n f o %
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % AcquireDistributeCacheInfo() allocates the DistributeCacheInfo structure.
98 % The format of the AcquireDistributeCacheInfo method is:
100 % DistributeCacheInfo *AcquireDistributeCacheInfo(ExceptionInfo *exception)
102 % A description of each parameter follows:
104 % o exception: return any errors or warnings in this structure.
108 static MagickSizeType CRC64(const unsigned char *message,const size_t length)
116 static MagickBooleanType
117 crc_initial = MagickFalse;
119 static MagickSizeType
122 if (crc_initial == MagickFalse)
127 for (i=0; i < 256; i++)
132 alpha=(MagickSizeType) i;
133 for (j=0; j < 8; j++)
135 if ((alpha & 0x01) == 0)
138 alpha=(MagickSizeType) ((alpha >> 1) ^
139 MagickULLConstant(0xd800000000000000));
143 crc_initial=MagickTrue;
146 for (i=0; i < (ssize_t) length; i++)
147 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
151 static int ConnectPixelCacheServer(const char *hostname,const int port,
152 MagickSizeType *session_key,ExceptionInfo *exception)
154 #if defined(MAGICKCORE_HAVE_SOCKET)
156 secret[MaxTextExtent];
175 session[MaxTextExtent];
178 Connect to distributed pixel cache and get session key.
181 shared_secret=GetPolicyValue("shared-secret");
182 if (shared_secret == (const char *) NULL)
184 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
185 "DistributedPixelCache","'%s'","shared secret expected");
188 (void) CopyMagickString((char *) session,shared_secret,MaxTextExtent-
189 DPCSessionKeyLength);
190 host=gethostbyname(hostname);
191 client_socket=socket(AF_INET,SOCK_STREAM,0);
192 if (client_socket == -1)
194 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
195 "DistributedPixelCache","'%s'",hostname);
198 (void) ResetMagickMemory(&address,0,sizeof(address));
199 address.sin_family=AF_INET;
200 address.sin_port=htons((uint16_t) port);
201 address.sin_addr=(*((struct in_addr *) host->h_addr));
202 status=connect(client_socket,(struct sockaddr *) &address,(socklen_t)
203 sizeof(struct sockaddr));
206 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
207 "DistributedPixelCache","'%s'",hostname);
210 count=read(client_socket,secret,MaxTextExtent);
213 (void) memcpy(session+strlen(shared_secret),secret,(size_t) count);
214 *session_key=CRC64(session,strlen(shared_secret)+count);
216 if (*session_key == 0)
218 close(client_socket);
221 return(client_socket);
223 (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
224 "DelegateLibrarySupportNotBuiltIn","distributed pixel cache");
229 static char *GetHostname(int *port,ExceptionInfo *exception)
246 Parse host list (e.g. 192.168.100.1:6668,192.168.100.2:6668).
248 hosts=(char *) GetImageRegistry(StringRegistryType,"cache:hosts",
250 if (hosts == (char *) NULL)
253 return(AcquireString(DPCHostname));
255 (void) SubstituteString(&hosts,","," ");
256 hostlist=StringToArgv(hosts,&argc);
257 hosts=DestroyString(hosts);
258 if (hostlist == (char **) NULL)
261 return(AcquireString(DPCHostname));
263 hosts=AcquireString(hostlist[(id++ % (argc-1))+1]);
264 for (i=0; i < (ssize_t) argc; i++)
265 hostlist[i]=DestroyString(hostlist[i]);
266 hostlist=(char **) RelinquishMagickMemory(hostlist);
267 (void) SubstituteString(&hosts,":"," ");
268 hostlist=StringToArgv(hosts,&argc);
269 if (hostlist == (char **) NULL)
272 return(AcquireString(DPCHostname));
274 host=AcquireString(hostlist[1]);
275 if (hostlist[2] == (char *) NULL)
278 *port=StringToLong(hostlist[2]);
279 for (i=0; i < (ssize_t) argc; i++)
280 hostlist[i]=DestroyString(hostlist[i]);
281 hostlist=(char **) RelinquishMagickMemory(hostlist);
285 MagickPrivate DistributeCacheInfo *AcquireDistributeCacheInfo(
286 ExceptionInfo *exception)
292 *distribute_cache_info;
297 distribute_cache_info=(DistributeCacheInfo *) AcquireMagickMemory(
298 sizeof(*distribute_cache_info));
299 if (distribute_cache_info == (DistributeCacheInfo *) NULL)
300 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
301 (void) ResetMagickMemory(distribute_cache_info,0,
302 sizeof(*distribute_cache_info));
303 distribute_cache_info->signature=MagickSignature;
305 Contact pixel cache server.
307 distribute_cache_info->port=0;
308 hostname=GetHostname(&distribute_cache_info->port,exception);
310 distribute_cache_info->file=ConnectPixelCacheServer(hostname,
311 distribute_cache_info->port,&session_key,exception);
312 distribute_cache_info->session_key=session_key;
313 (void) CopyMagickString(distribute_cache_info->hostname,hostname,
315 hostname=DestroyString(hostname);
316 if (distribute_cache_info->file == -1)
317 distribute_cache_info=DestroyDistributeCacheInfo(distribute_cache_info);
318 return(distribute_cache_info);
322 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
326 + D e s t r o y D i s t r i b u t e C a c h e I n f o %
330 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 % DestroyDistributeCacheInfo() deallocates memory associated with an
333 % DistributeCacheInfo structure.
335 % The format of the DestroyDistributeCacheInfo method is:
337 % DistributeCacheInfo *DestroyDistributeCacheInfo(
338 % DistributeCacheInfo *distribute_cache_info)
340 % A description of each parameter follows:
342 % o distribute_cache_info: the distributed cache info.
345 MagickPrivate DistributeCacheInfo *DestroyDistributeCacheInfo(
346 DistributeCacheInfo *distribute_cache_info)
348 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
349 assert(distribute_cache_info->signature == MagickSignature);
350 distribute_cache_info->signature=(~MagickSignature);
351 distribute_cache_info=(DistributeCacheInfo *) RelinquishMagickMemory(
352 distribute_cache_info);
353 return(distribute_cache_info);
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
361 + D i s t r i b u t e P i x e l C a c h e S e r v e r %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 % DistributePixelCacheServer() waits on the specified port for commands to
368 % create, read, update, or destroy a pixel cache.
370 % The format of the DistributePixelCacheServer() method is:
372 % void DistributePixelCacheServer(const size_t port)
374 % A description of each parameter follows:
376 % o port: connect the distributed pixel cache at this port.
378 % o exception: return any errors or warnings in this structure.
382 static MagickBooleanType CreateDistributeCache(SplayTreeInfo *image_registry,
383 int file,const MagickSizeType session_key)
394 register unsigned char
404 buffer[MaxTextExtent];
406 exception=AcquireExceptionInfo();
407 image=AcquireImage((ImageInfo *) NULL,exception);
408 exception=DestroyExceptionInfo(exception);
409 length=sizeof(image->columns)+sizeof(image->rows)+
410 sizeof(image->number_channels);
411 count=read(file,buffer,length);
412 if (count != (ssize_t) length)
415 (void) memcpy(&image->columns,p,sizeof(image->columns));
416 p+=sizeof(image->columns);
417 (void) memcpy(&image->rows,p,sizeof(image->rows));
418 p+=sizeof(image->rows);
419 (void) memcpy(&image->number_channels,p,sizeof(image->number_channels));
420 p+=sizeof(image->number_channels);
421 status=AddValueToSplayTree(image_registry,(const void *) session_key,image);
425 static MagickBooleanType DestroyDistributeCache(SplayTreeInfo *image_registry,
426 int file,const MagickSizeType session_key)
428 return(DeleteNodeFromSplayTree(image_registry,(const void *) session_key));
431 static MagickBooleanType ReadDistributeCacheMetacontent(
432 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
446 register const Quantum
449 register unsigned char
459 buffer[MaxTextExtent];
461 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
463 if (image == (Image *) NULL)
465 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
466 sizeof(region.y)+sizeof(length);
467 count=read(file,buffer,length);
468 if (count != (ssize_t) length)
471 (void) memcpy(®ion.width,q,sizeof(region.width));
472 q+=sizeof(region.width);
473 (void) memcpy(®ion.height,q,sizeof(region.height));
474 q+=sizeof(region.width);
475 (void) memcpy(®ion.x,q,sizeof(region.x));
476 q+=sizeof(region.width);
477 (void) memcpy(®ion.y,q,sizeof(region.y));
478 q+=sizeof(region.width);
479 (void) memcpy(&length,q,sizeof(length));
481 exception=AcquireExceptionInfo();
482 p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
484 exception=DestroyExceptionInfo(exception);
485 if (p == (const Quantum *) NULL)
487 metacontent=GetVirtualMetacontent(image);
488 count=write(file,metacontent,length);
489 if (count != (ssize_t) length)
494 static MagickBooleanType ReadDistributeCachePixels(
495 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
506 register const Quantum
509 register unsigned char
519 buffer[MaxTextExtent];
521 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
523 if (image == (Image *) NULL)
525 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
526 sizeof(region.y)+sizeof(length);
527 count=read(file,buffer,length);
528 if (count != (ssize_t) length)
531 (void) memcpy(®ion.width,q,sizeof(region.width));
532 q+=sizeof(region.width);
533 (void) memcpy(®ion.height,q,sizeof(region.height));
534 q+=sizeof(region.width);
535 (void) memcpy(®ion.x,q,sizeof(region.x));
536 q+=sizeof(region.width);
537 (void) memcpy(®ion.y,q,sizeof(region.y));
538 q+=sizeof(region.width);
539 (void) memcpy(&length,q,sizeof(length));
541 exception=AcquireExceptionInfo();
542 p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
544 exception=DestroyExceptionInfo(exception);
545 if (p == (const Quantum *) NULL)
547 count=write(file,p,length);
548 if (count != (ssize_t) length)
553 static MagickBooleanType WriteDistributeCacheMetacontent(
554 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
571 register unsigned char
581 buffer[MaxTextExtent],
584 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
586 if (image == (Image *) NULL)
588 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
589 sizeof(region.y)+sizeof(length);
590 count=read(file,buffer,length);
591 if (count != (ssize_t) length)
594 (void) memcpy(®ion.width,p,sizeof(region.width));
595 p+=sizeof(region.width);
596 (void) memcpy(®ion.height,p,sizeof(region.height));
597 p+=sizeof(region.width);
598 (void) memcpy(®ion.x,p,sizeof(region.x));
599 p+=sizeof(region.width);
600 (void) memcpy(®ion.y,p,sizeof(region.y));
601 p+=sizeof(region.width);
602 (void) memcpy(&length,p,sizeof(length));
604 exception=AcquireExceptionInfo();
605 q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
607 exception=DestroyExceptionInfo(exception);
608 if (q == (Quantum *) NULL)
610 metacontent=GetAuthenticMetacontent(image);
611 count=read(file,metacontent,length);
612 if (count != (ssize_t) length)
614 status=SyncAuthenticPixels(image,exception);
618 static MagickBooleanType WriteDistributeCachePixels(
619 SplayTreeInfo *image_registry,int file,const MagickSizeType session_key)
636 register unsigned char
646 buffer[MaxTextExtent];
648 image=(Image *) GetValueFromSplayTree(image_registry,(const void *)
650 if (image == (Image *) NULL)
652 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
653 sizeof(region.y)+sizeof(length);
654 count=read(file,buffer,length);
655 if (count != (ssize_t) length)
658 (void) memcpy(®ion.width,p,sizeof(region.width));
659 p+=sizeof(region.width);
660 (void) memcpy(®ion.height,p,sizeof(region.height));
661 p+=sizeof(region.width);
662 (void) memcpy(®ion.x,p,sizeof(region.x));
663 p+=sizeof(region.width);
664 (void) memcpy(®ion.y,p,sizeof(region.y));
665 p+=sizeof(region.width);
666 (void) memcpy(&length,p,sizeof(length));
668 exception=AcquireExceptionInfo();
669 q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
671 exception=DestroyExceptionInfo(exception);
672 if (q == (Quantum *) NULL)
674 count=read(file,q,length);
675 if (count != (ssize_t) length)
677 status=SyncAuthenticPixels(image,exception);
681 static void *DistributePixelCacheClient(void *socket)
710 session[MaxTextExtent];
713 Generate session key.
715 shared_secret=GetPolicyValue("shared-secret");
716 if (shared_secret == (const char *) NULL)
717 ThrowFatalException(CacheFatalError,"shared secret expected");
718 (void) CopyMagickString((char *) session,shared_secret,MaxTextExtent-
719 DPCSessionKeyLength);
720 random_info=AcquireRandomInfo();
721 secret=GetRandomKey(random_info,DPCSessionKeyLength);
722 (void) memcpy(session+strlen(shared_secret),GetStringInfoDatum(secret),
723 DPCSessionKeyLength);
724 session_key=CRC64(session,strlen(shared_secret)+DPCSessionKeyLength);
725 random_info=DestroyRandomInfo(random_info);
726 image_registry=NewSplayTree((int (*)(const void *,const void *)) NULL,
727 (void *(*)(void *)) NULL,(void *(*)(void *)) NULL);
728 client_socket=(*(int *) socket);
729 count=write(client_socket,GetStringInfoDatum(secret),DPCSessionKeyLength);
730 secret=DestroyStringInfo(secret);
733 count=read(client_socket,&command,1);
736 count=read(client_socket,&key,sizeof(key));
737 if ((count != (ssize_t) sizeof(key)) && (key != session_key))
744 status=CreateDistributeCache(image_registry,client_socket,session_key);
749 status=ReadDistributeCachePixels(image_registry,client_socket,
755 status=WriteDistributeCachePixels(image_registry,client_socket,
761 status=DestroyDistributeCache(image_registry,client_socket,session_key);
766 status=ReadDistributeCacheMetacontent(image_registry,client_socket,
772 status=WriteDistributeCacheMetacontent(image_registry,client_socket,
779 count=write(client_socket,&status,sizeof(status));
780 if (count != (ssize_t) sizeof(status))
783 (void) close(client_socket);
784 image_registry=DestroySplayTree(image_registry);
785 return((void *) NULL);
788 MagickExport void DistributePixelCacheServer(const size_t port,
789 ExceptionInfo *exception)
791 #if defined(MAGICKCORE_HAVE_SOCKET) && defined(MAGICKCORE_THREAD_SUPPORT)
806 Launch distributed pixel cache server.
808 server_socket=socket(AF_INET,SOCK_STREAM,0);
809 address.sin_family=AF_INET;
810 address.sin_port=htons(port);
811 address.sin_addr.s_addr=htonl(INADDR_ANY);
812 status=bind(server_socket,(struct sockaddr *) &address,(socklen_t)
815 ThrowFatalException(CacheFatalError,"UnableToBind");
816 status=listen(server_socket,1024);
818 ThrowFatalException(CacheFatalError,"UnableToListen");
819 pthread_attr_init(&attributes);
828 length=(socklen_t) sizeof(address);
829 client_socket=accept(server_socket,(struct sockaddr *) &address,&length);
830 if (client_socket == -1)
831 ThrowFatalException(CacheFatalError,"UnableToEstablishConnection");
832 status=pthread_create(&threads,&attributes,DistributePixelCacheClient,
833 (void *) &client_socket);
835 ThrowFatalException(CacheFatalError,"UnableToCreateClientThread");
837 (void) close(server_socket);
839 ThrowFatalException(MissingDelegateError,"distributed pixel cache");
844 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
848 + G e t D i s t r i b u t e C a c h e F i l e %
852 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % GetDistributeCacheFile() returns the file associated with this
855 % DistributeCacheInfo structure.
857 % The format of the GetDistributeCacheFile method is:
859 % int GetDistributeCacheFile(
860 % const DistributeCacheInfo *distribute_cache_info)
862 % A description of each parameter follows:
864 % o distribute_cache_info: the distributed cache info.
867 MagickPrivate int GetDistributeCacheFile(
868 const DistributeCacheInfo *distribute_cache_info)
870 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
871 assert(distribute_cache_info->signature == MagickSignature);
872 return(distribute_cache_info->file);
876 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 + G e t D i s t r i b u t e C a c h e H o s t n a m e %
884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
886 % GetDistributeCacheHostname() returns the hostname associated with this
887 % DistributeCacheInfo structure.
889 % The format of the GetDistributeCacheHostname method is:
891 % const char *GetDistributeCacheHostname(
892 % const DistributeCacheInfo *distribute_cache_info)
894 % A description of each parameter follows:
896 % o distribute_cache_info: the distributed cache info.
899 MagickPrivate const char *GetDistributeCacheHostname(
900 const DistributeCacheInfo *distribute_cache_info)
902 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
903 assert(distribute_cache_info->signature == MagickSignature);
904 return(distribute_cache_info->hostname);
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 + G e t D i s t r i b u t e C a c h e P o r t %
916 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % GetDistributeCachePort() returns the port associated with this
919 % DistributeCacheInfo structure.
921 % The format of the GetDistributeCachePort method is:
923 % int GetDistributeCachePort(
924 % const DistributeCacheInfo *distribute_cache_info)
926 % A description of each parameter follows:
928 % o distribute_cache_info: the distributed cache info.
931 MagickPrivate int GetDistributeCachePort(
932 const DistributeCacheInfo *distribute_cache_info)
934 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
935 assert(distribute_cache_info->signature == MagickSignature);
936 return(distribute_cache_info->port);
940 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 + O p e n D i s t r i b u t e P i x e l C a c h e %
948 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 % OpenDistributePixelCache() opens a pixel cache on a remote server.
952 % The format of the OpenDistributePixelCache method is:
954 % MagickBooleanType *OpenDistributePixelCache(
955 % DistributeCacheInfo *distribute_cache_info,Image *image)
957 % A description of each parameter follows:
959 % o distribute_cache_info: the distributed cache info.
961 % o image: the image.
964 MagickPrivate MagickBooleanType OpenDistributePixelCache(
965 DistributeCacheInfo *distribute_cache_info,Image *image)
970 register unsigned char
977 buffer[MaxTextExtent];
979 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
980 assert(distribute_cache_info->signature == MagickSignature);
981 assert(image != (Image *) NULL);
982 assert(image->signature == MagickSignature);
984 *p++='c'; /* create */
985 (void) memcpy(p,&distribute_cache_info->session_key,
986 sizeof(distribute_cache_info->session_key));
987 p+=sizeof(distribute_cache_info->session_key);
988 (void) memcpy(p,&image->columns,sizeof(image->columns));
989 p+=sizeof(image->columns);
990 (void) memcpy(p,&image->rows,sizeof(image->rows));
991 p+=sizeof(image->rows);
992 (void) memcpy(p,&image->number_channels,sizeof(image->number_channels));
993 p+=sizeof(image->number_channels);
994 count=write(distribute_cache_info->file,buffer,p-buffer);
995 if (count != (ssize_t) (p-buffer))
997 count=read(distribute_cache_info->file,&status,sizeof(status));
998 if (count != (ssize_t) sizeof(status))
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1008 + R e a d D i s t r i b u t e P i x e l C a c h e M e t a c o n t e n t %
1012 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014 % ReadDistributePixelCacheMetacontents() reads metacontent from the specified
1015 % region of the distributed pixel cache.
1017 % The format of the ReadDistributePixelCacheMetacontents method is:
1019 % MagickBooleanType *ReadDistributePixelCacheMetacontents(
1020 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1021 % const MagickSizeType length,unsigned char *metacontent)
1023 % A description of each parameter follows:
1025 % o distribute_cache_info: the distributed cache info.
1027 % o image: the image.
1029 % o region: read the metacontent from this region of the image.
1031 % o length: the length in bytes of the metacontent.
1033 % o metacontent: read these metacontent from the pixel cache.
1036 MagickPrivate MagickBooleanType ReadDistributePixelCacheMetacontent(
1037 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1038 const MagickSizeType length,unsigned char *metacontent)
1043 register unsigned char
1050 buffer[MaxTextExtent];
1052 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1053 assert(distribute_cache_info->signature == MagickSignature);
1054 assert(region != (RectangleInfo *) NULL);
1055 assert(metacontent != (unsigned char *) NULL);
1056 assert(length == ((size_t) length));
1058 *p++='m'; /* read */
1059 (void) memcpy(p,&distribute_cache_info->session_key,
1060 sizeof(distribute_cache_info->session_key));
1061 p+=sizeof(distribute_cache_info->session_key);
1062 (void) memcpy(p,®ion->width,sizeof(region->width));
1063 p+=sizeof(region->width);
1064 (void) memcpy(p,®ion->height,sizeof(region->height));
1065 p+=sizeof(region->height);
1066 (void) memcpy(p,®ion->x,sizeof(region->x));
1067 p+=sizeof(region->x);
1068 (void) memcpy(p,®ion->y,sizeof(region->y));
1069 p+=sizeof(region->y);
1070 (void) memcpy(p,&length,sizeof(length));
1072 count=write(distribute_cache_info->file,buffer,p-buffer);
1073 if (count != (ssize_t) (p-buffer))
1074 return(MagickFalse);
1075 count=read(distribute_cache_info->file,(unsigned char *) metacontent,(size_t)
1077 if (count != (ssize_t) length)
1078 return(MagickFalse);
1079 count=read(distribute_cache_info->file,&status,sizeof(status));
1080 if (count != (ssize_t) sizeof(status))
1081 return(MagickFalse);
1082 return(status != 0 ? MagickTrue : MagickFalse);
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090 + R e a d D i s t r i b u t e P i x e l C a c h e P i x e l s %
1094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1096 % ReadDistributePixelCachePixels() reads pixels from the specified region of
1097 % the distributed pixel cache.
1099 % The format of the ReadDistributePixelCachePixels method is:
1101 % MagickBooleanType *ReadDistributePixelCachePixels(
1102 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1103 % const MagickSizeType length,unsigned char *pixels)
1105 % A description of each parameter follows:
1107 % o distribute_cache_info: the distributed cache info.
1109 % o image: the image.
1111 % o region: read the pixels from this region of the image.
1113 % o length: the length in bytes of the pixels.
1115 % o pixels: read these pixels from the pixel cache.
1118 MagickPrivate MagickBooleanType ReadDistributePixelCachePixels(
1119 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1120 const MagickSizeType length,unsigned char *pixels)
1125 register unsigned char
1132 buffer[MaxTextExtent];
1134 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1135 assert(distribute_cache_info->signature == MagickSignature);
1136 assert(region != (RectangleInfo *) NULL);
1137 assert(pixels != (unsigned char *) NULL);
1138 assert(length == ((size_t) length));
1140 *p++='r'; /* read */
1141 (void) memcpy(p,&distribute_cache_info->session_key,
1142 sizeof(distribute_cache_info->session_key));
1143 p+=sizeof(distribute_cache_info->session_key);
1144 (void) memcpy(p,®ion->width,sizeof(region->width));
1145 p+=sizeof(region->width);
1146 (void) memcpy(p,®ion->height,sizeof(region->height));
1147 p+=sizeof(region->height);
1148 (void) memcpy(p,®ion->x,sizeof(region->x));
1149 p+=sizeof(region->x);
1150 (void) memcpy(p,®ion->y,sizeof(region->y));
1151 p+=sizeof(region->y);
1152 (void) memcpy(p,&length,sizeof(length));
1154 count=write(distribute_cache_info->file,buffer,p-buffer);
1155 if (count != (ssize_t) (p-buffer))
1156 return(MagickFalse);
1157 count=read(distribute_cache_info->file,(unsigned char *) pixels,(size_t)
1159 if (count != (ssize_t) length)
1160 return(MagickFalse);
1161 count=read(distribute_cache_info->file,&status,sizeof(status));
1162 if (count != (ssize_t) sizeof(status))
1163 return(MagickFalse);
1164 return(status != 0 ? MagickTrue : MagickFalse);
1168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1172 + R e l i n q u i s h D i s t r i b u t e P i x e l C a c h e %
1176 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1178 % RelinquishDistributePixelCache() frees resources acquired with
1179 % OpenDistributePixelCache().
1181 % The format of the RelinquishDistributePixelCache method is:
1183 % MagickBooleanType RelinquishDistributePixelCache(
1184 % DistributeCacheInfo *distribute_cache_info)
1186 % A description of each parameter follows:
1188 % o distribute_cache_info: the distributed cache info.
1191 MagickPrivate MagickBooleanType RelinquishDistributePixelCache(
1192 DistributeCacheInfo *distribute_cache_info)
1194 register unsigned char
1201 buffer[MaxTextExtent];
1203 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1204 assert(distribute_cache_info->signature == MagickSignature);
1206 *p++='d'; /* delete */
1207 (void) memcpy(p,&distribute_cache_info->session_key,
1208 sizeof(distribute_cache_info->session_key));
1209 p+=sizeof(distribute_cache_info->session_key);
1210 count=write(distribute_cache_info->file,buffer,p-buffer);
1211 if (count != (ssize_t) (p-buffer))
1212 return(MagickFalse);
1217 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1221 + W r i t e D i s t r i b u t e P i x e l C a c h e M e t a c o n t e n t %
1225 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1227 % WriteDistributePixelCacheMetacontents() writes image metacontent to the
1228 % specified region of the distributed pixel cache.
1231 % The format of the WriteDistributePixelCacheMetacontents method is:
1233 % MagickBooleanType *WriteDistributePixelCacheMetacontents(
1234 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1235 % const MagickSizeType length,const unsigned char *metacontent)
1237 % A description of each parameter follows:
1239 % o distribute_cache_info: the distributed cache info.
1241 % o image: the image.
1243 % o region: write the metacontent to this region of the image.
1245 % o length: the length in bytes of the metacontent.
1247 % o metacontent: write these metacontent to the pixel cache.
1250 MagickPrivate MagickBooleanType WriteDistributePixelCacheMetacontent(
1251 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1252 const MagickSizeType length,const unsigned char *metacontent)
1257 register unsigned char
1264 buffer[MaxTextExtent];
1266 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1267 assert(distribute_cache_info->signature == MagickSignature);
1268 assert(region != (RectangleInfo *) NULL);
1269 assert(metacontent != (unsigned char *) NULL);
1270 assert(length == ((size_t) length));
1272 *p++='M'; /* update */
1273 (void) memcpy(p,&distribute_cache_info->session_key,
1274 sizeof(distribute_cache_info->session_key));
1275 p+=sizeof(distribute_cache_info->session_key);
1276 (void) memcpy(p,®ion->width,sizeof(region->width));
1277 p+=sizeof(region->width);
1278 (void) memcpy(p,®ion->height,sizeof(region->height));
1279 p+=sizeof(region->height);
1280 (void) memcpy(p,®ion->x,sizeof(region->x));
1281 p+=sizeof(region->x);
1282 (void) memcpy(p,®ion->y,sizeof(region->y));
1283 p+=sizeof(region->y);
1284 (void) memcpy(p,&length,sizeof(length));
1286 count=write(distribute_cache_info->file,buffer,p-buffer);
1287 if (count != (ssize_t) (p-buffer))
1288 return(MagickFalse);
1289 count=write(distribute_cache_info->file,(unsigned char *) metacontent,(size_t)
1291 if (count != (ssize_t) length)
1292 return(MagickFalse);
1293 count=read(distribute_cache_info->file,&status,sizeof(status));
1294 if (count != (ssize_t) sizeof(status))
1295 return(MagickFalse);
1296 return(status != 0 ? MagickTrue : MagickFalse);
1300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1304 + W r i t e D i s t r i b u t e P i x e l C a c h e P i x e l s %
1308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310 % WriteDistributePixelCachePixels() writes image pixels to the specified
1311 % region of the distributed pixel cache.
1314 % The format of the WriteDistributePixelCachePixels method is:
1316 % MagickBooleanType *WriteDistributePixelCachePixels(
1317 % DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1318 % const MagickSizeType length,const unsigned char *pixels)
1320 % A description of each parameter follows:
1322 % o distribute_cache_info: the distributed cache info.
1324 % o image: the image.
1326 % o region: write the pixels to this region of the image.
1328 % o length: the length in bytes of the pixels.
1330 % o pixels: write these pixels to the pixel cache.
1333 MagickPrivate MagickBooleanType WriteDistributePixelCachePixels(
1334 DistributeCacheInfo *distribute_cache_info,const RectangleInfo *region,
1335 const MagickSizeType length,const unsigned char *pixels)
1340 register unsigned char
1347 buffer[MaxTextExtent];
1349 assert(distribute_cache_info != (DistributeCacheInfo *) NULL);
1350 assert(distribute_cache_info->signature == MagickSignature);
1351 assert(region != (RectangleInfo *) NULL);
1352 assert(pixels != (const unsigned char *) NULL);
1353 assert(length == ((size_t) length));
1355 *p++='u'; /* update */
1356 (void) memcpy(p,&distribute_cache_info->session_key,
1357 sizeof(distribute_cache_info->session_key));
1358 p+=sizeof(distribute_cache_info->session_key);
1359 (void) memcpy(p,®ion->width,sizeof(region->width));
1360 p+=sizeof(region->width);
1361 (void) memcpy(p,®ion->height,sizeof(region->height));
1362 p+=sizeof(region->height);
1363 (void) memcpy(p,®ion->x,sizeof(region->x));
1364 p+=sizeof(region->x);
1365 (void) memcpy(p,®ion->y,sizeof(region->y));
1366 p+=sizeof(region->y);
1367 (void) memcpy(p,&length,sizeof(length));
1369 count=write(distribute_cache_info->file,buffer,p-buffer);
1370 if (count != (ssize_t) (p-buffer))
1371 return(MagickFalse);
1372 count=write(distribute_cache_info->file,(unsigned char *) pixels,(size_t)
1374 if (count != (ssize_t) length)
1375 return(MagickFalse);
1376 count=read(distribute_cache_info->file,&status,sizeof(status));
1377 if (count != (ssize_t) sizeof(status))
1378 return(MagickFalse);
1379 return(status != 0 ? MagickTrue : MagickFalse);