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/list.h"
64 #include "MagickCore/locale_.h"
65 #include "MagickCore/memory_.h"
66 #include "MagickCore/pixel.h"
67 #include "MagickCore/policy.h"
68 #include "MagickCore/random_.h"
69 #include "MagickCore/registry.h"
70 #include "MagickCore/splay-tree.h"
71 #include "MagickCore/string_.h"
72 #include "MagickCore/string-private.h"
73 #include "MagickCore/version.h"
74 #if defined(MAGICKCORE_HAVE_SOCKET)
75 #include <netinet/in.h>
77 #include <sys/socket.h>
78 #include <arpa/inet.h>
84 #define DPCHostname "127.0.0.1"
85 #define DPCPendingConnections 10
87 #define DPCSessionKeyLength 8
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 + 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 %
98 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
100 % AcquireDistributeCacheInfo() allocates the DistributeCacheInfo structure.
102 % The format of the AcquireDistributeCacheInfo method is:
104 % DistributeCacheInfo *AcquireDistributeCacheInfo(ExceptionInfo *exception)
106 % A description of each parameter follows:
108 % o exception: return any errors or warnings in this structure.
112 static MagickSizeType CRC64(const unsigned char *message,
113 const MagickSizeType length)
118 register MagickOffsetType
121 static MagickBooleanType
122 crc_initial = MagickFalse;
124 static MagickSizeType
128 Generate a 64-bit cyclic redundancy check for the message.
130 if (crc_initial == MagickFalse)
135 for (i=0; i < 256; i++)
137 register MagickOffsetType
140 alpha=(MagickSizeType) i;
141 for (j=0; j < 8; j++)
143 if ((alpha & 0x01) == 0)
146 alpha=(MagickSizeType) ((alpha >> 1) ^
147 MagickULLConstant(0xd800000000000000));
151 crc_initial=MagickTrue;
154 for (i=0; i < (MagickOffsetType) length; i++)
155 crc=crc_xor[(crc ^ message[i]) & 0xff] ^ (crc >> 8);
159 static inline MagickSizeType MagickMin(const MagickSizeType x,
160 const MagickSizeType y)
167 static inline MagickOffsetType dpc_read(int file,const MagickSizeType length,
168 unsigned char *restrict message)
170 register MagickOffsetType
177 for (i=0; i < (MagickOffsetType) length; i+=count)
179 count=recv(file,message+i,(size_t) MagickMin(length-i,(MagickSizeType)
191 static int ConnectPixelCacheServer(const char *hostname,const int port,
192 MagickSizeType *session_key,ExceptionInfo *exception)
194 #if defined(MAGICKCORE_HAVE_SOCKET)
196 service[MaxTextExtent];
208 register unsigned char
216 secret[MaxTextExtent],
217 session[2*MaxTextExtent];
220 Connect to distributed pixel cache and get session key.
223 shared_secret=GetPolicyValue("shared-secret");
224 if (shared_secret == (const char *) NULL)
226 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
227 "DistributedPixelCache","'%s'","shared secret expected");
231 (void) CopyMagickString((char *) p,shared_secret,MaxTextExtent);
232 p+=strlen(shared_secret);
233 (void) ResetMagickMemory(&hint,0,sizeof(hint));
234 hint.ai_family=AF_INET;
235 hint.ai_socktype=SOCK_STREAM;
236 hint.ai_flags=AI_PASSIVE;
237 (void) FormatLocaleString(service,MaxTextExtent,"%d",port);
238 status=getaddrinfo(hostname,service,&hint,&result);
241 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
242 "DistributedPixelCache","'%s'",hostname);
245 client_socket=socket(result->ai_family,result->ai_socktype,
246 result->ai_protocol);
247 if (client_socket == -1)
249 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
250 "DistributedPixelCache","'%s'",hostname);
253 status=connect(client_socket,result->ai_addr,result->ai_addrlen);
256 (void) ThrowMagickException(exception,GetMagickModule(),CacheError,
257 "DistributedPixelCache","'%s'",hostname);
260 count=recv(client_socket,secret,MaxTextExtent,0);
266 (void) memcpy(p,secret,(size_t) count);
268 signature=MagickLibVersion;
269 (void) memcpy(p,&signature,sizeof(signature));
270 p+=sizeof(signature);
271 signature=MAGICKCORE_QUANTUM_DEPTH;
272 (void) memcpy(p,&signature,sizeof(signature));
273 p+=sizeof(signature);
274 signature=MAGICKCORE_HDRI_ENABLE;
275 (void) memcpy(p,&signature,sizeof(signature));
276 p+=sizeof(signature);
277 *session_key=CRC64(session,p-session);
279 if (*session_key == 0)
281 close(client_socket);
284 return(client_socket);
286 (void) ThrowMagickException(exception,GetMagickModule(),MissingDelegateError,
287 "DelegateLibrarySupportNotBuiltIn","distributed pixel cache");
292 static char *GetHostname(int *port,ExceptionInfo *exception)
309 Parse host list (e.g. 192.168.100.1:6668,192.168.100.2:6668).
311 hosts=(char *) GetImageRegistry(StringRegistryType,"cache:hosts",
313 if (hosts == (char *) NULL)
316 return(AcquireString(DPCHostname));
318 (void) SubstituteString(&hosts,","," ");
319 hostlist=StringToArgv(hosts,&argc);
320 hosts=DestroyString(hosts);
321 if (hostlist == (char **) NULL)
324 return(AcquireString(DPCHostname));
326 hosts=AcquireString(hostlist[(id++ % (argc-1))+1]);
327 for (i=0; i < (ssize_t) argc; i++)
328 hostlist[i]=DestroyString(hostlist[i]);
329 hostlist=(char **) RelinquishMagickMemory(hostlist);
330 (void) SubstituteString(&hosts,":"," ");
331 hostlist=StringToArgv(hosts,&argc);
332 if (hostlist == (char **) NULL)
335 return(AcquireString(DPCHostname));
337 host=AcquireString(hostlist[1]);
338 if (hostlist[2] == (char *) NULL)
341 *port=StringToLong(hostlist[2]);
342 for (i=0; i < (ssize_t) argc; i++)
343 hostlist[i]=DestroyString(hostlist[i]);
344 hostlist=(char **) RelinquishMagickMemory(hostlist);
348 MagickPrivate DistributeCacheInfo *AcquireDistributeCacheInfo(
349 ExceptionInfo *exception)
361 Connect to the distributed pixel cache server.
363 server_info=(DistributeCacheInfo *) AcquireMagickMemory(sizeof(*server_info));
364 if (server_info == (DistributeCacheInfo *) NULL)
365 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
366 (void) ResetMagickMemory(server_info,0,sizeof(*server_info));
367 server_info->signature=MagickSignature;
369 hostname=GetHostname(&server_info->port,exception);
371 server_info->file=ConnectPixelCacheServer(hostname,server_info->port,
372 &session_key,exception);
373 server_info->session_key=session_key;
374 (void) CopyMagickString(server_info->hostname,hostname,MaxTextExtent);
375 hostname=DestroyString(hostname);
376 if (server_info->file == -1)
377 server_info=DestroyDistributeCacheInfo(server_info);
382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386 + 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 %
390 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392 % DestroyDistributeCacheInfo() deallocates memory associated with an
393 % DistributeCacheInfo structure.
395 % The format of the DestroyDistributeCacheInfo method is:
397 % DistributeCacheInfo *DestroyDistributeCacheInfo(
398 % DistributeCacheInfo *server_info)
400 % A description of each parameter follows:
402 % o server_info: the distributed cache info.
405 MagickPrivate DistributeCacheInfo *DestroyDistributeCacheInfo(
406 DistributeCacheInfo *server_info)
408 assert(server_info != (DistributeCacheInfo *) NULL);
409 assert(server_info->signature == MagickSignature);
410 if (server_info->file > 0)
411 (void) close(server_info->file);
412 server_info->signature=(~MagickSignature);
413 server_info=(DistributeCacheInfo *) RelinquishMagickMemory(server_info);
418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
422 + 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 %
426 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
428 % DistributePixelCacheServer() waits on the specified port for commands to
429 % create, read, update, or destroy a pixel cache.
431 % The format of the DistributePixelCacheServer() method is:
433 % void DistributePixelCacheServer(const int port)
435 % A description of each parameter follows:
437 % o port: connect the distributed pixel cache at this port.
439 % o exception: return any errors or warnings in this structure.
443 static MagickBooleanType DestroyDistributeCache(SplayTreeInfo *registry,
444 int file,const MagickSizeType session_key)
447 Destroy distributed pixel cache.
449 return(DeleteNodeFromSplayTree(registry,(const void *) session_key));
452 static inline MagickOffsetType dpc_send(int file,const MagickSizeType length,
453 const unsigned char *restrict message)
458 register MagickOffsetType
462 Ensure a complete message is sent.
465 for (i=0; i < (MagickOffsetType) length; i+=count)
467 count=(MagickOffsetType) send(file,message+i,(size_t) MagickMin(length-i,
468 (MagickSizeType) SSIZE_MAX),MSG_NOSIGNAL);
479 static MagickBooleanType OpenDistributeCache(SplayTreeInfo *registry,
480 int file,const MagickSizeType session_key,ExceptionInfo *exception)
494 register unsigned char
498 message[MaxTextExtent];
501 Open distributed pixel cache.
503 image=AcquireImage((ImageInfo *) NULL,exception);
504 if (image == (Image *) NULL)
506 length=sizeof(image->storage_class)+sizeof(image->colorspace)+
507 sizeof(image->alpha_trait)+sizeof(image->mask)+sizeof(image->columns)+
508 sizeof(image->rows)+sizeof(image->number_channels)+MaxPixelChannels*
509 sizeof(*image->channel_map)+sizeof(image->metacontent_extent);
510 count=dpc_read(file,length,message);
511 if (count != (MagickOffsetType) length)
514 Deserialize image attributes.
517 (void) memcpy(&image->storage_class,p,sizeof(image->storage_class));
518 p+=sizeof(image->storage_class);
519 (void) memcpy(&image->colorspace,p,sizeof(image->colorspace));
520 p+=sizeof(image->colorspace);
521 (void) memcpy(&image->alpha_trait,p,sizeof(image->alpha_trait));
522 p+=sizeof(image->alpha_trait);
523 (void) memcpy(&image->mask,p,sizeof(image->mask));
524 p+=sizeof(image->mask);
525 (void) memcpy(&image->columns,p,sizeof(image->columns));
526 p+=sizeof(image->columns);
527 (void) memcpy(&image->rows,p,sizeof(image->rows));
528 p+=sizeof(image->rows);
529 (void) memcpy(&image->number_channels,p,sizeof(image->number_channels));
530 p+=sizeof(image->number_channels);
531 (void) memcpy(image->channel_map,p,MaxPixelChannels*
532 sizeof(*image->channel_map));
533 p+=MaxPixelChannels*sizeof(*image->channel_map);
534 (void) memcpy(&image->metacontent_extent,p,sizeof(image->metacontent_extent));
535 p+=sizeof(image->metacontent_extent);
536 status=AddValueToSplayTree(registry,(const void *) session_key,image);
540 static MagickBooleanType ReadDistributeCacheMetacontent(SplayTreeInfo *registry,
541 int file,const MagickSizeType session_key,ExceptionInfo *exception)
558 register const Quantum
561 register unsigned char
565 message[MaxTextExtent];
568 Read distributed pixel cache metacontent.
570 image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
571 if (image == (Image *) NULL)
573 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
574 sizeof(region.y)+sizeof(length);
575 count=dpc_read(file,length,message);
576 if (count != (MagickOffsetType) length)
579 (void) memcpy(®ion.width,q,sizeof(region.width));
580 q+=sizeof(region.width);
581 (void) memcpy(®ion.height,q,sizeof(region.height));
582 q+=sizeof(region.height);
583 (void) memcpy(®ion.x,q,sizeof(region.x));
585 (void) memcpy(®ion.y,q,sizeof(region.y));
587 (void) memcpy(&length,q,sizeof(length));
589 p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
591 if (p == (const Quantum *) NULL)
593 metacontent=GetVirtualMetacontent(image);
594 count=dpc_send(file,length,metacontent);
595 if (count != (MagickOffsetType) length)
600 static MagickBooleanType ReadDistributeCachePixels(SplayTreeInfo *registry,
601 int file,const MagickSizeType session_key,ExceptionInfo *exception)
615 register const Quantum
618 register unsigned char
622 message[MaxTextExtent];
625 Read distributed pixel cache pixels.
627 image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
628 if (image == (Image *) NULL)
630 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
631 sizeof(region.y)+sizeof(length);
632 count=dpc_read(file,length,message);
633 if (count != (MagickOffsetType) length)
636 (void) memcpy(®ion.width,q,sizeof(region.width));
637 q+=sizeof(region.width);
638 (void) memcpy(®ion.height,q,sizeof(region.height));
639 q+=sizeof(region.height);
640 (void) memcpy(®ion.x,q,sizeof(region.x));
642 (void) memcpy(®ion.y,q,sizeof(region.y));
644 (void) memcpy(&length,q,sizeof(length));
646 p=GetVirtualPixels(image,region.x,region.y,region.width,region.height,
648 if (p == (const Quantum *) NULL)
650 count=dpc_send(file,length,(unsigned char *) p);
651 if (count != (MagickOffsetType) length)
656 static void *RelinquishImageRegistry(void *image)
658 return((void *) DestroyImageList((Image *) image));
661 static MagickBooleanType WriteDistributeCacheMetacontent(
662 SplayTreeInfo *registry,int file,const MagickSizeType session_key,
663 ExceptionInfo *exception)
680 register unsigned char
684 message[MaxTextExtent],
688 Write distributed pixel cache metacontent.
690 image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
691 if (image == (Image *) NULL)
693 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
694 sizeof(region.y)+sizeof(length);
695 count=dpc_read(file,length,message);
696 if (count != (MagickOffsetType) length)
699 (void) memcpy(®ion.width,p,sizeof(region.width));
700 p+=sizeof(region.width);
701 (void) memcpy(®ion.height,p,sizeof(region.height));
702 p+=sizeof(region.height);
703 (void) memcpy(®ion.x,p,sizeof(region.x));
705 (void) memcpy(®ion.y,p,sizeof(region.y));
707 (void) memcpy(&length,p,sizeof(length));
709 q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
711 if (q == (Quantum *) NULL)
713 metacontent=GetAuthenticMetacontent(image);
714 count=dpc_read(file,length,metacontent);
715 if (count != (MagickOffsetType) length)
717 return(SyncAuthenticPixels(image,exception));
720 static MagickBooleanType WriteDistributeCachePixels(SplayTreeInfo *registry,
721 int file,const MagickSizeType session_key,ExceptionInfo *exception)
738 register unsigned char
742 message[MaxTextExtent];
745 Write distributed pixel cache pixels.
747 image=(Image *) GetValueFromSplayTree(registry,(const void *) session_key);
748 if (image == (Image *) NULL)
750 length=sizeof(region.width)+sizeof(region.height)+sizeof(region.x)+
751 sizeof(region.y)+sizeof(length);
752 count=dpc_read(file,length,message);
753 if (count != (MagickOffsetType) length)
756 (void) memcpy(®ion.width,p,sizeof(region.width));
757 p+=sizeof(region.width);
758 (void) memcpy(®ion.height,p,sizeof(region.height));
759 p+=sizeof(region.height);
760 (void) memcpy(®ion.x,p,sizeof(region.x));
762 (void) memcpy(®ion.y,p,sizeof(region.y));
764 (void) memcpy(&length,p,sizeof(length));
766 q=GetAuthenticPixels(image,region.x,region.y,region.width,region.height,
768 if (q == (Quantum *) NULL)
770 count=dpc_read(file,length,(unsigned char *) q);
771 if (count != (MagickOffsetType) length)
773 return(SyncAuthenticPixels(image,exception));
776 static void *DistributePixelCacheClient(void *socket)
798 register unsigned char
812 session[2*MaxTextExtent];
815 Distributed pixel cache client.
817 shared_secret=GetPolicyValue("shared-secret");
818 if (shared_secret == (const char *) NULL)
819 ThrowFatalException(CacheFatalError,"shared secret expected");
821 (void) CopyMagickString((char *) p,shared_secret,MaxTextExtent);
822 p+=strlen(shared_secret);
823 random_info=AcquireRandomInfo();
824 secret=GetRandomKey(random_info,DPCSessionKeyLength);
825 (void) memcpy(p,GetStringInfoDatum(secret),DPCSessionKeyLength);
826 p+=DPCSessionKeyLength;
827 signature=MagickLibVersion;
828 (void) memcpy(p,&signature,sizeof(signature));
829 p+=sizeof(signature);
830 signature=MAGICKCORE_QUANTUM_DEPTH;
831 (void) memcpy(p,&signature,sizeof(signature));
832 p+=sizeof(signature);
833 signature=MAGICKCORE_HDRI_ENABLE;
834 (void) memcpy(p,&signature,sizeof(signature));
835 p+=sizeof(signature);
836 session_key=CRC64(session,p-session);
837 random_info=DestroyRandomInfo(random_info);
838 exception=AcquireExceptionInfo();
839 registry=NewSplayTree((int (*)(const void *,const void *)) NULL,
840 (void *(*)(void *)) NULL,RelinquishImageRegistry);
841 client_socket=(*(int *) socket);
842 count=dpc_send(client_socket,DPCSessionKeyLength,GetStringInfoDatum(secret));
843 secret=DestroyStringInfo(secret);
846 count=dpc_read(client_socket,1,(unsigned char *) &command);
849 count=dpc_read(client_socket,sizeof(key),(unsigned char *) &key);
850 if ((count != (MagickOffsetType) sizeof(key)) || (key != session_key))
857 status=OpenDistributeCache(registry,client_socket,session_key,
859 count=dpc_send(client_socket,sizeof(status),(unsigned char *) &status);
864 status=ReadDistributeCachePixels(registry,client_socket,session_key,
870 status=ReadDistributeCacheMetacontent(registry,client_socket,
871 session_key,exception);
876 status=WriteDistributeCachePixels(registry,client_socket,session_key,
882 status=WriteDistributeCacheMetacontent(registry,client_socket,
883 session_key,exception);
888 status=DestroyDistributeCache(registry,client_socket,session_key);
894 if (status == MagickFalse)
899 count=dpc_send(client_socket,sizeof(status),(unsigned char *) &status);
900 (void) close(client_socket);
901 exception=DestroyExceptionInfo(exception);
902 registry=DestroySplayTree(registry);
903 return((void *) NULL);
906 MagickExport void DistributePixelCacheServer(const int port,
907 ExceptionInfo *exception)
909 #if defined(MAGICKCORE_HAVE_SOCKET) && defined(MAGICKCORE_THREAD_SUPPORT)
911 service[MaxTextExtent];
923 register struct addrinfo
934 Launch distributed pixel cache server.
936 (void) ResetMagickMemory(&hint,0,sizeof(hint));
937 hint.ai_family=AF_INET;
938 hint.ai_socktype=SOCK_STREAM;
939 hint.ai_flags=AI_PASSIVE;
940 (void) FormatLocaleString(service,MaxTextExtent,"%d",port);
941 status=getaddrinfo((const char *) NULL,service,&hint,&result);
943 ThrowFatalException(CacheFatalError,"UnableToListen");
944 for (p=result; p != (struct addrinfo *) NULL; p=p->ai_next)
949 server_socket=socket(p->ai_family,p->ai_socktype,p->ai_protocol);
950 if (server_socket == -1)
953 status=setsockopt(server_socket,SOL_SOCKET,SO_REUSEADDR,&one,(socklen_t)
957 status=bind(server_socket,p->ai_addr,p->ai_addrlen);
960 (void) close(status);
965 if (p == (struct addrinfo *) NULL)
966 ThrowFatalException(CacheFatalError,"UnableToBind");
967 freeaddrinfo(result);
968 status=listen(server_socket,DPCPendingConnections);
970 ThrowFatalException(CacheFatalError,"UnableToListen");
971 pthread_attr_init(&attributes);
980 length=(socklen_t) sizeof(address);
981 client_socket=accept(server_socket,(struct sockaddr *) &address,&length);
982 if (client_socket == -1)
983 ThrowFatalException(CacheFatalError,"UnableToEstablishConnection");
984 status=pthread_create(&threads,&attributes,DistributePixelCacheClient,
985 (void *) &client_socket);
987 ThrowFatalException(CacheFatalError,"UnableToCreateClientThread");
989 (void) close(server_socket);
991 ThrowFatalException(MissingDelegateError,"distributed pixel cache");
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 + G e t D i s t r i b u t e C a c h e F i l e %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006 % GetDistributeCacheFile() returns the file associated with this
1007 % DistributeCacheInfo structure.
1009 % The format of the GetDistributeCacheFile method is:
1011 % int GetDistributeCacheFile(const DistributeCacheInfo *server_info)
1013 % A description of each parameter follows:
1015 % o server_info: the distributed cache info.
1018 MagickPrivate int GetDistributeCacheFile(const DistributeCacheInfo *server_info)
1020 assert(server_info != (DistributeCacheInfo *) NULL);
1021 assert(server_info->signature == MagickSignature);
1022 return(server_info->file);
1026 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1030 + 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 %
1034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1036 % GetDistributeCacheHostname() returns the hostname associated with this
1037 % DistributeCacheInfo structure.
1039 % The format of the GetDistributeCacheHostname method is:
1041 % const char *GetDistributeCacheHostname(
1042 % const DistributeCacheInfo *server_info)
1044 % A description of each parameter follows:
1046 % o server_info: the distributed cache info.
1049 MagickPrivate const char *GetDistributeCacheHostname(
1050 const DistributeCacheInfo *server_info)
1052 assert(server_info != (DistributeCacheInfo *) NULL);
1053 assert(server_info->signature == MagickSignature);
1054 return(server_info->hostname);
1058 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1062 + G e t D i s t r i b u t e C a c h e P o r t %
1066 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1068 % GetDistributeCachePort() returns the port associated with this
1069 % DistributeCacheInfo structure.
1071 % The format of the GetDistributeCachePort method is:
1073 % int GetDistributeCachePort(const DistributeCacheInfo *server_info)
1075 % A description of each parameter follows:
1077 % o server_info: the distributed cache info.
1080 MagickPrivate int GetDistributeCachePort(const DistributeCacheInfo *server_info)
1082 assert(server_info != (DistributeCacheInfo *) NULL);
1083 assert(server_info->signature == MagickSignature);
1084 return(server_info->port);
1088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1092 + O p e n D i s t r i b u t e P i x e l C a c h e %
1096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1098 % OpenDistributePixelCache() opens a pixel cache on a remote server.
1100 % The format of the OpenDistributePixelCache method is:
1102 % MagickBooleanType *OpenDistributePixelCache(
1103 % DistributeCacheInfo *server_info,Image *image)
1105 % A description of each parameter follows:
1107 % o server_info: the distributed cache info.
1109 % o image: the image.
1112 MagickPrivate MagickBooleanType OpenDistributePixelCache(
1113 DistributeCacheInfo *server_info,Image *image)
1121 register unsigned char
1125 message[MaxTextExtent];
1128 Open distributed pixel cache.
1130 assert(server_info != (DistributeCacheInfo *) NULL);
1131 assert(server_info->signature == MagickSignature);
1132 assert(image != (Image *) NULL);
1133 assert(image->signature == MagickSignature);
1135 *p++='o'; /* open */
1137 Serialize image attributes (see ValidatePixelCacheMorphology()).
1139 (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1140 p+=sizeof(server_info->session_key);
1141 (void) memcpy(p,&image->storage_class,sizeof(image->storage_class));
1142 p+=sizeof(image->storage_class);
1143 (void) memcpy(p,&image->colorspace,sizeof(image->colorspace));
1144 p+=sizeof(image->colorspace);
1145 (void) memcpy(p,&image->alpha_trait,sizeof(image->alpha_trait));
1146 p+=sizeof(image->alpha_trait);
1147 (void) memcpy(p,&image->mask,sizeof(image->mask));
1148 p+=sizeof(image->mask);
1149 (void) memcpy(p,&image->columns,sizeof(image->columns));
1150 p+=sizeof(image->columns);
1151 (void) memcpy(p,&image->rows,sizeof(image->rows));
1152 p+=sizeof(image->rows);
1153 (void) memcpy(p,&image->number_channels,sizeof(image->number_channels));
1154 p+=sizeof(image->number_channels);
1155 (void) memcpy(p,image->channel_map,MaxPixelChannels*
1156 sizeof(*image->channel_map));
1157 p+=MaxPixelChannels*sizeof(*image->channel_map);
1158 (void) memcpy(p,&image->metacontent_extent,sizeof(image->metacontent_extent));
1159 p+=sizeof(image->metacontent_extent);
1160 count=dpc_send(server_info->file,p-message,message);
1161 if (count != (MagickOffsetType) (p-message))
1162 return(MagickFalse);
1164 count=dpc_read(server_info->file,sizeof(status),(unsigned char *) &status);
1165 if (count != (MagickOffsetType) sizeof(status))
1166 return(MagickFalse);
1171 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1175 + 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 %
1179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1181 % ReadDistributePixelCacheMetacontents() reads metacontent from the specified
1182 % region of the distributed pixel cache.
1184 % The format of the ReadDistributePixelCacheMetacontents method is:
1186 % MagickOffsetType ReadDistributePixelCacheMetacontents(
1187 % DistributeCacheInfo *server_info,const RectangleInfo *region,
1188 % const MagickSizeType length,unsigned char *metacontent)
1190 % A description of each parameter follows:
1192 % o server_info: the distributed cache info.
1194 % o image: the image.
1196 % o region: read the metacontent from this region of the image.
1198 % o length: the length in bytes of the metacontent.
1200 % o metacontent: read these metacontent from the pixel cache.
1203 MagickPrivate MagickOffsetType ReadDistributePixelCacheMetacontent(
1204 DistributeCacheInfo *server_info,const RectangleInfo *region,
1205 const MagickSizeType length,unsigned char *metacontent)
1210 register unsigned char
1214 message[MaxTextExtent];
1217 Read distributed pixel cache metacontent.
1219 assert(server_info != (DistributeCacheInfo *) NULL);
1220 assert(server_info->signature == MagickSignature);
1221 assert(region != (RectangleInfo *) NULL);
1222 assert(metacontent != (unsigned char *) NULL);
1223 if (length > (MagickSizeType) SSIZE_MAX)
1227 (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1228 p+=sizeof(server_info->session_key);
1229 (void) memcpy(p,®ion->width,sizeof(region->width));
1230 p+=sizeof(region->width);
1231 (void) memcpy(p,®ion->height,sizeof(region->height));
1232 p+=sizeof(region->height);
1233 (void) memcpy(p,®ion->x,sizeof(region->x));
1234 p+=sizeof(region->x);
1235 (void) memcpy(p,®ion->y,sizeof(region->y));
1236 p+=sizeof(region->y);
1237 (void) memcpy(p,&length,sizeof(length));
1239 count=dpc_send(server_info->file,p-message,message);
1240 if (count != (MagickOffsetType) (p-message))
1242 return(dpc_read(server_info->file,length,metacontent));
1246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1250 + 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 %
1254 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1256 % ReadDistributePixelCachePixels() reads pixels from the specified region of
1257 % the distributed pixel cache.
1259 % The format of the ReadDistributePixelCachePixels method is:
1261 % MagickOffsetType ReadDistributePixelCachePixels(
1262 % DistributeCacheInfo *server_info,const RectangleInfo *region,
1263 % const MagickSizeType length,unsigned char *pixels)
1265 % A description of each parameter follows:
1267 % o server_info: the distributed cache info.
1269 % o image: the image.
1271 % o region: read the pixels from this region of the image.
1273 % o length: the length in bytes of the pixels.
1275 % o pixels: read these pixels from the pixel cache.
1278 MagickPrivate MagickOffsetType ReadDistributePixelCachePixels(
1279 DistributeCacheInfo *server_info,const RectangleInfo *region,
1280 const MagickSizeType length,unsigned char *pixels)
1285 register unsigned char
1289 message[MaxTextExtent];
1292 Read distributed pixel cache pixels.
1294 assert(server_info != (DistributeCacheInfo *) NULL);
1295 assert(server_info->signature == MagickSignature);
1296 assert(region != (RectangleInfo *) NULL);
1297 assert(pixels != (unsigned char *) NULL);
1298 if (length > (MagickSizeType) SSIZE_MAX)
1302 (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1303 p+=sizeof(server_info->session_key);
1304 (void) memcpy(p,®ion->width,sizeof(region->width));
1305 p+=sizeof(region->width);
1306 (void) memcpy(p,®ion->height,sizeof(region->height));
1307 p+=sizeof(region->height);
1308 (void) memcpy(p,®ion->x,sizeof(region->x));
1309 p+=sizeof(region->x);
1310 (void) memcpy(p,®ion->y,sizeof(region->y));
1311 p+=sizeof(region->y);
1312 (void) memcpy(p,&length,sizeof(length));
1314 count=dpc_send(server_info->file,p-message,message);
1315 if (count != (MagickOffsetType) (p-message))
1317 return(dpc_read(server_info->file,length,pixels));
1321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1325 + 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 %
1329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1331 % RelinquishDistributePixelCache() frees resources acquired with
1332 % OpenDistributePixelCache().
1334 % The format of the RelinquishDistributePixelCache method is:
1336 % MagickBooleanType RelinquishDistributePixelCache(
1337 % DistributeCacheInfo *server_info)
1339 % A description of each parameter follows:
1341 % o server_info: the distributed cache info.
1344 MagickPrivate MagickBooleanType RelinquishDistributePixelCache(
1345 DistributeCacheInfo *server_info)
1353 register unsigned char
1357 message[MaxTextExtent];
1360 Delete distributed pixel cache.
1362 assert(server_info != (DistributeCacheInfo *) NULL);
1363 assert(server_info->signature == MagickSignature);
1366 (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1367 p+=sizeof(server_info->session_key);
1368 count=dpc_send(server_info->file,p-message,message);
1369 if (count != (MagickOffsetType) (p-message))
1370 return(MagickFalse);
1371 count=dpc_read(server_info->file,sizeof(status),(unsigned char *) &status);
1372 if (count != (MagickOffsetType) sizeof(status))
1373 return(MagickFalse);
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1382 + 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 %
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1388 % WriteDistributePixelCacheMetacontents() writes image metacontent to the
1389 % specified region of the distributed pixel cache.
1391 % The format of the WriteDistributePixelCacheMetacontents method is:
1393 % MagickOffsetType WriteDistributePixelCacheMetacontents(
1394 % DistributeCacheInfo *server_info,const RectangleInfo *region,
1395 % const MagickSizeType length,const unsigned char *metacontent)
1397 % A description of each parameter follows:
1399 % o server_info: the distributed cache info.
1401 % o image: the image.
1403 % o region: write the metacontent to this region of the image.
1405 % o length: the length in bytes of the metacontent.
1407 % o metacontent: write these metacontent to the pixel cache.
1410 MagickPrivate MagickOffsetType WriteDistributePixelCacheMetacontent(
1411 DistributeCacheInfo *server_info,const RectangleInfo *region,
1412 const MagickSizeType length,const unsigned char *metacontent)
1417 register unsigned char
1421 message[MaxTextExtent];
1424 Write distributed pixel cache metacontent.
1426 assert(server_info != (DistributeCacheInfo *) NULL);
1427 assert(server_info->signature == MagickSignature);
1428 assert(region != (RectangleInfo *) NULL);
1429 assert(metacontent != (unsigned char *) NULL);
1430 if (length > (MagickSizeType) SSIZE_MAX)
1434 (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1435 p+=sizeof(server_info->session_key);
1436 (void) memcpy(p,®ion->width,sizeof(region->width));
1437 p+=sizeof(region->width);
1438 (void) memcpy(p,®ion->height,sizeof(region->height));
1439 p+=sizeof(region->height);
1440 (void) memcpy(p,®ion->x,sizeof(region->x));
1441 p+=sizeof(region->x);
1442 (void) memcpy(p,®ion->y,sizeof(region->y));
1443 p+=sizeof(region->y);
1444 (void) memcpy(p,&length,sizeof(length));
1446 count=dpc_send(server_info->file,p-message,message);
1447 if (count != (MagickOffsetType) (p-message))
1449 return(dpc_send(server_info->file,length,metacontent));
1453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1457 + 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 %
1461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1463 % WriteDistributePixelCachePixels() writes image pixels to the specified
1464 % region of the distributed pixel cache.
1466 % The format of the WriteDistributePixelCachePixels method is:
1468 % MagickBooleanType WriteDistributePixelCachePixels(
1469 % DistributeCacheInfo *server_info,const RectangleInfo *region,
1470 % const MagickSizeType length,const unsigned char *pixels)
1472 % A description of each parameter follows:
1474 % o server_info: the distributed cache info.
1476 % o image: the image.
1478 % o region: write the pixels to this region of the image.
1480 % o length: the length in bytes of the pixels.
1482 % o pixels: write these pixels to the pixel cache.
1485 MagickPrivate MagickOffsetType WriteDistributePixelCachePixels(
1486 DistributeCacheInfo *server_info,const RectangleInfo *region,
1487 const MagickSizeType length,const unsigned char *pixels)
1492 register unsigned char
1496 message[MaxTextExtent];
1499 Write distributed pixel cache pixels.
1501 assert(server_info != (DistributeCacheInfo *) NULL);
1502 assert(server_info->signature == MagickSignature);
1503 assert(region != (RectangleInfo *) NULL);
1504 assert(pixels != (const unsigned char *) NULL);
1505 if (length > (MagickSizeType) SSIZE_MAX)
1509 (void) memcpy(p,&server_info->session_key,sizeof(server_info->session_key));
1510 p+=sizeof(server_info->session_key);
1511 (void) memcpy(p,®ion->width,sizeof(region->width));
1512 p+=sizeof(region->width);
1513 (void) memcpy(p,®ion->height,sizeof(region->height));
1514 p+=sizeof(region->height);
1515 (void) memcpy(p,®ion->x,sizeof(region->x));
1516 p+=sizeof(region->x);
1517 (void) memcpy(p,®ion->y,sizeof(region->y));
1518 p+=sizeof(region->y);
1519 (void) memcpy(p,&length,sizeof(length));
1521 count=dpc_send(server_info->file,p-message,message);
1522 if (count != (MagickOffsetType) (p-message))
1524 return(dpc_send(server_info->file,length,pixels));