$data = $this->ns_readln();\r
/*if($data===false)\r
{\r
- //If No NS Message Process SendMessageFileQueue\r
- if (time()-$this->LastPing > $this->ping_wait)\r
- {\r
- // NS: >>> PNG\r
- $this->ns_writeln("PNG");\r
- $this->LastPing = time();\r
- }\r
if(count($this->ChildProcess)<$this->MAXChildProcess)\r
{\r
$Index=0;\r
break;\r
case 'QNG':\r
// NS: <<< QNG {time}\r
- @list(/* QNG */, $this->ping_wait) = @explode(' ', $data);\r
- if ($this->ping_wait == 0) $this->ping_wait = 50;\r
+ //@list(/* QNG */, $this->ping_wait) = @explode(' ', $data);\r
+ //if ($this->ping_wait == 0) $this->ping_wait = 50;\r
//if (is_int($use_ping) && $use_ping > 0) $ping_wait = $use_ping;\r
//Mod by Ricky Set Online\r
break;\r
}\r
}\r
\r
- public function SendMessage($Message, $To) {\r
- if(!is_array($To))\r
- $To=array($To);\r
- $Receiver='';\r
- foreach($To as $Email)\r
+ public function sendMessageViaSB($message, $to) {\r
+ $socket = $this->switchBoardSessions[$to]['socket'];\r
+ $lastActive = $this->switchBoardSessions[$to]['lastActive'];\r
+ $joined = $this->switchBoardSessions[$to]['joined'];\r
+ \r
+ //TODO Probably not needed (we're not running in a loop anymore)\r
+ /*if($this->kill_me)\r
{\r
- list($name,$host,$network)=explode('@',$Email);\r
+ $this->log_message("*** SB Okay, kill me now!");\r
+ endSBSession($socket);\r
+ }*/\r
+ \r
+ if(!$Joined) {\r
+ // If our participant has not joined the session yet we can't message them!\r
+ //TODO Check the behaviour of the queue runner when we return false\r
+ return false;\r
+ }\r
+ \r
+ $aMessage = $this->getMessage($Message);\r
+ //CheckEmotion...\r
+ $MsnObjDefine=$this->GetMsnObjDefine($aMessage);\r
+ if($MsnObjDefine !== '')\r
+ {\r
+ $SendString="MIME-Version: 1.0\r\nContent-Type: text/x-mms-emoticon\r\n\r\n$MsnObjDefine";\r
+ $len = strlen($SendString);\r
+ $this->SB_writeln("MSG $id N $len");\r
+ $id++;\r
+ $this->SB_writedata($SendString);\r
+ $this->id++;\r
+ }\r
+ $len = strlen($aMessage);\r
+ $this->SB_writeln("MSG $id N $len");\r
+ \r
+ // Increment the trID\r
+ $this->switchBoardSessions[$to]['id']++;\r
+ \r
+ $this->SB_writedata($aMessage);\r
+ \r
+ if (feof($this->SBFp))\r
+ {\r
+ // lost connection? error? try OIM later\r
+ @fclose($this->SBFp);\r
+ //TODO introduce callback to add offline message to queue?\r
+ return false;\r
+ }\r
+ $this->SB_writeln("OUT");\r
+ @fclose($this->SBFp);\r
+ return true;\r
+ }\r
+ \r
+ //TODO Not sure if this is needed?\r
+ private function endSBSession($socket) {\r
+ if (feof($this->SBFp))\r
+ {\r
+ // lost connection? error? try OIM later\r
+ @fclose($this->SBFp);\r
+ return false;\r
+ }\r
+ $this->SB_writeln("OUT");\r
+ @fclose($this->SBFp);\r
+ return true;\r
+ }\r
+ \r
+ public function sendMessage($message, $to) {\r
+ if($message != '') {\r
+ list($name,$host,$network)=explode('@',$to);\r
$network=$network==''?1:$network;\r
- if($network==1 && isset($this->switchBoardSessions[$Email]) ) {\r
- $this->debug_message("*** SendMessage to $Receiver use SB message queue.");\r
- array_push($this->SwitchBoardMessageQueue,$Message);\r
- continue;\r
+ \r
+ if($network === 1 && isset($this->switchBoardSessions[$to])) {\r
+ $recipient = $name . $host;\r
+ $this->debug_message("*** Sending Message to $recipient using existing SB session");\r
+ $this->sendMessageViaSB($message, $recipient);\r
+ } else {\r
+ $this->debug_message("*** Not MSN network or no existing SB session");\r
+ \r
}\r
- $Receiver.="$name@$host@$network,";\r
}\r
- if($Receiver=='') return;\r
- $Receiver=substr($Receiver,0,-1);\r
- $this->debug_message("*** SendMessage to $Receiver use File queue.");\r
- file_put_contents($FileName,"TO: $Receiver\n$Message\n");\r
+ }\r
+ \r
+ /**\r
+ * Sends a ping command\r
+ * \r
+ * Should be called about every 50 seconds\r
+ */\r
+ public function send_ping() {\r
+ // NS: >>> PNG\r
+ $this->ns_writeln("PNG");\r
}\r
\r
public function getNSSocket() {\r
return $this->NSfp;\r
}\r
\r
+ // TODO Allow for multiple SB session sockets\r
public function getSBSocket() {\r
return $this->SBfp;\r
}\r
class MsnManager extends ImManager
{
-
public $conn = null;
+
+ protected $lastping = null;
+
+ const PING_INTERVAL = 50;
+
/**
* Initialize connection to server.
* @return boolean true on success
return array();
}
}
-
+
+ /**
+ * Idle processing for io manager's execution loop.
+ * Send keepalive pings to server.
+ */
+ public function idle($timeout=0)
+ {
+ $now = time();
+ if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
+ $this->send_ping();
+ }
+ }
+
/**
- * Process AIM events that have come in over the wire.
+ * Process MSN events that have come in over the wire.
* @param resource $socket
*/
public function handleInput($socket)
);
$this->conn->registerHandler("IMIn", array($this, 'handle_msn_message'));
$this->conn->signon();
+ $this->lastping = time();
}
return $this->conn;
}
-
+
+ function send_ping() {
+ $this->connect();
+ if (!$this->conn) {
+ return false;
+ }
+
+ $now = time();
+
+ $this->conn->send_ping();
+ $this->lastping = $now;
+ return true;
+ }
+
function handle_msn_message($data)
{
$this->plugin->enqueue_incoming_raw($data);