Tuesday, February 27, 2018

Google App Engine / Servlet / Dart client / CORS

The following would be obvious to the informed, but to the newbie trying to write a simple end-end sample, using a servlet on Google's App Engine communicating with a Dart / Angular client, it may be helpful.

You are trying to follow along the HTTP Client example on Dart for the web.
You have written a servlet on app engine, serving the list of heroes as json data.
You point your browser at the app engine URL and you see your json list.
You run your Dart Angular client code, from say WebStorm, but you don't see the list (but other text on the page shows up fine).
You open the browser development console and see an error  :

"XHMLHttpRequest cannot load . No 'Access-Control-Allow-Origin' header is present on the requested resource.  Origin 'http://localhost:42705' is therefore not allowed access."




The HTTP Client tutorial outlines the XMLHttpRequest issue.  In the article Using Dart with JSON Web Services there is the guidance under "A note on CORS and HttpRequest":

"One caveat: Make sure your app is served from the same origin (domain name, port, and application layer protocol) as the web service you are trying to access with HttpRequest. Otherwise your app will hit the Access-Control-Allow-Origin restriction build into your web browser. This is a security restriction to prevent loading data from a different server than the one serving the client app."

This is the situation during development, where the browser page launched by WebStorm is pointed to a local server hosting the page, but that page refers to json data coming from another origin.

The article references code for a Dart server to correctly handle the CORS interaction with the browser, commented nicely:

/**
* Add Cross-site headers to enable accessing this server from pages
* not served by this server
*
* See: http://www.html5rocks.com/en/tutorials/cors/
* and http://enable-cors.org/server.html
*/

So adding the doOptions method to the default servlet template (doPost and doGet) and setting the headers:

@Overrideprotected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
    // pre-flight request processing    resp.setHeader("Access-Control-Allow-Origin", "*");
    resp.setHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");
    resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
}
@Overridepublic void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {

    resp.setHeader("Access-Control-Allow-Origin", "*");
    resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
    resp.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    resp.setContentType("application/json");

    // ... 

The browser should now handshake with app engine correctly, eliminating the browser exception and you should see your json data rendered.

(There is also a topic Google App Engine and CORS on StackOverflow, it notes ...Allow-Headers only as "SUPPORTED_HEADERS" ... whereas the detail above worked for me)

Sunday, August 14, 2016

How might capitalism change ?

There is an old idea, that you need two drivers for a major change to occur ... like fiber optic cable became so prevalent because of the rising requirement for telecommunications bandwidth + a coincidental/consequential spike in the price of copper.

A lot has been happening over the last 20 years to give one pause as to whether Capitalism will be the engine of our society for the next 500 years, as it has for the last.  A lot has also been written about the impact of robots to jobs, but for many of us, this seems distant.

Then you read an article like Self-Driving Cars Will Improve Our Cities.  If They Don't Ruin Them.  and you realize several things.  2020 is just around the corner, 7+ million is a lot of US jobs and that those jobs had traditionally been considered safer.  Offshoring was the usual worry, but mitigated by taste, shipping or locality issues.

The dissatisfaction seen in the current US election, suggests the popular notion that the lobbyists and politicians have messed up Capitalism and that only a businessman can get us all back to prosperity.  Rather I posit that the financial excess common at the end of any great expansion and our technical progress have wildly outrun our societal structures, that there is no usual remedy needing just a stern hand to apply.  This will become apparent as an industry the size of transportation is soon revolutionized.

Tuesday, March 1, 2016

Dartino ... overnight success?


I joined Object Technology International (OTI) in the mid 90's, leveraging my Smalltalk and engineering background, to work in their embedded Smalltalk team.  Embedded Smalltalk in those days was a pretty tough sell, but some companies realized the advantages of superior language and tooling.  Many of the target boards had only a serial link to the development machine, so the traditional compile/download/printf cycle was a lengthy, productivity sapping exercise.  Envy/Developer offered a Smalltalk IDE and runtime environment, with the ability to do remote debugging and hot code replacement.  Once the runtime was loaded onto the target, smaller application code bundles could be downloaded and executed.  A remote debugger on the development machine meant you could set breakpoints and/or single step your code to find bugs, edit the offending code, drop the stack and resume execution all from inside the debugger.

Dave Thomas, OTI founder, in a wide ranging presentation You Can’t Do That With Smalltalk! - Can You? Lessons From The Past – Challenges For The Future gives an overview of the commercial impact of Smalltalk and references some of the embedded uses, particularly the Tektronix TDS 500 Series oscilloscope built with a 68020 processor, approx 250 classes and using less than 64kB RAM.

Fast forward through the introduction of Java, the drive with J2ME to address the mobile device market, then the Android asteroid which laid waste to many existing players. Web development meanwhile was moving to Javascript and Node.js, in response to the benefits of a dynamic language and simplified deployment.  There were issues with tooling and scale, and Google released the Dart language as one answer to these challenges.  To an outsider, Dart appears a very pragmatic and tempered approach to language design, with a careful balance between performance, familiarity and feature. Several of the core team were also instrumental in the development at Animorphic Systems in 1996 of Strongtalk, an advanced, high performance Smalltalk with optional typing.

The maker movement and success of Arduino raised the profile of embedded computing, but many of the old barriers to entry remained.  Some platforms offered simplified coding and numerous libraries, but poor debug.  Others were fine for simple demands, but failed to scale.  Then Fletch was announced last year and rebranded last month as Dartino.  With a catchy name, Dartino brings Dart to the world of microcontrollers. Nicely described in a talk by Kasper Lund "Internet of Programmable Things" at the GOTO Copenhagen 2015 conference, Dartino sets out to simplify embedded development and bring an advanced language onto constrained platforms.  Take a look at: characteristics of the runtime, consideration of the subsystems, the interface between the compiler and runtime, and the introduction of Fibers.  The 32bit STM32F7 microcontroller mentioned, with 320kB RAM and clocked at 216 MHz, is certainly an interesting starting point.

Dartino has the pedigree and potential to open embedded development to a wider and younger developer audience, to compliment the hardware opportunity offered by the Raspberry Pi and more.  An overnight success? 20 years in the making.

Monday, January 11, 2016

Err ...

Time is not on our side ....

From  Arctic Melting - 2015

"
That low pressure will suck air out of the planet’s middle latitudes and send it rushing to the Arctic. And so on Wednesday, the North Pole will likely see temperatures of about 35 degrees Fahrenheit, or 2 degrees Celsius. That’s 50 degrees hotter than average: it’s usually 20 degrees Fahrenheit below zero there at this time of year.
"

and The Atlantic

"
2015 is the warmest year ever recordedThirteen of the top 14 warmest years on the books have happened this century. And here in the United States, it has been a hot, strange month. Many cities across the northeast smashed their Christmas and Christmas Eve temperature records not at midday, but at the stroke of midnight. For the hundred-plus years that New York temperatures have been recorded, the city has never been warmer than 63 degrees Fahrenheit on a December 24. Yet at 1 a.m. on Christmas Eve of this year, the thermometermeasured 67 degrees.
"


"
Some of this North American heat is a regular feature of every El Niño. (Indeed, I wrote about this El Niño-associated heat a few weeks ago.) But in the Arctic, this level of warmth is unprecedented. In order for this huge, hot storm to reach Iceland on Wednesday, it’s punching  right through the Jet Stream, the atmospheric “river” that brings temperate weather to Europe. Yet El Niño should typically reinforce this current, explains the climate writer Robert Scribbler—for the Jet Stream toweaken is a sign that something else is going on.
"

Monday, November 2, 2015

Humans Need Not Apply

I just finished reading Humans Need Not Apply: A Guide to Wealth and Work in the Age of Artificial Intelligence a fascinating book, written in a very lively manner.  It raises some issues that we will likely face in the near future (and some potential solutions).  An excellent review is available in the Economist.

Then I saw this CES2015 demonstration where a mass-market manufacturer is approaching robotic vision and scene modeling, using a Tegra K1’s 192-core GPU.  What was really striking was that at the same CES show, NVIDIA announced their Tegra X1 mobile chip which achieves 1 Teraflop performance.  It was compared to the ASCI Red, the world's fastest supercomputer until late 2000. Excluding the many issues of 16 vs 64 bit FP, architecture and memory, what is fascinating is that the ASCI Red consumed 850 kW and occupied 150 m2 of space, versus the 10W, thumbnail size of the X1.

The pieces keep falling ... maybe they will self-assemble into place.

Tuesday, October 13, 2015

Photon Wi-Fi dev kit startup

I recently went to run my Photon Wi-Fi dev kit and ran into a couple of issues that I thought I would share.


  1. I could not get the Photon to connect to my Open-Mesh network, using either the Android app or Node CLI (more on that in a moment).  Reading the online doc under WiFi Network Requirements, I noticed "The Core is 802.11b/g. Most 802.11n networks are backwards compatible with 802.11b/g, but if yours is not, the device will not connect."  I had a Netgear WGR614 available, so setup up a dedicated WPA2 network.
  2. As of this posting, there are some version compatibility issues around Node.js and serialport, which precluded me from using the Node CLI to configure the Photon network settings.  I still could not get the Android app to work.  Fortunately as described in this support issue,  you can use Putty to connect to the Photon via USB.  Enter "i" for device ID (needed to claim your device) and "w" to setup the Wi-Fi.  This just worked, no muss, no fuss.
  3. When I tried to compile the "Blink an LED" sample app, I got a useless error message.  It was late and maybe they were doing server maintenance (the compiler runs in the cloud), because a day later it compiled just fine, as expected.
Not being able to connect to the Open-Mesh network on the property is really a show stopper, as my son's Li-Po powered Slash 4x4 short course truck would only take a few seconds to exceed the WRG614 range/coverage.

Monday, July 6, 2015

Banana Republic ... revisited

Nearly thirty years ago, Paul Keating said Australia was in danger of becoming a banana republic.  Now, those worries are re-surfacing, as resource prices plunge.  "Even some low-income countries like Nepal, Kenya, and Tanzania have greater export diversity than Australia.''  I used to work in engineering in Sydney and ran overseas 28 years ago.  I feel bad about that.