Thursday, December 4, 2014

Cocos2D-X: Substring

It seems like the best way is to convert to a std::string first and use its substr function, so e.g. from a Cocos2D-X String object:
cocos2dxString->_string.substr(pos,len);

Note: for Cocos2D-X 3.0 and above

Reference: http://www.cplusplus.com/reference/string/string/substr/

Git Push Specific Branch

E.g. to push master branch only:
git push origin master


Reference: http://stackoverflow.com/questions/1947972/how-can-i-push-a-specific-branch-of-git-to-my-server

Wednesday, December 3, 2014

Cocos2D-X: Conversion Between String, std::string, and C String (i.e. char*)

"String" refers to the String class (previously CCString and analogous to the NSString in Objective-C). This is for Cocos2D-X 3.0 and above.

From C String to String, it's actually the standard constructor
String* cocos2dxString = String::create(“xxxxx”);

From String to C String
cocos2dxString->getCString();

From String to std::string
cocos2dxString->_string;

From std::string to String (this requires an indirect conversion it seems)
String::create(cocos2dxString->_string.c_str());

From std::string to C String
stdString.c_str();

Tuesday, December 2, 2014

Cocos2D-X Formatting Number to Two Decimal Places

Format with "%.2f" assuming the number is a floating point type.

Reference: http://stackoverflow.com/questions/4784336/two-decimal-places-using-printf

Cocos2D-X Wrapping Text

To do that, call the "setDimensions" function on the Label. The wrapping will be done automatically. Just ensure that there is sufficient height for the additional lines.

Gimp MacOS "Embedded Color Profile"

In summary, if you don't know otherwise, convert it.

Reference: http://docs.gimp.org/en/gimp-imaging-color-management.html

Error Unzipping File on MacOS: "Error 2 - No such file or directory"

Not sure why this happens but this means the ZIP file is corrupted.

To fix it:

zip -FF zipfile.zip --out repairedcopy.zip

Reference: http://apple.stackexchange.com/questions/135853/error-opening-a-zip-file-no-such-file-or-directory

Tuesday, November 18, 2014

Friday, November 14, 2014

Linux Enable chkconfig

After creating a script in "/etc/init.d" to start/stop/restart a service, you will still need to use chkconfig to ensure that the process starts during server bootup.

To enable chkconfig, add the line in red at the top of the script in "/etc/init.d/"
#!/bin/sh

# chkconfig: 35 90 10

start(){
       # start the service here
}

# --- snipped ---


  • "35" means the service will be activated for runlevels 3 and 5
  • "90" means that the service will be started at priority 90 (bigger is later)
  • "10" means that the service will be stopped at priority 10

Tomcat Enable Self-Signed SSL

Assuming Java tools are already on your PATH:

As root user:
keytool -genkey -alias tomcat -keyalg RSA

Then answer the questions accordingly

Reference: http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html

Linux Set Time Zone

Third method on this page: http://www.wikihow.com/Change-the-Timezone-in-Linux

e.g.


ln -sf /usr/share/zoneinfo/Singapore /etc/localtime

Thursday, November 13, 2014

Linux Putting Process Into Background

When you are running a process that takes a long time and you need to logout of the server, and if you didn't run it with the "&" at the end to run it in the background, you can still send the process to the background with the following steps:

  1. CTRL-Z (This will give you your bash prompt back)
  2. If you type "jobs" you will be able to see the list of jobs (YMMV but what I saw was my process listed as "Job 1" but it was stopped)
  3. To resume it in the background, I had to tell issue the command "bg 1" (or whatever the job number is)
  4. You can also move it back to the foreground with "fg 1"

Wednesday, November 12, 2014

Monday, November 10, 2014

Enabling CORS With Cookies on Jersey

This previous post shows how to enable basic CORS functionality in Jersey. However, to enable cookies and authentication, more needs to be done. First of all the "Access-Control-Allow-Credentials" header needs to be set, then the other issue is that a wildcard "*" cannot be used with "Access-Control-Allow-Origin". You will either have to specify in hard-code the origins that you will support, or you'll have to use the Origin in the request for this response header.

Sample code:

public class SimpleCorsResponseFilter implements ContainerResponseFilter {
   @Override
   public ContainerResponse filter(ContainerRequest req, ContainerResponse resp) {
       String requestOrigin = req.getHeaderValue("Origin");
       if (requestOrigin == null) {
           return resp;
       }
       else{
             MultivaluedMap<String, Object> headers = resp.getHttpHeaders();
             headers.add("Access-Control-Allow-Origin", requestOrigin);
             headers.add("Access-Control-Allow-Credentials", "true");
             headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
             headers.add("Access-Control-Allow-Headers", "Content-Type");
             return resp;
       }
   }
}

References (for the wildcard issue):

Thursday, November 6, 2014

AWS SES: Setting Sender Name

This is done by setting the same "source" field used for the email. Instead of using the email address by itself, include the name in this format "The Name <the@email.com>"

Reference:

Enabling CORS with Jersey

CORS is a framework for allowing a webpage from one site to call e.g. REST services on site.

In Java, there are open source frameworks that provides filters to support this easily e.g. http://software.dzhuvinov.com/cors-filter.html

However, for Jersey, it is actually very simple and these Servlet Filter based frameworks can be too overcomplicated / heavyweight for the need.

To enable CORS in Jersey is just a matter of implementing and registering a ContainerResponseFilter with the following:


public class SimpleCorsResponseFilter implements ContainerResponseFilter {
   @Override
   public ContainerResponse filter(ContainerRequest req, ContainerResponse resp) {
       MultivaluedMap<String, Object> headers = resp.getHttpHeaders();
       headers.add("Access-Control-Allow-Origin", "*");
       headers.add("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
       headers.add("Access-Control-Allow-Headers", "Content-Type");
       return resp;
   }
}

To register this with Jersey, add the following Servlet init-params in the web.xml


<init-param>
   <param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
   <param-value>xxx.yyy.zzz.SimpleCorsResponseFilter</param-value>
</init-param>

Note: code was written for Jersey version 1.17

References:

Tuesday, October 28, 2014

Fetching Large Datasets With Hibernate

The Query.list() method in Hibernate returns a List which contains all the results from the query. This works fine in most cases but if the query returns a large number of rows, memory utilisation could be an issue because the List will be a huge in-memory object.

The solution is to stream the results and scroll through them using the Query.scroll() method. For best results, the following also should be done:

  • Scroll with ScrollMode.FORWARD_ONLY
  • Use a StatelessSession (to avoid caching)
  • Set fetch size to Integer.MIN_VALUE (this one I'm not 100% sure about, but it's recommended in some blogs)
References:

Sunday, October 19, 2014

Open Source Columnar Databases

Columnar databases are actually SQL databases with a different storage format where data is organised according to columns rather than rows. This is far more optimal for fact table queries.

If cloud hosting is a the only option, then the best solution is probably Amazon Redshift. However, if you need to install and operate your own the open source options (which have not been evaluated) are:

If these are not suitable, there is also a commercial option in Infobright: https://www.infobright.com/

Wednesday, October 8, 2014

Google Drive Revoke Shares to All Documents for User


  1. From the list of folders on the left, find an item that says "All Items"
  2. Check all items
  3. More -> Share
  4. You will see the list of all users that you have shared documents with, just delete the user from the list