Thread: [Phpfreechat-svn] SF.net SVN: phpfreechat: [484] trunk/testcase
Status: Beta
Brought to you by:
kerphi
From: <ke...@us...> - 2006-04-29 17:57:29
|
Revision: 484 Author: kerphi Date: 2006-04-29 10:57:20 -0700 (Sat, 29 Apr 2006) ViewCVS: http://svn.sourceforge.net/phpfreechat/?rev=484&view=rev Log Message: ----------- optimization: add a cache for the nickname list Modified Paths: -------------- trunk/src/containers/file.class.php trunk/testcase/container_generic.php Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-04-28 22:34:41 UTC (rev 483) +++ trunk/src/containers/file.class.php 2006-04-29 17:57:20 UTC (rev 484) @@ -29,8 +29,8 @@ */ class pfcContainer_File extends pfcContainer { - // var $_users = array(); - //var $_cache_nickid = array(); + var $_users = array(); + var $_cache_nickid = array(); function pfcContainer_File(&$config) { @@ -118,10 +118,6 @@ flock ($fp, LOCK_UN); // unlock fclose($fp); - - // if (!in_array($nickname, $this->_users)) - // $this->_users[] = $nickname; // _users will be used by getOnlineUserList - return true; } @@ -150,14 +146,13 @@ @unlink($nick_filename); - /* // remove the nickname from the cache list - if (in_array($nick, $this->_users)) + if (isset($this->_users[$chan]) && + in_array($nick, $this->_users[$chan])) { - $key = array_search($nick, $this->_users); - unset($this->_users[$key]); + $key = array_search($nick, $this->_users[$chan]); + unset($this->_users[$chan][$key]); } - */ return true; } @@ -182,6 +177,13 @@ if (file_exists($nick_filename)) $there = true; @touch($nick_filename); @chmod($nick_filename, 0777); + + // append the nickname to the cache list + if (isset($this->_users[$chan]) && + !in_array($nick, $this->_users[$chan])) + { + $this->_users[$chan][] = $nick; + } return $there; } @@ -204,6 +206,20 @@ $oldnick_filename = $nick_dir."/".$this->_encode($oldnick); $ok = @rename($oldnick_filename, $newnick_filename); + + // update the nick cache list + if($ok) + { + if (isset($this->_users[$chan]) && + in_array($oldnick, $this->_users[$chan])) + { + // remove the oldnick from the cache + $key = array_search($oldnick, $this->_users[$chan]); + unset($this->_users[$chan][$key]); + // append the new nick to the cache + $this->_users[$chan][] = $newnick; + } + } return $ok; } @@ -216,33 +232,31 @@ */ function getNickId($nickname) { - //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)) + if (!isset($this->_cache_nickid[$nickname])) { - $fsize = filesize($nick_filename); - if ($fsize>0) + $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)) { - // write the nickid into the new nickname file - $fp = fopen($nick_filename, "r"); - $nickid = fread($fp, $fsize); - if ($nickid == "") $nickid = "undefined"; - fclose($fp); + $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; } - //$this->_cache_nickid[$nickname] = $nickid; - //if ($c->debug) pxlog("getNickId[".$c->sessionid."]: nickname=".$nickname." nickid=".$nickid, "chat", $c->getId()); - //} - return $nickid; //$this->_cache_nickid[$nickname]; + return $this->_cache_nickid[$nickname]; } - /** * 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 @@ -278,7 +292,8 @@ } } - // $this->_users =& $users; // _users will be used by getOnlineUserList + // cache the updated user list + $this->_users[$chan] =& $users; return $deleted_user; } @@ -290,8 +305,10 @@ */ function getOnlineNick($chan) { - // if (is_array($this->_users)) - // return $this->_users; + // return the cached user list if it exists + if (isset($this->_users[$chan]) && + is_array($this->_users[$chan])) + return $this->_users[$chan]; $c =& $this->c; @@ -306,6 +323,10 @@ if ($file == "." || $file == "..") continue; // skip . and .. generic files $users[] = $this->_decode($file); } + + // cache the user list + $this->_users[$chan] =& $users; + return $users; } Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-04-28 22:34:41 UTC (rev 483) +++ trunk/testcase/container_generic.php 2006-04-29 17:57:20 UTC (rev 484) @@ -177,27 +177,27 @@ // on the channel $this->ct->createNick($chan, $nick, $nickid); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); + $this->assertTrue(in_array($nick, $online_nick), "1-nickname should be online on the channel"); sleep(2); $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "nickname should be correctly updated on the channel"); + $this->assertTrue($ret, "2-nickname should be correctly updated on the channel"); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertFalse(in_array($nick, $ret), "nickname should not be removed from the channel because it has been updated"); + $this->assertFalse(in_array($nick, $ret), "3-nickname should not be removed from the channel because it has been updated"); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the channel"); + $this->assertTrue(in_array($nick, $online_nick), "4-nickname should be online on the channel"); // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); + $this->assertTrue(in_array($nick, $online_nick), "5-nickname should be online on the server"); sleep(2); $ret = $this->ct->updateNick($chan, $nick); - $this->assertTrue($ret, "nickname should be correctly updated on the server"); + $this->assertTrue($ret, "6-nickname should be correctly updated on the server"); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertFalse(in_array($nick, $ret), "nickname should not be removed from the server because it has been updated"); + $this->assertFalse(in_array($nick, $ret), "7-nickname should not be removed from the server because it has been updated"); $online_nick = $this->ct->getOnlineNick($chan); - $this->assertTrue(in_array($nick, $online_nick), "nickname should be online on the server"); + $this->assertTrue(in_array($nick, $online_nick), "8-nickname should be online on the server"); } function testchangeNick_Generic() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <ke...@us...> - 2006-05-12 14:06:44
|
Revision: 493 Author: kerphi Date: 2006-05-12 07:06:35 -0700 (Fri, 12 May 2006) ViewCVS: http://svn.sourceforge.net/phpfreechat/?rev=493&view=rev Log Message: ----------- Container api modification: removeObsoleteNick now returns the disconnected users nicknames and their associated timestamp. Modified Paths: -------------- trunk/src/commands/getonlinenick.class.php trunk/src/containers/file.class.php trunk/testcase/container_generic.php Modified: trunk/src/commands/getonlinenick.class.php =================================================================== --- trunk/src/commands/getonlinenick.class.php 2006-05-12 13:55:14 UTC (rev 492) +++ trunk/src/commands/getonlinenick.class.php 2006-05-12 14:06:35 UTC (rev 493) @@ -14,7 +14,7 @@ foreach ($disconnected_users as $u) { $cmd =& pfcCommand::Factory("notice"); - $cmd->run($xml_reponse, $clientid, _pfc("%s quit (timeout)",$u), $sender, $recipient, $recipientid, 2); + $cmd->run($xml_reponse, $clientid, _pfc("%s quit (timeout)",$u["nick"]), $sender, $recipient, $recipientid, 2); } // get the cached nickname list Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-05-12 13:55:14 UTC (rev 492) +++ trunk/src/containers/file.class.php 2006-05-12 14:06:35 UTC (rev 493) @@ -281,7 +281,7 @@ * 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() contains all disconnected nicknames + * @return array("nick"=>???, "timestamp"=>???) contains all disconnected nicknames and there timestamp */ function removeObsoleteNick($chan, $timeout) { @@ -299,16 +299,18 @@ while (false !== ($file = readdir($dir_handle))) { if ($file == "." || $file == "..") continue; // skip . and .. generic files - if (time() > (filemtime($nick_dir."/".$file)+$timeout/1000) ) // user will be disconnected after 'timeout' secondes of inactivity + $f_time = filemtime($nick_dir."/".$file); + if (time() > ($f_time+$timeout/1000) ) // user will be disconnected after 'timeout' secondes of inactivity { - $deleted_user[] = $this->_decode($file); - unlink($nick_dir."/".$file); // disconnect expired user + $deleted_user[] = array("nick" => $this->_decode($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), - "timestamp" => filemtime($nick_dir."/".$file)); + "timestamp" => $f_time); } } Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-05-12 13:55:14 UTC (rev 492) +++ trunk/testcase/container_generic.php 2006-05-12 14:06:35 UTC (rev 493) @@ -113,18 +113,18 @@ $this->ct->createNick($chan, $nick, $nickid); sleep(2); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertTrue(in_array($nick, $ret), "nickname should be removed from the channel"); + $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 on the channel"); + $this->assertFalse($isonline, "nickname shouldn't be online anymore"); // on the server $chan = NULL; $this->ct->createNick($chan, $nick, $nickid); sleep(2); $ret = $this->ct->removeObsoleteNick($chan, "1000"); - $this->assertTrue(in_array($nick, $ret), "nickname should be removed from the server"); + $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 on the server"); + $this->assertFalse($isonline, "nickname shouldn't be online anymore"); } function testSetGetRmMeta_Generic() @@ -138,25 +138,25 @@ // set / get $this->ct->setMeta($nickid, "key1", "nickname", $nick); $metadata = $this->ct->getMeta("key1", "nickname", $nick); - $this->assertEquals($nickid, $metadata, "1-metadata value is not correct"); + $this->assertEquals($nickid, $metadata, "metadata value is not correct"); // set / rm / get $this->ct->setMeta($nickid, "key2", "nickname", $nick); $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertEquals($nickid, $metadata, "2-metadata value is not correct"); + $this->assertEquals($nickid, $metadata, "metadata value is not correct"); $this->ct->rmMeta("key2", "nickname", $nick); $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertNull($metadata, "3-metadata should not exists anymore"); + $this->assertNull($metadata, "metadata should not exists anymore"); // set / rm (all) / get $this->ct->setMeta($nickid, "key2", "nickname", $nick); $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertEquals($nickid, $metadata, "4-metadata value is not correct"); + $this->assertEquals($nickid, $metadata, "metadata value is not correct"); $this->ct->rmMeta(NULL, "nickname", $nick); $metadata = $this->ct->getMeta("key2", "nickname", $nick); - $this->assertNull($metadata, "5-metadata should not exists anymore"); + $this->assertNull($metadata, "metadata should not exists anymore"); $metadata = $this->ct->getMeta("key1", "nickname", $nick); - $this->assertNull($metadata, "6-metadata should not exists anymore"); + $this->assertNull($metadata, "metadata should not exists anymore"); } function testupdateNick_Generic() This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
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. |
From: <ke...@us...> - 2006-06-15 20:21:36
|
Revision: 592 Author: kerphi Date: 2006-06-15 13:21:26 -0700 (Thu, 15 Jun 2006) ViewCVS: http://svn.sourceforge.net/phpfreechat/?rev=592&view=rev Log Message: ----------- Finish the optimization work on the default file container. All the concerned commands has been updated. Modified Paths: -------------- trunk/src/commands/connect.class.php trunk/src/commands/getonlinenick.class.php trunk/src/commands/nick.class.php trunk/src/commands/send.class.php trunk/src/containers/file.class.php trunk/testcase/container_generic.php Modified: trunk/src/commands/connect.class.php =================================================================== --- trunk/src/commands/connect.class.php 2006-06-15 16:34:28 UTC (rev 591) +++ trunk/src/commands/connect.class.php 2006-06-15 20:21:26 UTC (rev 592) @@ -19,7 +19,7 @@ { // check if the user is alone on the server, and give it the admin status if yes $users = $container->getOnlineNick(NULL); - if (count($users) == 0) $isadmin = true; + if (count($users["nickid"]) == 0) $isadmin = true; } // setup some user meta Modified: trunk/src/commands/getonlinenick.class.php =================================================================== --- trunk/src/commands/getonlinenick.class.php 2006-06-15 16:34:28 UTC (rev 591) +++ trunk/src/commands/getonlinenick.class.php 2006-06-15 20:21:26 UTC (rev 592) @@ -11,10 +11,10 @@ // take care to disconnect timeouted users on this channel $disconnected_users = $container->removeObsoleteNick($recipient,$c->timeout); - foreach ($disconnected_users as $u) + foreach ($disconnected_users["nickid"] as $nid) { $cmd =& pfcCommand::Factory("notice"); - $cmd->run($xml_reponse, $clientid, _pfc("%s quit (timeout)",$u["nick"]), $sender, $recipient, $recipientid, 2); + $cmd->run($xml_reponse, $clientid, _pfc("%s quit (timeout)",$container->getNickname($nid)), $sender, $recipient, $recipientid, 2); } // get the cached nickname list @@ -23,26 +23,30 @@ // get the real nickname list $users = $container->getOnlineNick($recipient); - sort($users); - // check if the nickname list must be updated - if ($oldnicklist != $users) + if ($oldnicklist != $users["nickid"]) // check if the nickname list must be updated on the client side { + $_SESSION[$nicklist_sid] = $users["nickid"]; + + // sort the nicknames + $nicklist = array(); + foreach($users["nickid"] as $nid) + $nicklist[] = $container->getNickname($nid); + sort($nicklist); + if ($c->debug) { - $nicklist = array(); foreach($users as $u) $nicklist[] = $u["nick"]; $nicklist = implode(",",$nicklist); + $nicklist = implode(",",$nicklist); pxlog("/getonlinenick (nicklist updated - nicklist=".$nicklist.")", "chat", $c->getId()); } - $_SESSION[$nicklist_sid] = $users; - + // build and send the nickname list $js = ""; - foreach ($users as $u) + foreach ($nicklist as $nick) { - $nickname = addslashes($u["nick"]); // must escape ' charactere for javascript string + $nickname = addslashes($nick); // must escape ' charactere for javascript string $js .= "'".$nickname."',"; } - $js = substr($js, 0, strlen($js)-1); // remove last ',' - + $js = substr($js, 0, strlen($js)-1); // remove last ',' $xml_reponse->addScript("pfc.updateNickList('".$recipientid."',Array(".$js."));"); } Modified: trunk/src/commands/nick.class.php =================================================================== --- trunk/src/commands/nick.class.php 2006-06-15 16:34:28 UTC (rev 591) +++ trunk/src/commands/nick.class.php 2006-06-15 20:21:26 UTC (rev 592) @@ -34,13 +34,13 @@ // 'BoB' and 'bob' must be considered same nicknames $nick_in_use = false; $online_users = $container->getOnlineNick(NULL); - foreach($online_users as $ou) + foreach($online_users["nickid"] as $nid) { - if (preg_match("/^".preg_quote($ou["nick"])."$/i",$newnick)) + if (preg_match("/^".preg_quote($container->getNickname($nid))."$/i",$newnick)) { // the nick match // just allow the owner to change his capitalised letters - if ($container->getNickId($ou["nick"]) != $oldnickid) + if ($nid != $oldnickid) $nick_in_use = true; } } @@ -55,14 +55,11 @@ $oldnick != $newnick && $oldnick != "") { // really change the nick (rename it) - $container->changeNick(NULL, $newnick, $oldnick); - foreach($u->channels as $chan) - $container->changeNick($chan["recipient"], $newnick, $oldnick); - foreach( $u->privmsg as $pv ) - $container->changeNick($pv["recipient"], $newnick, $oldnick); + $container->changeNick($newnick, $oldnick); $u->nick = $newnick; $u->saveInCache(); + // notify all the joined channels/privmsg $cmd =& pfcCommand::Factory("notice"); foreach($u->channels as $id => $chan) $cmd->run($xml_reponse, $clientid, _pfc("%s changes his nickname to %s",$oldnick,$newnick), $sender, $chan["recipient"], $id, 1); Modified: trunk/src/commands/send.class.php =================================================================== --- trunk/src/commands/send.class.php 2006-06-15 16:34:28 UTC (rev 591) +++ trunk/src/commands/send.class.php 2006-06-15 20:21:26 UTC (rev 592) @@ -22,18 +22,15 @@ $can_send = true; if (isset($u->privmsg[$recipientid])) { - $pvnick = $u->privmsg[$recipientid]["name"]; - // now check if this user is currently online $container =& $c->getContainerInstance(); + $pvnick = $u->privmsg[$recipientid]["name"]; + $pvnickid = $container->getNickId($pvnick); + + // now check if this user is currently online $onlineusers = $container->getOnlineNick(NULL); - $uid = 0; $isonline = false; - while($uid < count($onlineusers) && !$isonline) + if (!in_array($pvnickid, $onlineusers["nickid"])) { - if ($onlineusers[$uid]["nick"] == $pvnick) $isonline = true; - $uid++; - } - if (!$isonline) - { + // send an error because the user is not online $cmd =& pfcCommand::Factory("error"); $cmd->run($xml_reponse, $clientid, _pfc("Can't send the message, %s is offline", $pvnick)); $can_send = false; Modified: trunk/src/containers/file.class.php =================================================================== --- trunk/src/containers/file.class.php 2006-06-15 16:34:28 UTC (rev 591) +++ trunk/src/containers/file.class.php 2006-06-15 20:21:26 UTC (rev 592) @@ -29,8 +29,8 @@ */ class pfcContainer_File extends pfcContainer { - var $_users = array(); - var $_cache_nickid = array(); + var $_users = array("nickid" => array(), + "timestamp" => array()); function pfcContainer_File(&$config) { @@ -130,8 +130,8 @@ $_chan = ($chan == NULL) ? "SERVER" : $chan; if ($id<0) { - $this->_users[$_chan][] = array("nickid" => $nickid, - "timestamp" => filemtime($nickid_filename)); + $this->_users[$_chan]["nickid"][] = $nickid; + $this->_users[$_chan]["timestamp"][] = filemtime($nickid_filename); } return true; @@ -184,7 +184,11 @@ // remove the nickname from the cache list $id = $this->isNickOnline($chan, $nick); $_chan = ($chan == NULL) ? "SERVER" : $chan; - if ($id >= 0) unset($this->_users[$_chan][$id]); + if ($id >= 0) + { + unset($this->_users[$_chan]["nickid"][$id]); + unset($this->_users[$_chan]["timestamp"][$id]); + } return $ok; } @@ -220,13 +224,13 @@ $id = $this->isNickOnline($chan, $nick); if ($id < 0) { - $this->_users[$_chan][] = array("nickid" => $nickid, - "timestamp" => filemtime($nickid_filename)); + $this->_users[$_chan]["nickid"][] = $nickid; + $this->_users[$_chan]["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($nickid_filename); + $this->_users[$_chan]["timestamp"][$id] = filemtime($nickid_filename); } return $there; @@ -309,7 +313,7 @@ * 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("nickid"=>???, "timestamp"=>???) contains all disconnected nickids and there timestamp + * @return array("nickid"=>array("nickid1", ...),"timestamp"=>array(timestamp1, ...)) contains all disconnected nickids and there timestamp */ function removeObsoleteNick($chan, $timeout) { @@ -322,7 +326,7 @@ $errors = @test_writable_dir($nick_dir, $chan."/nicknames"); $deleted_user = array(); - $users = array(); + $online_user = array(); $dir_handle = opendir($nick_dir); while (false !== ($file = readdir($dir_handle))) { @@ -330,31 +334,31 @@ $f_time = filemtime($nick_dir."/".$file); if (time() > ($f_time+$timeout/1000) ) // user will be disconnected after 'timeout' secondes of inactivity { - $deleted_user[] = array("nickid" => $file, - "timestamp" => $f_time); + $deleted_user["nickid"][] = $file; + $deleted_user["timestamp"][] = $f_time; @unlink($nick_dir."/".$file); // disconnect expired user } else { // optimisation: cache user list for next getOnlineNick call - $users[] = array("nickid" => $file, - "timestamp" => $f_time); + $online_user["nickid"][] = $file; + $online_user["timestamp"][] = $f_time; } } // remove the user metadata if he is disconnected from the server - if ($chan == NULL) + if ($chan == NULL && isset($deleted_user["nickid"])) { - foreach($deleted_user as $du) + foreach($deleted_user["nickid"] as $du_nid) { - $this->rmMeta("nickid", "fromnickname", $this->getNickname($du["nickid"])); - $this->rmMeta("nickname", "fromnickid", $du["nickid"]); + $this->rmMeta("nickid", "fromnickname", $this->getNickname($du_nid)); + $this->rmMeta("nickname", "fromnickid", $du_nid); } } // cache the updated user list $_chan = ($chan == NULL) ? "SERVER" : $chan; - $this->_users[$_chan] =& $users; + $this->_users[$_chan] =& $online_user; return $deleted_user; } @@ -362,7 +366,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("nickid"=>???,"timestamp"=>???) contains the nickid list with the associated timestamp (laste update time) + * @return array("nickid"=>array("nickid1", ...),"timestamp"=>array(timestamp1, ...)) contains the nickid list with the associated timestamp (laste update time) */ function getOnlineNick($chan) { @@ -378,17 +382,17 @@ $c->container_cfg_server_dir."/nicknames"; if (!is_dir($nick_dir)) mkdir_r($nick_dir); - $users = array(); + $online_user = array(); $dir_handle = opendir($nick_dir); while (false !== ($file = readdir($dir_handle))) { if ($file == "." || $file == "..") continue; // skip . and .. generic files - $users[] = array("nickid" => $file, - "timestamp" => filemtime($nick_dir."/".$file)); + $online_user["nickid"][] = $file; + $online_user["timestamp"][] = filemtime($nick_dir."/".$file); } // cache the user list - $this->_users[$_chan] =& $users; + $this->_users[$_chan] =& $online_user; return $this->_users[$_chan]; } @@ -412,16 +416,19 @@ return file_exists($nick_dir."/".$nickid); */ + + $nickid = $this->getNickId($nick); // get the nickname list $_chan = ($chan == NULL) ? "SERVER" : $chan; - $online_users = isset($this->_users[$_chan]) ? $this->_users[$_chan] : $this->getOnlineNick($chan); - + $online_user = isset($this->_users[$_chan]) ? $this->_users[$_chan] : $this->getOnlineNick($chan); + $uid = 0; $isonline = false; - while($uid < count($online_users) && !$isonline) + if (!isset($online_user["nickid"])) return -1; + while($uid < count($online_user["nickid"]) && !$isonline) { - if ($online_users[$uid]["nick"] == $nick) + if ($online_user["nickid"][$uid] == $nickid) $isonline = true; else $uid++; Modified: trunk/testcase/container_generic.php =================================================================== --- trunk/testcase/container_generic.php 2006-06-15 16:34:28 UTC (rev 591) +++ trunk/testcase/container_generic.php 2006-06-15 20:21:26 UTC (rev 592) @@ -112,7 +112,7 @@ $this->ct->createNick($chan, $nick, $nickid); sleep(2); $ret = $this->ct->removeObsoleteNick($chan, 1000); - $this->assertEquals(count($ret), 1, "1 nickname should be obsolete"); + $this->assertEquals(count($ret["nickid"]), 1, "1 nickname should be obsolete"); $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); $this->assertFalse($isonline, "nickname shouldn't be online anymore"); @@ -121,7 +121,7 @@ $this->ct->createNick($chan, $nick, $nickid); sleep(2); $ret = $this->ct->removeObsoleteNick($chan, 1000); - $this->assertEquals(count($ret), 1, "1 nickname should be obsolete"); + $this->assertEquals(count($ret["nickid"]), 1, "1 nickname should be obsolete"); $isonline = ($this->ct->isNickOnline($chan, $nick) >= 0); $this->assertFalse($isonline, "nickname shouldn't be online anymore"); } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |