Monthly Archives: January 2012

VBox : Sharing a Folder

create a folder to mount

mkdir /mnt/shared

Then mount

mount -t vboxsf rhel_shared /mnt/shared

Here rhel_shared is my folder name. (VBox gui 😉 )

Advertisements

VBox : Installing Guest Additions

Go to Devices and Install guest additions.
This will mount the drive.

go to /media/Vboxxxx

then execute

# sh ./VBoxLinuxAdditions.run

It will install the guest additions.

PHP : CDR stuff

// 28, 29, 30

	$fDate = '12/02/28';
	$fTime = '20:30';
	$tDate = '12/03/01';
	$tTime = '21:30';
	
	$from = getYMDHm($fDate, $fTime);		
	$fromTimeStamp = mktime($from['hour'], $from['minute'], $from['second'], $from['month'], $from['day'], $from['year']);
	$to = getYMDHm($tDate, $tTime);
	$toTimeStamp = mktime($to['hour'], $to['minute'], $to['second'], $to['month'], $to['day'], $to['year']);

	$_30min = array('timeUnit' => 48, 'frequency' => 1800); // 24 * 2, 60 * 30
	$_60min = array('timeUnit' => 24, 'frequency' => 3600); // 24 * 1, 60 * 30 * 2
	$_1440min = array('timeUnit' => 1, 'frequency' => 86400); // 24 * (1 / 24), 60 * 60 * 24
	
	//$cdrs = getCdrs30Minutes($fromTimeStamp, $toTimeStamp);
	//$cdrs = getCdrFileNames($fromTimeStamp, $toTimeStamp, (24 * 2), (60 * 30));
	$cdrs = getCdrFileNames($fromTimeStamp, $toTimeStamp, $_1440min['timeUnit'], $_1440min['frequency']);
	foreach ($cdrs as $cdr) {
		echo '<br />' . $cdr;
	}
	
	echo '<br />done';
	//$fromDate = date('Y/m/d H:i:s', $fromTimeStamp);	
	//$toDate = date('Y/m/d H:i:s', $toTimeStamp);	
	//echo $fromDate . '<br />';
	//echo $toDate . '<br />';
	
	function getCdrs30Minutes($from, $to) {
		$numUnits = round(($to - $from) / 86400 * 24 * 2) + 1;
		//echo '<br />' . date('Y/m/d H:i:s', $from);
		//echo '<br />' . date('Y/m/d H:i:s', $to);
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * 60 * 30)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			//echo $cdr;
		}        
		return $cdrs;
	}
	
	function getCdrs60Minutes($from, $to) {
		$numUnits = round(($to - $from) / 86400 * 24) + 1;
		//echo '<br />' . date('Y/m/d H:i:s', $from);
		//echo '<br />' . date('Y/m/d H:i:s', $to);
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * 60 * 60)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			//$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . '*';
		}        
		return $cdrs;
	}
	
	function getCdrs1440Minutes($from, $to) {
		$numUnits = round(($to - $from) / 86400) + 1;
		//echo '<br />' . date('Y/m/d H:i:s', $from);
		//echo '<br />' . date('Y/m/d H:i:s', $to);
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * 60 * 60 * 24)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			//$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . '*';
		}        
		return $cdrs;
	}
	
	function getCdrFileNames($from, $to, $timeUnit, $frequency) {
		echo "$from, $to, $timeUnit, $frequency";
		$numUnits = round(($to - $from) / 86400  * $timeUnit) + 1;
		$cdrs = array();        
		for ($i = 0; $i < $numUnits; $i++) {
			$cdr = date('Y/m/d H:i:s', ($from + ($i * $frequency)));
			$date_time = explode(" ", $cdr);
			$date = explode("/", $date_time[0]);
			$time = explode(":", $date_time[1]);
			$cdrs[] = substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
			//echo $cdr;
		}        
		return $cdrs;
	}
	
	function getYMDHm($date_, $time_) {
		$date = explode('/', $date_);
		$time = explode(':', $time_);
		// TODO: fix 20
		return array('year' => '20' . $date[0], 'month' => $date[1], 'day' => $date[2], 'hour' => $time[0], 'minute' => $time[1], 'second' => 0);
	}
	
	function getCdr($cdr) {
		$date_time = explode(" ", $cdr);
		$date = explode("/", $date_time[0]);
		$time = explode(":", $date_time[1]);
		return substr($date[0], -2) . $date[1] . $date[2] . $time[0] . $time[1];
	}

Linux : sort command

I have a file called test.txt
File content ->

1 2 3
3 2 1
2 3 1
2 1 3

If we want to sort it on the first column/field

$ sort +0 -1 test.txt
1 2 3
2 1 3
2 3 1
3 2 1

Sort on the second field/column

$ sort +1 -2 test.txt
2 1 3
1 2 3
3 2 1
2 3 1

On third column

$ sort +2 -3 test.txt
2 3 1
3 2 1
2 1 3
1 2 3

Sort on the first and the second column
2 3 1
3 2 1
2 1 3
1 2 3
We should get the 1 2 3 as the first row, because 1 2 is the lowest

$ sort +0 -2 test.txt
1 2 3
2 1 3
2 3 1
3 2 1

Sort it on second and third columns

$ sort +1 -3 test.txt
2 1 3
3 2 1
1 2 3
2 3 1

If we want to specify a separator (default space)

sort -t <separator> <file>
Tagged

SOAP : NuSOAP

Server Code

<?php
require_once ('../lib/nusoap.php');
$server = new soap_server();
$namespace = 'urn:dingdongtictic';
$server -> configureWSDL('ddtt-gen-prov', $namespace);

$server->wsdl->addComplexType(
	'parameter',
	'complexType',
	'struct',
	'sequence',
	'',
	array(
		'key' => array(
			'name' => 'key',
			'type' => 'xsd:string',
		),
		'value' => array(
			'name' => 'value',
			'type' => 'xsd:string',
		),
	)
);

$server->wsdl->addComplexType(
	'parameters',
	'complexType',
	'struct',
	'sequence',
	'',
	array(
		'param' => array(
			'name' => 'param',
			'type' => 'tns:parameter',
			'maxOccurs' => 'unbounded',
			'minOccurs' => '0',
		)
	)
);

$server->wsdl->addComplexType(
	'responseType',
	'complexType',
	'struct',
	'sequence',
	'',
	array(
		'resultCode' => array(
			'name' => 'resultCode',
			'type' => 'xsd:string',
		),
		'resultDesc' => array(
			'name' => 'resultDesc',
			'type' => 'xsd:string',
		),
	)
);

$server->register('login',                							// method name
    array('userName' => 'xsd:string', 'password' => 'xsd:string'),  // input parameters
    array('response' => 'tns:responseType'),    					// output parameters
    $namespace,                      	  							// namespace
    $namespace . '#login',                							// soapaction
    'rpc',                                							// style
    'encoded',                            							// use
    'This command will be used to get the authentication key'      	// documentation
);

$server->register('activateService',
    array('identifier' => 'xsd:string', 'paramMap' => 'tns:parameters'),
	array('response' => 'tns:responseType'),
	$namespace,
	$namespace . '#activateService',
	'rpc',
	'encoded',
	'This command will be used for initial activation & reactivation'
);

function login($username, $password) {
        return array('resultCode' => $username, 'resultDesc' => $password);
}

function activateService($identifier, $paramMap) {
		return array('resultCode' => $identifier, 'resultDesc' => print_r($paramMap, TRUE));
}

$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
$server->service($HTTP_RAW_POST_DATA);
exit();
?>

Client Code

<?php

require_once 'lib/nusoap.php';

class GenProxy {

	private static $singleton;
	private $client;
	private $wsdl_path;
	private $username;
	private $password;
	private $authKey;

	private function __construct($url) {
		$this -> wsdl_path = $url;
		$this -> client = new nusoap_client($this -> wsdl_path, true);
	}

	public static function getProxy($url) {
		if (!isset(self::$singleton)) {
			self::$singleton = new GenProxy($url);
		}
		return self::$singleton;
	}

	public function getWsdl() {
		echo $this -> wsdl_path;
	}

	// change the access modifier to private
	public function getAuthKey() {
		echo $this -> authKey;
	}

	private function debugRequest() {
		echo '<h2>Request</h2>';
		echo '<pre>' . htmlspecialchars($this -> client -> request, ENT_QUOTES) . '</pre>';
	}

	private function debugResponse() {
		echo '<h2>Response</h2>';
		echo '<pre>' . htmlspecialchars($this -> client -> response, ENT_QUOTES) . '</pre>';
	}

	private function debugResult($client, $result) {
		if ($client -> fault) {
			echo '<h2>Fault</h2><pre>';
			print_r($result);
			echo '</pre>';
		} else {
			// Check for errors
			$err = $client -> getError();
			if ($err) {
				// Display the error
				echo '<h2>Error</h2><pre>' . $err . '</pre>';
			} else {
				// Display the result
				echo '<h2>Result</h2><pre>';
				print_r($result);
				echo '</pre>';
			}
		}
	}

	public function login($username, $password) {
		$this -> username = $username;
		$this -> password = $password;
		$result = $this -> client -> call('login', array('userName' => $username, 'password' => $password));
		$this -> debugRequest();
		//$this -> debugResponse();
		// check result for errors
		// store auth key
		$this -> authKey = $result['resultCode'];
	}

	public function activate() {
		$arr = array('identifier' => '506', 'paramMap' => array('param' => array( array('key' => 'key1', 'value' => 'value1'), array('key' => 'key2', 'value' => 'value2'))));
		echo print_r($arr);
		$result = $this -> client -> call('activateService', $arr);
		$this -> debugRequest();
		$this -> debugResponse();
		//echo $result['resultDesc'];
	}

	public function deactivate() {
		echo $this -> client -> call('deactivateService', array('identifier' => '506', 'paramMap' => array('key' => 'key 1', 'value' => 'value 1')));
	}

	public function modify() {
		echo $this -> client -> call('modifyService', array('identifier' => '507', 'oldParamMap' => 'should be an array type thing', 'newParamMap' => 'should be an array type thing'));
	}

}
?>

Usage

<?php
require_once 'GenProxy.php';
$proxy = GenProxy::getProxy(<path to server wsdl>);
$proxy -> login('banda', 'bandapwd');
$proxy -> activate();
$proxy -> getAuthKey();
?>
Tagged

Extjs : Manipulating Items in Containers

Create two Windows and add a panel.

	var panelX = {
		html : 'panelX',
		id : 'panelX',
		frame : true
	}

	var parentX = new Ext.Window({
		title : 'parentX',
		id : 'parentX',
		height : 250,
		width : 250,
		items : [panelX]
	});

	var parentY = new Ext.Window({
		title : 'parentY',
		id : 'parentY',
		height : 250,
		width : 250
	});

	parentX.show();
	parentY.show();

Remove the panel from a Window and add it to the other Window.

	var panel = Ext.getCmp('panelX');
	Ext.getCmp('parentX').remove(panel, false);
	//Ext.getCmp('parentX').doLayout();
	// panel is still there in the parentX, because we passed false to remove()
	
	Ext.getCmp('parentY').add(panel);
	// Now it is in the parentY

Complete Code

Ext.onReady(function() {

	var panelX = {
		html : 'panelX',
		id : 'panelX',
		frame : true
	}

	var parentX = new Ext.Window({
		title : 'parentX',
		id : 'parentX',
		height : 250,
		width : 250,
		items : [panelX]
	});

	var parentY = new Ext.Window({
		title : 'parentY',
		id : 'parentY',
		height : 250,
		width : 250
	});

	parentX.show();
	parentY.show();

	var panel = Ext.getCmp('panelX');
	Ext.getCmp('parentX').remove(panel, false);
	// Ext.getCmp('parentX').doLayout();
	// panel is still there in the parentX, because we passed false to remove()
	
	Ext.getCmp('parentY').add(panel);
	// Now it is in the parentY
});

Another Example

Ext.onReady(function() {

	var panelX = {
		html : 'panelX',
		id : 'panelX',
		frame : true
	}

	var button = new Ext.Button({
		text : 'Knock',
		handler : function() {
			var panel = Ext.getCmp('panelX');
			Ext.getCmp('parentX').remove(panel, false);
			Ext.getCmp('parentY').add(panel);
		}
	});

	var parentX = new Ext.Window({
		title : 'parentX',
		id : 'parentX',
		height : 250,
		width : 250,
		items : [panelX, button]
	});

	var parentY = new Ext.Window({
		title : 'parentY',
		id : 'parentY',
		height : 250,
		width : 250
	});

	parentX.show();
	parentY.show();

});
Tagged ,

Extjs : Events

Software driven events

Create an object of Observable

var myObservable = new Ext.util.Observable();

Add custom events

myObservable.addEvents('sayHello');
// or
myObservable.addEvents({
  'sayHello' : true // enabled by default
});

Register event handler

myObservable.on('sayHello', function() {
  console.log('Hello stranger');
});
// myObservable.on is the shorthand for myObservable.addListener

Fire the event

myObservable.fireEvent('sayHello');

If we want to pass parameters

myObservable.addEvents('sayHello');
var sayGoodbyeFn = function(firstName, lastName) {
console.log('Goodbye ' + firstName + ' ' + lastName + '!');
}
myObservable.on('sayGoodbye', sayGoodbyeFn);
myObservable.fireEvent('sayGoodbye', 'Holly', 'Cow');

Remove listeners

myObservable.un('sayGoodbye', sayGoodbyeFn);
// myObservable.un is the shorthand for myObservable.removeListener

Originally from [ Ext JS in Action by Jesus Garcia ]


Complete Example

Ext.onReady(function() {
	Ext.define('Test', {
		extend : 'Ext.util.Observable',
		constructor : function(config) {
			this.name = config.name;
			this.addEvents({
				'test' : true
			});
			this.listeners = config.listeners;
			this.callParent(arguments);
		}
	});

	var test = new Test({
		name : 'Test X',
		listeners : {
			test : function(param_x, param_y) {
				console.log('initiating ' + this.name + ' : ' + param_x +  ': ' + param_y);
			}
		}
	});	
	test.fireEvent('test', 'x', 'y');		
});
// http://docs.sencha.com/ext-js/4-0/#!/api/Ext.util.Observable

result : initiating Test X : x : y

For more details Extjs docs

Tagged ,

MySQL : Creating a new User

Creates a new User called user1 at localhost and with the password pass1

CREATE USER 'user1'@'localhost' IDENTIFIED BY 'pass1';

Grants permission for all kind of operations on all databases.

GRANT ALL ON *.* TO 'user1'@'localhost';

Original Source @databasef1

Javascript : OOP Basics

Original Source from Mozilla Developer Network

function makePerson(first, last) {
    return {
        first: first,
        last: last,
        fullName: function() {
            return this.first + ' ' + this.last;
        },
        fullNameReversed: function() {
            return this.last + ', ' + this.first;
        }
    }
}

3WT?
Everytime we create a Person it creates two brand new function objects.

function personFullName() {
	return this.first + ' ' + this.last;
}

function personFullNameReversed() {
	return this.last + ', ' + this.first;
}

function Person(first, last) {
	this.first = first;
	this.last = last;
	this.fullName = personFullName;
	this.fullNameReversed = personFullNameReversed;
}

WCWT?
Function code is shared.

function Person(first, last) {
	this.first = first;
	this.last = last;
}
Person.prototype.fullName = function() {
	return this.first + ' ' + this.last;
}
Person.prototype.personFullNameReversed = function() {
	return this.last + ', ' + this.first;
}

WCWT?
We can add extra methods to an existing object at runtime.

Example : Adding a function at runtime.

Person.prototype.firstNameCaps = function() {
    return this.first.toUpperCase()
}

Another Example : Changing prototype.toString function

Person.prototype.toString = function() {
   return '<Person: ' + this.fullName() + '>';
}
var s = new Person("Sim", "Son");

If we print s

[object Object] – Without our new toString()
<Person: Simon Willison> – With our new toString()

Flex : MMS editor

MXML Component


<?xml version="1.0" encoding="utf-8"?>
<s:Panel xmlns:fx="http://ns.adobe.com/mxml/2009"
		 xmlns:s="library://ns.adobe.com/flex/spark"
		 xmlns:mx="library://ns.adobe.com/flex/mx"
		 title="MMS Template Editor">
	<fx:Declarations>
		<fx:XMLList id="menuXML">
			<fx:menuitem label="Template">
				<fx:menuitem label="Save" data="Save"/>
				<fx:menuitem label="Save As" data="Save As"/>
				<fx:menuitem label="Close" data="Close"/>
			</fx:menuitem>
			<fx:menuitem label="Slide">	
				<fx:menuitem label="New" data="New"/>
				<fx:menuitem label="Duplicate" data="Duplicate"/>
				<fx:menuitem label="Delete" data="Delete"/>
			</fx:menuitem>
			<fx:menuitem label="Insert">
				<fx:menuitem label="Text" data="Text"/>
				<fx:menuitem label="Image" data="Image"/>
				<fx:menuitem label="Audio" data="Audio"/>
				<fx:menuitem label="Video" data="Video"/>
			</fx:menuitem>
		</fx:XMLList>
	</fx:Declarations>
	<s:layout>
		<s:VerticalLayout paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10"/>
	</s:layout>
	
	<fx:Script>
		<![CDATA[
			import mx.controls.Alert;
			import mx.events.MenuEvent;
			
			import spark.layouts.HorizontalLayout;
			private function itemClickInfo(event:MenuEvent):void {
				if(event.label == 'New') {
					var newPanel:Panel = new Panel();
					newPanel.title = 'New Slide';
					slides.addElement(newPanel);
				}
			}
		]]>
	</fx:Script>	
	
	<mx:VDividedBox width="100%" height="100%">
		<mx:HDividedBox width="100%" height="100%">
			<s:Panel width="400" height="400" title="MMS Viewer">
			</s:Panel>		
			<s:Panel width="200" height="400" title="Property Viewer">
			</s:Panel>
		</mx:HDividedBox>	
		<s:Panel width="606" title="Slides Viewer" id="slides">
			<s:layout>
				<s:HorizontalLayout paddingBottom="4" paddingLeft="4" paddingRight="4" paddingTop="4"/>
			</s:layout>
			<s:Panel title="Slide x">				
			</s:Panel>
		</s:Panel>
	</mx:VDividedBox>
	
	<s:controlBarContent>
		<mx:MenuBar height="100%" dataProvider="{menuXML}" labelField="@label" showRoot="true" itemClick="itemClickInfo(event)" />
		<mx:HBox paddingBottom="5" paddingTop="5">
			<mx:Spacer width="100%"/>
		</mx:HBox>
	</s:controlBarContent>   
</s:Panel>

MMS Template Editor

Tagged