MyThinkPond

On Java, Python, Groovy, Grails, Spring, Node.js, Linux, Arduino, ARM, Embedded Devices & Web

Hello World with SparkCore

Posted by Venkatt Guhesan on June 17, 2014

Finally got my SparkCore, an Arduino built with on-board WiFi (no need for a Arduino WiFi shield). This version is a different take on the “Internet of Things” and “Build internet-connected hardware”

Here’s a picture of the Spark connected to the WiFi running my first HelloWorld app downloaded directly from the Spark IDE to the core.

Spark Core

Looking forward to building some designs around it! To learn more about Arduino and SparkCore visit their corresponding sites.

Happy Blogging!

 

Posted in Embedded Systems, SparkCore - Arduino | Tagged: , , , | Leave a Comment »

Switching back to wordpress site

Posted by Venkatt Guhesan on June 17, 2014

For the past year, I’ve tried to use my own hosted WordPress blog but I’ve noticed that my hits seems to be much higher on WordPress hosted site than my personal site. So I’ve decided to make my switch back to the WordPress hosted blog.

So starting today, both MyThinkPond.com and MyThinkPond.wordpress.com link back to the same blog site.

 

Posted in Uncategorized | Leave a Comment »

Grails 2.3.5 – grails stop-app – does not stop the app running via run-app

Posted by Venkatt Guhesan on February 2, 2014

If you’re using Grails 2.3.X and you’re developing, most likely you’re running your app like this:

grails run-app
#in one command-prompt/shell-terminal and
grails stop-app
#in another command-prompt/shell-terminal

With the latest version of Grails (version 2.3.5), the stop-app say:

grails stop-app
| Server Stopped
# But nothing happens and the server-process continues to run#

Here’s an undocumented fix that can come in handy:

# On terminal/command-prompt #1
# Run the way you do today
grails run-app
# On terminal/command-prompt #2, change-directory (cd) to the root folder where you have your Grails project
# Create a file with a file-name ".kill-run-app"
# For Linux (*Nix) environments
touch .kill-run-app
# For Windows where you do not have 'touch' command do the following instead
echo hello > .kill-run-app
# Wait for a few seconds and Grails will kill the app that's running

Now you can resume with starting a new instance of “grails run-app”.

Cheers & Happy Coding!

Posted in Grails, Groovy, Java | Tagged: , , , , | Leave a Comment »

Grails – Adding JavaScript to bottom of page

Posted by Venkatt Guhesan on February 2, 2014

In Grails using the templating (Sitemesh) if you were to include per-page JavaScript resources then it shows up much earlier in the layout content as part of the <g:layoutBody>

Here is an example illustrating the problem:

SamplePage.gsp

<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="layoutPage"/>
<head>MyThinkPond.com Custom Page</head>
...
</head>
<body>
Some this page content
<script type="text/javascript" src="${request.contextPath}js/samplePage.js"></script>
</body>
</html>

and the layout page (layoutPage.gsp)

<!DOCTYPE html>
<html>
<head>
<title><g:layoutTitle default="MyThinkPond.com"/></title>
...
</head>
<body>
<div>
Some template (header) content
<g:layoutBody/>
</div>
<!-- Common JS Files -->
<script type="text/javascript" src="${request.contextPath}js/common.js"></script>
<!-- Begin: Custom Page JavaScript Should Go Here -->
<!-- End: Custom Page JavaScript Should Go Here -->
</body>
</html>

results in the following page in browser

<!DOCTYPE html>
<html>
<head>
<titleMyThinkPond.com Custom Page</title>
...
</head>
<body>

<div>
Some template (header) content
Some this page content
<script type="text/javascript" src="${request.contextPath}js/samplePage.js"></script>
</div>

<!-- Common JS Files -->
<script type="text/javascript" src="${request.contextPath}js/common.js"></script>
<!-- Begin: Custom Page JavaScript Should Go Here -->
<!-- End: Custom Page JavaScript Should Go Here -->
</body>
</html>

You can see that the JavaScript is included as part of the body and not at the bottom.

Here’s how you resolve this issue:

In your custom page, define a content block like this:

SamplePage.gsp

<!DOCTYPE html>
<html>
<head>
<meta name="layout" content="layoutPage"/>
<head>MyThinkPond.com Custom Page</head>
...
</head>
<body>
Some this page content
<content tag="javascript">
<script type="text/javascript" src="${request.contextPath}js/samplePage.js"></script>
</content>
</body>
</html>

In your template layout page add the content block to the bottom as needed like this:
layoutPage.gsp

<!DOCTYPE html>
<html>
<head>
<title><g:layoutTitle default="MyThinkPond.com"/></title>
...
</head>
<body>
<div>
Some template (header) content
<g:layoutBody/>
</div>
<!-- Common JS Files -->
<script type="text/javascript" src="${request.contextPath}js/common.js"></script>
<!-- Begin: Custom Page JavaScript Should Go Here -->
<g:pageProperty name="page.javascript"/>
<!-- End: Custom Page JavaScript Should Go Here -->
</body>
</html>

This will extract the JavaScript portion from samplePage and insert at the bottom of the layoutPage.

Here is the result of this magic in a page in the browser:

<!DOCTYPE html>
<html>
<head>
<titleMyThinkPond.com Custom Page</title>
...
</head>
<body>

<div>
Some template (header) content
Some this page content
</div>

<!-- Common JS Files -->
<script type="text/javascript" src="${request.contextPath}js/common.js"></script>
<!-- Begin: Custom Page JavaScript Should Go Here -->
<script type="text/javascript" src="${request.contextPath}js/samplePage.js"></script>
<!-- End: Custom Page JavaScript Should Go Here -->
</body>
</html>

You can see that the page specific JavaScript content got added towards the bottom as you intended it to be.

If this article has helped you, please add this article to your favorite social links so that others may also find this article.

Cheers & Happy Coding!

Posted in Grails, Groovy, Java | Tagged: , , , | Leave a Comment »

Grails 2.X .gitignore file

Posted by Venkatt Guhesan on November 16, 2013

Grails

With a new Grails 2.X project you run into challenges on which folders to check-in into a GIT repository. You want to remove any non-essential files that Grails can rebuild at run-time. And if you are using either GITHub or BitBucket for your GIT repo’s the default .gitignore file created or provided by GITHub is setup for configured for a Grails 1.X project and not a Grails 2.X project.

 

 

 

So here are a few simple steps to help you create the correct .gitignore file for a Grails 2.X project:

Step-1: Create the following .gitignore file under the root Grails project folder:

*.iws
*Db.properties
*Db.script
.settings
.classpath
.project
.idea
eclipse
stacktrace.log
target
target-eclipse
/plugins
/web-app/plugins
/web-app/WEB-INF/classes
web-app/WEB-INF/tld/c.tld
web-app/WEB-INF/tld/fmt.tld

Step-2: Git does not allow you to check in empty (but essential folders). To avoid this you can run the following command:

find . -type d -empty -exec touch {}/.gitignore ;

The above command creates a empty “.gitignore” file below all folders. And since you now have non-empty folders, you can now check them in into Git so that if you check-out/clone the project in the future, you will have those essential but empty folders.

If you find this article useful, Tweet me on your Twitter account or +1 me on Google-Plus so that others can also benefit from this information.

Cheers

Posted in Grails, Groovy, Java, SourceControl - GIT | Tagged: , , , , | Leave a Comment »

Cubieboard2 with ARM AllWinner Processor – A20 finally arrived

Posted by Venkatt Guhesan on November 3, 2013

Cubieboard2 with ARM AllWinner Processor – A20 finally arrived. Pretty excited about building my ARM Linux embedded system.

CubieTruck

Learn more about Cubieboard here

Posted in CubieBoard2 & CubieTruck, Embedded Systems, Linux, Technology | Tagged: , , | Leave a Comment »

Working with zeromq (0mq), Java, JZMQ on a CentOS platform

Posted by Venkatt Guhesan on June 24, 2013

Recently I decided to port some of my development using ZeroMQ onto my CentOS development machine and I ran into some challenges. I’m documenting those challenges so that if someone else runs into the same pitfalls I did, they can avoid it.

In this example today, we will work with the first “HelloWorld” examples in the ZeroMQ guide found here. I added a few modifications to the sample such as a package name and a try-catch around the Thread and an exception.tostring() to display any stack-trace.

Source code for src/zmq/hwserver.java


package zmq;

import java.io.PrintWriter;
import java.io.StringWriter;

import org.zeromq.ZMQ;

//
// Hello World server in Java
// Binds REP socket to tcp://*:5555
// Expects "Hello" from client, replies with "World"
//

public class hwserver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ZMQ.Context context = ZMQ.context(1);
		// Socket to talk to clients
		ZMQ.Socket socket = context.socket(ZMQ.REP);
		socket.bind ("tcp://*:5555");
		try {
			while (!Thread.currentThread ().isInterrupted ()) {
				byte[] reply = socket.recv(0);
				System.out.println("Received Hello");
				String request = "World" ;
				socket.send(request.getBytes (), 0);
				Thread.sleep(1000); // Do some 'work'
			}
		} catch(Exception e) {
			StringWriter sw = new StringWriter();
			PrintWriter pw = new PrintWriter(sw);
			e.printStackTrace(pw);
			System.out.println(sw.toString());
		}
		socket.close();
		context.term();

	}

}

Similarly, source code for the client, src/zmq/hwclient.java

package zmq;

import org.zeromq.ZMQ;

public class hwclient {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ZMQ.Context context = ZMQ.context(1);

		// Socket to talk to server
		System.out.println("Connecting to hello world server");

		ZMQ.Socket socket = context.socket(ZMQ.REQ);
		socket.connect ("tcp://localhost:5555");

		for(int requestNbr = 0; requestNbr != 10; requestNbr++) {
			String request = "Hello" ;
			System.out.println("Sending Hello " + requestNbr );
			socket.send(request.getBytes (), 0);

			byte[] reply = socket.recv(0);
			System.out.println("Received " + new String (reply) + " " + requestNbr);
		}

		socket.close();
		context.term();

	}

}

Now that you have the sample code, how do you compile using the ZeroMQ?

Assumption: You have installed Java (1.7 or above)

Step-1: Installing ZeroMQ onto CentOS [Following steps are performed under root account]

  1. Install “Development Tools” if it’s not already installed on your CentOS as root:        yum groupinstall “Development Tools”
  2. Download the “POSIX tarball”  ZeroMQ source code onto your CentOS development machine from here. At the time of writing this article, ZeroMQ version 3.2.3 was the stable release. You might want to download the latest stable release.
  3. Unpack the .tar.gz source archive.
  4. Run ./configure, followed by “make” then “make install“.
  5. Run ldconfig after installation.

Step-2: Installing a Language Binding for Java. In this case, we will use JZMQ from https://github.com/zeromq/jzmq

  1. Download the latest stable release from GITHub link above. (git clone git://github.com/zeromq/jzmq.git)
  2. Change directory, cd jzmq
  3. Compile and Install:
    ./autogen.sh
    ./configure
    make
    make install
    
  4. Where did it install?
    # JAR is located here: /usr/local/share/java/zmq.jar
    # .so link files are located here: /usr/local/lib
    
  5. Important Step: Add /usr/local/lib to a line in /etc/ld.so.conf (here is my copy after editing)
    include ld.so.conf.d/*.conf
    /usr/local/lib
    
  6. Reload “ldconfig“. This clears the cache.

 

Step-3: Compile and run the Java examples above.

cd ~/dev/zeromq/example/
# Compile hwserver.java
javac -classpath  /usr/local/share/java/zmq.jar ./zmq/hwserver.java
# Compile hwclient.java
javac -classpath  /usr/local/share/java/zmq.jar ./zmq/hwclient.java
# Run hwserver in a separate prompt
java -classpath .: /usr/local/share/java/zmq.jar -Djava.library.path=/usr/local/lib zmq.hwserver
# Run hwclient in a seperate prompt
java -classpath .:/usr/local/share/java/zmq.jar -Djava.library.path=/usr/local/lib zmq.hwclient

Output on the hwserver console:

Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello
Received Hello

output on the hwclient console:

Connecting to hello world server
Sending Hello 0
Received World 0
Sending Hello 1
Received World 1
Sending Hello 2
Received World 2
Sending Hello 3
Received World 3
Sending Hello 4
Received World 4
Sending Hello 5
Received World 5
Sending Hello 6
Received World 6
Sending Hello 7
Received World 7
Sending Hello 8
Received World 8
Sending Hello 9
Received World 9

Few interesting points to note are as follows:

  • What happens if you started the client first and then the server? Well, the client waits until the server becomes available (or in other words, until some process connects to socket port 5555) and then sends the message. When you say socket.send(…), ZeroMQ actually enqueues a message to be sent later by a dedicated communication thread and this thread waits until a bind on port 5555 happens by “server”.
  • Also observe that the “server” is doing the connecting, and the “client” is doing the binding.

What is ZeroMQ (ØMQ)?

(Excerpt from the ZeroMQ website!)

ØMQ (also seen as ZeroMQ, 0MQ, zmq) looks like an embeddable networking library but acts like a concurrency framework. It gives you sockets that carry atomic messages across various transports like in-process, inter-process, TCP, and multicast. You can connect sockets N-to-N with patterns like fanout, pub-sub, task distribution, and request-reply. It’s fast enough to be the fabric for clustered products. Its asynchronous I/O model gives you scalable multicore applications, built as asynchronous message-processing tasks. It has a score of language APIs and runs on most operating systems. ØMQ is from iMatix and is LGPLv3 open source.

If you find this article useful, please subscribe to my blog and/or share my link with others.

Posted in Java, ZeroMQ | Tagged: , , , , , | Leave a Comment »

Getting started with vertx (vert.io) – an alternative to node.js

Posted by Venkatt Guhesan on November 15, 2012

Article Getting started with vertx (vert.io) – an alternative to node.js – has been posted on the new website at:

http://www.mythinkpond.com/getting-started-with-vertx-vert-io-an-alternative-to-node-js/

Posted in Node JS, VertX | Tagged: , , | Leave a Comment »

Getting started with vertx (vert.io) – an alternative to node.js

Posted by Venkatt Guhesan on November 15, 2012

Lately there has been a lot of synergy and publicity around node.js (especially if you want to utilize the robust features of a JavaScript language on the server-side). If you are not familiar with this, I would suggest you look at the following link:

Node.js – Event-driven I/O server-side JavaScript environment based on V8.

If you are a Java developer and as you try out the examples, you will notice how this will be a great addition into your infrastructure. But then as you delve deeper, you may notice some of the short comings such as having to support another full stack of package manager (Node uses NPM as it’s package manager) and it has it’s own deployment, unit testing, etc. to support. And then the thought might occur that – what-if I had the same infrastructure available natively in the Java stack??? Would that not solve a lot of the logistical challenges? If your answer is “yes”, then you should look at this library…

VertX from vert.io - Effortless asynchronous application development for the modern web and enterprise

The VertX solution is built upon (and requires) a JDK/JRE under the skin. But then because Java support other languages under it’s skin, you can leverage the benefits of languages such as - JavaScript, CoffeeScript, Ruby, Python, Groovy or Java.

According to the VertX website – it says and I quote “Write your application components in JavaScript, CoffeeScript, Ruby, Python, Groovy or Java. Or mix and match several programming languages in a single app.”

Here’s their “Hello-World”:

load('vertx.js')

vertx.createHttpServer().requestHandler(function(req) {
    var file = req.path === '/' ? 'index.html' : req.path;
    req.response.sendFile('webroot/' + file);
}).listen(8080)

As always, you need to evaluate this option to see if this solution fits in your infrastructure and it’s pros and cons.

If you find this article useful, share this with others and/or subscribe to this blog.

Posted in Node JS, VertX | Tagged: , , , | Leave a Comment »

Simulating Load on a File-System

Posted by Venkatt Guhesan on November 14, 2012

load

Sometimes you want an easy way to simulate load on a file-system that you are trying to test. Here’s a quick and easy way.

Suppose your mount point you want to perform this IO is “/myspecialmount”. (Assuming you have plenty of space to test)

Then the easiest way to load some IO is through the following bash-script:

#!/bin/bash

while true
do
  echo "=== Starting clean-up ===="
  rm -fr /myspecialmount/usr
  echo "=== Starting load ===="
  rsync -avp /usr /myspecialmount
done

In the above code sample, Line-6 – cleans up the filesystem sub-folder “/myspecialmount/usr”. The options “-fr” allows you to perform the clean-up recursively with a force option. And in Line-8, we synchronize the systems “/usr” folder and files onto “/myspecialmount/usr”. And these two steps are done on an infinite-loop.

Please note that this is not a true load-testing where you have parallel-simultaneous IO requests being sent to a filesystem but rather a simple way to create some load.

There are some specialized tools such as “iozone“, “bonnie” and “dbench” and others (see Filesystems section) that are sophisticated bench-marking tools available to the Linux community.

If you find this article useful, please subscribe to my blog and/or share my link with others.

Posted in Linux | Tagged: , , , , , , | Leave a Comment »

 
Follow

Get every new post delivered to your Inbox.

Join 117 other followers