public function testWootDocument() { $alice = new WootDocument(self::$SITE_ID, 'Alice'); $bob = new WootDocument(self::$SITE_ID + 1, 'Bob'); $carol = new WootDocument(self::$SITE_ID + 2, 'Carol'); function doMessageLoops($clients) { foreach ($clients as $client) { while ($client->hasMessages()) { $client->doMessageLoop(); } } } $msgsABC = $alice->generateInsertMessagesFromString('abc', 0); $this->assertEquals('abc', $alice->value()); $bob->addMessages($msgsABC); $carol->addMessages($msgsABC); doMessageLoops(array($alice, $bob, $carol)); $this->assertThreequals($alice->value(), $bob->value(), $carol->value()); $msgX = $bob->generateInsertMessage('x', 0); $msgY = $alice->generateInsertMessage('y', 1); $this->assertEquals('xabc', $bob->value()); $this->assertEquals('aybc', $alice->value()); $msgsDel = $carol->generateDeleteMessages(0, 2, true); $this->assertEquals('c', $carol->value()); $bob->addMessages($msgsDel); $alice->addMessages($msgsDel); $bob->addMessage($msgY); $alice->addMessage($msgX); $carol->addMessages(array($msgX, $msgY)); doMessageLoops(array($alice, $bob, $carol)); $this->assertThreequals($alice->value(), $bob->value(), $carol->value()); }
private function integrateMessages($document_id) { $dbg = array('docid' => $document_id); if (($msgs = TlonMessage::getUnappliedByDocumentID($document_id)) && ($doc = $this->_get($document_id))) { $dbg['num_messages'] = count($msgs); $wd = new WootDocument(new WootID(0, 0), $doc['title'], array_map(array($this, 'arrToWootChar'), $doc['content'])); $dbg['before'] = $this->unserialize($doc['content']); foreach ($msgs as $msg) { $applied = true; $ops = $this->unserialize($msg['operations']); foreach ($ops as $op) { $r = $op['op'] == WootMessage::OP_DEL ? $wd->integrateDelete($this->arrToWootChar($op['char'])) : $wd->integrateInsert($this->arrToWootChar($op['char']), $this->arrToWootChar($op['prev']), $this->arrToWootChar($op['next'])); if ($r === false) { $applied = false; } } if ($applied) { TlonMessage::setApplied($msg['message_id']); } } $dbg['after'] = array_map(array($this, 'wootCharToArr'), $wd->content()); $doc['content'] = $this->serialize(array_map(array($this, 'wootCharToArr'), $wd->content())); $dbg['res'] = TlonDocument::update($document_id, $doc['title'], $doc['content'], $doc['parent']); } $this->cleanMessages($document_id); return $dbg; }