Tag Archive for system

Chat system example

<?php
/**
 * Add a message to the chat
 * @param Redis $redis Redis connection handler
 * @param String $message message to add
 * @return void
 */
function addMessage($redis, $message) {
	$redis->lPush("chat", $message);
	if($redis->lLen("chat") > 100) {
		$redis->rPop("chat");
	}
}

source

AS3 MemoryMonitor

package
{
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	import flash.system.System;

	public class MemoryMonitor extends Sprite
	{
		private var memory_tf:TextField;
		private var myTimer:Timer;
		private var currMemory:Number;

		public function MemoryMonitor():void
		{
			memory_tf = new TextField();
			memory_tf.border = true;
			memory_tf.width = 100;
			memory_tf.height = 20;
			currMemory = System.totalMemory;
			
			myTimer = new Timer(0,0);			
			myTimer.addEventListener(TimerEvent.TIMER, timerHandler);
			
			addChild(memory_tf);
		}
		
		public function start(interval:Number):void
		{
			myTimer.delay = interval;
			myTimer.start();
		}

		public function stop():void
		{
			myTimer.stop();
		}
		
		public function get currentMemory():Number
		{
			return currMemory;
		}
		
		private function timerHandler(evt:TimerEvent):void
		{
			currMemory = System.totalMemory;
			memory_tf.text = String((currMemory * 0.000000954).toFixed(3)) + " kb";
		}
		
	}
}

// USAGE EXAMPLE ...
// var myMemMon = new MemoryMonitor();
// addChild(myMemMon);
// myMemMon.start(2000);

source

Print Date/Time in DOS Batch File

set DATESTAMP=%DATE:~10,4%_%DATE:~4,2%_%DATE:~7,2%

set TIMESTAMP=%TIME:~0,2%_%TIME:~3,2%_%TIME:~6,2%

set DATEANDTIME=%DATESTAMP%_%TIMESTAMP%



echo Today's date is %DATESTAMP%

echo The local time is %TIMESTAMP%

echo Date and time: %DATEANDTIME%

source

(Perl) using WMI to get system information

#!/usr/bin/perl -w

# use binperl.exe wmi.pl to run.
# have fun!
# 2009/6/17 twitter.com/vinocui
#
# useful links:
# (WMI space definition) <a href="http://msdn.microsoft.com/en-us/library/aa394084" >http://msdn.microsoft.com/en-us/library/aa394084</a>(VS.85).aspx
# (OLE usage on CPAN)    <a href="http://cpan.uwinnipeg.ca/htdocs/Win32-OLE/Win32/OLE.html#Object_methods_and_properties" >http://cpan.uwinnipeg.ca/htdocs/Win32-OLE/Win32/OLE.html#Object_methods_and_properties</a>
#

use Win32::OLE;

#my $wmi = Win32::OLE->GetObject("winmgmts://./root/cimv2") or die "failed to retrieve cimv2.";
# winmgmts means to access WMI service.

my $wmi = Win32::OLE->GetObject("WinMgmts://./root/cimv2") or die "Failed: GetObject
";
my $list, my $v;


$list = $wmi->InstancesOf("Win32_Processor") or die "Failed: InstancesOf
";

foreach $v (Win32::OLE::in $list){
print "CPU:
";
print "	", $v->{Name}, "
";
print "	", $v->{Caption}, "
";
}

$list = $wmi->InstancesOf("Win32_OperatingSystem") or die "Failed: InstancesOf
";

foreach $v (Win32::OLE::in $list){
print "OS:
";
print "	", $v->{Name}, "
";
}

0;

source

Simplify Flow Between Screens with the “Scene System”

package com.michaeljameswilliams.gamedev.gamescenes
{

/**
* Based on <a href="http://www.rivermanmedia.com/programming/6-object-oriented-game-programming-the-scene-system" >http://www.rivermanmedia.com/programming/6-object-oriented-game-programming-the-scene-system</a>
* More info: <a href="http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/" >http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/</a>
* @author MichaelJWilliams
*/

public interface IGameScene
{
/**
* Update logic for this game scene.
*/
function update():void;
/**
* Redraw everything on screen.
*/
function redraw():void;
/**
* Load all of the data and graphics that this scene needs to function.
*/
function load():void;
/**
* Unload everything that the garbage collector won't unload itself, including graphics.
*/
function unload():void;
}

}





package com.michaeljameswilliams.gamedev.gamescenes
{
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;

/**
* Based on <a href="http://www.rivermanmedia.com/programming/6-object-oriented-game-programming-the-scene-system" >http://www.rivermanmedia.com/programming/6-object-oriented-game-programming-the-scene-system</a>
* More info: <a href="http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/" >http://gamedev.michaeljameswilliams.com/2009/04/13/as3-scene-system/</a>
* Singleton template from here: <a href="http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html" >http://www.gskinner.com/blog/archives/2006/07/as3_singletons.html</a>
* @author MichaelJWilliams
*/

public class GameSceneManager {
private static var _instance:GameSceneManager;
private static var _allowInstantiation:Boolean;
private static var _currentScene:IGameScene;
private static var _sceneContainer:DisplayObjectContainer;

public static function getInstance( p_sceneContainer:DisplayObjectContainer = null ):GameSceneManager
{
if ( _instance == null )
{
_allowInstantiation = true;
_instance = new GameSceneManager();
_allowInstantiation = false;
}

if ( p_sceneContainer )
{
setSceneContainer( p_sceneContainer );
}

return _instance;
}

/**
*
* @param	p_sceneContainer	Object that is parent of each scene -- for example, the document class
*/
public function GameSceneManager():void
{
if ( !_allowInstantiation )
{
throw new Error("Error: Instantiation failed: Use GameSceneManager.getInstance() instead of new.");
}
}

public static function setSceneContainer( p_sceneContainer:DisplayObjectContainer ):void
{
_sceneContainer = p_sceneContainer;
}

/**
* Update logic for the current scene.
*/
public static function update():void
{
_currentScene.update();
}

/**
* Redraw everything on screen.
*/
public static function redraw():void
{
_currentScene.redraw();
}

/**
* A simplified way of loading and unloading scenes
* Notice that the method is static so that anyone can call it.
* @param	p_newScene	The scene to switch to.
*/
public static function changeScene( p_newScene:IGameScene ):void
{
if ( ( p_newScene is DisplayObject ) && ( _sceneContainer ) )
{
if ( _currentScene != null )
{
_sceneContainer.removeChild( _currentScene as DisplayObject );
_currentScene.unload();
}
p_newScene.load();
_currentScene = p_newScene;
_sceneContainer.addChild( _currentScene as DisplayObject );
}
else
{
throw new Error("Error: game scenes must be of type DisplayObject and scene container must be set.");
}
}
}
}

source

File System Service

<?php

class FileSystemService
{
private $filename;
private $filepath;

function __construct()
{
error_reporting ( E_ERROR | E_USER_ERROR | E_PARSE );
}

/**
* @return unknown
*/
public function getFilename()
{
return $this->filename;
}

/**
* @param unknown_type $filename
*/
public function setFilename( $filename )
{
$this->filename = $filename;
}

/**
* @return unknown
*/
public function getFilepath()
{
return $this->filepath;
}

/**
* @param unknown_type $filepath
*/
public function setFilepath( $filepath )
{
$this->filepath = $filepath;
}

public function browseDirectory( $aPath, $aResultFormat = '' )
{
$directoryHandle = opendir ( $aPath );

while ( $file = readdir ( $directoryHandle ) )
{
//loop files
$fileInfoArray[] = array(
'aFilename' => $file, 'aSize' => filesize ( $file ), 'aCreated' => filemtime ( $file ), 'aType' => filetype ( $file ), 'aDatabase' => '', 'aPermissions' => decoct ( fileperms ( $file ) ), 'isDir' => ( is_dir ( $file ) ) ? 'true' : 'false', 'isFile' => ( is_file ( $file ) ) ? 'true' : 'false', 'isReadable' => ( is_readable ( $file ) ) ? 'true' : 'false', 'isWritable' => ( is_writable ( $file ) ) ? 'true' : 'false'
);
}
closedir ( $directoryHandle );

return json_encode ( $fileInfoArray );
}

public function getDiskInfo( $aPath )
{
//disk information
$diskInfoArray = array(
'diskFreeSpace' => disk_free_space ( $aPath ), 'diskTotalSize' => disk_total_space ( $aPath )
);

return $diskInfoArray;
}

public function changePermissions( $whatDir, $aPermissions = 0755 )
{
// Everything for owner, read and execute for others
$change = chmod ( $whatDir, $aPermissions );
if ( $change )
{
return true;
}

}

public function createDirectory( $aFolder, $aPermissions = 0777 )
{
$oldmask = umask ( 0 );
$newPath = mkdir ( $aFolder, $aPermissions );

umask ( $oldmask );
if ( ! $newPath )
{
echo 'Error creating folders';
} else
{

echo 'created';
}
}

public function removeFile( $whatDir, $whatFile )
{
$filepath = "$whatDir/$whatFile";
$this->changePermissions ( $filepath, 0777 );

$removeFile = unlink ( $filepath );

if ( $removeFile )
{
return "$whatFile was removed.";
}
return 'There was a problem removing the file.';
}

public function getFormattedDate( $aFile )
{
$newDate = date ( 'j F Y H:i', $aFile );

return $newDate;
}

public function dump_var( $var )
{
print "<pre>
";
print_r ( $var );
print "</pre>
";
}
}

?>

source

CI sample models / controlers structure

/*
|MODELS
||usersdb.php
|CONTROLLERS
||users.php

the model and controller can then have different methods eg, add a user, remove a user, view all users etc.
*/


class Usersdb extends Model
{
/**
* The Initialisation method
*/

function Usersdb()
{
parent::Model();
}
function add($name)
{
...
}
function delete($userid)
{
...
}
}



class User extends Controller {

function User()
{
parent::Controller();
$this->load->model('usersdb');
$this->load->scaffolding('users');
}
function add()
{
$name = 'test';
$res = $this->usersdb->add($name);
$data['res'] = $res;
$this->load->view('users/user_add_v', $data);
}
function delete($id)
{
$res = $this->usersdb->delete($id);
$data['res'] = $res;
$this->load->view('users/user_remove_v', $data);
}




/*
so to add a user you would use the URL

whatever.com/codeigniter/index.php/user/add
*/

source

Update RubyGems to the latest version (how to upgrade the gem command)

sudo gem update --system



# OLD WAY, for historical purposes

sudo gem install rubygems-update --no-rdoc --no-ri
sudo update_rubygems

source

Monitor process id with dtruss on Leopard

sudo dtruss -fp 4149

source