Skip to content

Something simple – like a socket set, maybe

Understand from the start that I have forgotten more about TCP port programming than many people ever get to know. I’m not joking, either; I don’t remember large APIs at all well, and have always relied on decent documentation to keep it all together. Liz and I sometimes remark that my modus operandi is that I am extremely good at deducing things, but awful at remembering anything.

In the early 90s (probably ’93), I wrote the core communication mechanism of a Reuters feed distribution system, using a copy of Stevens (probably Advanced Unix Programming, I don’t remember). Since then I have written a few similar but smaller port handlers, but nothing recently.

So, I approach a project to make a handler for the management interface of Asterisk (a very good portable phone switchboard application). I imagine that I can just look up a couple of classes in the Cocoa docs, and get started. I’m sorry, it doesn’t work that way. Check out the documentation for NSPort, which leads you to NSSocketPort; they sound like generic port handlers – but they aren’t. Spotlight on NSSocketPort only finds discussions on the Omni macosx-dev list, which is ominous; it turns out that these two classes are poorly named low level components for Distributed Objects – which is great, but not what I want.

At the moment, it looks like I am forced to choose between using CFSocket, or the basic BSD socket() call, with an NSFileHandle to manage it. I have also found a link to a project called smallsockets; this is a wrapper on BSD sockets. CFSocket is intended to push socket requests into the Run Loop for processing, which avoids the need to write multi-threaded code. If you are interesting in pursuing this course, look in the documentation for CFNetwork to get the overview.

CFSocket is in 10.0, but luckily it has a Cocoa wrapper class, NSStream (although actually it is a wrapper for CFStream), which is however only in 10.3 and above. So that looks like the one to use.

The Programming Topic in the documentation on Streams covers the basic code set up very well, and a test app mostly copied from there works very well as a pseudo telnet client. If you want to write a server, you need to wait for connections, which the NSStreams won’t do, so you have to directly program CFSocket for that. There’s a decent example here (you’ll will have to scroll down to “Source Code” to find the files).

Example code to come, once I write it.

Tagged

Amazon lists

I have created two Amazon booklists, one for WebObjects, the other for Cocoa.

They should show up if you do a search on Amazon (UK only, I think), for “Cocoa Programming”, or “WebObjects”. I don’t get any referral fees from these lists, so they are a labour of love, and I intend to keep them updated should any new books be released on either topic. On most classes I carry in most of the books on the list, and talk a little about them in the wrap up session; for the last couple of classes, since I made the lists, I am also mentioning the lists, and showing them on the screen.

I may make a similar list for classic Unix books, my main problem with that is finding them all in the garage.

Tagged , , ,

Mary Berry's Ultimate Cake Book

I bought this book a few weeks ago because it seemed to be close enough to being an important reference book for English cake baking. On that basis, it comes close, but doesn’t win. As a large compendium of English baking, it is very good.

Since then, I have baked my way through about a quarter of the recipes, which is very good going. Most of the results have been close to perfect: I can especially recommend her Bara Brith recipe, which is utterly delicious, and her carrot cake and rich brownies are likewise almost perfect. The pictures are luxurious and tempting, and cover the majority of recipes; they are also very representative of what the recipes actually will produce, and aren’t overembellished.

I have a few criticisms. Cooking times aren’t always precise, and I find that I have to check very carefully to make sure that the cakes are really ready. Additional sugar is a very popular topping on rock cakes, sponges and other types, which I find excessive. Mary Berry also invariably specifies margarine instead of butter, except for one or two recipes where the taste of margarine would be truly obnoxious. The chocolate chip cookies were the only recipe so far to disappoint, with the result being more like a sponge than a cookie. The only sponge technique used is all-in-one, even for the Victoria sponge, and examples of other traditional sponge techniques would be appreciated. Most of the cheesecake recipes relied on gelatine rather than baking, which I felt was inapproriate for a book with a full chapter on cheesecakes.

The chapter division and sequence I found rather puzzling. The first chapters are on chocolate cakes, meringues, “traybakes”, and celebration cakes. Most books would have saved three of these more difficult chapters for the end of the sequence. Then there are separate chapters on celebration cakes, Bazaars, gift cakes, continental cakes and then family cakes; although this is feasible, I find it hard to decide in which of these chapters some recipes should really belong.

Back to the good; the beginning chapters on tools, ingredients and techniques are brief but excellent, and the short chapter at the end on problem solving is also very good.

If you want an easy to follow cake book with many British traditional recipes, as well as a few surprises, where 90% or more will give excellent results – then this is a book for you. It isn’t complete, and doesn’t include many representative international recipes – but that may not be a fault for you.

Tagged ,

Books about Bread

I promised to give my suggestions on books about break making. Down below I list a few books that are absolutely first rate, with every aspect showing a deep understanding. Further down I add a few extra books that have helped me along the way, although they may not be as comprehensive as the first part of the list.

Edward Espe Brown, the Tassajara Bread Book. Shambhala Publications, 1970, 157062089X

This was the first book that helped me to bake wholemeal bread. Reading it again today, the instructions on proving and kneading are spot on. The general section on breads is relatively brief, as he includes desserts, muffins and pancakes in the book. Incidentally, Tassajara Fruit Cake is by parts terrifying, awesome, and the iconic cake of the era for me.

Elizabeth David, British Bread and Yeast Cookery. Penguin, 1977, 0140299742

Largely of academic interest, and I don’t mean that slightingly. There is an absolute dearth of information about the documented history of bread cookery, and this is all that most of us will have access to. The recipes aren’t given to be baked from, but as background information about the history of bread.

Joe Ortiz, The Village Baker. Ten Speed Press, 1993, 0-89815-916-4

A very detailed account of the bread process, with emphasis on the modern French style. A third of the book consists of “professional” (scaled up and suitable for an industrial mixer) versions of the recipes given in the first section, which is a little odd. His techniques include poolish, old dough, etc.

Laurel Robertson with Carol Flinders and Bronwen Godfrey, The Laurel’s Kitchen Bread Book. Random House, 1984, 0-394-72434-8

Strong advocates of the Flemish desem technique for making wholemeal bread with a chef starter. It also includes the lengthy section called “A Loaf for Learning”, which should really be published as a separate spiral bound and laminated pamphlet for reference in the kitchen. The Loaf for Learning is a detailed technique for making a single loaf of wholemeal bread with commercial yeast, which is as close to foolproof as you will ever find. This loaf is one of my favourites, and proved to me that a simple 100% wholemeal loaf can have as fully developed and bread like texture as any white bread.

Nancy Silverton’s Breads from the La Brea Bakery. Villard Books, 1996, 0-679-40907-6

Sound techniques for making naturally leavened artisan bread. Nancy gives an emphasis on the use of retarding doughs and a lengthy time span for baking. She mostly uses the French chef form of starter.

Daniel Wing and Alan Scott, The Bread Builders: Hearth Loaves and Masonry Ovens. Chelsea Green Publishing Company, 1999, 189013205-5

Half the book is on building masonry ovens, but don’t be fooled; this book has the most detailed and accurate account of the process of baking bread given anywhere. This book is authoritative in areas where other books start to skimp.

And Also

Eric Treuille and Ursula Ferrigno, Bread. Dorling Kindersley, 1998, 0-7513-0607-X

This book includes reasonable recipes for most of the classic European breads; the only flaw is a tendancy to insert commercial yeast into all their supposedly naturally leavened recipes. But a good starter on European techniques with lots of pictures.

George and Cecilia Scurfield, Home Baked. Faber, 1956, 0-571-09844-4

For me, this is quite a classic. My mother had read the two books by these authors, and they gave the basic process for hand baking bread that I followed for my first efforts.

Ed Wood, Classic Sourdoughs A Home Baker’s Handbook. Ten Speed Press, 2001, 1-58008-344-7

A detailed explanation of natural leavening, with many recipes for making uncomplicated breads. He uses the American batter style of starter for most recipes. Technically very sound, this book isn’t in the first section only because Wing & Scott is so incredibly erudite.

Tagged , ,

Bread Making Tips

First, some background: I think the first time I made bread I was about eight. I can remember doing the shopping for my mother, and buying a ‘bloomer’ from the local bakers in Hayes, or from Parkers in West Ealing; and the special treat of having the top off a cottage loaf. The bread I made had a strange taste, and was underbaked; the result of the available dried yeast (which may well have been brewers yeast), soft flour, and the unpredictable oven temperatures in the early sixties (I now realise).

On holiday with my family in Bulgaria (I must have been about 14), I remember the texture and colour of the local bread; almost yellow, soft and unbleached.

Since then, I made wholemeal bread at university, which turned out like most commercial wholefood wholemeal of the era: dense, small, and somewhat cake like. I didn’t much care for it. My guide at the time was the Tassajara Bread Book, which stands up remarkably well.

After that, on business travels in Italy I was always impressed with the local restaurant bread. Breadsticks, plain rolls, focaccia, and so on. Then later on I came to San Francisco. The delicious sourdough breads of Boudin cafes, and the memorable combination of a sourdough bread bowl and clam chowder.

Which in turn made me want to cook bread like it. At first, I thought that this couldn’t be possible, without commercial scale bakery equipment, the local yeast cultures, and even the local atmosphere. But I was wrong.

The tips

Modern active dried yeast and bread flour mean that anyone can make good quality breads. That was the first tip. The second is: bread machines make it almost trivial to make good bread. However, don’t use the bread machine to bake the bread, just use it as a mixer/kneader; kneading is the hard part of making bread, or rather the judgement required to know when dough is sufficiently kneaded. Use a bread machine to mix, knead and carry out the initial fermentation; then shape, prove and bake the bread yourself.

To get good colour and crispness in the crust, use steam. I start with the oven at full temperature (190C, convection oven), and add about 4 oz of water, just thrown from a mug, on the bottom of the oven. Close, let it come back up to heat, and add the bread. Five minutes later, I put in the same amount of water again; another ten minutes, assuming a 45 minutes bake, I turn down the temperature to the recommended level.

The texture of the crumb depends almost entirely on hydration. A low amount of water will create a dense crumb with small holes, almost like a car sponge, very firm. A high amount of water will create open, irregular shaped holes in the crumb, which is much more attractive. Low hydration would be 60% water; high would be 65%. A word on hydration: 60% means for every 100g flour, 60g of water would be used; not that the dough would contain 60% water. This is bakers definition of hydration. Wetter doughs are much harder to handle.

Kneading is important. Making wholemeal bread, the end result can be very light and bread like, not doughy at all, provided it is given full kneading. Commercial bakers use mixing machines to do this, but they aren’t at all necessary. A minimum of 10 minutes kneading, or 300 turns, for a single 1 lb loaf (which is baked in a 2 lb tin, and contains 450g flour) is about right. The feel of the dough changes significantly about half way through, and a smaller change is noticeable towards the end. For double the quantity, double the kneading time.

There is an alternative technique to continuous kneading – if you let the dough rest for 5 or more minutes, then give a brief burst of kneading, maybe a minute at a time, in 45 minutes the dough will be fully developed. This may help explain some alternative approaches that give good results.

A fermentation stage is said to be complete when the dough no longer springs back in 10 seconds when gently prodded with your finger. We normally let each fermentation proceed to completion, or almost to it. A very brief kneading will allow the microbes access to enough fresh nutrients to ferment again. This is far more important than the amount of time spent rising, or the (very hard to measure accurately) volume increase.

Sourdough

Sourdough is the combination of certain strains of yeast with some lactobacilli. The yeast strains are tolerant of the high acid environment generated by the baceria; bakers yeast is not tolerant. The yeast may come from the air, or may be a contaminant from the bakers skin; the lactobacillus is almost certainly a contaminant from the baker (the same strains can be found in almost anyone’s mouth and gut). The distinctive taste mostly comes from the lactic acid products of the bacteria; but the value of sourdough is in the fact that it ferments a lot slower than commerial yeast, allowing the dough to have more fermentation time. Sourdough bakers often cool their dough to allow longer fermentations. As the yeast and bacteria have different growth rates at different temperatures, a complex equation of different temperatures through the fermentation period will control the final acidity.

The sourdough yeast and bacteria cooperate: the bacteria ferment maltose, which the yeast can’t, and the yeast in turn can ferment the products of the maltose fermention (and I hope I got that bit right). This is advantageous when baking with rye flour, which has a glorious flavour, but very low gluten (protein), and so is almost impossible to rise adequately using commercial yeast. The acid partially gelatinises the starches in rye, allowing rye dough to hold its shape.

I’ll write up a book list for bread baking later.

Tagged , ,

Cocoa Internet Version Checking – check for updates…

It seems that almost every new app you see out there has an on-line version checking system. I remember when the first ones started to appear in Cocoa land (probably from Omni), and I sort of coasted and ignored them. Anyway, this week I was running a Cocoa class, and the question arose. There’s an article on CocoaDevCentral on the subject, but I didn’t like the way it was written – the author starts by assuming that he’d have to write some PHP server side code to handle it, and shows that first, before moving on the the real meat.

Thinking about the problem, we have to be able to uniquely identify our app and recognise its version (that’s easy), and access data over the Internet using a URL. So let’s start with these two.

All Cocoa apps include a Resource called Info.plist; we can edit it directly, or use Xcode inspectors to change values. These include an app identifier, and a version string, which are used for storing user defaults uniquely, and in the standard About panel, respectively. This file is used in all bundles, and we can access the bundle for your application by:

[NSBundle mainBundle]

NSBundle returns bundleIdentifier, which is suggested should be in a unique form like that recommended for Java classes; eg com.plsys.MyApp. It also returns infoDictionary, which is a dictionary of all Info.plist values. The key used to store the version string is CFBundleVersion. So far we have:


NSBundle *app = [NSBundle mainBundle];
NSString *ident = [app bundleIdentifier];
NSDictionary *infoDict = [app infoDictionary];
NSString *myVersion = (NSString *)[infoDict valueForKey:@"CFBundleVersion"];

The next step is to find out how to retrieve our current version information over the Internet. You are going to need a place to store this – any web site that you can update will do: for my example, I just used my .Mac homepage. I need a very small amount of space to hold a text file with the version information, and extra if I want (optionally) to have the user download the latest version of my app from there as well – or from any URL.

Foundation provides a useful class for dealing with Internet content, called NSURL. So we could implement all we need using NSURL, but several other classes make use of NSURL as part of an initialiser. One that is very convenient for us is NSDictionary:

NSDictionary *plist = [NSDictionary dictionaryWithContentsOfURL:[NSURL URLWithString:[self versionURL]]];

The NSURL acts as a holder for our URL string, which can supply the contents of that location to NSDictionary’s initialiser. NSDictionary requires a string in a valid plist format, which can be the property list version of XML that Apple have adopted, or the traditional property list format. I find the latter easier to read and sufficient for the purpose. Property lists are composed of expressions, which are C-like values. If these are a ; separated list of assignments enclosed in {}, then the assignments are keys for a dictionary; if they are a comma separated list of values enclosed in (), they represent an array.

A dictionary, keyed by our application identifier, should be sufficient. If the contents of the dictionary are themselves dictionaries, this is a flexible system we could expand for any new ideas we get. Initially, I will use two keys to this dictionary: version, and url. Version will return a version string to compare against the bundle version, and url will be an optional location that will contain the latest version of our application.

NSDictionary *versionDict = (NSDictionary *)[plist valueForKey:ident];
NSString *currentVersion = (NSString *)[versionDict valueForKey:@"version"];
NSString *downloadURL = (NSString *)[versionDict valueForKey:@"url"];

At this point, we have enough information to perform a version check. The only remaining problem to solve is to start a download of the latest version. Again, this is easy to do, using NSWorkspace, which has the ability to open any file/URL for us.

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:downloadURL]];

That’s all.

My approach to using this code is to create a utility class that can be dropped into all my projects. all it needs is one accessor pair, for the instance variable used above called versionURL, and one action-like method to perform the version check (checkVersion: ). As defined, this class should be instantiated in a nib file so that the action method can be called from the application menu.

Then to use it:

- (void)awakeFromNib {
[versionChecker setVersionURL:@"http:////homepage.mac.com/paullynch/version"];
[versionChecker checkVersion:self];
}

Finally, the contents of my version checking plist file, which is at the URL above:

{
com.plsys.TestBed = {
version = "1.1";
url = "http:////homepage.mac.com/paullynch/TestBed.tar.gz";
};
}

This must end with a return.

You can download an example application including all the code and the utility class mentioned above.

Tagged

Python

I like Python. It’s a scripting language, in a similar vein to Perl. However, to steal an analogy from someone else, where Perl is executable line-noise, Python is executable pseudocode.

It’s an easy language to get into. Its most immediately visible difference from, say, C or Java is that it doesn’t use semi-colons or {}. Rather, all code is blocked out by way of indentation. Perhaps it’s just me being new at this, but it felt strange to write a program and not have it give a cryptic error message that I eventually traced to a missed semi-colon.

The main virtues of Python, however, are its extreme committment to object oriented programming (quite literally everything is an object, which seems to make some tasks much easier), its large standard library, and its very powerful list datatype.

Above all, it seems to result in understandable and maintainable programs. Which is nice.

–David

Tagged

Search and Replace

This is more complex than it needs to be, for unobvious reasons. The problem, succinctly stated, is implementing regular expressions with Java.

For historical WebObjects reasons, I have not had easy access to a regexp library. Java people have used the open source JEdit libraries to implement regular expressions, but with WebObjects this was too much like hard work. Now, with Java 1.4, we get java.util.regex, and a couple of regex methods added in to the String class. These support full regular expression patterns, like I would expect to find in sed, awk and perl.

With WebObjects, I could use the array split and combine functions to implement a simple replace method:


static public String replace(String a, String b, String text) {
NSArray bits = NSArray.componentsSeparatedByString(text, a);
return bits.componentsJoinedByString(b);
}

As I am always frustrated by the inflexibility of Java, I had to extend this to take a dictionary of parameters – keys being the search string, values the replacement:


static public String replaceFromDict(String text, NSDictionary dict) {
String result = text;
if (dict == null) return result;
Enumeration i = dict.keyEnumerator();
while (i.hasMoreElements()) {
String key = (String)i.nextElement();
result = PLUtilities.replace(key, (String)dict.valueForKey(key), result);
}
return result;
}

This was sufficient with Java 1.3. I used it to deal with form mails, using fixed parameters to replace with user names, etc. Mostly to implement a “forgotten password” routine for my stock login procedures.

Today I had a different problem. I was creating multipart emails using WebObjects components to generate the html. I already knew that the standard WOMailDelivery routine generates emails, with just a single html content, that gets caught up in SpamAssassin. The fix for this is to convert the email into a multi-part email, and give it a text content part. This is reasonably easy, although laborious. So I needed to create a plain text counterpart to a component html; the simple solution is to write a de-html routine. This has to rely on intelligent creation of the component to work successfully.


static public String dehtml(String value) {
String result = new String(value);
NSMutableArray patternArray = new NSMutableArray();
NSMutableArray replaceArray = new NSMutableArray();

patternArray.addObject(" "); replaceArray.addObject(" ");
patternArray.addObject("\\s+"); replaceArray.addObject(" ");
patternArray.addObject("(?i)<p>"); replaceArray.addObject("\n\n");
patternArray.addObject("(?i)<br>"); replaceArray.addObject("\n");
patternArray.addObject("(?i)</tr>"); replaceArray.addObject("\n");
patternArray.addObject("(?i)</td>"); replaceArray.addObject("\t");
patternArray.addObject("(?i)<head>.+</head>"); replaceArray.addObject("");
patternArray.addObject("<!--.+?-->"); replaceArray.addObject("");

patternArray.addObject("<.+?>"); replaceArray.addObject("");

for (int i = 0; i < patternArray.count(); i++) {
result = result.replaceAll((String)patternArray.objectAtIndex(i),
(String)replaceArray.objectAtIndex(i));
}
return result;
}

This version uses the String replaceAll method, which works just fine. The only trap in creating the patterns was the second string, which replaces white space with a single space: the Java compiler requires you to write \\s rather than \s. The final pattern to remove all html tags needs to use the “reluctant” form, with the ?, as the simple </+> pattern will otherwise eat up the entire string from first tag to last.

Tagged ,

Decision tables, when to use

This is genuine 1960s technology, but it moves forward into 1970s technology (OOP) very well.

Here’s the situation: you have a combination of two variables that need checking; for an example, perhaps an object has a status flag with a few different states, like failed, on hold, incomplete and registered – and you need to take different actions according to the state transition. Change of status from on hold to failed means set a processed date, and send failure email; change from incomplete to registered also sets the date and sends a different email, change from incomplete to on hold does nothing, etc. This is complex. You start coding it with if statements, perhaps, because you imagine that there are only one or two state transitions that need an action. Before you know it, you have 20 lines of code that you don’t understand.

The simple solution is to use a decision table. Grab a piece of paper (I have just coded one up from the back of an envelope that was already used for a different design problem), and lay down a grid. Set the actions for each state combination in the grid.

To implement this, I use a dictionary of dictionaries (most state tables are going to involve 2 variables, so this works). The hardest part is getting the Java code to correctly initialise the dictionaries. I use a string to represent the possible actions, space separated.


NSDictionary aDict = (NSDictionary)statusChangeDict.valueForKey(oldStatus);
String actionString = (String)aDict.valueForKey(company.status());
NSSet actions = new NSSet(actionString.split(" "));
if (actions.containsObject("D")) {
// action
}
if (actions.containsObject("E")) {
// action
}
if (actions.containsObject("S")) {
// action
}

Initialising the dictionaries:


protected String [] statusKeys = new String[] {
Company.OnHold, Company.Registered
};

public NSDictionary statusChangeDict = new NSDictionary(
// values
new Object[] {

new NSDictionary(
new String[] {
"", "S"
}, statusKeys),
new NSDictionary(
new String[] {
"S E D", "S"
}, statusKeys)
},
// keys
statusKeys
);

This is cut down considerably from the real example, of course; for a 2 x 2 table, I wouldn’t use this technique, but would stick with the nested if’s. Note also that I am not using a switch statement, as the logic combinations for this, with fall throughs and break statements, over complicates the logic for simple understanding.

To make this a real object oriented programming example, we need to implement this as an object. I think I’d want a way to create decision tables as files that were simpler to create than the annoying Java object array technique, and probably use reflection to execute the actions (just name the method calls in the decision table file).

That is an exercise for later, for when spending the time creating this code is cost effective. Don’t do anything now if you don’t need to do it.

Tagged , ,

Java date arithmetic

I am used to using the old (deprecated) WebObjects NSTimestamp class to do basic arithmetic on dates; it was easy with one method call to add an arbitrary number of days/weeks and get the correct date, or even to calculate the difference between two dates. In the new (release 5.0, so not that new) world of WebObjects, these methods are deprecated, although they do still exist. I see that NSTimestamp is a subclass of Date, so that lets us into the world of pure Java. The documentation points you to GregorianCalendar for further edification. A bit of scanning around shows me that Date has an add() method; for differences, use this: double days = (toDate.getTime()-fromDate.getTime())/1000/60/60/24; Seems very crude to me. It will calculate a date interval in days, meaning 24 hour periods rather than midnight transitions. It will also round down, courtesy of the cast to (double), which is necessary to prevent a “loss of precision” compiler error.

Tagged ,