[Phpfreechat-svn] SF.net SVN: phpfreechat: [592] trunk/testcase
Status: Beta
Brought to you by:
kerphi
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. |