Create base class for Client Side Request Processors

This commit is contained in:
Dmitriy Simushev 2013-01-16 09:07:10 +00:00
parent 0933263cc9
commit a300b99171
4 changed files with 110 additions and 66 deletions

View File

@ -57,9 +57,9 @@ $dbtables = array(
"groupid" => "int references ${mysqlprefix}chatgroup(groupid)", "groupid" => "int references ${mysqlprefix}chatgroup(groupid)",
), ),
"${mysqlprefix}chatrequestbuffer" => array( "${mysqlprefix}requestbuffer" => array(
"requestid" => "int NOT NULL auto_increment PRIMARY KEY", "requestid" => "int NOT NULL auto_increment PRIMARY KEY",
"threadid" => "int NOT NULL references ${mysqlprefix}chatthread(threadid)", "requestkey" => "varchar(128) NOT NULL",
"request" => "text NOT NULL" "request" => "text NOT NULL"
), ),
@ -167,8 +167,8 @@ $dbtables_indexes = array(
"groupid" => "groupid", "groupid" => "groupid",
"operatorid" => "operatorid" "operatorid" => "operatorid"
), ),
"${mysqlprefix}chatrequestbuffer" => array( "${mysqlprefix}requestbuffer" => array(
"threadid" => "threadid" "requestkey" => "requestkey"
), ),
"${mysqlprefix}chatmessage" => array( "${mysqlprefix}chatmessage" => array(
"idx_agentid" => "agentid" "idx_agentid" => "agentid"
@ -194,7 +194,7 @@ $memtables = array();
$dbtables_can_update = array( $dbtables_can_update = array(
"${mysqlprefix}chatthread" => array("agentId", "userTyping", "agentTyping", "messageCount", "nextagent", "shownmessageid", "userid", "userAgent", "groupid", "dtmchatstarted"), "${mysqlprefix}chatthread" => array("agentId", "userTyping", "agentTyping", "messageCount", "nextagent", "shownmessageid", "userid", "userAgent", "groupid", "dtmchatstarted"),
"${mysqlprefix}chatrequestbuffer" => array("requestid", "threadid", "request"), "${mysqlprefix}requestbuffer" => array("requestid", "requestkey", "request"),
"${mysqlprefix}chatmessage" => array("agentId"), "${mysqlprefix}chatmessage" => array("agentId"),
"${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"), "${mysqlprefix}chatoperator" => array("vcavatar", "vcjabbername", "iperm", "istatus", "idisabled", "vcemail", "dtmrestore", "vcrestoretoken"),
"${mysqlprefix}chatban" => array(), "${mysqlprefix}chatban" => array(),

View File

@ -0,0 +1,96 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Base class for all request processors that interact with JavaScript
* applications at the client side.
*/
abstract class ClientSideProcessor extends RequestProcessor {
/**
* Call function at client side
*
* @param array $functions Array of functions to call. See Mibew API for
* details.
* @param array|null $callback callback array for synchronous requests.
* @return mixed request result or boolean false on failure.
*/
public function call($functions, $callback = null) {
return parent::call($functions, true, $callback);
}
/**
* Sends asynchronous responses
*
* @param array $responses An array of the 'Request' arrays. See Mibew API
* for details
*/
protected function sendAsyncResponses($responses) {
header("Content-type: text/plain; charset=UTF-8");
echo($this->mibewAPI->encodePackage(
$responses,
$this->config['signature'],
true
));
}
/**
* Add request to client side to the buffer. Use database as storage.
* Override this method if you want to use another storage and/or save logic.
*
* @param String $key Request key. Use to load request from buffer.
* @param $request Request array.
*/
protected function addRequestToBuffer($key, $request) {
// Save request to database
$db = Database::getInstance();
$db->query(
"INSERT INTO {requestbuffer} (request, requestkey) VALUES (:request, :key)",
array(':request' => serialize($request), ':key' => $key)
);
}
/**
* Load stored requests to the client side
*
* @param String $key Request key
* @return array Array of requests with given key
*/
protected function getRequestsFromBuffer($key) {
$db = Database::getInstance();
// Get requests from database
$requests = $db->query(
"SELECT request FROM {requestbuffer} WHERE requestkey = :key",
array(':key' => $key),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
// Remove got requests from database
$db->query(
"DELETE FROM {requestbuffer} WHERE requestkey = :key",
array(':key' => $key)
);
// Unserialize requests
$result = array();
foreach($requests as $request_info) {
$result[] = unserialize($request_info['request']);
}
return $result;
}
}
?>

View File

@ -29,7 +29,7 @@
* *
* Implements Singleton pattern * Implements Singleton pattern
*/ */
class ThreadProcessor extends RequestProcessor { class ThreadProcessor extends ClientSideProcessor {
/** /**
* An instance of the ThreadProcessor class * An instance of the ThreadProcessor class
@ -61,7 +61,10 @@ class ThreadProcessor extends RequestProcessor {
$thread = Thread::load($thread_id, $last_token); $thread = Thread::load($thread_id, $last_token);
// Check thread // Check thread
if (! $thread) { if (! $thread) {
throw new ThreadProcessorException('Wrong thread', ThreadProcessorException::ERROR_WRONG_THREAD); throw new ThreadProcessorException(
'Wrong thread',
ThreadProcessorException::ERROR_WRONG_THREAD
);
} }
// Return thread // Return thread
return $thread; return $thread;
@ -102,17 +105,6 @@ class ThreadProcessor extends RequestProcessor {
)); ));
} }
/**
* Call function at window side
*
* @param array $functions Array of functions to call. See Mibew API for details.
* @param array|null $callback callback array for synchronous requests.
* @return mixed request result or boolean false on failure.
*/
public function call($functions, $callback = null) {
return parent::call($functions, true, $callback);
}
/** /**
* Creates and returns an instance of the MibewAPI class. * Creates and returns an instance of the MibewAPI class.
* *
@ -122,20 +114,6 @@ class ThreadProcessor extends RequestProcessor {
return MibewAPI::getAPI('MibewAPIChatInteraction'); return MibewAPI::getAPI('MibewAPIChatInteraction');
} }
/**
* Sends asynchronous responses
*
* @param array $responses An array of the 'Request' arrays. See Mibew API for details
*/
protected function sendAsyncResponses($responses) {
header("Content-type: text/plain; charset=UTF-8");
echo($this->mibewAPI->encodePackage(
$responses,
$this->config['signature'],
true
));
}
/** /**
* Sends asynchronous request * Sends asynchronous request
* *
@ -161,42 +139,11 @@ class ThreadProcessor extends RequestProcessor {
); );
} }
} }
// Save request to database // Store request in buffer
$db = Database::getInstance(); $this->addRequestToBuffer('thread_'.$thread_id, $request);
$db->query(
"INSERT INTO {chatrequestbuffer} (request, threadid) VALUES (:request, :threadid)",
array(':request' => serialize($request), ':threadid' => $thread_id)
);
return true; return true;
} }
/**
* Load stored requests to window from database
*
* @param Thread $thread Requests loads for this thread
* @return array Array of requests to $thread thread
*/
protected function getStoredRequests(Thread $thread) {
$db = Database::getInstance();
// Get requests from database
$requests = $db->query(
"SELECT request FROM {chatrequestbuffer} WHERE threadid = :threadid",
array(':threadid' => $thread->id),
array('return_rows' => Database::RETURN_ALL_ROWS)
);
// Remove got requests from database
$db->query(
"DELETE FROM {chatrequestbuffer} WHERE threadid = :threadid",
array(':threadid' => $thread->id)
);
// Unserialize requests
$result = array();
foreach($requests as $request_info) {
$result[] = unserialize($request_info['request']);
}
return $result;
}
/** /**
* Send new messages to window * Send new messages to window
* *
@ -285,7 +232,7 @@ class ThreadProcessor extends RequestProcessor {
$this->sendMessages($thread, $args['user'], $args['lastId']); $this->sendMessages($thread, $args['user'], $args['lastId']);
// Load stored requests // Load stored requests
$stored_requests = $this->getStoredRequests($thread); $stored_requests = $this->getRequestsFromBuffer('thread_'.$thread->id);
if ($stored_requests !== false) { if ($stored_requests !== false) {
$this->responses = array_merge($this->responses, $stored_requests); $this->responses = array_merge($this->responses, $stored_requests);
} }

View File

@ -23,6 +23,7 @@ require_once('libs/classes/mibew_api.php');
require_once('libs/classes/mibew_api_interaction.php'); require_once('libs/classes/mibew_api_interaction.php');
require_once('libs/classes/mibew_api_chat_interaction.php'); require_once('libs/classes/mibew_api_chat_interaction.php');
require_once('libs/classes/mibew_api_execution_context.php'); require_once('libs/classes/mibew_api_execution_context.php');
require_once('libs/classes/client_side_processor.php');
require_once('libs/classes/thread_processor.php'); require_once('libs/classes/thread_processor.php');
$processor = ThreadProcessor::getInstance(); $processor = ThreadProcessor::getInstance();