Sunday, August 31, 2014

AWS EC2 Creating New Instance with Image Created from Snapshot

When I tried to do this, I encountered an error (which I saw in the System Log):
error: couldn't mount because of unsupported optional features (240)

This is due to ext-fs compatibility issues, which is in-turn caused by trying to boot the server up with an incompatible kernel ID.

To fix this, go to the original instance and note down the kernel ID (be sure to take this down every time you create an AMI to be used to create instances) and use this kernel ID when you are creating the AMI.

Reference:

Wednesday, August 27, 2014

Increase Disk Size in VirtualBox

First of all if the virtual disk is using the VMDK format, it needs to be converted to the VDI format. Unfortunately, this can't really be done, at least not on the original virtual disk. You'll have to make a clone of it. After detaching the drive from the VM via the VirtualBox GUI, do this on the command line on the host system.
VBoxManage clonehd old-disk.vmdk new-disk.vdi --format VDI --variant Standard

Then, resize the clone to e.g. 100GB
VBoxManage modifyhd new-disk.vdi --resize 100000

Attach this virtual disk to the VM but at this point, the VM will not have access to the extra space. It needs to first be added as a partition and the recommended way to do this is to use the GParted live CD. Download the ISO file and then assign it as the CD to boot from in the VM. Make sure that the boot order of the VM is to boot from CD first. Both these steps are done via the VirtualBox GUI.

In GParted, assign the new space to an existing or new partition. After this, shutdown the VM, remove the CD (i.e. ISO) from the CD, and boot up the VM again.

The following steps are for CentOS users who have LVM setup by default. The next step is to configure the LVM to use the additional space for the volume. Here, I added 90GB to the LVM volume from the device /dev/sda2.
lvextend -L +90G /dev/mapper/vg_thevm-lv_root /dev/sda2

After this, the final step is to make the ext3 filesystem use the extra space.
resize2fs -p /dev/mapper/vg_thevm-lv_root

After this, do a "df" and you should see the extra disk space available.

References:

Tuesday, August 19, 2014

MongoDB Delete Items from Array

It is possible to delete items from array based on very specific criteria using $pull.

Works for both primitives in an array or complex objects in the array.

Reference: http://docs.mongodb.org/manual/reference/operator/update/pull/

Tomcat Suppress Stacktrace on Status 500 Error Page for REST Services

Whenever there is a HTTP Status 500 error, Tomcat by default displays an error page with the stacktrace on it. In production servers, this should be disabled. One common way to do this is to specify custom error pages in web.xml, like this:
<error-page>
<error-code>500</error-code>
<location>/error-500.html</location>
</error-page>

This works in most cases, but for a lot of Java REST backends, all URLs are mapped to the REST framework. For example, with Jersey, all URLs are mapped to the Servlet handling the REST requests.
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
In this situation the above solution will fail because RestServlet is unable to serve "error-500.html".

The solution is to use another Servlet to handle error paths. For static files, the "default" Servlet works. In order for Tomcat to use the default Servlet for error pages, this has to appear before the RestServlet in web.xml.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/error*</url-pattern>
</servlet-mapping>

However, there is a problem with this solution. If the original REST request is a PUT, the default Servlet will reject it with a 403. The solution is to create a custom error handling Servlet that does not process the request but instead prints out the error page depending on the error code embedded in the request URI. This is a Servlet that overrides doGet, doPost, doPut and doDelete, and within each of them, outputs the appropriate error page using out.print().

Thursday, August 14, 2014

MongoDB Sorting

Mongo DB command line:
db.collection.find().sort( { score: -1 } )

Mongo DB Java driver:
DBCursor cursor = collection.find();
cursor.sort(new BasicDBObject("score", -1));

Note that "-1" has to be an int, not a String

Reference: http://docs.mongodb.org/manual/reference/operator/meta/orderby/

MongoDB Index

Single field index: http://docs.mongodb.org/manual/core/index-single/

Compound index: http://docs.mongodb.org/manual/core/index-compound/

Thursday, August 7, 2014

Bitnami Wordpress Root

Here it is: "/opt/bitnami/apps/wordpress/"

Wordpress Pingback Attack

This attack makes use of the pingback/trackback feature to generate a lot of traffic to a target via many third-party uncompromised Wordpress servers. This is because when this request is made (via xmlrpc.php) to a server, it will attempt to download data from the target server.

These links describe the attack:
Here are some suggested solutions:

Cocos2D-X HttpClient: Adding HTTP Headers

It's done like this:
std::vector<std::string> headers;
headers.push_back("Content-Type: application/json; charset=utf-8");
request->setHeaders(headers);

Reference: http://www.snip2code.com/Snippet/15942/HTTP-Client-for-Cocos2dx

Cocos2D-X: Basic Guide to Using HttpClient

This guide is pretty much covers the basics: http://www.cocos2d-x.org/wiki/How_to_use_CCHttpClient

The only part lacking is the signature of the callback, which needs to have 2 arguments. Example:
void onHttpRequestCompleted(cocos2d::network::HttpClient* client, cocos2d::network::HttpResponse* response);

Blog Post Comparing JSON Libraries in C++

Link: http://www.thomaswhitton.com/blog/2013/06/28/json-c-plus-plus-examples/

Covers:

  • jsoncpp
  • rapidjson
  • jansson

Using Rapidjson in Cocos2D-X: Creating a JSON Document in Code and Serializing it

This is based on Cocos2D-X version 3.2

This import is necessary for creating a JSON document and manipulating it:
#include "external/json/document.h"

These imports are necessary for Serialization:
#include "external/json/writer.h"
#include "external/json/stringbuffer.h"

Example code:
rapidjson::Document jsonDoc; // create the object
jsonDoc.SetObject(); // flag it as an object rather than an array,a necessary step for Serialization

// to set a value (using Cocos2D-X String type in this example here)
String* key = “key”;
String* value = “value”
jsonDoc.AddMember(key->getCString(),value->getCString(),jsonDoc.GetAllocator());

// to Serialize to JSON String
rapidjson::StringBuffer sb;
rapidjson::Writer<rapidjson::StringBuffer> writer(sb);
jsonDoc.Accept(writer);
String* jsonString = String::create(sb.GetString());

References:

Enabling HttpClient in Cocos2D-X in Android

To do this, you'll need to uncomment the following lines in Android.mk

LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static
and
$(call import-module,network)

Otherwise you'll get error messages saying HttpClient cannot be found.

Android Error "Binary XML file line #xxx: Error inflating class "

There seems to be many causes of it but one of them seem to be out of memory error, just bad error handling and unclear error message:

References:

Cocos2D-X 9 Patch Technique with Scale9Sprite

Links:

The key thing step here is the "setCapInsets" function.

Cocos2D-X 3.0: What's New

As summarised in this blog post http://www.cocos2d-x.org/news/216 :

  • A lot of refactoring with "CC" removed from many class names, the API design being more C++ than just a straight port of Cocos2D etc
  • Huge performance improvements (or so they claim)
  • New UI widget collection
  • Many many more

Cocos2D-X 3.0 Changes Part 1

A lot of refactoring was done moving to Cocos2D-X 3.0, with a lot of renaming being done, making it difficult to map old classes to new ones. Here are some observations and notes:

  • CCObject has been removed, if you wish to create an object that supports auto-release and reference counting, use "Ref" instead.
  • CCLOG doesn't seem to work anymore (nothing showed up in Android logcat), but switching to use "log" worked fine.
  • CCString is now just "String" (this is a bit confusing as you don't exactly find a "String" class in the API references)
  • CCPoint is now Vec2
  • Generally CC has been removed from many class names, but some classes (such as those above), have been completely renamed

Wednesday, August 6, 2014

Mac OS SVN

No longer installed by default (Mac OS 10.8.X). To install, open XCode, then Preferences, go to the Downloads tab and install "Command Line Tools".

Reference: http://blog.grapii.com/2012/08/svn-missing-in-mac-os-x-10-8-mountain-lion/

Monday, August 4, 2014

Cocos2D-X Creating Solid Color Rectangular Sprite

There are times where instead of overriding the "draw" method, you need to create a sprite that is a solid rectangle. There are a few ways to do it, but the easiest for me is to create a 1x1px PNG file. The color can be changed during runtime as needed.

Reference: http://stackoverflow.com/questions/3339955/cocos2d-solid-color-rectangular-sprite

Android "gravity" vs "layout_gravity"

"gravity": affects layout of children within the view

"layout_gravity": affects the layout of the view within its parent

Reference: http://stackoverflow.com/questions/3482742/android-gravity-and-layout-gravity

Cocos2D-X Android: "Resources" vs "assets" Folders

In Cocos2D-X, the art assets are placed in a "Resources" folder that is above the project folder for every project. However, in Android, the folder designated for this is "assets", and it is inside the Android project folder.

How Cocos2D-X manages this (tested on Android ADT), is that the "Resources" folder should contain the master copy for all the assets, and that all assets should be managed from there. When the "assets" folder is refreshed, it is automatically synced with the "Resources" folder.

Cocos2D-X Android: Cocos Extension cocos-ext.h

To enable the "cocos-ext.h" Cocos Extension library, you'll need to edit the Android.mk file by uncommenting (or adding) the following lines:

LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_static
and
$(call import-module,extensions)

In your source files, include it like this:
#include "extensions/cocos-ext.h"

This is for Cocos2D-X 3.2.

Friday, August 1, 2014

Cocos2D-X Device Orientation

The configuration for this is platform-specific: http://www.cocos2d-x.org/wiki/Device_Orientation

Building a Cocos2D-X Android App

The steps here are generally correct though I had to work through some version-specific issues:
http://www.cocos2d-x.org/wiki/How_to_Build_an_Android_Project_with_Eclipse

The above link is for Cocos2D-X version 3.0 while I was using version 3.2.

Versions:

  • As for Eclipse & Android SDK it's the Android ADT 20140702 bundle
  • NDK r10
Issues:

The first issue I ran into was NDK_ROOT not found in the environment (even though it has been defined in .bash_profile). I hardcoded it into the build_native.py and it solved the problem.


NDK_ROOT="path_to_ndk"

The next issue is a CPP compile issue in the file "cocos2d/cocos/3d/CCBundleReader.cpp". I changed the return type in the header to "ssize_t" and it worked.


   /**
    * Returns the position of the file pointer.
    */
   ssize_t tell();

Lastly, the Java compiler was missing libcocos2dx.jar

I had to open the project at "cocos2d-x-3.2/cocos/platform/android/java", compile the library project and copy the libcocos2dx.jar from the "bin" folder to my project's "lib" folder.

Note: I later found out that this is because I imported the Android project only in Eclipse by selecting the Eclipse project file. If I were to select the project at the top-level folder instead, I would have been given the option of importing the libcocos2dx library as well. In which case the project would have compiled without the steps above.

After these I could build the app. The CPP compile error was a bit of a surprise thought. It shouldn't be happening.