TweetFollow Us on Twitter

Distribute Processing
Volume Number:8
Issue Number:2
Column Tag:Jörg's Folder

Related Info: Apple Event Mgr

A Way to Distribute Processing

With today’s Apple Event technology, you could set up a distributed processor

By Jörg Langowski, MacTutor Regular Contributing Author

Note: Source code files accompanying article are located on MacTech CD-ROM or source code disks.

The example that I presented last month showed a very simple way to communicate between Fortran programs using the built-in Apple event handling of the Language Systems (LS) Fortran runtime system. Although the F_SendEvent routine can be used to send high-level events of class 'aevt' in a very simple way, its possibilities are limited. The event class can not be changed, and therefore you cannot declare your own suite of events with a new class identifier. Also, the only kind of parameter that you can add to an Apple event that you send with F_SendEvent is a filename, which identifies a file to be associated with the Apple event. This makes sense when you want to send an 'odoc' or 'pdoc' event, but the file remains the only way to send data with the Apple event.

On the other hand, the LS Fortran runtime system has Apple event handling built in, so you don’t have to take care to make the main event loop System 7-aware. This is the great advantage of LS Fortran when you are porting programs from other machines, and nevertheless want to add easily some System 7 goodies. Exchange of arbitrary types of data between programs on the same machine or over a network is certainly one of the more important features of System 7. So this time, I’ll show you how to create and send an arbitrary Apple event in LS Fortran. The event can contain data that will be processed by the receiving program and sent back when it’s done.

A math library server

When you look at a large computer installation with many workstations and mainframes coupled together through a network, you can’t avoid the impression that most of the time the workstations - at least when its users are engaged in non-productive activities such as sleeping, eating, editing programs or writing manuscripts - are doing nothing but sit there and wait for something to happen. The idle CPU time that accumulates in a place where tens or even hundreds of MacII - class machines are kept must be enormous. Why couldn’t that idle time be used by other machines to do CPU-intensive things like operations on large matrices? Even if any single node on the network is not any more powerful than the machine that needs the extra CPU power, one could split up operations into blocks that would be executed by different idle nodes on the network.

You can imagine the implications of such a system: for instance, a small process turning in the background of each MacII on the network and implementing, for example, the Linpack math library. The process would accept requests from other nodes for calculating matrix operations, do the calculations only during the time when really nothing else is happening (the user at the node should always have priority), and send the result back to the requesting node.

The CPU-intensive program that is executing on one node is responsible for splitting its operations into pieces that can be executed independently, sending out the requests for calculations, and reassembling the answers into one final result.

In this column, I won’t show you all the pieces that are necessary for building such a system - this would much exceed the scope of this column, and anyway, if I had everything done perfectly, I’d sell it and make lots of money. But we can look at a very simple example: a “server process” that accepts an array of real numbers, squares every element and sends back the resulting array, and a program that requests this service.

Setting up an Apple Event and sending it

We’ll define the Apple event first in which we are going to encapsulate our data. Let’s give it the class 'JLMT', and ID 'MULT' (why not). Certain data is always associated with any apple event: a target address descriptor that specifies where the event is going to be sent, a return ID that can be used by a program that sent several Apple events to find out who sent the reply, and a transaction ID. In addition, if you want to send data with the event, you have to add descriptors to it that specify that data.

In the example given below the setting up and sending of the event are done by the routine send_array. First of all, PPCBrowser is called to select the process to which we send the event (this is the routine that displays the dialog “Please select a program to link to:”). The information returned by PPCBrowser is then put into a target address descriptor which we created with AECreateDesc. We need this target address descriptor for creating the actual Apple event with AECreateAppleEvent. The other parameters after the event class, ID, and the target address are the return ID (here we specify that a unique return ID is generated automatically) and a transaction ID (the parameter kAnyTransactionID means that the event does not belong to any particular group of events which form a separate transaction).

After the Apple event is created, we can add data to it. We’ll add three parameters: the x and y dimensions of a 2-dimensional array, both 32-bit integers (type 'long'), and the array data itself, which is sent as an unformatted string of bytes (type 'text'). The array is actually of type real*4, but since only the length (in bytes) and pointer to the first element are required, we can use the 'text' type in the Apple event. The three parameters will also get names ('XDIM','YDIM' and 'ARRY') to identify them uniquely.

Having set up the Apple event, we can then send it to the process which does the calculation. On sending (with the AESend routine) we specify the address of the Apple event that we want to send (normal), a pointer to a reply Apple event structure where the reply will be received, and a parameter that specifies that we wish a reply that will be received through the normal event queue (kAEQueueReply), and that the receiving program doesn’t need to notify the user that the event was received (kAENeverInteract). We might also have specified kAEWaitReply here, in that case, the sending program would idle and yield the CPU to other programs on the same machine until the reply was received. Since we are planning to send out several Apple events to different processes for parallel processing without having to wait for a reply each time, we use the queue reply mode.

After sending the Apple event, send_array returns to the Fortran runtime system.

The server process

The Apple event is received by the second program in the example. The main program simply installs a handler for the JLMT / MULT Apple event (the routine get_array) and then drops into an idle loop. The Fortran output window is never activated.

The event handler will receive the Apple event and a pointer to a reply event. It extracts the data out of the received event, processed it and puts it into the reply, which is automagically sent back by the Apple event manager. Extracting parameters is done by the AEGetParamPtr routine, where you have to specify the address of the Apple event (of course), and the identifier and type of the parameter to extracted. You also have to provide a pointer to a space where the data can be stored. The routine returns the parameter, or an error if such a parameter does not exist.

Thus, we extract the x and y dimensions of the array into two 32 bit integers, calculate the array size, and extract the array data into a real*4 array. We then call the process_array routine, which squares every element and divides it by 10000, and then put the three parameters XDIM, YDIM and ARRY into the reply Apple event record using the routine AEPutParamPtr which is very analogous to AEGetParamPtr. When our event handler returns, the Apple event manager will send the reply Apple event back to the program that sent the original event.

Receiving the result

The reply event is of class 'aevt', ID 'ansr'. Since the main program called AESend with the kAEQueueReply parameter, we will receive the reply through the normal event loop. Therefore, we must install our own handler for a reply event, in our case the routine get_reply. In this routine we extract the XDIM, YDIM and ARRY parameters out of the reply, write a message to the output window that the reply was received and return. The processed array data can then be displayed with the show_array routine which is selected from a menu.

[I have forgotten to mention the array setup routine, also selected from a menu, which puts the initial data into the array, and the menu selection send array, which calls the send_array routine. You’ll already have discovered them.]

What is missing?

Of course, this example is far from the idea of the math routine server that I mentioned initially. Several things would have to be added to make distributed processing really work: First of all, all replies will have the same class and ID ('aevt'/'ansr'), and since you might have sent out several requests for calculations, you have to remember the question when you are getting an answer. This is what the return ID is good for -- by keeping a table of pending requests and their return IDs, a reply can be easily identified. Implementation is left as an exercise for the reader, as is the automatic identification and selection of available server processes on a large Appletalk internetwork. Error handling, too, is very rudimentary in the example; in practice, the program would have to be stable against wrong parameters, values out of range, missing parameters, and send back error messages with some meaningful content.

Anyway, I hope this example has given you an impression about the many things that can be done with Apple events. See you next month with more interesting things from the Fortran side and elsewhere.

Example: Distributed processing with Apple Events in LS Fortran

!!M Inlines.f
!!G AEvent.finc
c
c
 program AEMenu

 implicit none
 
 external get_reply,send_array
 integer*2 err
 
 err = AEInstallEventHandler  
(%val('aevt'),%val('ansr'),%val(%loc(get_reply)), %val(int4(0)),%val(int2(0))) 

 
 if (err. ne. 0) then
 type *,'Error installing Apple event, result code = ',err
 end if

 call AddMenuItem ('AE menu', 'setup array', setup_array)
 call AddMenuItem ('AE menu', 'send array', send_array)
 call AddMenuItem ('AE menu', 'show array', show_array)
 
 end
 
 subroutine setup_array
 implicit none
 
 real*4 myarray(10000)
 integer xdim,ydim
 global xdim,ydim,myarray
 
 xdim = 10
 ydim = 15
 call setarray(myarray,xdim,ydim)
 
 return
 end

 subroutine setarray(array,xdim,ydim)
 integer xdim,ydim
 real*4 array(xdim,ydim)
 
 do i=1,xdim
 do j=1,ydim
 array(i,j) = 10000.*(i-1) + 1.*(j-1)
 end do
 end do
 
 return
 end

 subroutine show_array
 implicit none
 
 real*4 myarray(10000)
 integer xdim,ydim
 global xdim,ydim,myarray
 
 xdim = 10
 ydim = 15
 call display(myarray,xdim,ydim)
 
 return
 end

 subroutine display(array,xdim,ydim)
 integer xdim,ydim
 real*4 array(xdim,ydim)
 
 write (*,'(1x,10(1xf7.0))') ((array(i,j),i=1,xdim),j=1,ydim)
 
 return
 end

 subroutine send_array
 implicit none
 real*4 myarray(10000)
 integer xdim,ydim
 global xdim,ydim,myarray
 
 integer totalsize
 
 integer*2 err
 record /AppleEvent/ theAppleEvent,reply
 record /targetID/ target
 record /LocationNameRec/ myLocation
 record /PortInfoRec/ myPortInfo
 record /AEAddressDesc/ targetAddress
 
 err = PPCBrowser(%val(int4(0)),%val(int4(0)),
 1 %val(int2(0)),%ref(myLocation),
 2 %ref(myPortInfo),%val(int4(0)),%val(int4(0)))
 if (err .ne. 0) then
 type *,'PPC Browser: error ',err
 return
 end if
 
 target.location = myLocation
 target.name = myPortInfo.name
 
 type *,'Session ID = ',target.sessionid,
 1 ', target name = ',target.name.name
 
 err = AECreateDesc(%val(typeTargetID),
 1 %val(%loc(target)),%val(sizeof(target)),
 2 %ref(targetAddress))
 if (err .ne. 0) then
 type *,'AECreateDesc: error ',err
 return
 end if 
 
 err=AECreateAppleEvent(%val('JLMT'),%val('MULT'),
 1 %ref(targetAddress),
 2 %val(kAutoGenerateReturnID),
 3 %val(int4(kAnyTransactionID)),
 4 %ref(theAppleEvent))
 if (err .ne. 0) then
 type *,'AECreateAppleEvent: error ',err
 return
 end if 
 
 err = AEPutParamPtr(%ref(theAppleEvent),
 1 %val('XDIM'),%val(typeInteger),
 2 %val(%loc(xdim)),%val(sizeof(xdim)))
 if (err .ne. 0) then
 type *,'AEPutParamPtr: error ',err
 return
 end if 
 
 err = AEPutParamPtr(%ref(theAppleEvent),
 1 %val('YDIM'),%val(typeInteger),
 2 %val(%loc(ydim)),%val(sizeof(ydim)))
 if (err .ne. 0) then
 type *,'AEPutParamPtr: error ',err
 return
 end if 
 
 totalsize = xdim * ydim * 4
 
 err = AEPutParamPtr(%ref(theAppleEvent),
 1 %val('ARRY'),%val(typeChar),
 2 %val(%loc(myarray)),%val(totalsize))
 if (err .ne. 0) then
 type *,'AEPutParamPtr: error ',err
 return
 end if 
 
 err = AESend(%ref(theAppleEvent),%ref(reply),
 1 %val(int4(kAEQueueReply+kAENeverInteract)),
 2 %val(kAENormalPriority),%val(int4(120)),
 3 %val(int4(0)),%val(int4(0)) )
 if (err .ne. 0) then
 type *,'AESend: error ',err
 return
 end if 
 
 type *,'Sent test array of size ',xdim*ydim

 return
 end

 integer*2 function get_reply(theAppleEvent,reply,
 1 %val(handlerRefCon))

 record /AppleEvent/ theAppleEvent
 record /AppleEvent/ reply
 integer*4 handlerRefCon
 
 real*4 myarray(10000)
 integer xdim,ydim
 global xdim,ydim,myarray
 
 integer totalsize
 
 err = AEGetParamPtr(%ref(theAppleEvent),
 1 %val('XDIM'),%val(typeInteger),returnedType,
 2 %val(%loc(xdim)),%val(sizeof(xdim)),actualSize)
 if (err .ne. 0) then
 type *,'AEGetParamPtr: error ',err
 goto 9999
 end if 
 
 err = AEGetParamPtr(%ref(theAppleEvent),
 1 %val('YDIM'),%val(typeInteger),returnedType,
 2 %val(%loc(ydim)),%val(sizeof(ydim)),actualSize)
 if (err .ne. 0) then
 type *,'AEGetParamPtr: error ',err
 goto 9999
 end if 
 
 totalsize = xdim * ydim * 4
 
 err = AEGetParamPtr(%ref(theAppleEvent),
 1 %val('ARRY'),%val(typeChar),returnedType,
 2 %val(%loc(myarray)),%val(totalsize),actualSize)
 if (err .ne. 0) then
 type *,'AEGetParamPtr: error ',err
 goto 9999
 end if 
 
 type *,'Reply received from server'
 
 get_reply = 0 ! noErr
 return

9999  get_reply = err
 return
 end



!!M Inlines.f
!!G AEvent.finc
c
c
 program Array_process

 implicit none
 
 external get_array
 integer*2 err
 
 err = AEInstallEventHandler(%val('JLMT'),%val('MULT'),
 1    %val(%loc(get_array)),%val(int4(0)),%val(int2(0))) 
 if (err. ne. 0) call alertbox
 1 ('Array_process: Apple Event install error')

 do while (.true.)
 call F_DoBackground
 end do
 
 end

 integer*2 function get_array(theAppleEvent,reply,
 1 %val(handlerRefCon))
 implicit none
 
 record /AppleEvent/ theAppleEvent
 record /AppleEvent/ reply
 integer*4 handlerRefCon
 
 integer*2 err
 integer*4 keywd,returnedType,actualSize
 
 real*4 myarray(10000)

 integer xdim,ydim
 global xdim,ydim,myarray
 
 integer totalsize
 
 err = AEGetParamPtr(%ref(theAppleEvent),
 1 %val('XDIM'),%val(typeInteger),returnedType,
 2 %val(%loc(xdim)),%val(sizeof(xdim)),actualSize)
 if (err .ne. 0) goto 9999
 
 err = AEGetParamPtr(%ref(theAppleEvent),
 1 %val('YDIM'),%val(typeInteger),returnedType,
 2 %val(%loc(ydim)),%val(sizeof(ydim)),actualSize)
 if (err .ne. 0) goto 9999
 
 totalsize = xdim * ydim * 4
 
 err = AEGetParamPtr(%ref(theAppleEvent),
 1 %val('ARRY'),%val(typeChar),returnedType,
 2 %val(%loc(myarray)),%val(totalsize),actualSize)
 if (err .ne. 0) goto 9999

cwe don't check whether actualSize = totalsize 
cand returnedType = typeChar.
c
cIn an actual application, such errors 
chave to be trapped, of course.
c
 call process_array(myarray,xdim,ydim)
 
 err = AEPutParamPtr(%ref(reply),%val('XDIM'),
 1 %val(typeInteger),%val(%loc(xdim)),
 2 %val(sizeof(xdim)))
 if (err .ne. 0) goto 9999
 
 err = AEPutParamPtr(%ref(reply),%val('YDIM'),
 1 %val(typeInteger),%val(%loc(ydim)),
 2 %val(sizeof(ydim)))
 if (err .ne. 0) goto 9999
 
 err = AEPutParamPtr(%ref(reply),%val('ARRY'),
 1 %val(typeChar),%val(%loc(myarray)),
 2 %val(totalsize))
 if (err .ne. 0) goto 9999
 
 get_array = 0 ! noErr
 return

9999  get_array = err
 return
 
 end


 subroutine process_array(array,xdim,ydim)
 integer xdim,ydim
 real*4 array(xdim,ydim)
 
 do i=1,xdim
 do j=1,ydim
 array(i,j) = array(i,j)*array(i,j)/10000.
 end do
 end do
 
 return
 end
 

Community Search:
MacTech Search:

Software Updates via MacUpdate

LaunchBar 6.18.5 - Powerful file/URL/ema...
LaunchBar is an award-winning productivity utility that offers an amazingly intuitive and efficient way to search and access any kind of information stored on your computer or on the Web. It provides... Read more
Affinity Designer 2.3.0 - Vector graphic...
Affinity Designer is an incredibly accurate vector illustrator that feels fast and at home in the hands of creative professionals. It intuitively combines rock solid and crisp vector art with... Read more
Affinity Photo 2.3.0 - Digital editing f...
Affinity Photo - redefines the boundaries for professional photo editing software for the Mac. With a meticulous focus on workflow it offers sophisticated tools for enhancing, editing and retouching... Read more
WhatsApp 23.24.78 - Desktop client for W...
WhatsApp is the desktop client for WhatsApp Messenger, a cross-platform mobile messaging app which allows you to exchange messages without having to pay for SMS. WhatsApp Messenger is available for... Read more
Adobe Photoshop 25.2 - Professional imag...
You can download Adobe Photoshop as a part of Creative Cloud for only $54.99/month Adobe Photoshop is a recognized classic of photo-enhancing software. It offers a broad spectrum of tools that can... Read more
PDFKey Pro 4.5.1 - Edit and print passwo...
PDFKey Pro can unlock PDF documents protected for printing and copying when you've forgotten your password. It can now also protect your PDF files with a password to prevent unauthorized access and/... Read more
Skype 8.109.0.209 - Voice-over-internet...
Skype is a telecommunications app that provides HD video calls, instant messaging, calling to any phone number or landline, and Skype for Business for productive cooperation on the projects. This... Read more
OnyX 4.5.3 - Maintenance and optimizatio...
OnyX is a multifunction utility that you can use to verify the startup disk and the structure of its system files, to run miscellaneous maintenance and cleaning tasks, to configure parameters in the... Read more
CrossOver 23.7.0 - Run Windows apps on y...
CrossOver can get your Windows productivity applications and PC games up and running on your Mac quickly and easily. CrossOver runs the Windows software that you need on Mac at home, in the office,... Read more
Tower 10.2.1 - Version control with Git...
Tower is a Git client for OS X that makes using Git easy and more efficient. Users benefit from its elegant and comprehensive interface and a feature set that lets them enjoy the full power of Git.... Read more

Latest Forum Discussions

See All

Pour One Out for Black Friday – The Touc...
After taking Thanksgiving week off we’re back with another action-packed episode of The TouchArcade Show! Well, maybe not quite action-packed, but certainly discussion-packed! The topics might sound familiar to you: The new Steam Deck OLED, the... | Read more »
TouchArcade Game of the Week: ‘Hitman: B...
Nowadays, with where I’m at in my life with a family and plenty of responsibilities outside of gaming, I kind of appreciate the smaller-scale mobile games a bit more since more of my “serious" gaming is now done on a Steam Deck or Nintendo Switch.... | Read more »
SwitchArcade Round-Up: ‘Batman: Arkham T...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for December 1st, 2023. We’ve got a lot of big games hitting today, new DLC For Samba de Amigo, and this is probably going to be the last day this year with so many heavy hitters. I... | Read more »
Steam Deck Weekly: Tales of Arise Beyond...
Last week, there was a ton of Steam Deck coverage over here focused on the Steam Deck OLED. | Read more »
World of Tanks Blitz adds celebrity amba...
Wargaming is celebrating the season within World of Tanks Blitz with a new celebrity ambassador joining this year's Holiday Ops. In particular, British footballer and movie star Vinnie Jones will be brightening up the game with plenty of themed in-... | Read more »
KartRider Drift secures collaboration wi...
Nexon and Nitro Studios have kicked off the fifth Season of their platform racer, KartRider Dift, in quite a big way. As well as a bevvy of new tracks to take your skills to, and the new racing pass with its rewards, KartRider has also teamed up... | Read more »
‘SaGa Emerald Beyond’ From Square Enix G...
One of my most-anticipated releases of 2024 is Square Enix’s brand-new SaGa game which was announced during a Nintendo Direct. SaGa Emerald Beyond will launch next year for iOS, Android, Switch, Steam, PS5, and PS4 featuring 17 worlds that can be... | Read more »
Apple Arcade Weekly Round-Up: Updates fo...
This week, there is no new release for Apple Arcade, but many notable games have gotten updates ahead of next week’s holiday set of games. If you haven’t followed it, we are getting a brand-new 3D Sonic game exclusive to Apple Arcade on December... | Read more »
New ‘Honkai Star Rail’ Version 1.5 Phase...
The major Honkai Star Rail’s 1.5 update “The Crepuscule Zone" recently released on all platforms bringing in the Fyxestroll Garden new location in the Xianzhou Luofu which features many paranormal cases, players forming a ghost-hunting squad,... | Read more »
SwitchArcade Round-Up: ‘Arcadian Atlas’,...
Hello gentle readers, and welcome to the SwitchArcade Round-Up for November 30th, 2023. It’s Thursday, and unlike last Thursday this is a regular-sized big-pants release day. If you like video games, and I have to believe you do, you’ll want to... | Read more »

Price Scanner via MacPrices.net

Deal Alert! Apple Smart Folio Keyboard for iP...
Apple iPad Smart Keyboard Folio prices are on Holiday sale for only $79 at Amazon, or 50% off MSRP: – iPad Smart Folio Keyboard for iPad (7th-9th gen)/iPad Air (3rd gen): $79 $79 (50%) off MSRP This... Read more
Apple Watch Series 9 models are now on Holida...
Walmart has Apple Watch Series 9 models now on Holiday sale for $70 off MSRP on their online store. Sale prices available for online orders only, in-store prices may vary. Order online, and choose... Read more
Holiday sale this weekend at Xfinity Mobile:...
Switch to Xfinity Mobile (Mobile Virtual Network Operator..using Verizon’s network) and save $500 instantly on any iPhone 15, 14, or 13 and up to $800 off with eligible trade-in. The total is applied... Read more
13-inch M2 MacBook Airs with 512GB of storage...
Best Buy has the 13″ M2 MacBook Air with 512GB of storage on Holiday sale this weekend for $220 off MSRP on their online store. Sale price is $1179. Price valid for online orders only, in-store price... Read more
B&H Photo has Apple’s 14-inch M3/M3 Pro/M...
B&H Photo has new Gray and Black 14″ M3, M3 Pro, and M3 Max MacBook Pros on Holiday sale this weekend for $100-$200 off MSRP, starting at only $1499. B&H offers free 1-2 day delivery to most... Read more
15-inch M2 MacBook Airs are $200 off MSRP on...
Best Buy has Apple 15″ MacBook Airs with M2 CPUs in stock and on Holiday sale for $200 off MSRP on their online store. Their prices are among the lowest currently available for new 15″ M2 MacBook... Read more
Get a 9th-generation Apple iPad for only $249...
Walmart has Apple’s 9th generation 10.2″ iPads on sale for $80 off MSRP on their online store as part of their Cyber Week Holiday sale, only $249. Their prices are the lowest new prices available for... Read more
Space Gray Apple AirPods Max headphones are o...
Amazon has Apple AirPods Max headphones in stock and on Holiday sale for $100 off MSRP. The sale price is valid for Space Gray at the time of this post. Shipping is free: – AirPods Max (Space Gray... Read more
Apple AirTags 4-Pack back on Holiday sale for...
Amazon has Apple AirTags 4 Pack back on Holiday sale for $79.99 including free shipping. That’s 19% ($20) off Apple’s MSRP. Their price is the lowest available for 4 Pack AirTags from any of the... Read more
New Holiday promo at Verizon: Buy one set of...
Looking for more than one set of Apple AirPods this Holiday shopping season? Verizon has a great deal for you. From today through December 31st, buy one set of AirPods on Verizon’s online store, and... Read more

Jobs Board

Senior Software Engineer - *Apple* Fundamen...
…center of Microsoft's efforts to empower our users to do more. The Apple Fundamentals team focused on defining and improving the end-to-end developer experience in Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Housekeeper, *Apple* Valley Villa - Cassia...
Apple Valley Villa, part of a senior living community, is hiring entry-level Full-Time Housekeepers to join our team! We will train you for this position and offer a Read more
Senior Manager, Product Management - *Apple*...
…Responsibilities** We are seeking an ambitious, data-driven thinker to assist the Apple Product Development team as our Wireless Product division continues to grow Read more
Mobile Platform Engineer ( *Apple* /AirWatch)...
…systems, installing and maintaining certificates, navigating multiple network segments and Apple /IOS devices, Mobile Device Management systems such as AirWatch, and Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.