Life, Surf, Code and everything in between
White Papers | Free Tools | Products | Message Board | News |

Ads Via DevMavens

Accessing Html Document Content in other Frames



Here’s a question that I see pop up quite frequently on newsgroups: How do I access content across frames or an iFrame using JavaScript code? It’s actually quite easy once you understand how the windowing mechanism in the browser works.

Imagine you have two pages – one that holds an iFrame and another that holds the content for the iFrame. Both the host page and the iFrame contain a <div id=”message”> tag that is used to write some content into the other frame.

Here’s the top level page:      

    <div class="errordisplay" id="message">
       original
   </div>


   <iframe src="FramesPage2.aspx" id="frame1" height="400">
   </iframe>    
   
    
   <script type="text/javascript">
       window.onload = function() {
           var frame = document.getElementById("frame1");
           var msg = frame.contentDocument.getElementById("message");
           msg.innerHTML = "Hello World from Frame Page 1";
       };    
   </script>

And here the iFrames source page that gets loaded into the iFrame:

<div class="errordisplay" id="message">
original
</div>

<script type="text/javascript">        
        var msg = window.parent.document.getElementById("message");
        msg.innerHTML = "Updated from iFrame";    
</script>

The above works both with iFrames, Frames or separate windows that were loaded from the current window.

From the top level window that contains the iFrame you start by getting a reference to the iFrame by using plain old getElementById to select the iFrame by Id. Alternately you can also access the frame via window.frames[0] (or appropriate numeric index). Once the iFrame is selected you can use the contentDocument property to access the child frame content. From there you can access document methods as you normally would – in this case by using getElementById() and then assigning some HTML to the display <div> tag in the child frame.

In the child frame to reference back to the parent frame is easier yet: You can use the window.parent property to get a reference to the parent frame. You can then simply reference the document and continue on accessing elements in the parent document.

Cross Frame Functions

The previous code is  - ahem - functional, but realistically accessing cross frame elements is probably not something you should do extensively. When you control elements in another browser window you are stuck with directly coding against the DOM – using libraries like jQuery for example isn’t possible since tools like it work against content in the current window.

If your interaction is any more complex that say grabbing or setting a single value , it’s probably better to create functions in the target frame and call these functions across the frame border rather than the individual DOM commands individually. That way you can encapsulate the functionality in the child page, which makes it easier to code and test the functionality, and it also allows you to use any libraries the way you normally would. You can then simply call the function from the parent page to perform more complex tasks indirectly.

To call a function across frames is fairly easy. Assume that the iFrame code I showed earlier also includes a function called showMessage in script code that updates it’s internal message <div> tag like this:

    <script type="text/javascript">
            function showMessage(m) {
                document.getElementById("message").innerHTML = m;
            }
    </script>

To call this function from the parent frame you can use a couple of approaches. The easiest is to use the frames collection:

<script type="text/javascript">
    window.onload = function() {
        window.frames[0].showMessage("Hello from Main Page in iFrame");
    };    
</script>  

The individual frame is essentially a reference to the window object in the child frame and all global JavaScript functions are functions off the window object. So you can directly call the function of the frame. Easy. The main issue with this that you need to know (or find) the frame index first.

You can also get it through the frame DOM element in the parent page and use its contentWindow property to get at the child window:

<script type="text/javascript">
    window.onload = function() {
        var frame = document.getElementById("frame1");
        var win =  frame.contentWindow;
        win.showMessage("Hello from Main Page in iFrame");
    };    
</script>

and this works as well.

With the abillity to call functions it’s much easier to offload processing to other frames more efficiently which allows for creation of self contained modules that let you interact with only a few small interface points. For example, you can create window objects that load content dynamically and yet act independently  and without interfering with each other.



Oil's cheap again, but can we afford to be careless?



Oil is under $40 as I write this, which is down from over $147 sometime in July. So isn’t it high time to go out and buy those bargain basement Hummers to start burning up this cheap oil? I’m kidding of course, but some people never learn apparently as they are falling for lower oil prices and incentives from auto-manufacturers to continue down gas guzzling alley.

It’s not all that surprising actually. Gas is now down to the $1.30 range in some of the US. Even on Maui we’re down to $2.58 (we tend to have the most expensive gas in the country), which is as low as I can remember.  With gas this cheap, it seems easy to fall back into thinking that the recent rise in oil prices was nothing more than an unnatural energy futures bubble. While some of the price hikes were caused by speculation, another just as important aspect of high oil prices was that world supply for some time was running at full production capacity in relation to demand, with barely any oil to spare and national reserves drained near record low levels. Oil prices are largely determined by the interplay of supply and demand and as we all know by now supply can be a very fickle beast where the smallest interruption can cause price hikes. The current price drop is largely due to the projection that oil demand is going to be dropping drastically due to the current economic downturn with industrial production reduced and consumer spending curbed significantly.

With the drop in oil it’s back to the old days when people forget momentarily the pinch of $5 a gallon of gas and it’s written off as just another ploy by the oil companies, or what was it this time – oil speculators. But just because oil is cheap now doesn’t mean that it can’t go back up and go back up very, very quickly just as quickly as it went down. Certainly the current level of oil prices is a lot lower than where even our historical average should be (which based on historical averages is somewhere between $60-$70 a barrel + some offset for the low dollar) so I wouldn’t expect oil prices to stay this low for long, economic downturn or not.

Should we worry?

Our society and way of life is so intricately linked to oil, that it’s hard to not worry about the stability of oil prices.

From our society – especially in the US - that is so intricately dependent on cars and trucks for all transportation needs for lack of alternatives in most parts of the country (Suburbia Hell), to our agriculture that depends on oil based products for fertilizer, pesticides as well as the fuel to power farm machinery, to the myriad of consumer items that depend on oil based manufacturing as well as the energy inputs to produce actual products. Hi tech gadgets like our computers, phones, TVs etc. – even though often small -   require massive amounts energy to produce. There are very few industries that aren’t dependent on large scale energy usage and a lot of that energy comes from oil.

Oil is a near perfect energy source – it’s (at the moment at least) ridiculously cheap for the amount of energy it contains. It’s also easy to transport and store (as it’s liquid) so this energy can be easily transported around the world without significant loss of energy (other than transportation costs). To put this into perspective consider this quote from oil industry analyst Matthew R. Simmons:

“At $100 per barrel, oil costs about 15 cents a cup. A cup of oil converted to transportation fuel such as gasoline will carry a vehicle loaded with six people about 1.5 miles.”

Try getting that kind of output from human labor or even animal labor (think feed cost) at this rate or anywhere near it. Oil is damn convenient with its high energy output and portability. And that’s even with the inherent inefficiency of engine technology which wastes 20-30% of the energy available.

We’ve lived through 150 years of continuous growth in oil production with only a few small breaks in continuity – the biggest of which was in the 70’s during the energy embargo which forcibly reduced production, which also caused a severe recession, and brought about the first efforts of creating energy efficient cars, and household utilities etc which actually resulted in reduced energy usage for an extended period.

Oil usage started picking up early in the 20th century and since than has affected just about all aspects of the growth of our modern societies. It’s also been the main driving factor for population growth as the increased productivity of human population and our machines as well as oil based agriculture has driven up the count of the human population.

 Population and oil production

The dependence of our society on energy and specifically oil as the primary energy source has become a primary driving force of growth both real and economic. Imagine what would happen if that growth was no longer supported by an adequate supply of energy to go along with it.

Pure production doesn’t tell the whole story either. For the most part in this tremendous time of growth since the early 1900’s energy demand and supply were pretty closely matched. Our society grew from a mostly agricultural base to a mostly industrial one in a matter of 100 years. With a few exceptions – the oil shocks in the 70’s specifically – energy supply and demand have been largely in sync or at least in a positive balance with supply larger than demand. But earlier last year supply was once again starting to outrun demand which in large part resulted in the run up of oil prices up until the middle of the year.

This is a scary thought: it’s almost as if there was some anticipation – although not publicly made – that oil supply would continue to run short for the foreseeable future which in turn made in  or the speculation in oil futures that only made things worse. There’s been a lot of talk about demand increasing rapidly from emerging markets like China and India with production flattening out.

If there’s indeed some credence to the concept of Peak Oil (which says that oil production will peak and thereafter flatten and slowly decline when the half way point of oil reserves are produced), then even a moderate climb in demand quickly results in a large discrepancy between the actual production and world demand for oil which gets worse every year.

http://gailtheactuary.files.wordpress.com/2007/06/world-oil.jpeg

Last year’s divergence and shortage was fairly minor and mostly caused by interruptions in the supply chain rather than real shortages in production, but it gives a damn good taste how quickly the price of oil can shoot up and how adversely it ends up affecting the economy. Another disturbing sign is that oil production actually peaked in 2005 – since which production has stayed flat and has actually very slightly declined. In 2008 the numbers are going to be even lower although in this year the numbers aren’t going to be driven by production shortcomings but by the drastic drop-off of demand in the latter half of the year after the economy collapsed.

But imagine if you will that energy available is reduced even by very small percentages each year, especially in light of how our economy’s growth over the years that has pretty much always been accompanied by increased energy consumption. If you remove any amount of energy output from the economy it stands to reason that this reduction will also translate into reduced economic output and activity. Energy is extremely tightly coupled with our economy and whole way of life that we’ve been used to. Most people alive in the ‘western’ world at least don’t know any different – constant growth and no shortage of energy has been a given over the last 100 years when oil has been exploited.

Peak Oil?

The current situations of plummeting oil prices pretty much flies in the face of the Peak Oil theory which takes a fairly depressing view of oil production and when production of oil reaches its apex. The idea is that production (as well as discovery which proceeds it) follows a bell shaped curve the top of which is reached when half of the oil resources have been produced.

The production curve ramps up as the oil discovered is exploited and pulled out of the ground. At first both curves climb steeply as new discoveries of previously unexplored areas occur and production can pry off the easy pickings of top level easy to access oil (and often natural gas). As time goes on discoveries slow down and finally peak as ever more sophisticated (and expensive) discovery mechanism are used to track and find resources, that eventually are ever more difficult to find. In production the curve is similar – steep rise as the easy locations and wells are tapped and then slowly become more expensive and energy intensive to retrieve the oil out of the ground. The production curve eventually peaks where a maximum amount of oil has been retrieved after which the curve flattens out and then starts an steady and unstoppable decline.

The problem with Peak Oil is that it’s not easily quantifiable. Oil production rates are known, but the amount of reserves available in the world are obscured in that most of the big producers do not allow inspection of fields to determine actual production capacities. This has resulted in speculation on when the actual peak is supposed to occur. This naturally is an easy counter argument for this phenomen since it can’t be quantified exactly, so naysayers have plenty of counter arguments to field.

However, from a purely common sense perspective the idea of Peak Oil makes sense. The problem isn’t that we’re going to run out of oil, but that the oil that is left around the half way point – which most of the proponents of Peak Oil point to at this point in time or very soon – is going to be much more difficult to retrieve and get out of the ground. Whether it’s existing oil fields that have been drained and require pressurization to bring up oil, or new fields that are buried deep in the soil or under miles of ocean water (like some of the newer oil discoveries off Brazil and Mexico) procuring that oil is more difficult, expensive and most importantly more energy intensive.

The problem with energy production is that one has to overcome the energy deficit – the amount of energy required to retrieve the resource without expending large amounts of energy. Most of the ‘easy’ oil has been produced and now we’re into the phase of producing oil that requires more energy to retrieve. This means a barrel of oil produced today has a lesser net energy value than a barrel of oil produced say in the 1950s when the US was still the leading oil producer.

It wouldn’t do to expend more energy to retrieve oil than the energy that is contained in the actual oil retrieved. We're not there yet but a lot of the ‘alternative’ fossil fuel resources like extremely deep reserves or tar sands are very energy intensive compared to the easy oil that literally came spilling out of the ground in the early days of oil exploration. Half way in exhaustion of reserves is thus much further along the way, because 100% retrieval is likely to hit the point of diminishing returns way before resources are completely used up.

Nobody can say reliably when Peak Oil will hit but certainly I feel like there will be a peak. The question is whether it’s imminent, has already happened or still a few years off. Peak oil production currently is 3 years back in 2005 – 2006 through 2008 has not touched the 2005 peak production rates. However, it’s unclear whether that’s due to production limitations or whether it’s simply been due to supply chain issues – wars, natural disasters that got in the way of getting the oil to market. 2008 has seen a drastic drop in demand and usage that in turn has resulted in cut backs in production. If the economy continues its downward spiral demand is bound to continue low for some time to come producing a dip in the statistic similar to the one in the 70’s oil shocks and following economizing of energy consumption.

Still – while these are seem like small reprieve’s from a potentially disastrous problem, this slowdown is all but delaying the inevitable – which is that oil is a limited resource and it will get more scarce no matter what, even if it takes a little longer.

One of the scariest issues surrounding this problem is that modern society is so ill prepared to deal with even  modest reduction in energy resources available. Imagine a United States with gas at $10 a gallon or more and tell me that you can see things not get out of hand very quickly. Our suburban lifestyle, our food distribution network would break down very quickly. Again think about the last year prior to last summer has resulted in massive inflation in food prices. I know my food prices both on Maui and on the Mainland in Oregon had gone up by at least 40% and often much more year over year (while the gov’t numbers lie and exclude energy and food pricing in core inflation). Imagine what further rises along these lines could do to an economy that’s already in decline and where most people can’t do much else but spend their income on basic survival needs.

What alternatives?

What sucks about the current situation is that oil has plummeted so low, so quickly just at a time when serious thinking about alternative energy usage and more importantly the viability of alternative energies was finally gaining some steam. One way or another we need to re-focus on energy alternatives to get off the sinking ship that is massive oil consumption. There’s been a lot of talk about the general issue during this election cycle, but the discussion has been superficial at best with vague hints of addressing unspecified problems. But rarely does the discussion enter any state of urgency about how critical any reduction of energy resources would be for this country both on the national as well as on the individual, personal level.

There’s no way that the folks in the national security branch of the US don’t have this on their radar – they’re just not putting out the word to the public. Our energy dependence on foreign oil is a huge national security threat to say the least especially with most of our petro dollars going to foreign countries that are not friendly towards Western countries.

Yet even with the prospect of potentially disastrous changes in the energy balances of the world there are precious few alternatives that have any chance of providing a reasonable replacement for the cheap and easy to manage energy that comes from Oil. It’s no big surprise that innovation in energy has been shunned for so long while oil has been so relatively cheap because it’s impossible to compete with it on a calorie to dollar ratio.

The alternatives are slim and worse even once industries recognize the need to move to these alternatives they are very slow to do so. Bringing out hybrid cars is a baby step in the right direction but it sure seems like too little, too late. Look at how long cycle times are between a concept and actually having a car on the road. Even if a new technology was found, the replacement cost to produce the needed cars and infrastructure wouldn’t be able to ramp up for probably a decade or more. And that’s just for cars – factor all the aspects of the economy, agriculture etc. and the thought of switching to some other energy technology is staggering to the max.

Alternative fuels are practically non-existent – nothing more than vapor ware. There’s been a big PR push towards hydrogen power, but hydrogen is a storage medium rather than an energy source so something has to get the energy into it first. Volatility and energy inefficiency make hyrdogen a non-starter. Bio-fuels have horrible energy balances – it takes nearly as much energy to produce these non-waste bio-fuels than what they produce and the only reason that they are viable at all is that the crops in question (corn/sugarcane) are typically heavily gov’t subsidized. Without the subsidy there’s no way growing current bio fuels would make any sense. Nuclear power is often mentioned as an alternative and while it can produce a lot of power there’s of course the disposal issue plus the fact that large scale use would eat up all available uranium with a 20-30 years – it’s not a long term solution. Renewables like solar and wind have potential for small scale localized applications, unless adopted in large scale where every household and building becomes a part in a solar grid. Solar is getting very popular as a renewable, but currently it’s still so inefficient (with the best panels converting less than 40% of captured energy – with common more around 25%) and at the same time expensive. Solar equipment is also extremely environmentally unfriendly in production requiring much of the same processes used for computer/hardware production – it has a tremendous energy footprint that has to be made up.

Almost any alternative power proposed ends up having to do with electricity. But  electricity alone is not a complete replacement for what liquid fuels provide today. Heavy machinery – trucks, industrial machines, large airplanes etc. are not likely to run on electricity alone. Pure electricity also has the problem of not being very portable – the longer it has to be routed the more signal loss there is reducing the overall power output due to travel from site of generation to site of use. Storage of electricity is also a problem as power stored in batteries slowly drains and storage mediums available currently are very innefficient both in charging and then releasing energy resulting in significant power loss. Electricity use makes most sense near the source.

All in all the picture for alternatives look slim, yet it seems we – as a society – are relying on technology to save us from a potential energy shortage. If there is innovation in this space it’s happening behind closed doors and in baby steps. I doubt that if there was some great solution out there – even if it was in a pre-production stage - that we wouldn’t know about it. If it was private industry somebody would want to captilize on it. If it was the gov’t – somebody would want to take credit for it. :-}

It’s safe to say that if we run into problems with energy availability there’s not going to be one single technology that will help us out of the situation – it’ll have to be a combination of strong energy preservation and a combination of alternate energy technologies combined. But even then it will be hard to make up for the shortcomings that are likely to occur through oil shortages.

The problem is once such an energy shortage hits it will become that much harder to find a solutions to the energy problem as resources and any sort of funding will become less and less available in a declining energy and economic environment. Our best chance is to have some sort of Plan B in place before that happens, but it looks like this problem whether it hits soon or 5 years from now will catch us unprepared.

All of this may sound alarmist and unfounded and heck maybe it is. As mentioned above, the theory of Peak Oil can’t be pinned down exactly without reliable reserve numbers that are not available. But you can certainly search around on the Internet in relation to Peak Oil and find plenty of validated and scientifically backed analysis of this concept by many varied sources some of which would seem to have a conflict of interest even. The material is available, but in the end it is a very negative scenario and as we all know politicians are afraid of being harbingers of bad news. The other thing is that the Peak Oil concept has proven itself in many smaller scale scenarios of specific oil producing regions. The US was the prime example that M. King Hubbard (the originator of the Peak Oil theory) himself  used and predicted fairly accurately. There’s the North Sea example, Mexico, Russian production all of which are now in decline and all of which followed the Hubbart curve model.

If you want to find out more about peak oil there are a number of books and videos available. Here are a few I’ve looked at over the years that are worth mentioning (yes there’s also a lot of complete whacko stuff out there so reader/viewer beware holds).

The Party's Over: Oil, War and the Fate of Industrial Societies The Party's Over: Oil, War and the Fate of Industrial Societies
by Richard Heinberg
New Society Publishers (April 1, 2003)

Read more...

There are also a few videos you can check out including the 2006 Crude Awakening:

A Crude Awakening - The Oil Crash
by n/a
DOCURAMA

Read more...

and The end of Suburbia:

The End of Suburbia: Oil Depletion and the Collapse of the American Dream
by Matt Simmons, Colin Campbell, Steve Andrews,
Michael Ruppert, Michael T. Klare
Microcinema DVD

Read more...

Both of these are not very recent and fairly alarmist, but they both provide the background on Peak Oil and discuss some of the issues at hand. Crude Awakening focuses more on the general issue of peak oil and where we are at in the world currently (or as of 2006 anyway), while The End of Suburbia is much more speculative and aggressively argues through some of the potential effects an end of oil scenario might have. You may not agree with the ideas and conclusions especially of the End of Suburbia, but both videos are interesting to start thinking about the what if implications that an end of oil scenario engenders.

What me worry?

I’ve long held the belief that our current path of development on this Earth is simply not sustainable. We can’t expect limitless growth to keep going forever and not to run out of the resources available on this finite planet’s resources. Additionally our use of conniving financing practices and debt to finance consumption and public expansion is only accelerating the dramatic explosion of populations and resource use of the world. Warnings about hitting some of our limits in expansion and never ending growth seem only reasonable. It’s happened many times before in history. Check out Collapse by Jared Diamond for some interesting case studies on how pissing into your own backyard doesn’t usually work out so well, even when in hindsight it only seems so extremely obvious with reality staring you in the face. We’re much on the same path today except that now we’re looking at it on a global scale for the first time.

The idea that oil resources running out are going to cause major changes in the balance of the world is the understatement of the millenium. The question is just whether this is something we really need to worry about or not at this moment in time. I think that some healthy skepticism at the very least is in order because a lot of the signs are pointing in the direction that there is a problem brewing…

For me personally I’ve thought about this quite a bit for some time which is why I’m writing about it here in this personal message on my blog. I think that while we can’t be sure whether there is an immediate problem in resource and energy shortages on the planet it stands to reason that at some point in the not too distant future we are going to be facing these issues head on. It seems incredibly childish not to address this possibility now while there is still time to soften the blow that a potential energy shock would have.

Think about it, even if you do nothing more than a little thought experiment with yourself. How would you live if you had to make do with a more primitive society that doesn’t run on power or power that is treated as a luxury rather than an abundant ever available resource as it is now? Or even one that doesn’t run with private cars? Do you live in the suburbs with no way to even get to a store by foot and no public transportation? How will you get to work if your job is in the city that’s 20, 50 or 80 miles away? Will you even have a job? In a drastically shrunken economy that has paid a heavy energy tax that is bound to deflate any economic growth that isn’t likely to come back, do you think you’ll still have a paper pushing job? Or an abstract job like programmer for example? How do you code when there’s no consistent electricity and which business would still need abstract work.

Once you start on this path of thinking there are a million other little things that pop up that make you realize how much of our lives depends on technology and most of all power to get us through the day. If nothing else, it might make you more energy aware, to show you how you are using energy in your daily life and you can possibly reduce your energy footprint.

The thoughts are not pleasant to say the least. Most of us have grown up in a society that has always been growing  – always with the assumption the next generation will have it better than the last (until fairly recently when we’ve flattened out and even have reversed the growth trend). Many of us have never worked with our hands. Instead we sit in offices or park our butts in front of computers and push paper and bits around rather than producing something tangible that has material value. Ask yourself, what would you do if you couldn’t do this sort of abstract work? What other skills do you have? Could you keep yourself alive self-sufficiently for any period of time without the comforts of modern day life – without electricity? Do you know how to build something, grow food? And would you die of boredom (hardly – survival tends to keep you awake?) not knowing what to do with out a computer, TV or video games to drain your brain in?

These are some uncomfortable questions at least to me, because I know the honest answers I have for them are not the way I would like them to be. Yet even a few years after thinking about this I’ve not been motivated enough to do a heck of a lot about it although it continues to be on my mind. Procrastination or lack of motivation – or to some degree seeing nobody else being the least bit concerned makes these kind of thoughts feel like ‘crazy thoughts’ at best. But they nag nevertheless.



Understanding the jQuery Object returned from a jQuery Selector Query



Cody Lindley has a great post that explains some of the innards of the jQuery object returned from selector queries. As you may know if you are using jQuery, the jQuery object returned from a jQuery selector expression is an object that acts like an array, but is in fact an object with numeric properties that masquerade as an array. If this sounds a little confusing you’re not alone. Even though I had figured this out some time ago, every time I talk about jQuery I seem to get my terminology mixed up calling it the jQuery object and jQuery array interchangeably <snicker>... which is not correct – it’s definitely an object.

Anyway, check out Cody’s Post on the Learning jQuery site which explains in some detail how the jQuery object is constructed and how it is that you can treat an object like an array which is effectively a neat hack that might be useful to you in other JavaScript situations.

BTW the Learning jQuery site is a good site to keep bookmarked and RSS hooked since they frequently cover interesting content that explains some of the inner workings of jQuery… highly recommended.



Online Video Streaming – Not quite there yet



In the last few months I’ve been experimenting around with watching streaming video online for movies and TV shows. I’ve been highly skeptical towards online streaming  and the hype that has preceded it for so many years, but I also have a few friends who love using streaming content. Given that nowadays you can rent videos and have them delivered to your door within a couple of days makes the effort of trying to stream a video and tie up your computer and internet connection seemed kind of silly of me. Still, it seems the technology to for online streaming has gotten a lot better in the last couple of years and video has become much more commonplace on the Web albeit mostly for lo bandwidth usage ala YouTube. So I started playing around with streaming video initially with Netflix streaming player while I was travelling in the last year. For the most part I’ve only been using two services Netflix and Hulu.com.

Netflix Online Streaming

I’ve been a NetFlix subscriber for a few years now and I’ve been very happy with the service they provide. I’m always amazed by the turnaround that manages to get videos returned back in two days from returning a previous video. I was also pleased to see NetFlix starting to offer streaming content with the Watch Instantly service. Although the selection for downloadable content is not anywhere near their full video collection, the selection is fairly decent (12,000 titles they say). What’s nice about the service is that Netflix makes it really easy to select movies add them to your queue and then play them on demand – usually once I start running out of videos rented physically.

NetflixQueue

The new Silverlight Netflix video player (currently in beta and an opt-in option until it gets released) is pretty slick and a big improvement over the old Flash based viewer in several ways. The UI for the player is much more flexible and more importantly it doesn’t suck CPU resources nearly as much as the old Flash player did.

NetflixPlayer

Video quality steps up and down, depending on bandwidth available, which is actually unfortunate. There seems to be no option to determine your desired setting – Netflix does and can do so mid watching which really sucks when it happens and you get down throttled from TV quality 480p output back to whatever low bandwidth settings they use that pixelate heavily.

Hulu.com

Another site I’ve been using quite a bit is Hulu.com. Hulu provides streaming video content of a decent selection of movies and a ton of current TV serial content. Many currrent TV Shows are available on Hulu and you can watch them with only brief and bearable advertising breaks. For TV content – if you’re into that sort of thing - Hulu is hard to beat. Given that these days many TV shows provide higher quality content than the crap movies Hollywood is pumping out that’s not as far fetched as it once seemed to me.

Hulu also has a pretty nice flash based player that you can customize in a number of ways.

Hulu

Quality is excellent in 480p mode but really sucks in the lower res mode as both video and sound quality are drastically reduced. The sound is actually more annoying than the lower video res in the low res mode.

One thing that’s a bummer about both Netflix and Hulu is that they cannot be accessed from out of country (or at least not easily and without IP spoofing). Due to licensing rules I suspect, but still a pisser – it sure would be nice to tap into online Netflix content while I’m out of the country but no such luck. Interesting side note: I’m in Austria at Markus Egger’s right now and he’s using XBox Live’s Netflix hookup from here to view streaming content. Apparently XBox gets around the international limitation when you sign in with a US account at least.

Other Video Sites

There are a few other sites I use occasionally. There’s obviously YouTube ,Google Video, Yahoo Video and MSN video which provide avenues for video sharing for everyday users as well as lots of current affairs and educational content. I like Google’s service as it has the advantage of allowing for longer videos vs. YouTube’s limited length videos that are limited to 10 minutes. Both sites really don’t fall into the same category as the others above – they are not for watching movies but for sharing video. Resolution is also much lower so video playback is never really a problem although quality is pretty weak even in the higher resolution modes offered by some videos.

Video is also becoming ever more popular for news and current affairs content. Most news sites and especially traditional news TV sites provide much of their content online. I for one have been pleased with this development as I travel quite a bit especially out of country and it’s nice to have access to US news source abroad as well as European sources when I’m in the US. Most news content is of surprisingly good video quality and runs reliably as well.

Then there are some kinda shady sites that offer movies like Watch Movies that provide links to movies including very recent ones. Shady – yeah definitely. You can watch movies that aren’t even out on video yet, and stored on diverse video sites under obscure names. I’ve checked this out mainly because a few people mentioned it to me – especially typically non-computer folks and kids – and I went just for novelty value and sure enough there’s lots of movie content of recent movies in all sorts of qualities from hand held bootlegs to fairly decent small screen quality. This is probably not worth anything except for novelty value, but not much beyond that.

Another thing I’ve noticed is that there are a lot of of YouTube like clones are popping up that provide specialized video content for a specific focused area of interest. Video content is getting much more popular all the time and content is popping up all over the place and in different contexts which is cool. As cool as all that is though it’s actually not as easy to find that content easily. Because it’s scattered all over the place and there is no official tagging mechanism with each site using its own search mechanism finding what’s available is a problem.

A few thoughts on High Quality Streaming

Having the ability to stream high quality video for watching movies like Netflix and Hulu is pretty cool mainly because it gives you choice. I tend to be a random TV watcher – usually I watch a movie or a show after I’m done at night to wind down. Having a physical DVD to watch is Ok, but you get to watch what you got home right now. With digital delivery the choice can be made based on how the mood strikes me and that’s a good thing. It’s also good to have access to some content for reference or be able to revisit is – useful for political documentaries I frequently watch.

But even though there’s a certain cool factor to watching full hi-res video on your laptop computer or piping the output from laptop to the big screen tv in the living room, the process of doing that is still pretty painful. Browsing for movies on these sites using the Web Browser and two screens is a pain in the ass if you’re watching on the big screen TV because the browsing and watching experiences are so separate.

Netflix is addressing this with software that’s getting built into many hardware devices that work directly with your TV. There’s the Roku console who’s sole purpose is to work with Netflix, and there are devices like a host Blue-Ray Net capable devices (Samsung, LG )that have network connections and can interface with the streaming service to pick and play movies directly from hardware you already have in the living room.

Using a computer in the living room might seem cool for the novelty of it, but realistically it is still a pain. So unless you have a full hardware setup that’s specific to a media center purpose it’s less than ideal as it fails the remote control UI test in my book.

Bandwidth and Service Issues

As nice as it is to watch digital content for me the experience has been a choppy one even on a fairly high bandwidth connection. Here on Maui, I have Roadrunner cable with what they call a 5 megabit connection and while I’ve never seen that kind of throughput it looks like 3-4 megabits, which is pretty good especially given the remote island gig and all (and that we had NO high speed at all until 2003!).

Even with this connection though, when running in 480p mode it’s barely enough to keep the video buffer stuffed. I’m not sure if the problem is on the inbound end or the outbound end, but it’s either way high definition streaming is choppy at best. Netflix is much worse in this respect as it frequently down shifts me into a lo-res mode and I have no control over this process. Hulu stays in whatever mode I selected, but it too stutters and loses frames from time to time.

It would really be nice if these streaming players would give you an option to buffer data for video up front – even allowing for downloading the entire movie up front possibly so that some of the choppiness could be mitigated, but alas the buffering is only real time so any network hiccup screws up playback.

Also if I put a strain on the bandwidth elsewhere on the network (even as trivial as checking email) and video play will definitely hiccup. For a typical internet connection the bandwidth is still not quite enough, unless you go for the gold plan of super high-bandwidth that some providers now offer (not in my area though <s>).

What’s also problematic is that recently many Internet Service Providers have started putting limits on the amount of bandwidth you are allowed on your ‘unlimited’ Internet access accounts. Video certainly doesn’t help bandwidth usage. When I started playing around with this stuff a few months back and watched a ton of online content after I returned from Europe I got a warning notice from RoadRunner that I went over some unspecified limit. Looks like more providers are going that route and that doesn’t bode well for video on the wire.

Incidentally, other than video my Internet Service is solid and plenty

CPU Usage

Also both Netflix and Hulu are major CPU hogs. If you look at the Hulu screen shot you can see process manager running and it’s got 1 of the dual core processors nearly pegged at 100% and the machine is audible running in overdrive. The CPU usage for Flash apparently is so high that on an older Pentium M class machine 480p conversion doesn’t play smooth at all with constant dropped frames which are apparently not due to the bandwidth alone as it plays smoother on my dual core machine. Heck even in lo-res mode the older machine can barely keep up even though it has a reasonably decent video card. Ouch.

The Netflix Silverlight player fares a bit better, but CPU usage is still pretty high in the upper 70’s% range on the core it runs on.

Content is lacking

The final road block is that although content is rapidly expanding, when it comes to commercial video online you’re bound to have a much more limited choice online than you get with DVDs from a place like Netflix. For example, Netflix’s on demand library is a fraction of the full DVD library which is surprising. Hulu.com’s library is frustrating in its incompleteness especially for serial content – usually you can view only most recent content back for about a year. The truly universal library that provides access to most or all video content doesn’t really exist yet, but that’s really the siren call for online, ondemand video streaming.

Ultimately this point is the biggest strike against online streaming I think – selection above all wins! This all comes back to the movie industry’s fear of digital distribution I suspect, copy protection and byzantine rights management agreements which are the most likely culprit that stand in the way of broader distribution. Bummer.

Bottom Line

The bottom line for me is this: Streaming video in hi-res with either of these sites uses tons of bandwidth and computer horsepower. As far as we’ve come with video on the computer, it still looks like the sweet spot is another generation of hardware and bandwidth away at least for mainstream computer hardware. Combined with the short

At the moment I wouldn’t want to trade in my DVD rentals for only online even if Netflix actually had all content available online, but still it’s interesting to see how far we’ve come. The new apps look and work slick but it feels like we need another generation of bandwidth improvements and video hardware improvements to make this all work a little smoother. Small scale video ala YouTube and News Feeds seems Ok right now – reliable and smooth, but the full screen hi-res movie experience is not there yet.

So what about you? What video services are you using if any and what’s you’re experience been for quality, bandwidth usage and limits from ISPs? I’d be curious to hear especially from folks who have those really high bandwidth accounts.



String Extraction



Ah thought I go back to basics today for a post. An operation I rely on a lot when I’m working with text is the ability to extract strings with delimiters from within another string. RegEx can be quite useful for many scenarios of finding matches and extracting text, but personally for the 10 times a year when I need to use RegEx expressions I tend to relearn most of the cryptic syntax each time. What should take me a couple minutes usually turns into a half an hour of experimenting with RegEx Buddy for me.

So a long time ago I created some helper functions that make it easier to help me with the common task of string extraction. One of them is ExtractString() which is something I use quite frequently to well extract a string from within another string. This can be in small strings or larger strings. The function accepts a source string and a couple of delimiters around which text gets extracted.

Usage looks something like this:

// json Date format is "\/Date(9221211)\/"
jsonDate = StringUtils.ExtractString(jsonDate, @"\/Date(", @")\/");

which extracts the only the milliseconds portion of the string inside of the date delimiter. Now here’s actually a good example of why this function is easier than using RegEx – try constructing that particular pattern with RegEx escape codes for some real fun and undecipherable gobbledygook. :-} Been there done that (for the JavaScript portion of the parser and it took a while to get right!).

ExtractString() also has a few additional parameters for case sensitivity and how to behave if the end delimiter is not found which is useful. For example in the following snippet a query string is passed and I need to find the value of value r parameter:

string res = StringUtils.ExtractString(Url,"?r=","&",false,true);

And no, before you ask: I couldn’t use Request.QueryString() because the string in this case is a parsed URL that doesn’t come from a query string input. The function call searches for a r= and & as delimiters, does so case insensitively (false) and specifies that the “&” delimiter can be missing which means the result returns up to the end of the string. In fact to create a generic query string parser you could now write:

public static string GetUrlEncodedKey(string urlEncodedString, string key)
{
    string res = StringUtils.ExtractString(urlEncodedString, key + "=", "&", false, true);
    return HttpUtility.UrlDecode(res);
}

Again RegEx can solve this fairly easily as well, but doing or’d  expressions are a pain to get right and since I use this functionality all the time I don’t want to fuck around with RegEx’s constant relearning curve (for me).

I certainly prefer:

string res = StringUtils.ExtractString(Url,"?r=","&",false,true);

to:

string sourceString = "ScriptCompression.ahx?r=ww.jquery.js";
Match match = Regex.Match(sourceString, @"r=(.*)(&|$)",RegexOptions.IgnoreCase);
string res = match.Groups[1].Value;
string expected = "ww.jquery.js";
Assert.AreEqual(expected, res, "Failed to extract string with missing end parameter");

I’m not saying that RegEx hasn’t got a place. Obviously for more complex searches RegEx is much more flexible. But for simple repetive scenarios RegEx is overkill and likely more resource intensive than a simple function that finds a couple of string indexes and extracts a substring. And to me at least RegEx usage seriously affects code readibility so if I have a chance to abstract behavior in such a way that I don’t have to use RegEx I will do so vehemently – either by wrapping the RegEx code or creating an alternative.

Anyway, here’s the implementation of ExtractString with various overloads:

/// <summary>
/// Extracts a string from between a pair of delimiters. Only the first 
/// instance is found.
/// </summary>
/// <param name="source">Input String to work on</param>
/// <param name="StartDelim">Beginning delimiter</param>
/// <param name="endDelim">ending delimiter</param>
/// <param name="CaseInsensitive">Determines whether the search for delimiters is case sensitive</param>
/// <returns>Extracted string or ""</returns>
public static string ExtractString(string source, string beginDelim, 
                                   string endDelim, bool caseSensitive, 
                                   bool allowMissingEndDelimiter)
{           
    int at1, at2;

    if (string.IsNullOrEmpty(source))
        return string.Empty;

    if (caseSensitive)
    {
        at1 = source.IndexOf(beginDelim);
        if (at1 == -1)
            return string.Empty;

        at2 = source.IndexOf(endDelim, at1 + beginDelim.Length);
    }
    else
    {
        //string Lower = source.ToLower();
        at1 = source.IndexOf(beginDelim,0,source.Length,StringComparison.OrdinalIgnoreCase);
        if (at1 == -1)
            return string.Empty;

        at2 = source.IndexOf(endDelim, at1 + beginDelim.Length, StringComparison.OrdinalIgnoreCase);
    }

    if (allowMissingEndDelimiter && at2 == -1)
        return source.Substring(at1 + beginDelim.Length);

    if (at1 > -1 && at2 > 1)
        return source.Substring(at1 + beginDelim.Length, at2 - at1 - beginDelim.Length);

    return string.Empty;
}

public static string ExtractString(string source, string beginDelim, string endDelim, bool caseSensitive)
{
    return ExtractString(source, beginDelim, endDelim, caseSensitive, false);
}

public static string ExtractString(string source, string beginDelim, string endDelim)
{
    return ExtractString(source, beginDelim, endDelim, false, false);
}

It’s pretty basic stuff, but it’s one of those handy utility functions that are useful so frequently it’s nice to have them in my Utility set of classes (StringUtils to be exact).

So I’m curious to hear what the RegEx aficionados will have to say. I wonder if RegEx could be used inside of the function – the big issue I always have with RegEx in reusable/generic scenarios is that patterns that contain the start and end delimiters have to escaped properly. If I have backslash in the starting delimiter a generic expression will die the painful RegEx parsing death. So (he asks mockingly) - could the generic internals of ExtractString be re-written to use RegEx instead of indexOf and SubString to retrieve the string match – frankly I don’t know how (and it’s not really necessary, but an interesting thought).

BTW, for some of the FoxPro folks lurking this function might look familiar. It closely matches West Wind Web Connection’s Extract method as well as VFP’s native StrExtract functions on which I had relied for many years. I think the above functions was one of the first things I ever created in .NET because its use was sorely missed when starting out in .NET.

Hopefully this method will be useful to some of you.



Problems with opening CHM Help files from Network or Internet



As a publisher of a Help Creation tool in Help Builder, I’ve seen a lot of problems with people not being able to run their help files properly. Here’s the scenario: You go ahead and happily build your fancy, schmanzy Help File and deploy it to your customer or let your customers download them off the Internet directly.

The customer downloads the file, opens the zip file and copies the help file contained in the zip file to disk, then opens the help file and finds the following help file:


 

 

...a help file that comes up with all topics in the tree on the left, but a Page Not found or Operation Aborted error in the IE content window; an IE error. The CHM file obviously opened since the topic list is there, but the Help Viewer refuses to display the content. Looks like a broken help file, right? But it's not - it's merely a Windows security 'feature' that tries to be overly helpful in protecting you.


The reason this happens is because files downloaded off the Internet - including ZIP files and files cotained in those zip files - are marked as potentially malicious and so do not get full browsing rights on the local machine – they can’t access local content, which is exactly what help topics are. If you look at the URL of a help topic you see something like this:

 
mk:@MSITStore:C:\wwapps\wwIPStuff\wwipstuff.chm::/indexpage.htm

which points at a special Microsoft Url Moniker that in turn points the CHM file and a relative path within that HTML help file. Although it looks weird this still equates to a call to the local computer zone, the same as if you had navigated to a local file in IE which by default is not allowed.  Unlike IE you don’t get an option to allow access via the yellow header security bar that IE usually shows to allow overriding or setting permanent security options for local content.

Instead you get a page like above or one that says that Navigation has failed.

How to Fix This

The easiest way to fix this issue is to set properties on the CHM Html Help file and allow it to be opened right when you download or open it for the first time:


openHelpFile 


There’s more detailed information of the issue and links to Microsoft KB articles as well as some nasty workarounds for this issue here: http://www.helpscribble.com/chmnetwork.html. Unfortunately on newer versions of Windows you won't even see this dialog - you'll get straight into the help file but with the topic not displaying and no hint as to what's going on.


Another option is to right click on the file's properties and 'Unblock' the file so that the security block is bypassed:

unblock 

This latter approach is probably the better choice as it's not a global setting but specific to the particular help file. Once set Windows will display the help file properly and without any extra prompting for security. However, if you do the latter you have to do it to each file individually.


It's annoying as all hell that this sort of obtrusive marking is necessary, but it's admittedly a necessary evil because of Microsoft's use of the insecure Internet Explorer engine that drives the CHM Html Engine's topic viewer. Because help files are viewing local content and script is allowed to execute in CHM files there's potential for malicious code hiding in CHM files and the above precautions are supposed to avoid any issues.



Removing the .SVC Extension from WCF REST URLs



I’ve been getting a lot of questions in the last few months regarding the URLs that WCF REST uses. WCF REST – new in .NET 3.5 – provides a pure Web based Binding (webHttpBinding specifically) that allows for creating REST based URLs that return non SOAP Http results in Xml, JSON, RSS, Atom or raw binary formats. One of the nice things that you can do with WCF REST endpoints is that you can specify a UriTemplate that allows you to customize the way the URL looks when accessing the endpoint.

For example I might have a method endpoint defined like this:

[OperationContract]
[WebInvoke(
    Method = "GET",
    ResponseFormat = WebMessageFormat.Xml,
    BodyStyle = WebMessageBodyStyle.Bare,
    UriTemplate="Quotes/{symbol}")  
]          
StockQuote GetStockQuote(string symbol);

Normally a WCF endpoint is accessed through the .SVC file plus a path that specifies the method name plus any parameters specified on the query string (or in POST data). Without the URI template to load a stock would look like this:

http://localhost/wcfAjax/RestStockService.svc/GetStockQuote?symbol=MSFT

Using the UriTemplate the URL becomes a little nice and more readable:

http://localhost/wcfAjax/RestStockService.svc/Quotes/MSFT

Note that once you apply a UriTemplate the method/parameter syntax no longer works – only a single endpoint URL maps to the specific endpoint.

Url formatting is of course mostly semantics – the latter URL is easier to parse by a human consumer having more of the feel of a breadcrumb that leads you down the path. You can also create your own ‘rules’ for this so if you also wanted to return that same data in a different format like Json (where the original returned XML) you might want to set up a second endpoint that does this and looks like this:

[OperationContract]
[WebInvoke(
    Method = "GET",
    ResponseFormat = WebMessageFormat.Json,
    BodyStyle = WebMessageBodyStyle.Wrapped,
    UriTemplate = "Quotes/{symbol}/json")
]        
StockQuote GetStockQuoteJson(string Symbol);

and can be called like this:

http://localhost/wcfAjax/RestStockService.svc/Quotes/MSFT/json

WCF REST allows for some welcome URL flexibility which is a nice feature when dealing with REST based endpoints.

One important limitation with UriTemplate to keep in mind that parameter mapping ( inside of {} ) only works with string parameters, so even simple type mapping like int, bool, or dates don’t work with them.

Getting rid of the pesky .SVC Extension

One of the first questions I usually get when I talk about the WCF REST features usually is: How do I get rid of the .SVC extension in those URLs? Back to semantics, eh? If you look at the URLs above you notice that each of the URLs references the .SVC file including its .SVC extension. The UriTemplate defined on the WCF endpoint method specifies the a path that follows the .svc file, so whatever template you set up it is appended to the main service URL which ends in the .Svc file.

Here are two approaches you can use to remove the .svc extension and both of them work through Url Redirection at the server level.

IIS 7 Rewrite Module

A couple of months ago Microsoft released the IIS 7 Rewrite module which allows you to easily and declaratively define Url Rewrite rules in your Web.config file. The module provides RegEx based replace syntax and is ideal for fixing up .SVC urls. What’s nice is that it also includes a nice UI for the IIS 7 Management console so that you can visually see all rewrite rules installed and even experiment and test them out.

IisRewrite

To get a URL like this:

http://localhost/wcfAjax/Rest/RestStockService/Quotes/MSFT/json

You can use the following rule definition:

<?xml version="1.0" encoding="UTF-8"?><configuration>
    <system.webServer>
    <rewrite>
        <rules>
            <rule name="RestStockService" stopProcessing="true">
              <match url="^rest/reststockservice/(.*)$"/>
              <action type="Rewrite" url="rest/reststockservice.svc/{R:1}" />
            </rule>
        </rules>
    </rewrite>
    </system.webServer>  
</configuration>

You use RegEx expressions to match the service Url without the .svc extension. The RegEx expression is based on virtual directory as its base path – it’s important that you consider any subdirectory paths in the URL. As you can see my service lives in a Rest folder underneath the virtual directory root and that’s reflected in the match expression for the URL searched for. Note the RegEx group after the service name – an explicit group is used to capture the entire extra path of the URL so it can be used in the rewrite operation. You can reference any of the group matches as {R:1} for the first group match ( {R:0} is the entire match).

This is a pretty painless way to do URL routing that’s easy to modify declaratively and store in a configuration file.

You can download the IIS RewriteModule here:

http://www.iis.net/downloads/default.aspx?tabid=34&g=6&i=1691

I’d highly recommend this module even if you’re not using it for WCF REST Urls. It’s been really useful to me in setting many URL rewriting tasks that I previously used custom modules for.

Custom Http Module

The IIS Rewrite module only work in IIS 7 only unfortunately so if you’re running IIS 6/5 or you need more sophisticated URL routing than RegEx expressions can provide you can create a custom module.

IIS 5/6 requires Wildcard Mapping

If you’re not running IIS 7 in order for this to work you need to enable Wildcard Script Mapping and map the wild card map to ASP.NET. Wildcard script mapping sets up all URLs to be routed through the ASP.NET pipeline so every resource – static or otherwise - is fired through ASP.NET so you can run HttpModules against them. This allows even extensionless Urls to (like http://localhost/wcfAjax/Rest/RestStockService/Quotes/MSFT/json ) fire through your ASP code and hit your modules.

Once you have requests firing into the ASP.NET pipeline you can then set up an HttpModule that reroutes requests. The following is a somewhat generic .svc rerouting mechanism where you can just specify a part fragment that is treated as a service.

/// <summary>
/// Redirect module that allows specifying a set of .svc urls
/// by stripping the svc extension off and accessing without it.
/// 
/// To use add any non-svc path segments (ie. service.svc should be service)
/// to the ServiceMap below.
/// 
/// Note that any path that uses one of these service map entries needs to 
/// end with a trailing backslash.
/// </summary>
public class  ServiceRedirector : IHttpModule
{

    static List<string> ServiceMap = new List<string> 
        {
              "reststockservice", 
              "ajaxservice",  
              "jsonstockservice"
        };        

    public void Dispose()
    { 
    
    }

    public void Init(HttpApplication app)
    {       
        app.BeginRequest += delegate
        {
            HttpContext ctx = HttpContext.Current;
            string path = ctx.Request.AppRelativeCurrentExecutionFilePath.ToLower();

            foreach (string mapPath in ServiceMap)
            {
                if (path.Contains("/" + mapPath + "/") || path.EndsWith( "/" + mapPath) )
                {
                    string newPath = path.Replace("/" + mapPath + "/", "/" + mapPath + ".svc/");
                    ctx.RewritePath(newPath, null, ctx.Request.QueryString.ToString(), false);
                    return;
                }
            }
        };
    }
}

The idea is that you provide a list of Urls (statically in this case but you could easily modify this to load the list dynamically either from config settings or some external source) that are to be routed as simple strings. In short the names of the URL that you want routed without the SVC extension. This code still assumes you want to route to the same name as the SVC file (ie. /reststockservice/ becomes /reststockservice.svc/).

Other than that the code just runs through list and and tries to match it to the current URL. If found it replace the match by adding the .svc extension to it and off it goes.

To hook up the module you’ll need to add it to your web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web> <httpModules> <add name="ServiceRedirector" type="WcfAjax.ServiceRedirector"/> </httpModules> </system.web>
<system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules> <add name="ServiceRedirector" type="WcfAjax.ServiceRedirector"/> </modules> </system.webServer>
</configuration>

The <system.web> section is for IIS 5/6, <system.webServer> for IIS 7. The module’s code is a pretty simplistic approach that uses simple string replacement to expand non-.SVC Urls to .SVC urls, but it should give you enough of an idea on how to create something more flexible should you need it. This approach works both in IIS 5/6 with the wildcard script mapping or in IIS 7 where no mapping is required as all requests already fire through the ASP.NET pipeline.

While it’d by nice if WCF had a  native mechanism for removing the .SVC extension from URLs, you can see that’s it’s pretty straightforward to do so either with the IIS 7 Rewrite Module or by creating a small custom module.

Does it really matter?

I’m not sure if changing a Url by removing an extension is really providing any benefits, perceived or otherwise. To me this is semantics. Sure a Url without the .svc looks nicer, but who cares? For the most part COMPUTERS not people access those Urls and computers have no sense of esthetics <s> at least last time I checked. And if it’s an API, it’s still only a developer who looks at it once and surely a developer can determine the difference between a URL with or without an extension buried in the extra path.

I think what it really comes down to is having a clean URL structure that's natural to follow. Certainly an API that is consistent will be easier to learn for a developer to use and easier to work with in the long run. Whether stripping of an .SVC extension helps that cause or whether you use a query string vs. a path separator is arguable at least in my mind. The biggest concern for an API is consistency - it should be logical, readable and most of all consistent. And frankly query strings are the easiest way to be consistent because they are non-positional unlike path separators which are.

However, as I mentioned at the outset of this post – a lot of people actually seem to care quite vehemently about the Url format, because it’s easily one of the most frequent requests I have heard since I started writing/speaking about WCF. Hence this post.

So, what’s it to you? Does the URL matter and if so why?



Quest for a Driver – or how to waste your Life Away



Last month I inherited an old laptop – an Averatec 2100 for selling which was to be put on Craigslist and sold for a few bucks. Well, it didn’t work out that way. It’s been a journey and I think eventually this thing is bound for the dump after all rather than going to a needing home.

It all started innocuous enough with a new computer being bought to replace this the Averatec. The Averatec is going on 3 years and it was actually quite useful for the person who’d used it mostly for email, web surfing, basic image editing, writing and little more. It’s small and lightweight, albeit a bit underpowered. When the call was made to ‘dispose’ of the machine, I offered to reformat the drive and reinstall Windows because that’s the main reason the machine had started to slow down and feel like an overweight sludgy snail. I figured re-installing would have been easier than selectively wiping data off and uninstalling apps and try to get the machine to run at reasonable speed.

Big mistake!

The issue is that the machine never came with any disks. Everything was – and I emphasize WAS – online and was meant for download if needed. Of course when the machine was bought everything was pre-installed so nobody thought of actually downloading the driver or recovery disks from the Averatec site which also was a big mistake.

Windows Key don’t work

I started with a reformat and reinstallation. No disk – no problem. Download the MSDN Windows XP Home Basic, start installing and use the Windows  key from the bottom of the machine. But immediately it became clear the key from the bottom doesn’t work even though I was installing the same version of Windows (XP Home Basic 64 bit). Apparently the difference is that this was a more recent version (Home Basic with SP2). I called Microsoft’s support number and the tech insisted that the key on the bottom of the machine was not valid downright accusing me of piracy. Thanks dude. Nice! Now I’m a pirate for re-installing Windows. Fine. I have other unused licenses of XP Home I never used  – so screw you too! Eventually (after two more tries) that worked.

Drivers oh Drivers

Next problem: Some drivers didn’t auto-install. Most things came down fine, but others didn’t. No video (ATI Drivers aren’t on Windows Update?), Wireless drivers (Broadcom fairly standard drivers either). Ok, that’s annoying but as long as you can figure out what hardware’s in use it’s not too bad to download drivers from the hardware vendors.

So I managed to get most things installed and got the machine functional with one exception: The Audio Driver. Could not for the life of me figure out what hardware is in the machine and no driver to install.

So as I mentioned the computer came with no CDs. So I went to the Averatec site which is absolute crap. That site has drivers for sale on CD, but not for the 2100. Averatec is also a subdivision of TriGem and they actually have a drivers download area, except – you guessed it – they don’t have drivers for the 2100. They have 1100 and 2200 downloads but not 2100. WTF? Nowhere is there any contact information on either of these sites. Nothing. No links, no phone numbers, no automated email nothing. Well, not true after digging around for about half an hour I found an email link (that I can’t find again now) that hinted at support questions. I emailed, but wasn’t disappointed when I didn’t get a response.

So after fucking around with this machine for a bit by using the 1100 and 2200 drivers I was actually able to get everything to work except for the Audio driver. That was two weeks ago and I’m still driverless. Without audio drivers this low powered machine which is useful for little more than Web Browsing and typing letters to Aunt Maya is basically worthless – not something I’d even want to give away to somebody. A Web machine with out audio is kind of pointless.

BTW, why is it that Audio drivers are sooo problematic on machines? If there is going to be a problem on any machine you install it’s most likely to be an audio driver. I’ve had Dells which I tried to install on Win 2003 and the one thing that never worked were the damn audio drivers. But the real issue with audio drivers is that the don’t auto detect or provide any information about themselves. They always show up as Unknown Multi-Media device, rather than the make of the device. So even if the manufacturer might have drivers figuring out exactly what the damn driver is painful.

Driver Sites Galore

While searching for drivers one thing that really struck out was that there’s just all sorts of commercial bullshit getting in the way in search results. If you search for Averatec 2100 Audio Drivers you get a mouthful of sites that claim to fix just about any computer driver related issue. You get everything EXCEPT the actual Averatec site.

After I had mucked around with this for some time I was about to give up. Via Twitter several people recommended some of the driver download sites like DriverAgent.com  and Driver Detective. I was hesitant at first because this is obviously hardly worth putting any money into. But after the hassles of trying to find a driver I figured a few dollars are cheaper than the time it had already taken me to get this far. Checked out the sites, but predictably they too failed to identify the audio hardware so they couldn’t make a driver recommendation. No go.

Other sites promised to provide full driver disks but most of these seem very shady at best. I actually broke down and tried to get a driver from the people at User-Guides.co.uk which looked no less shady but at least sounded like it would provide everything I need in one place. Well that site sucks too – it’s been 5 days and I still haven’t been able to download the drivers. I spoke to tech support and they claim a backlog and they will refund my money if I choose. I don’t hold up much hope that they’ll have more luck finding drivers than I have. Apparently these people build their own configuration/driver disks based on the specified computer. But of course there’s no mention on the site of this – they make it sound as if the disk and drivers are ready to download. Perfect example of a predatory business…

Predatory and just bad Business Practices

And that’s really what I got out of this experience: There’s so much crap out on the Internet and so much trash that is aiming to confuse, obfuscate and take advantage of people any way they can. This is nothing new and it’s easy to say – you gotta be careful where you go and who you give your information to (the order at User-guides was through PayPal – no way would I have given these folks a credit card!). Most of us as computer professionals are aware of the malice that exists on the net and if something looks even slightly shady chances are pretty good that it really is and a good sign to stay away.

And that’s all fine and good until you get into a situation where you really need something – like this damn audio driver for an Averatec 2100. Ok I don’t really need it, but by now this has become an obsession that has gone beyond any value proposition and I know it <s>. It still irks me to throw the computer away vs. trying to pass it on to somebody less fortunate so I would like to get this one small missing piece for this machine so it’s not a complete piece of junk.

Further it’s sad that a company – Averatec or TriGem – would be so callous as to not put the drivers onto their Web site. The TriGem site is complete piece of shit anyway and the driver portion of that site is actually broken with the fancy Javascript lists not actually working and depending on which browser you use not even be visible. But even forgiving that, why would you not publish the drivers for ALL machines you’ve ever sold? How hard could it be to get that online and keep it there? Apparently these drivers where available at some point in the past when drivers where found on the actual Averatec site (as opposed to the TriGem site) but since then – nothing. Insane.

Ok, I would have never bought a machine like that since it’s clearly a cheaply made consumer level machine and stuff like this isn’t entirely unexpected, but it still makes you wonder about how our resources and supply path are spread out all over the world and over different work and business ethics.



Silverlight ListBox Selections or what’s with SelectionChanged Events?



 

I was musing today on Microsoft’s obsession with OnSelectionChanged events on list controls in general and particularily in Silverlight, which is what I’m working with at the moment.In one of the apps I’m mucking around with I have a ton of sidebar menu selections that get displayed and the easiest way to drive these is with ListBoxes.

They look something like this:

SilverlightMenuList

As you can see this ‘menu’ is a collection of controls – a StackPanel with a border element at the top:

<StackPanel x:Name="SidebarMenu2" Margin="10,40,10,10">
    <Border x:Name="SidebarMenu2Header" Style="{StaticResource MenuHeaderStyle}" >
        <TextBlock Text="Update" Style="{StaticResource MenuHeaderTextStyle}"></TextBlock>
    </Border>

    <Border BorderBrush="Black" BorderThickness="1" >
        <StackPanel Background="White" >
            <ListBox x:Name="SidebarMenu2MenuItems" BorderThickness="0" Padding="5" 
                     SelectionChanged="SidebarMenu2MenuItems_SelectionChanged"
                     >
                <ListBox.Items>
                    <ListBoxItem Tag="Save" Content="Save" ></ListBoxItem>
                    <ListBoxItem Tag="Close" Content="Close"></ListBoxItem>
                    <ListBoxItem Tag="Delete" Content="Delete"></ListBoxItem>
                </ListBox.Items>
            </ListBox>
        </StackPanel>
    </Border>
</StackPanel>

I thought to use a listbox for this because it provides the default behavior of clickability as well highlighting the menu selection. The alternative would be having to create all of this behavior manually which is decidedly non-trivial. Additionally listboxes can also be data bound, which isn’t the example above,  but if menus get more complex using dictionary data sure would be easier to use than individual control markup for each item.

Ok, so using a listbox is handy, but as shown above with the SelectionChanged event the behavior is not working all that well for a menu. The selection change event is implemented like this:

private void SidebarMenu2MenuItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    object item = this.SidebarMenu2MenuItems.SelectedItem;
    if (item == null)
        return;

    ListBoxItem listItem = item as ListBoxItem;
    string tag = (string)listItem.Tag;

    if (tag == "Save")            
        this.SaveItem();            
    else if (tag == "Close")
        NavigatorPanel.Navigate(new ItemListPage());
    
}

So far so good. This actually works to make a single selection or another selection that is different which is the most common thing you might do.

But in this scenario SelectionChanged becomes a problem when you try to save more than once in a row. This is a common problem with SelectionChanged events. As the name implies SelectionChanged fires only – well when the selection changes. But what if I saved to accept my changes to commit the changes, then make some more edits and save again? I’m not  re-selecting the same selection and I’m expecting the save to fire again. Only it doesn’t…

This behavior is also prominent in list controls in ASP.NET and even Windows Forms and frankly the absence of an easy way to trap ItemClicks as opposed to SelectionChanged operations are annoying. I’d argue 90% the behavior that is expected is an ItemClick not a SelectionChange.

Ok, so how can this be fixed in the Silverlight code above? My first thought was to hook up a MouseLeftButtonDown event:

 

<ListBox x:Name="SidebarMenu2MenuItems" BorderThickness="0" 
         Padding="5" 
         MouseLeftButtonDown="MenuClickHandler"    
         >
    <ListBox.Items>
        <ListBoxItem Tag="Save" Content="Save" MouseLeftButtonDown="MenuClickHandler"></ListBoxItem>
        <ListBoxItem Tag="Close" Content="Close"></ListBoxItem>
        <ListBoxItem Tag="Delete" Content="Delete"></ListBoxItem>
    </ListBox.Items>
</ListBox>

But I was surprised that this didn’t work. Either by handling the event on the listbox itself or on each of the items. Apparently the listbox eats those events.

What did work in the end was the MouseLeftButtonUp event on the listbox:

<ListBox x:Name="SidebarMenu2MenuItems" BorderThickness="0" Padding="5" 
         MouseLeftButtonUp="MenuClickHandler">

which did handle the click. I now end up with a generic menu click handler that all the listbox ‘menus’ are hooked up to on the form:

private void MenuClickHandler(object sender, MouseButtonEventArgs e)
{
    ListBox list = sender as ListBox;
    object item = list.SelectedItem;
    if (item == null)
        return;

    ListBoxItem listItem = item as ListBoxItem;
    string tag = (string)listItem.Tag;

    if (tag == "Save")
        this.SaveItem();
    else if (tag == "Close")
        NavigatorPanel.Navigate(new ItemListPage());

    this.SidebarMenu2MenuItems.SelectedItem = null;
}

One thing to keep in mind with this approach is that it doesn’t account for keyboard navigation – so if you move the up and down arrows manually the menu will move, but nothing actually handles the selection. Incidentally this is another example of the shortcoming of SelectedChanged events as a mechanism for selection – when you use keyboard navigation you probably don’t want to fire the operations in the menu – only when you click or press space/enter should anything happen. But this is a pain in the ass to handle. Handling selection reliably on a control is not trivial as you have a host of events to capture (Mouse, Keyboa