Archive for March, 2012

Mou – Interesting Markdown Editor for Mac OS X

Friday, March 30th, 2012

Mou - Markdown Editor

When putting work up on GitHub, it really pays to have a nice Markdown previewer or editor for building your README.md file as it's the first thing a new user sees when coming to your project page on GitHub. You want to make a nice impression on the user, have good introductory docs, and even the license on the project, and all this is going to be rendered in Markdown - at least GitHub's version of Markdown.

When I was initially doing this, I was using BBEdit, and while it had a preview that worked with Markdown, it didn't support the GitHub CSS that it was going to be rendered in, and it still required that I edit the file, check it in, push it up to GitHub, and then I could see if I liked what it looked like. Not a great workflow, really.

So this morning I was on the hunt for a decent Markdown editor for Mac OS X. I knew there were several available in the Mac App Store, so I was willing to buy one, but I wanted to search the web first, to see what the opinions were out there of the best one to get. I've seen several, and there are two that seemed likely candidates, and I decided to try one, but I'm not really sure that it's what I am looking for. Still, it appears to be a lot closer to what I'm looking for than what I'm doing now.

Mou is a free app that does a split-pane view of source and rendered text. The position of the split pane isn't really resizable like a lot of Mac apps, but it is moveable in the sense that it can be moved by some menu commands to change the split from 1:1 to 1:2 or 2:1, and such. It's not ideal, but it's free. It's certainly a very nice start in the right direction as it currently support GitHub flavored Markdown.

BBEdit can work with Markdown, and even preview it, but it doesn't really use the GitHub style for the preview, and try as I might, I was totally unable to get some CSS file for BBEdit's preview that would work. There were perl and PHP versions, but that's far too much, and the Mac App would then be far better.

The problems with Mou are that it's taking up a ton of space on the screen. There's another application called Valletta 1.0, and it's got a unique take on the two-pane system: it keeps it all in one by having the line with the cursor in input mode and the rest of the document in display mode. This is a good idea, but if you're moving between different sized text, you're going to be having your text move all over the place. What I want is a great preview.

Mou does the preview good enough, and it's got a simple editor built in. Not bad for free. There's another app that does just the previewing, and it's on the Mac App Store for $4 - Marked. I might end up going for this as it's exactly what I'm looking for. I can edit in BBEdit all I want, and then preview in a good GitHub renderer and then check it in and push it all up to GitHub.

[4/10] UPDATE: I ended up getting Marked from the Mac App Store. They updated it to 1.4, and included the color syntax highlighting on GitHub. It's exactly what I'm looking for. $4 well spent. I can edit it in BBEdit, save, and it's automatically updated in Marked. Very nice indeed.

Interesting Bug in OS X Lion – and Great Service

Tuesday, March 27th, 2012

Mac OS X Lion

Today I had a crash in MarsEdit, and it had to do with looking up a word in a post I was generating, and I sent the data to the author. Amazingly, Daniel got back to me that there's a known bug with Mac OS X Lion when it comes to the window animations - and specifically turning them off, as I had. This was one of those hidden preferences that I found and used, and, as Daniel said, many have used as well.

The bug is that it's not really working right without it, and so the right thing to do is to turn it back on:

  $ defaults delete -g NSAutomaticWindowAnimationsEnabled

And things will go back to normal.

The real kicker for me is not that there's a bug in OS X Lion - it's that Daniel was on top of this support issue that wasn't even emailed from me to him. It was a crash report that I sent in, and he responded to it. That's amazing customer service. I'm certainly going to remember this as I (hopefully) move towards indie Mac developer. It's something to respond to emails, it's another thing entirely to respond to crash dumps.

Amazing.

Working with XML-RPC, Mac OS X 10.7 and Xcode 4.3

Monday, March 26th, 2012

xcode.jpg

For the last week or so I've been working getting something going with an old friend that involves a centralized service, Macs and iOS devices all sharing data. Most of this data is numeric, but that's only a current design consideration, and what we basically needed was something that was similar to iCloud, but not file or key/value based. We needed to have a specific local database (SQLite3 for Mac and iOS), and have that be a local copy of some data in a more centralized database (PostgreSQL) on a centralized server (Apache, PHP).

So the question was How to do it?

My initial thoughts went back to The Broker codec that I worked with back at The Shop, but then I realized that it was a really bad idea to use that as there's no proven need for that level of complexity, and that a far easier solution - and one I've used on the Mac in the past, is to have a simple XML-RPC client and server, and allow the server to be in PHP, and the client in the native frameworks and libraries on Mac OS X (and iOS). So I went that direction instead, and I think I've got something that works really well - and stays away from the entire marshalling/demarshalling issues that The Broker had.

But the XML-RPC route was not without it's trials and tribulations. Neither was using the wonderful FMDB Open Source library for Mac OS X and SQLite3. I had several things to figure out, and while it's all working now, it's not simple to figure all these things out, so I wanted to write this all down in a nice, concise way.

Step 1 - The Changes in Xcode 4.3

One of the firs things I wanted to do was to start a new Xcode project from scratch. Most of the projects I have weren't started with Xcode 4, and I wanted to see what I was going to get if I started clean, and put everything into git right from the start.

The first thing was that the new Xcode projects allow for a simple unit test framework that's very similar to the JUnit framework. You have a nice pair of set-up and tear-down methods, and then anything you define in the implementation file (.m), that starts with test is going to get run in the alphabetical order in which it was defined.

This is very nice and clean, and it means that you really don't have to mess around with the interface (header) file for the tests. Sure, you have to put in the ivars that you might need, but there than that, it's pretty bare. It's all in the implementation file.

There are also a set of methods for assertions like true and not nil, and all kinds of tests that you can sprinkle in the test code to make sure it's running the way you think it should. Very simple to use.

Running the tests is equally easy as it's just a simple, single key-stroke in Xcode to build the project, and the tests, and run the tests. I was very impressed as it made it very easy to run the tests until I got everything working just the way I wanted it.

Step 2 - FMDB as a Framework (well… almost)

I knew I wanted to use Flying Meat's FMDB library on GitHub for access to the SQLite3 database. I've read a lot about this library, and it looks to be set up exactly as I might have done it - but I didn't have to do it. Excellent.

But it wasn't without a little bit of grief. First off, it's not a Framework. Or a Library. It's a group of files. More than that, it's a group of files with a test app that's put together in a way that makes it a little unclear how to actually use it. I had to do a little googling to see that what you needed to do was to extract the files you needed from the project, place them into your project, and then start using them. In short, it's a library that's really a bunch of files.

This isn't bad, but a few sentences in the README would have made it very clear what I needed to do, and based on the things I've seen in Xcode 4.3 - converting it to Xcode 4.3 and using the unit test framework would make this all unnecessary. It could be a library, and it could have unit tests, and it would be the same code, it just would be a lot easier to use. But so it goes.

I added the files to my project, placed them in a 'group' (a folder in Xcode 4, but not on the disk itself), and then I was ready to go. Not horrible, but again, a few sentences, or a migration to Xcode 4 and this would all have been unnecessary.

Step 3 - XML-RPC Service in PHP

I had some code from a while back when I used a different XML-RPC implementation than what now is standard in PHP 5. So I was a little careful about how much to expect to be the same, and how much was implementation-dependent. Turns out, the built-in XML-RPC in PHP 5 is far nicer than the previous version I was using, and far faster as well. Win-win.

I have had no problems writing simple XML-RPC code in PHP 5, and it's nicely compact as well:

  <?php
  /**
   * This is the simple 'ping' service that let's the client know that we
   * are here and answering requests.
   */
  function doPing($method, $args) {
    return "PONG";
  }
 
  /**
   * At this point, we need to set up the XML-RPC server, bind the method
   * calls to the individual functions in this file, and then let the
   * user's call get processed and return the results. It's all pretty
   * simple, and handles all the ugly parts of the marshalling/
   * de-marshalling and method invocation.
   */
  // first off, make an XML-RPC server
  $server = xmlrpc_server_create() or die("Can't create rpc server!");
  // next, let's register the PHP functions as methods
  xmlrpc_server_register_method($server, 'ping', 'doPing') or
      die("Can't register the 'ping' method!");
 
  // now let's get the request and options...
  $request = $GLOBALS['HTTP_RAW_POST_DATA'];
  $options = array('output_type' => 'xml', 'version' => 'xmlrpc');
  // ...and make the call, returning the data to the client.
  print xmlrpc_server_call_method($server, $request, NULL, $options) or
      die("Can't call the method!");
 
  // finally, we can shut down the XML-RPC server.
  xmlrpc_server_destroy($server);
  ?>

This is a simple ping service that was the first thing I built to make sure that I could communicate with the PHP service and the Obj-C client. It's not bad at all in PHP, and since it's implemented in C, it's as fast as we're going to get.

So far, looking very good

Step 4 - XML-RPC Client in Objective-C

The client side was a little more code, but logically, it was very simple as well. I wanted to encapsulate the XML-RPC call into a simple method that would take Foundation objects and return Foundation objects so that I wouldn't have to worry about data type conversions, or any marshaling or demarshalling issues. What I came up with was a general 'call' method:

  - (NSObject*) call:(NSURL*)aURL method:(NSString*)aMethod
     with:(NSDictionary*)aParams orderedBy:(NSArray*)aKeyOrder
  {
    BOOL      error = NO;
    NSObject* retval = nil;
 
    // first, make sure that we have something to do
    if (!error) {
      if (aURL == nil) {
        error = YES;
        NSLog(@"[SKPipe(Protected) -call:method:with:orderedBy:] - the
               provided URL is nil and that means that there's nothing
               I can do!");
      }
    }
    if (!error) {
      if (aMethod == nil) {
        error = YES;
        NSLog(@"[SKPipe(Protected) -call:method:with:orderedBy:] - the
               provided method name is nil and that means that there's
               nothing I can do!");
      }
    }
 
    // make the XML-RPC method invocation as it's the basis of the call
    WSMethodInvocationRef    rpcCall;
    if (!error) {
      rpcCall = WSMethodInvocationCreate((__bridge CFURLRef)aURL,
                  (__bridge CFStringRef)aMethod, kWSXMLRPCProtocol);
      if (rpcCall == NULL) {
        error = YES;
        NSLog(@"[SKPipe(Protected) -call:method:with:orderedBy:] - the
               XML-RPC method invocation to '%@' with method '%@' could
               not be created and this is a real problem. Please check
               on it.", aURL, aMethod);
      } else {
        // set the parameters for the invocation - if we have any
        if (aParams != nil) {
          // see if the user has given us the key order to use
          if (aKeyOrder != nil) {
            WSMethodInvocationSetParameters(rpcCall,
                               (__bridge CFDictionaryRef)aParams,
                               (__bridge CFArrayRef)aKeyOrder);
          } else {
            WSMethodInvocationSetParameters(rpcCall,
                               (__bridge CFDictionaryRef)aParams, NULL);
          }
        }
      }
    }
 
    // do the actual XML-RPC call and get the results
    if (!error) {
      NSDictionary* pkg = (__bridge NSDictionary *)
                                 (WSMethodInvocationInvoke(rpcCall));
      if (pkg == nil) {
        error = YES;
        NSLog(@"[SKPipe(Protected) -call:method:with:orderedBy:] - the
               XML-RPC call returned nothing at all and that's a serious
               problem. Check on it.");
      } else if (WSMethodResultIsFault((__bridge CFDictionaryRef)pkg)) {
        error = YES;
        NSLog(@"[SKPipe(Protected) -call:method:with:orderedBy:] - the
               XML-RPC invocation of '%@' to '%@' returned a fault: %@",
               aMethod, aURL,
               [pkg objectForKey:(__bridge NSString *)kWSFaultString]);
      } else {
        // looks OK... get the results
        retval = [pkg objectForKey:(__bridge NSString *)
                                         kWSMethodInvocationResult];
      }
    }
 
    return (error ? nil : retval);
  }

The code is pretty simple - we first check to see if we have the necessary arguments to the method, and then we construct the method invocation. If there are parameters to pass to the call, then we add them, and if there is an array defining the order of the arguments in the dictionary, then we supply that as well.

We then first off the method invocation and get the return value. Pretty clean, but you have to know all the little tricks to get it to work. Thankfully, once we have this one method, we don't have to mess with XMl-RPC any more and can just stay in Objective-C and Foundation objects.

Calling the ping method isn't too bad… in fact, one of my tests does exactly that:

  - (void) test_1_Ping
  {
    if (![_pipe ping]) {
      STFail(@"The server ping failed!");
    }
  }

where:

  - (BOOL) ping:(NSURL*)aURL
  {
    BOOL      error = NO;
 
    NSObject* ans = [self call:aURL method:@"ping"];
    if (![ans isEqual:@"PONG"]) {
      error = YES;
      NSLog(@"[SKPipe(Protected) -pingServiceAt:] - the XML-RPC response
             was not good: %@", ans);
    }
 
    return !error;
  }

It's really pretty simple to use for strings, integers, arrays, dictionaries, and most data types and values. But there are two things that XML-RPC doesn't do really well, and I needed them both: NULL and Date/Time values.

Step 5 - Dealing with NULLs

XML-RPC just can't send a NULL. No way to tag it. I found a reasonable solution in the idea of sending a special value and then interpreting that special value as a NULL in the different parts of the code. For instance, if we sent a string: "__NULL__", then we could test for the data being a string, and it's value being '__NULL__'. It's possible that a call would have that as an argument, but not very likely.

If we decode this as the true NULL, then we're OK. Not easy given all the places that I'd like to have an argument or return value be NULL, but it's possible.

Step 6 - Dealing with Dates

NSDate values can be passed through - but they are passed through as string values where the NSDate is expanded out to it's human-readable value. This is OK, but it means that we need to have quite a bit of standardization on the format across different machines and platforms, and it means that we have to take the time to decode it and encode it on every call. Non-ideal.

Far nicer would be to send milliseconds since epoch as the date and then reconstruct the dates on the other side. This is easily done in Obj-C using the NSDate class:

  // build up the parameters to pass into the call - the local 'as of'
  NSMutableDictionary*	params = [NSMutableDictionary
                                     dictionaryWithCapacity:3];
  [params setObject:[NSNumber numberWithInt:aPID] forKey:@"pid"];
  [params setObject:[NSNumber numberWithDouble:
                        [aDate timeIntervalSince1970]]
             forKey:@"asof"];

I essentially turn it into a simple 'number' and then pass it through without any problems. On the other end, we have to worry about how to interpret this data properly. After all, it's not a date, it's a number of milliseconds since epoch, and that means that in PHP and PostgreSQL we need to account for this. What I found was that it was fairly simple to do something like:

  function removeData($method, $args) {
    // first, get the SQL that we need based on the method name invoked
    if ('removeIt' == $method) {
      $sql = "delete from meas where pid=" . (int)$args[0]
             . " and mid=" . (int)$args[1]
             . " and extract(epoch from taken)=" . (double)$args[2];
    }
    // now we need to open up the database, make the call, get the response
    $conn = pg_connect('host=localhost port=5432 dbname=primary');
    if (!$conn) {
      $retval = "no db";
    } else {
      $result = pg_exec($conn, $sql);
      if (!$result) {
        $retval = "no result for: " . $sql;
      } else {
        $result = pg_exec($conn, $sql);
        if (!$result) {
          $retval = "BAD";
        } else {
          $retval = "OK";
          // make sure to free up the result
          pg_free_result($result);
        }
        // make sure to close up the database connection
        pg_close($conn);
      }
      // make sure to close up the database connection
      pg_close($conn);
    }
    return $retval;
  }

Finishing Up

This has a lot of partial code, but it's all in the git repos for the project, and if you really need it, let me know. However, I think this is a great start for what you need, and a little experimentation should get you where you need to be. It's not perfect, but it's pretty close.

Google Chrome dev 19.0.1077.3 is Out

Monday, March 26th, 2012

Google Chrome

This morning I noticed that Google Chrome dev 19.0.1077.3 was out with a few little updates and a few known Mac issues. Interestingly, the new V8 javascript engine is there, but there are a few incorrectly drawn icons in the Mac version. Odd little combination of changes, but I'm guessing it's a merge that needs to be done, and then things will clear up. There's also a fix for the Flash plug-in, but it's going to be interesting to see what becomes of Flash now that Adobe is all but giving up on it. Google may go back to HTML5 and the more widely supported codecs. Hard to say.

Open Source Falling Out – ZeroMQ

Thursday, March 15th, 2012

ZeroMQ

Well… today has been an interesting day for one open source project that I've been working with for about the last year or so - ZeroMQ. Today I learned that two of the key developers in the group have "jumped ship" and forked the codebase, and are calling themselves Crossroads I/O. Their project, libxs, is really just a fork of the ZeroMQ code, and they are off to the races with a new Open Source company/project, and without the backing and support of a lot of the ZeroMQ community.

Today in IRC, it was a real surprise to hear Pieter talking about all the grief that was hidden from me, at least, in 2011. Trying to keep the group together was his job - and in the end, he realized that it just wasn't possible. So away Mato and Martin went, and the group is now light those two major developers.

I look at this with a lot of curiosity. When they had 2.1.x, it was good, decent, and while not perfect, it could have used a lot of improvement. Rather than do that, they started a 3.0 branch, with a completely different API, and all of a sudden, the upgrade path to me wasn't clear. I didn't mind it so much as we were moving away from it as the delivery transport in the project, but it still bothered me that they wanted to have a new API and a re-write of large sections of the code so soon after they just got 2.x out the door.

Dynamic and flamboyant individuals are nice to really move a project, but I can see that they can also be the worst liability to a project like ZeroMQ, as well. If they want to take the project in directions that the majority of users don't want, then they are going to make the project appear to be run by lunatics. But without them, you may face the problem that fixing the bugs they made are near to impossible in any reasonable timeframe.

So on the whole, I hope things work out for ZeroMQ, I really do. I think it's got a good idea that needs a little work to make it a really robust and reliable messaging system. But it's going to need to attract a few key, strong, developers to make it work.

Looking at Going Back to College

Thursday, March 15th, 2012

I've spent a bit of time thinking about what to do in my forced hiatus, and when I happened upon a posting at North Central College, I thought "Hey, maybe it's something I should do?" So I sent in an email inquiry, and talked to a friend that loves teaching as much as I do, and was getting excited about the thought of teaching, doing a little consulting, and enjoying life a while lot more.

So I found the citations for all my papers, and put them on my resumes, and started thinking about writing up the first of many essays they wanted as part of the application packet.

Then I thought - I wonder if the pay is good enough? I don't need to supplement my complete salary from coding, as the consulting will help out quite a bit, but I need it to be enough that I can afford to take the job. So I hit up the web to see if there were anything with typical, maybe even average, salaries for faculty positions on campus.

And there were.

And it wasn't great news.

The average for Assistant Professors that I could find was in the high-50's. Then I looked nationally, and again, it confirmed what I had known in my years at Auburn, and that's the simple fact that faculty members don't make a ton of cash from teaching, it's from the books, and the consulting. So it's not really a great deal for me. I'd need to have such an amazing consulting income that I'd likely just skip the teaching and do more consulting. Or coding.

I was a little disappointed, but not totally surprised. It's something that you can do after you make your money - or if you can live on a lot less than downtown Naperville housing prices. It's just not really feasible at this time.

I may still go through with it - just to see what the actual numbers may be, but I know it's not going to be something I'm able to count on as my primary income. That's going to have to be back to the finance world.

Darn. It would have been nice.

Creating DKit

Thursday, March 15th, 2012

DKit Laboratory

Since I've been looking for a job, I haven't been doing a lot of posting because not a lot has been happening - except all the stuff associated with getting a new job. But one of the things I wanted to do this time around was to create a GitHub project that could showcase a little bit of my coding skills as well as being generally useful for folks - me included, and just get something out there. So I created DKit.

The idea behind DKit is simple - it's a very specialized C++ library with classes and templates, that contains atomic and lockless data structures for very efficient C++ coding. I've used classes like these in projects at previous employers, but I started fresh, and expanded the classes significantly so that the atomic integers paralleled those in the stdint.h header - signed and unsigned, 8, 16, 32, and 64 bit sizes. These will make counters trivial as they are all atomic and so you'll get great counts even if you don't go through the overhead of locking.

I also added in two groups of containers, so far: single-producer/single-consumer, and multiple-producer/single-consumer. To the first, I put in a simple circular FIFO, and to the latter I put in a linked FIFO and a circular FIFO. These were ideas I had at a previous job, but I wanted to make them fresh so as not to get into any IP issues.

All told, it's a nice start. I want to keep it free of the issues that I might have had in the past, and just keep it OPen Source. I picked the MIT license as it seems to be the most liberal while protecting the original copyright and the removal of any liability. I think it's the start of something really nice.

Interesting CUDA C++ Toolkit – Thrust

Thursday, March 15th, 2012

NVIDIA and CUDA

This morning a good friend messaged me about something I might want to keep in mind as I do more massively parallel projects in the future, and that's a C++ library that fits in will with the STL, and is written by a couple of engineers at NVIDIA - Thrust. This looks very interesting.

The idea is that with the CUDA nvcc compiler, and a little pre-processing and some templates, they have made the STL std::vector equivalent for GPU/CPU usage. You can say where you want this vector to be held, and then process a functor on it to get things done very quickly. It's not as clean as OpenCL, in my opinion, but it's far more likely to be used as throwing NVIDIA cards into boxes - even server boxes, and using GCC/nvcc is a lot more likely in the typical business use-case.

Macs still aren't getting significant penetration there, but it's getting better.

So this is something to keep in mind, for sure. Thankfully, it'll run on Macs as well - so I just need to get a Mac Pro with NVIDIA card(s) and then I can start playing with it. Sweet.

Google Chrome dev 19.0.1068.0 is Out

Wednesday, March 14th, 2012

This morning, sometime, I noticed that Google Chrome dev 19.0.1068.0 was released and the release notes seem focused on the Android stack and a few side things, but I guess not every release can come up with a new way of doing things, or a massive speed improvement. But progress is progress.

Google Chrome dev 19.0.1061.1 is Out

Wednesday, March 7th, 2012

V8 Javascript Engine

This morning I noticed that Google Chrome dev 19.0.1061.1 is out and it's got a few nice things in it. Like a new V8 javascript engine (3.9.13.0), and support for remote file systems - could be interesting stuff. Glad to see they are still making improvements in V8 - that's something that I think I'm going to end up in sooner than later, and it'll be nice to have some improved performance there.