[Phpfreechat-svn] SF.net SVN: phpfreechat: [591] trunk/testcase
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-06-15 16:59:15
|
Revision: 591 Author: kerphi Date: 2006-06-15 09:34:28 -0700 (Thu, 15 Jun 2006) ViewCVS: http://svn.sourceforge.net/phpfreechat/?rev=591&view=rev Log Message: ----------- Work in progress (this version doesn't work with pfc commands): Optimizations work on file container, it also touches the generic interface. Modified Paths: -------------- trunk/src/containers/file.class.php trunk/testcase/container_file.php trunk/testcase/container_generic.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-06-15 14:50:17 UTC (rev 590) +++ trunk/src/containers/file.class.php 2006-06-15 16:34:28 UTC (rev 591) @@ -89,6 +89,10 @@ */ function createNick($chan, $nick, $nickid) { + // store nickid -> nickname and nickname -> nickid correspondance + $this->setMeta($nick, "nickname", "fromnickid", $nickid); + $this->setMeta($nickid, "nickid", "fromnickname", $nick); + $c =& $this->c; $nick_dir = ($chan != NULL) ? $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : @@ -99,10 +103,10 @@ if ($c->debug) { if (count($errors)>0) - pxlog("createNick(".$nick.", ".$nickid.") - Error: ".var_export($errors), "chat", $c->getId()); + pxlog("createNick(".$nick.", ".$nickid.") - Error: ".var_export($errors,true), "chat", $c->getId()); } - $nick_filename = $nick_dir."/".$this->_encode($nick); + $nickid_filename = $nick_dir."/".$nickid; //$this->_encode($nick); // check the if the file exists only in debug mode! if ($c->debug) @@ -112,19 +116,22 @@ } // trust the caller : this nick is not used - $fp = fopen($nick_filename, "w"); + touch($nickid_filename); + /* + $fp = fopen($nickid_filename, "w"); flock ($fp, LOCK_EX); // lock fwrite($fp, $nickid); flock ($fp, LOCK_UN); // unlock fclose($fp); - + */ + // append the nickname to the cached nickname list $id = $this->isNickOnline($chan, $nick); $_chan = ($chan == NULL) ? "SERVER" : $chan; if ($id<0) { - $this->_users[$_chan][] = array("nick" => $nick, - "timestamp" => filemtime($nick_filename)); + $this->_users[$_chan][] = array("nickid" => $nickid, + "timestamp" => filemtime($nickid_filename)); } return true; @@ -139,27 +146,38 @@ */ function removeNick($chan, $nick) { + // retrive the nickid to remove + $nickid = $this->getNickId($nick); + if ($nickid == "undefined") return false; + $c =& $this->c; $nick_dir = ($chan != NULL) ? $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : $c->container_cfg_server_dir."/nicknames"; - $nick_filename = $nick_dir."/".$this->_encode($nick); + $nickid_filename = $nick_dir."/".$nickid; //$this->_encode($nick); if ($c->debug) { // @todo: check if the removed nick is mine in debug mode! // check the nickname file really exists - if (!file_exists($nick_filename)) + if (!file_exists($nickid_filename)) pxlog("removeNick(".$nick.") - Error: the nickname data file to remove doesn't exists", "chat", $c->getId()); } - $ok = @unlink($nick_filename); + $ok = @unlink($nickid_filename); + // remove the user metadata if he is disconnected from the server + if ($chan == NULL) + { + $this->rmMeta("nickid", "fromnickname", $nick); + $this->rmMeta("nickname", "fromnickid", $nickid); + } + if ($c->debug) { // check the nickname file is correctly deleted - if (file_exists($nick_filename)) + if (file_exists($nickid_filename)) pxlog("removeNick(".$nick.") - Error: the nickname data file yet exists", "chat", $c->getId()); } @@ -179,6 +197,10 @@ */ function updateNick($chan, $nick) { + // retrive the nickid to update + $nickid = $this->getNickId($nick); + if ($nickid == "undefined") return false; + $c =& $this->c; $there = false; @@ -188,23 +210,23 @@ if (!is_dir($nick_dir)) mkdir_r($nick_dir); // update my online status file - $nick_filename = $nick_dir."/".$this->_encode($nick); - if (file_exists($nick_filename)) $there = true; - @touch($nick_filename); - @chmod($nick_filename, 0700); + $nickid_filename = $nick_dir."/".$nickid; //$this->_encode($nick); + if (file_exists($nickid_filename)) $there = true; + @touch($nickid_filename); + @chmod($nickid_filename, 0700); // append the nickname to the cache list $_chan = ($chan == NULL) ? "SERVER" : $chan; $id = $this->isNickOnline($chan, $nick); if ($id < 0) { - $this->_users[$_chan][] = array("nick" => $nick, - "timestamp" => filemtime($nick_filename)); + $this->_users[$_chan][] = array("nickid" => $nickid, + "timestamp" => filemtime($nickid_filename)); } else { // just update the timestamp if the nickname is allready present in the cached list - $this->_users[$_chan][$id]["timestamp"] = filemtime($nick_filename); + $this->_users[$_chan][$id]["timestamp"] = filemtime($nickid_filename); } return $there; @@ -212,25 +234,37 @@ /** * Change the user' nickname - * Notice: this call must take care to update all channels the user joined - * @param $chan where to update the nick, if null then update the server nick + * Notice: the caller will just call this function one time, this function must take care to update if necessary all channels the user joined * @param $newnick * @param $oldnick + * @return true on success, false on failure */ - function changeNick($chan, $newnick, $oldnick) + function changeNick($newnick, $oldnick) { + $oldnickid = $this->getNickId($oldnick); + $newnickid = $this->getNickId($newnick); + if ($oldnickid == "undefined") return false; // the oldnick must be connected + if ($newnickid != "undefined") return false; // the newnick must not be inuse + + $this->rmMeta("nickid", "fromnickname", $oldnick); // remove the oldnickname -> oldnickid association + $this->setMeta($newnick, "nickname", "fromnickid", $oldnickid); + $this->setMeta($oldnickid, "nickid", "fromnickname", $newnick); + + /* $c =& $this->c; - $nick_dir = ($chan != NULL) ? $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : $c->container_cfg_server_dir."/nicknames"; - $newnick_filename = $nick_dir."/".$this->_encode($newnick); - $oldnick_filename = $nick_dir."/".$this->_encode($oldnick); - + // $newnickid_filename = $nick_dir."/".$this->_encode($newnick); + $oldnickid_filename = $nick_dir."/".$oldnickid; //$this->_encode($oldnick); + $ok = @rename($oldnick_filename, $newnick_filename); + */ // update the nick cache list - if($ok) + + //if($ok) + /* { $_chan = ($chan == NULL) ? "SERVER" : $chan; $id = $this->isNickOnline($chan, $oldnick); @@ -240,49 +274,42 @@ $this->_users[$_chan][$id]["timestamp"] = filemtime($newnick_filename); } } - - return $ok; + */ + + return true; } /** - * Returns the nickid, this is a unique id used to identify a user (taken from session) - * By default this nickid is just stored into the user' metadata, same as :->getNickMeta("nickid") + * Returns the nickid corresponding to the given nickname + * The nickid is a unique id used to identify a user (generated from the browser sessionid) * @param $nick * @return string the nick id */ - function getNickId($nickname) + function getNickId($nick) { - if (!isset($this->_cache_nickid[$nickname])) - { - $c =& $this->c; - $nickid = "undefined"; - - $nick_dir = $c->container_cfg_server_dir."/nicknames"; - $nick_filename = $nick_dir."/".$this->_encode($nickname); - - if (file_exists($nick_filename)) - { - $fsize = filesize($nick_filename); - if ($fsize>0) - { - // write the nickid into the new nickname file - $fp = fopen($nick_filename, "r"); - $nickid = fread($fp, $fsize); - if ($nickid == "") $nickid = "undefined"; - fclose($fp); - } - } - $this->_cache_nickid[$nickname] = $nickid; - } - return $this->_cache_nickid[$nickname]; + $nickid = $this->getMeta("nickid", "fromnickname", $nick); + if ($nickid == NULL) $nickid = "undefined"; + return $nickid; } /** + * Returns the nickname corresponding the the given nickid + * @param $nickid + * @return string the corresponding nickname + */ + function getNickname($nickid) + { + $nick = $this->getMeta("nickname", "fromnickid", $nickid); + if ($nick == NULL) $nick = ""; + return $nick; + } + + /** * Remove (disconnect/quit) the timeouted nickname from the server or from a channel * Notice: this function must remove all nicknames which are not uptodate from the given channel or from the server * @param $chan if NULL then check obsolete nick on the server, otherwise just check obsolete nick on the given channel * @param $timeout - * @return array("nick"=>???, "timestamp"=>???) contains all disconnected nicknames and there timestamp + * @return array("nickid"=>???, "timestamp"=>???) contains all disconnected nickids and there timestamp */ function removeObsoleteNick($chan, $timeout) { @@ -303,18 +330,28 @@ $f_time = filemtime($nick_dir."/".$file); if (time() > ($f_time+$timeout/1000) ) // user will be disconnected after 'timeout' secondes of inactivity { - $deleted_user[] = array("nick" => $this->_decode($file), + $deleted_user[] = array("nickid" => $file, "timestamp" => $f_time); @unlink($nick_dir."/".$file); // disconnect expired user } else { // optimisation: cache user list for next getOnlineNick call - $users[] = array("nick" => $this->_decode($file), + $users[] = array("nickid" => $file, "timestamp" => $f_time); } } + // remove the user metadata if he is disconnected from the server + if ($chan == NULL) + { + foreach($deleted_user as $du) + { + $this->rmMeta("nickid", "fromnickname", $this->getNickname($du["nickid"])); + $this->rmMeta("nickname", "fromnickid", $du["nickid"]); + } + } + // cache the updated user list $_chan = ($chan == NULL) ? "SERVER" : $chan; $this->_users[$_chan] =& $users; @@ -325,7 +362,7 @@ /** * Returns the nickname list on the given channel or on the whole server * @param $chan if NULL then returns all connected user, otherwise just returns the channel nicknames - * @return array(array("nick"=>???,"timestamp"=>???) contains the nickname list with the associated timestamp (laste update time) + * @return array(array("nickid"=>???,"timestamp"=>???) contains the nickid list with the associated timestamp (laste update time) */ function getOnlineNick($chan) { @@ -346,7 +383,7 @@ while (false !== ($file = readdir($dir_handle))) { if ($file == "." || $file == "..") continue; // skip . and .. generic files - $users[] = array("nick" => $this->_decode($file), + $users[] = array("nickid" => $file, "timestamp" => filemtime($nick_dir."/".$file)); } @@ -363,6 +400,19 @@ */ function isNickOnline($chan, $nick) { + // @todo optimise with this piece of code + /* + $nickid = $this->getNickId($nick); + if ($nickid == "undefined") return false; + + $nick_dir = ($chan != NULL) ? + $c->container_cfg_channel_dir."/".$this->_encode($chan)."/nicknames" : + $c->container_cfg_server_dir."/nicknames"; + if (!is_dir($nick_dir)) mkdir_r($nick_dir); + + return file_exists($nick_dir."/".$nickid); + */ + // get the nickname list $_chan = ($chan == NULL) ? "SERVER" : $chan; $online_users = isset($this->_users[$_chan]) ? $this->_users[$_chan] : $this->getOnlineNick($chan); Modified: trunk/testcase/container_file.php =================================================================== --- trunk/testcase/container_file.php 2006-06-15 14:50:17 UTC (rev 590) +++ trunk/testcase/container_file.php 2006-06-15 16:34:28 UTC (rev 591) @@ -25,25 +25,7 @@ function tearDown() { pfcContainerTestcase::tearDown(); - } - - // this is a specific test for the File container - function testCreateNick_File() - { - $c =& $this->c; - $ct =& $this->ct; - $nick = $this->nick; - $nickid = $this->nickid; - $chan = $this->chan; - - $ct->createNick($chan, $nick, $nickid); - - $nick_dir = ($chan != NULL) ? $c->container_cfg_channel_dir."/".$ct->_encode($chan)."/nicknames" : $c->container_cfg_server_dir."/nicknames"; - $nick_filename = $nick_dir."/".$ct->_encode($nick); - - $this->assertTrue(file_exists($nick_filename), "nickname file doesn't exists"); - $this->assertEquals(file_get_contents($nick_filename), $nickid, "nickname file doesn't contains correct nickid"); - } + } } // on desactive le timeout car se script peut mettre bcp de temps a s'executer Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-06-15 14:50:17 UTC (rev 590) +++ trunk/testcase/container_generic.php 2006-06-15 16:34:28 UTC (rev 591) @@ -111,7 +111,7 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); sleep(2); - $ret = $this->ct->removeObsoleteNick($chan, "1000"); + $ret = $this->ct->removeObsoleteNick($chan, 1000); $this->assertEquals(count($ret), 1, "1 nickname should be obsolete"); $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); $this->assertFalse($isonline, "nickname shouldn't be online anymore"); @@ -120,7 +120,7 @@ $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); sleep(2); - $ret = $this->ct->removeObsoleteNick($chan, "1000"); + $ret = $this->ct->removeObsoleteNick($chan, 1000); $this->assertEquals(count($ret), 1, "1 nickname should be obsolete"); $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); $this->assertFalse($isonline, "nickname shouldn't be online anymore"); @@ -171,7 +171,7 @@ sleep(2); $ret = $this->ct->updateNick($chan, $nick); $this->assertTrue($ret, "nickname should be correctly updated"); - $ret = $this->ct->removeObsoleteNick($chan, "1000"); + $ret = $this->ct->removeObsoleteNick($chan, 1000); $this->assertFalse(in_array($nick, $ret), "nickname shouldn't be removed because it has been updated"); $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); $this->assertTrue($isonline, "nickname should be online"); @@ -182,7 +182,7 @@ sleep(2); $ret = $this->ct->updateNick($chan, $nick); $this->assertTrue($ret, "nickname should be correctly updated"); - $ret = $this->ct->removeObsoleteNick($chan, "1000"); + $ret = $this->ct->removeObsoleteNick($chan, 1000); $this->assertFalse(in_array($nick, $ret), "nickname shouldn't be removed because it has been updated"); $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); $this->assertTrue($isonline, "nickname should be online"); @@ -197,24 +197,14 @@ $nickid = $this->nickid; $chan = $this->chan; - // create on the channel + // create a nick on a channel and change it $this->ct->createNick($chan, $nick1, $nickid); - $ret = $this->ct->changeNick($chan, $nick2, $nick1); + $ret = $this->ct->changeNick($nick2, $nick1); $this->assertTrue($ret, "nickname change function should returns true (success)"); $isonline1 = ($this->ct->isNickOnline($chan, $nick1) >= 0); $isonline2 = ($this->ct->isNickOnline($chan, $nick2) >= 0); $this->assertFalse($isonline1, "nickname shouldn't be online"); $this->assertTrue($isonline2, "nickname shouldn't be online"); - - // create on the server - $chan = NULL; - $this->ct->createNick($chan, $nick1, $nickid); - $ret = $this->ct->changeNick($chan, $nick2, $nick1); - $this->assertTrue($ret, "nickname change function should returns true (success)"); - $isonline1 = ($this->ct->isNickOnline($chan, $nick1) >= 0); - $isonline2 = ($this->ct->isNickOnline($chan, $nick2) >= 0); - $this->assertFalse($isonline1, "nickname shouldn't be online"); - $this->assertTrue($isonline2, "nickname shouldn't be online"); } function testwrite_Generic() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |