RSS Feed
Dec 11

multi process

Posted on Sunday, December 11, 2011 in Uncategorized

http://www.php-code.net/2010/05/running-multiple-processes-in-php/
pref = 0;
$this->buffer = “”;
$this->pipes = (array)NULL;
$this->output = “”;
$this->error=”";

$this->start_time = time();
$this->timeout = 0;
}

function Create ($command) {
$t = new Thread;
$descriptor = array (0 => array (”pipe”, “r”), 1 => array (”pipe”, “w”), 2 => array (”pipe”, “w”));
//Open the resource to execute $command
$t->pref = proc_open($command,$descriptor,$t->pipes);
//Set STDOUT and STDERR to non-blocking
stream_set_blocking ($t->pipes[1], 0);
stream_set_blocking ($t->pipes[2], 0);
return $t;
}

//See if the command is still active
function isActive () {
$this->buffer .= $this->listen();
$f = stream_get_meta_data ($this->pipes[1]);
return !$f["eof"];
}

//Close the process
function close () {
$r = proc_close ($this->pref);
$this->pref = NULL;
return $r;
}

//Send a message to the command running
function tell ($thought) {
fwrite ($this->pipes[0], $thought);
}

//Get the command output produced so far
function listen () {
$buffer = $this->buffer;
$this->buffer = “”;
while ($r = fgets ($this->pipes[1], 1024)) {
$buffer .= $r;
$this->output.=$r;
}
return $buffer;
}

//Get the status of the current runing process
function getStatus(){
return proc_get_status($this->pref);
}

//See if the command is taking too long to run (more than $this->timeout seconds)
function isBusy(){
return ($this->start_time>0) && ($this->start_time+$this->timeoutpipes[2], 1024)) {
$buffer .= $r;
}
return $buffer;
}
}

//Wrapper for Thread class
class Multithread{
var $output;
var $error;
var $thread;
var $commands = array();

function __construct($commands){
$this->commands = $commands;

foreach ($this->commands as $key=>$command){
$this->thread[$key]=Thread::create($command);
}
}

function run(){
$commands = $this->commands;
//Cycle through commands
while (count($commands)>0){
foreach ($commands as $key=>$command){
//Get the output and the errors
$this->output[$key].=$this->thread[$key]->listen();
$this->error[$key].=$this->thread[$key]->getError();
//Check if command is still active
if ($this->thread[$key]->isActive()){
$this->output[$key].=$this->thread[$key]->listen();
//Check if command is busy
if ($this->thread[$key]->isBusy()){
$this->thread[$key]->close();
unset($commands[$key]);
}
} else {
//Close the command and free resources
$this->thread[$key]->close();
unset($commands[$key]);
}
}
}
return $this->output;
}
}
?>
&1′,’ffmpeg -i ‘.$inputFile[0].’ ‘.$outputFile[0].’ 2>&1′);
$threads = new Multithread($commands);
$threads->run();
foreach ($threads->commands as $key=>$command){
echo “Command “.$command.”:
“;
echo “Output “.$threads->output[$key].”
“;
echo “Error “.$threads->error[$key].”

“;
}
?>

Aug 5

Paging Class I found in PHP builder – good one

Posted on Friday, August 5, 2011 in Uncategorized


class PagedResults {

/* These are defaults */
var $TotalResults;
var $CurrentPage = 1;
var $PageVarName = "page";
var $ResultsPerPage = 20;
var $LinksPerPage = 10;

function InfoArray() {
$this->TotalPages = $this->getTotalPages();
$this->CurrentPage = $this->getCurrentPage();
$this->ResultArray = array(
"PREV_PAGE" => $this->getPrevPage(),
"NEXT_PAGE" => $this->getNextPage(),
"CURRENT_PAGE" => $this->CurrentPage,
"TOTAL_PAGES" => $this->TotalPages,
"TOTAL_RESULTS" => $this->TotalResults,
"PAGE_NUMBERS" => $this->getNumbers(),
"MYSQL_LIMIT1" => $this->getStartOffset(),
"MYSQL_LIMIT2" => $this->ResultsPerPage,
"START_OFFSET" => $this->getStartOffset(),
"END_OFFSET" => $this->getEndOffset(),
"RESULTS_PER_PAGE" => $this->ResultsPerPage,
);
return $this->ResultArray;
}

/* Start information functions */
function getTotalPages() {
/* Make sure we don't devide by zero */
if($this->TotalResults != 0 && $this->ResultsPerPage != 0) {
$result = ceil($this->TotalResults / $this->ResultsPerPage);
}
/* If 0, make it 1 page */
if(isset($result) && $result == 0) {
return 1;
} else {
return $result;
}
}

function getStartOffset() {
$offset = $this->ResultsPerPage * ($this->CurrentPage - 1);
if($offset != 0) { $offset++; }
return $offset;
}

function getEndOffset() {
if($this->getStartOffset() > ($this->TotalResults - $this->ResultsPerPage)) {
$offset = $this->TotalResults;
} elseif($this->getStartOffset() != 0) {
$offset = $this->getStartOffset() + $this->ResultsPerPage - 1;
} else {
$offset = $this->ResultsPerPage;
}
return $offset;
}

function getCurrentPage() {
if(isset($_GET[$this->PageVarName])) {
return $_GET[$this->PageVarName];
} else {
return $this->CurrentPage;
}
}

function getPrevPage() {
if($this->CurrentPage > 1) {
return $this->CurrentPage - 1;
} else {
return false;
}
}

function getNextPage() {
if($this->CurrentPage TotalPages) {
return $this->CurrentPage + 1;
} else {
return false;
}
}

function getStartNumber() {
$links_per_page_half = $this->LinksPerPage / 2;
/* See if curpage is less than half links per page */
if($this->CurrentPage TotalPages LinksPerPage) {
return 1;
/* See if curpage is greater than TotalPages minus Half links per page */
} elseif($this->CurrentPage >= ($this->TotalPages - $links_per_page_half)) {
return $this->TotalPages - $this->LinksPerPage + 1;
} else {
return $this->CurrentPage - $links_per_page_half;
}
}

function getEndNumber() {
if($this->TotalPages LinksPerPage) {
return $this->TotalPages;
} else {
return $this->getStartNumber() + $this->LinksPerPage - 1;
}
}

function getNumbers() {
for($i=$this->getStartNumber(); $igetEndNumber(); $i++) {
$numbers[] = $i;
}
return $numbers;
}

}

?>


TotalResults = 13283;

/* If you want to change options, do so like this INSTEAD of changing them directly in the class! */
$Paging->ResultsPerPage = 50;
$Paging->LinksPerPage = 20;
$Paging->PageVarName = “page”;

/* Get our array of valuable paging information! */
$InfoArray = $Paging->InfoArray();

/* Print that crap out */
print(”

"); print_r($InfoArray); print("

“);

/* Everything below here are just examples! */

/* Print our some info like “Displaying page 1 of 49″ */
echo “Displaying page ” . $InfoArray["CURRENT_PAGE"] . ” of ” . $InfoArray["TOTAL_PAGES"] . ”
“;
echo “Displaying results ” . $InfoArray["START_OFFSET"] . ” – ” . $InfoArray["END_OFFSET"] . ” of ” . $InfoArray["TOTAL_RESULTS"] . ”
“;

/* Print our first link */
if($InfoArray["CURRENT_PAGE"]!= 1) {
echo “<< “;
} else {
echo “<< “;
}

/* Print out our prev link */
if($InfoArray["PREV_PAGE"]) {
echo “Previous | “;
} else {
echo “Previous | “;
}

/* Example of how to print our number links! */
for($i=0; $i<count($InfoArray["PAGE_NUMBERS"]); $i++) {
if($InfoArray["CURRENT_PAGE"] == $InfoArray["PAGE_NUMBERS"][$i]) {
echo $InfoArray["PAGE_NUMBERS"][$i] . ” | “;
} else {
echo “” . $InfoArray["PAGE_NUMBERS"][$i] . “ | “;
}
}

/* Print out our next link */
if($InfoArray["NEXT_PAGE"]) {
echo ” Next“;
} else {
echo ” Next”;
}

/* Print our last link */
if($InfoArray["CURRENT_PAGE"]!= $InfoArray["TOTAL_PAGES"]) {
echo ” >>“;
} else {
echo ” >>”;
}

?>

Mar 29

ERROR 2002 (HY000): Can’t connect to local MySQL server

Posted on Tuesday, March 29, 2011 in Uncategorized

ERROR 2002 (HY000): Can’t connect to local MySQL server

chmod +t /tmp

cd /tmp

ln -fs /var/run/mysqld/mysql.sock

killall mysqld

/etc/init.d/mysql restart

It worked for me.

Jul 13

nokia 6080 airtel gprs , using phone as modem

Posted on Tuesday, July 13, 2010 in Uncategorized

if *99***1# is being put in the place of phone , in a dial up connection in standard modem option, it works. No need of trying to find out the modems.

even if all are bluetuth modems in the standard modem list.

DKU5 or CA-42 both should work fine. If supplied software does not work

you may download the new Nokia PC suit from web.

this much helps, speed is slow should not be compared with broadband…….

port number should be 8 or 7 as used by mobile.

Jul 18

Choosing between text editor for debian lenny and other linux distro

Posted on Saturday, July 18, 2009 in Text Editor in Debian Lenny

A Text Editor namely Kate has following features

1. File Search Folder Based with regular expressions

2. Match Bracket

3. View line numbers

Those of us programmers are searching for these facilities in a text editor for linux – debian lenny distro , may go for this editor. If this can be installed on any other linux distro it is well and good.

Bluefish another text editor for linux has above utilities It has PHP helps intigrated , in addition to some other help files

It opens up the bulk search search results files

One may choose one of the above text editors based on the needs.

Thanks

Jun 17

8139too.ko Realtek problem on debian,madriva solved

Posted on Wednesday, June 17, 2009 in debian-8139too network modules

The point i want to emphasize is that the card will work with speed as 10mbps and
duplex = full. Due to this card was failing to connect to the internet with 100mbps speed.
$ ethtool eth0 will show more details

and for dual boot PC It may be needed to unplug the lan cable and then plugin again
after 2-3 sec. (But this step is optional)

Steps to solve the 8139too.ko Realtek problem on debian,madriva

I tested like this

go to using cd
/lib/modules/2.6.26-1-686/kernel/drivers/net in debian(see in madriva where it is)

copy 8139too.ko

cp 8139too.ko /tmp/8139.ko
(in madriva it is in gz zip file, uncompress to get 8139too.ko file)

from /tmp folder

rmmod 8139too.ko
insmod 8139too.ko media=0×01

reboot the machine

______________________________________________________________

Straight forward and final solution is

open /etc/rc.local with your text editor and put one line as below and save rc.local
modprobe 8139too media=0×01;

and reboot your machine

To Remember : load module correctly with 10mbps as speed, duplex half or full will both work.


___________________________________________________
Optional steps may not be necessary.
#!/bin/sh
ETHTOOL=”/usr/sbin/ethtool”
DEV=”eth0″
SPEED=”10 duplex full”
case “$1″ in
start)
echo -n “Setting eth0 speed 10 duplex full…”;
$ETHTOOL -s $DEV speed $SPEED;
echo ” done.”;;
stop)
;;
esac
exit 0

Save and close the file. Setup executable permission:
# chmod +x /etc/init.d/10mbs OR $ sudo chmod +x  /etc/init.d/10mbs

sudo update-rc.d 100Mbs defaults

Reboot the system to take effect or just type scrit name:
# /etc/init.d/10Mbs start OR $ sudo /etc/init.d/10Mbs start

Apr 2

Zend Acl – how to

Posted on Thursday, April 2, 2009 in Uncategorized

Few points to keep in memory  “zend acl”   from zendguru posts

class My_Controller_Helper_Acl
{
public $acl;
public function __construct()
{
$this->acl = new Zend_Acl();

}

Methods to setroles();
setresources();
setprivilages();
public function setacl()
{
Zend_Registry::set(‘acl’,$this->acl);
}
}

My_Controller_Plugin_Acl which extends Zend_Controller_Plugin_Abstract

class My_Controller_Plugin_Acl extends Zend_Controller_Plugin_Abstract
{
public function preDispatch(Zend_Controller_Request_Abstract $req)
{

$acl = Zend_Registry::get(‘acl’);
to call new Zend_Session_NameSpace
get the role of users from session variable

then get role or privilage name from $request as below $privilageName=$req->getActionName();then

if(!$acl->isAllowed($roleName,null,$privilageName)){
$req->setControllerName(‘Error’);
$req->setActionName(‘index’);
}
}
}

// then front controller part

$help= new My_Controller_Helper_Acl();
The to call the methods
setroles();
setresources();
setprivilages();
setacl();

The register plugin
$front->registerPlugin(new My_Controller_Plugin_Acl());

Out of my readings from the site below
http://zendguru.wordpress.com/2008/11/05/zend-framework-acl-with-example/