Personal Project

Monday, February 29, 2016

Development Tool for Quick-Cocos2dx-Community Lua


I have tried many IDE tools for programming Lua. Cocos IDE v1.2 only supports Quick-cocos2d v3.3 and Sublime is nothing but an editor. I want to find an IDE that could make break point for debugging and easy to use. ZeroBraneStudio is the most easiest IDE i recommend you use.

// Download ZeroBraneStudio - https://studio.zerobrane.com/
1.Extract zip ZeroBraneStudio-1.30-38


2.Execute zbstudio.exe

3.Import source codes into zbstudio

4. Add ZeroBrane Studio [ZeroBrane]/lualibs/mobdebug/mobdebug.lua)to src/

5. Add the following line of code to  coinflip/scripts/main.lua
require("mobdebug").start()
6. At ZeroBrane Studio, select Project->Start Debugger Server
7.Set up break point
8.Open main.lua by player3
9.Debug

How to buld Quick-Cocos2dx-Community for Android and iPhone ?

Build APK for Android


1.Install


    JDK  

        http://www.oracle.com/technetwork/java/javase/downloads/index.html

    Android SDK 

        http://developer.android.com/sdk/installing/index.html?pkg=tools

    Android NDK 9d

        http://dl.google.com/android/ndk/android-ndk-r9-windows-x86_64.zip

    Apache Ant

        http://ant.apache.org/bindownload.cgi



2.Configure environment 


// Change settings for JAVA_HOME, ANT_ROOT,

//  NDK_ROOT, ANDROID_NDK_ROOT

//  ANDROID_SDK_ROOT,  ANDROID_HOME

$ vim ~/.profile

export JAVA_HOME="$(/usr/libexec/java_home)"

# Add environment variable ANT_ROOT for cocos2d-x

export ANT_ROOT=/usr/local/apache-ant/bin

        

# Add environment variable NDK_ROOT for cocos2d-x

export NDK_ROOT=/Users/Tester/Desktop/cocos2dx/android/android-ndk-r9d

export PATH=$NDK_ROOT:$PATH

export ANDROID_NDK_ROOT=/Users/Tester/Desktop/cocos2dx/android/android-ndk-r9d

export PATH=$ANDROID_NDK_ROOT:$PATH

   

# Add environment variable ANDROID_SDK_ROOT for cocos2d-x

export ANDROID_SDK_ROOT=/Users/Tester/Desktop/cocos2dx/android/android-sdk-macosx

export ANDROID_HOME=/Users/Tester/Desktop/cocos2dx/android/android-sdk-macosx

export PATH=$ANDROID_HOME:$PATH

PATH=/usr/local/bin:$PATH:~/bin:~/bin/python:$ANDROID_SDK_ROOT/tools:$ANDROID_SDK_ROOT/platform-tools:$ANT_ROOT:

# Quick cocos2dx

export QUICK_V3_ROOT=`cat ~/.QUICK_V3_ROOT`


3. Create a project named “dev” by player


// Goto Android folder of dev

cd dev/frameworks/runtime-src/proj.android

// clean files

./clean.sh

// Compile  Quick-Cocos2dx-Community Engine Lib and output libcocos2dlua.so

./ build_native.sh

// Update Android Settings

./android update project -p . -t 1

// Set up properties pointing to Quick-Cocos2d-Community folder

// Relative Path

vim project.properties

android.library.reference.1=../../../../../cocos2dx/Quick-Cocos2dx-Community/cocos/platform/android/java


4. Update Android settings in Quick-cocos2dx-Community folder


 cd cocos2dx/Quick-Cocos2dx-Community/cocos/platform/android/java

./android update project -p . -t 1

5.  Build apk and debug


// Goto Android folder of dev

cd dev/frameworks/runtime-src/proj.android

// Build

// Dev mode

ant debug 

// Release

ant release

//  Clean settings

ant clean

// Deploy Apk to Android device

apk is saved in proj.android/bin/dev-debug.apk

cd proj.android/bin/

adb install dev-debug.apk


Build IPA for iPhone


// Goto iPhone project folder

cd dev/frameworks/runtime-src/proj.ios_mac/

// Open xcode project and build ipa

cd dev/frameworks/runtime-src/proj.ios_mac/


Sunday, February 28, 2016

How to setup PHP development for Couchbase ?

// Install PHP Lib for couchbase

gie clone git@github.com:couchbase/php-ext-couchbase.git


// Install couchbase.so

http://packages.couchbase.com/ubuntu/couchbase.key
wget -O http://packages.couchbase.com/ubuntu/couchbase.key
sudo apt-key add
echo "deb http://packages.couchbase.com/ubuntu trusty trusty/main" >> /etc/apt/sources.list.d/couchbase
apt-get update
sudo apt-get install libcouchbase2-core libcouchbase2-libevent libcouchbase2-devel
pecl install couchbase-1.2.5


// Setup  couchbase.so

cd /etc/php5/apache2/conf.d
ln -s ../../mods-available/couchbase.ini 30-couchbase.ini
cd /etc/php5/cli/conf.d
ln -s ../../mods-available/couchbase.ini 30-couchbase.ini


// Check command

php -i | grep ini

 

Install, Uninstall, Backup, and Restore Couchbase.

Install Couchbase

# sudo dpkg -i couchbase-server-enterprise_3.0.2-ubuntu12.04_amd64.deb 

Uninstall Couchbase

# sudo dpkg -r couchbase-server

 Backup Couchbase

# ./cbbackup http://IP Address:8091 /home/backup-dump

Restore Couchbase

#./cbrestore /home/backup-dump http://username:password@IP Address:8091 --bucket-source=default 

reference : http://docs.couchbase.com/couchbase-manual-2.5/cb-install/

Friday, February 26, 2016

How to sync time with NTP on Ubuntu ?

sudo apt-get install ntp

vim /etc/ntp.conf
server ntp.nict.jp
server ntp1.jst.mfeed.ad.jp
server ntp2.jst.mfeed.ad.jp
server ntp3.jst.mfeed.ad.jp

service ntp restart
ntpdate ntp.nict.jp


sudo dpkg-reconfigure tzdata

Monday, February 8, 2016

How to rsync local folder to remote folder ?



Rsync local folder to remote folder 


Command

rsync -avz --delete -e 'ssh -i key' localfolder username@remote ip:remote folder

Ex: 
rsync -avz --delete -e 'ssh -i server.pem' test ubuntu@192.168.0.1:/var/www/html/test/



Sunday, February 7, 2016

Install Apache, PHP, and Memcached on CentOS

Install Apache 

# yum install update 

Setup Apache


# vim /etc/httpd/conf/httpd.conf

<Directory "/var/www/html">
   AllowOverride all
#
# Controls who can get stuff from this server.
#
    Order allow,deny
    Allow from all

</Directory>

Install PHP


# yum install php

# yum install php-gd php-ldap php-odbc php-pear php-xml php-xmlrpc php-mbstring php-snmp php-soap curl curl-devel
curl-d

Check PHP

# vim /var/www/html/info.php
<?php
phpinfo();
?>

// Test Link
http://Server`s IP/info.php

Install Memcached

#yum install memcached

Start Memcached


# chkconfig --level 235 memcached on
# /etc/init.d/memcaached start
# /etc/init.d/memcached sratus

Install Memcached Perl Library

Install perl library for Memcached.
# yum install perl-Cache-Memcached

Install Memcached Python Library

Install python library for Memcached.
# yum install python-memcached

Restart Apache

Restart the Apache service to reflect changes.
# /etc/init.d/httpd restart
OR
# service httpd restart

Check Memcache


# vim /var/www/html/memtest.php

<?php
$memcache = new Memcache;
$memcache->connect('localhost', 11211) or die ("Could not connect");
$version = $memcache->getVersion();
echo "Server's version: ".$version."<br/>\n";
$tmp_object = new stdClass;
$tmp_object->str_attr = 'test';
$tmp_object->int_attr = 123;
$memcache->set('key', $tmp_object, false, 10) or die ("Failed to save data at the server");
echo "Store data in the cache (data will expire in 10 seconds)<br/>\n";
$get_result = $memcache->get('key');
echo "Data from the cache:<br/>\n";
var_dump($get_result);
?>

// Test Link
http://Server`s IP/memtest.php


Restrat Apache

# /etc/init.d/httpd restart


Thursday, February 4, 2016

How to optimize Apache, setup Memcached and PHP on Ubuntu ?

Install and setup Apache

// Install Apache2  
sudo apt-get install apache2

// Edit Config
vim /etc/apache2/apache2.conf

<IfModule mpm_prefork_module>
StartServers       10
MinSpareServers    10
MaxSpareServers   10
ServerLimit      256
MaxClients       256
MaxRequestsPerChild 10000
</IfModule>


// check module

apachectl -V | grep -i mpm


// disable event

a2dismod mpm_prefork


// Enable Prefork

a2enmod mpm_prefork

// Restart Apache

service apache restart


Install Memcahced

// Install memcached

sudo apt-get update
sudo apt-get install php5 php5-memcache
sudo apt-get install memcached
sudo apt-get install php-pear
sudo apt-get install build-essential
sudo pecl install memcache

// restart memcached

sudo service memcached restart 

You should add "extension=memcache.so" to php.ini  and restart Apache 
 

// Check Memcache
ps aux | grep memcache 

Install PHP

sudo apt-get install php5 php5-mysql
sudo apt-get install --purge mysql-client mysql-common 

// Remove MYSQL
sudo apt-get remove --purge mysql-server mysql-client mysql-common
sudo apt-get autoremove
sudo apt-get autoclean

Solutions to common problems

Problem1:  
The mcrypt extension is missing. Please check your PHP configuration.

Solution:
sudo apt-get install php5-mcrypt
sudo apt-get install mcrypt

Problem2:  
Need CURL PHP extension to connect facebook

Solution:
sudo apt-get install curl libcurl3 libcurl3-dev php5-curl

Problem3:
Echo PHP Not Support at HTML

Solution:
vim /etc/php5/apache2/php.ini
short_open_tag = On  
service apache2 stop

service apache2 start

   
chown -R www-data /var/www/html/
chmod -R u+w /var/www/html/


Tuesday, February 2, 2016

Build Quick-Cocos2dx-Lua environment on Windows and Mac

I have used Corona SDK developing games for months.  Its not free and flexible to add some native features.  Quick Cocos2ds-Lua is open source and you have more freedom to add any features.
If you want to develop a 2D game and make it time to market, Quick Cocos2ds-Lua would be the best tool to boost your development speed.

Install Quick-Cocos2dx-Community_3.6 on Windows



uzip Quick-Cocos2dx-Community_3.6_release_190dccc.zip


cd C:\Cocos2d-x\Quick-Cocos2d-x\Quick-Cocos2dx-Community


execute setup_win.bat


Execute Simulator

C:\Cocos2d-x\Quick-Cocos2d-x\Quick-Cocos2dx-Community\quick\player\proj.win32/player3.exe



Install Quick-Cocos2dx-Community_3.6 on Mac

cd /Quick-Cocos2dx-Community

execute setup_mac.sh

Execute Simulator

/Applications/player3.app


Monday, February 1, 2016

Using Amazon route 53 to create DNS failover for MYSQL

AWS Route 53 allows you to set a timer to periodically check the connection status of MYSQL via TCP or HTTP protocol. There are many approaches to achieve this, such as LAMP (PHP + Apache), Java + Tomcat, and C/C++ Sock Programming.
I used LAMP method to save time and efforts in terms of development and testing.

Create a health check  trigger by the following settings. 
IP Address : 192.168.0.1
Port  :  80
Site :  testdb.php
domain name: test.com

Create a testdb.php to check mysql connection.
If the connection is fine, then respond 200 OK.
Otherwise respond 500 error code to AWS Route 53. 


testdb.php 
<?php
/**
 * Description of Connection
 */
class Connection {
/** Instance */
private  static $_singleton = null;
// DB Settings
    private $db;
private $dbHost;
private $dbName;
private $dbUserName;
private $dbPassword;
private $dbPort = "3306";
/**
* DB Instance
*/
public static function getInstance() {
if(self::$_singleton == null) {
self::$_singleton = new Connection();
}
return self::$_singleton;
}

private function __construct(){
}
  
public function createConnection($host, $dbname, $username, $password, $port = '3306')
{
$this->dbHost = $host;
$this->dbName = $dbname;
$this->dbUserName = $username;
$this->dbPassword = $password;
try
        {
            $this->db = new PDO('mysql:host='.$this->dbHost.';port='.$this->dbPort.';dbname='.$this->dbName.'', $this->dbUserName, $this->dbPassword);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$sql = "SELECT host from user LIMIT 1";
$result = $this->db->query($sql);
foreach ($result as $row) {
echo "SUCC";
break;
}
 
$this->db = null; // close the database connection
        }
        catch (PDOException $e)
        {
            throw new Exception("Connection to database  failed. (".$e->getMessage().")");
die();
        }
    
}

}

/**
 * Initial Database Connection
 *DbManager::getInstance()->dbConnection($DBi, $db1, $DBu, $DBp)
* @param $DBi  Database server IP
* @param $db1  Database name
* @param $DBu  user name
* @param $DBp  user passwoard
*/

// DEV 
$DBi   = '127.0.0.1';
$db1   = 'mysql';
$DBu   = 'account';
$DBp   = 'password';


// Initial DB Connection
Connection::getInstance()->createConnection($DBi, $db1, $DBu, $DBp);

?>

Create a Primary hosted zone for test.com.

Now, in the Edit Record Set panel on the right side of the page, do the following:
  1. Set the TTL to 60 seconds. This limits the amount of time this DNS record will be cached within the Internets DNS system, which means that there will be a shorter delay between the time failover occurs and the time that end users begin to be routed to your backup site.
  2. Set the Routing Policy to Failover.
  3. Select Primary as the Failover Record Type.
  4. Select Yes for Associate Record Set with Health Check.
  5. Select the health check to associate with this record. In the drop-down that appears, you should see the health check we just created. Select this health check.
  6. Click Save Record Set.

Create a Slave Record Set

  1. Set the TTL to 60 seconds. This limits the amount of time this DNS record will be cached within the Internets DNS system, which means that there will be a shorter delay between the time failover occurs and the time that end users begin to be routed to your backup site.
  2. Set the Routing Policy to Failover.
  3. Select Slave as the Failover Record Type.
  4. Select NO for Associate Record Set with Health Check.
  5. Select NO for health check to associate with this record. 
  6. Click Save Record Set.

The switching time between MYSQL Master DB and Slave DB details as below.
test.com -> DB Master IP Address
DB Master  or Appache is down
test.com  --> DB Slave  IP Address
time : 30 sec 
DB Master is UP.
test.com  --> DB Master  IP Address
time : 60 sec    

DNS failover approach is not a perfect solution for MYSQL HA, because it will take at
least 60 sec to switch database. It means the system will have 60 sec of down time.
An architecture of  a load balancer and a MYSQL cluster in sync mode might be superior to this.