From: hikarupsp Date: Mon, 28 Apr 2014 12:42:55 +0000 (+0900) Subject: ノードの更新が反映されるようになった X-Git-Url: http://git.osdn.jp/view?p=chnosproject%2FAI004.git;a=commitdiff_plain;h=07ec1ee2ceaa28ccab74530d1fc4718cc8f38d2c ノードの更新が反映されるようになった --- diff --git a/memdb/memdb.js b/memdb/memdb.js index faf069d..968dc8d 100644 --- a/memdb/memdb.js +++ b/memdb/memdb.js @@ -11,12 +11,13 @@ function MemoryDB(syncPHPURL){ this.syncPHPURL = syncPHPURL; this.isEnabledNetDB = true; // - this.callback_refreshedNode = null; // function(nodeinstance){}; + this.callback_updatedNode = null; // function(nodeinstance){}; } MemoryDB.prototype = { UUID_Null: "00000000-0000-0000-0000-000000000000", UUID_NodeType_DecimalNumber: "e3346fd4-ac17-41c3-b3c7-e04972e5c014", UUID_Node_MemoryDBNetworkTimestamp: "a2560a9c-dcf7-4746-ac14-347188518cf2", + UUID_Node_MemoryDBNetworkResponseCode: "1eeb6d3d-751f-444f-91c8-ed940e65f8bd", createRequestObject: function(){ var rq = null; // XMLHttpRequest @@ -94,11 +95,11 @@ MemoryDB.prototype = { if(d === undefined){ continue; } - this.refreshNodeInternal(d[2], d[1], d[0]); + this.updateNodeInternal(d[2], d[1], d[0]); } - console.log(this.root); + //console.log(this.root); }, - refreshNode: function(ident, tid, nid){ + updateNode: function(ident, tid, nid){ // 該当タグのデータを書き換え、もしくは新規作成する。 // 可能であればネットワークに反映する // nid(nodeid)は省略可能で、省略時は新たなUUIDが自動的に付与される @@ -106,9 +107,9 @@ MemoryDB.prototype = { // identはnullもしくは空文字でもかまわない。 // 戻り値はMemoryDBNodeTagインスタンス // エラー発生時はundefinedを返す。 - this.refreshNodeInternal(ident, tid, nid, true); + this.updateNodeInternal(ident, tid, nid, true); }, - refreshNodeInternal: function(ident, tid, nid, enableSync){ + updateNodeInternal: function(ident, tid, nid, enableSync){ // 基本的にローカルデータのみ変更 // enableSync == trueでネットワーク同期する var t, s, r; @@ -122,7 +123,16 @@ MemoryDB.prototype = { t = this.getNodeFromUUID(nid); if(t){ // 変更 - return undefined; + t.typeid = tid; + t.identifier = ident; + if(enableSync && this.isEnabledNetDB){ + s = this.syncPHPURL + "?action=updatenode"; + s += "&nid=" + encodeURIComponent(nid); + s += "&tid=" + encodeURIComponent(tid); + s += "&ident=" + encodeURIComponent(ident); + r = this.sendRequestSync("GET", s, null); + //console.log(r); + } } else{ // 新規作成 t = new MemoryDBNodeTag(nid, tid, ident); @@ -134,11 +144,11 @@ MemoryDB.prototype = { s += "&tid=" + encodeURIComponent(tid); s += "&ident=" + encodeURIComponent(ident); r = this.sendRequestSync("GET", s, null); - console.log(r); + //console.log(r); } } - if(this.callback_refreshedNode){ - this.callback_refreshedNode(t); + if(this.callback_updatedNode){ + this.callback_updatedNode(t); } }, createUUID: function(){ diff --git a/memdb/memdb.php b/memdb/memdb.php index e3b055a..c9c9088 100644 --- a/memdb/memdb.php +++ b/memdb/memdb.php @@ -37,7 +37,6 @@ create table Edge ( "); // - define("QUERY_ADD_Node", " insert into Node ( nodeid, typeid, identifier, modtimestamp @@ -46,7 +45,7 @@ insert into Node ( ) "); define("QUERY_ADD_Node_TYPES", "sssi"); - +// define("QUERY_ADD_Edge", " insert into Node ( edgeid, typeid, nodeid0, nodeid1, modtimestamp @@ -55,7 +54,14 @@ insert into Node ( ) "); define("QUERY_ADD_Edge_TYPES", "ssssi"); - +// +define("QUERY_UPDATE_Node", " +UPDATE Node SET + typeid=unhex(replace(?, '-', '')), identifier=?, modtimestamp=? +WHERE + nodeid=unhex(replace(?, '-', '')) +"); +define("QUERY_UPDATE_Node_TYPES", "ssis"); // define("QUERY_SELECT_ALL_Node", "select hex(nodeid), hex(typeid), identifier from Node"); @@ -87,14 +93,11 @@ if(isset($_GET['action'])){ $stmt->bind_result($uuid, $typeid, $ident); while($stmt->fetch()){ - $uuid = strtolower($uuid); - echo('["'); - echo(getFormedUUIDString($uuid)); - echo('","'); - echo(getFormedUUIDString($typeid)); - echo('","'); - echo($ident); - echo('"]'); + echoNode( + getFormedUUIDString($uuid), + getFormedUUIDString($typeid), + $ident + ); echo(PHP_EOL); } $stmt->close(); @@ -118,14 +121,11 @@ if(isset($_GET['action'])){ $stmt->bind_result($uuid, $typeid, $ident); while($stmt->fetch()){ - $uuid = strtolower($uuid); - echo('["'); - echo(getFormedUUIDString($uuid)); - echo('","'); - echo(getFormedUUIDString($typeid)); - echo('","'); - echo($ident); - echo('"]'); + echoNode( + getFormedUUIDString($uuid), + getFormedUUIDString($typeid), + $ident + ); echo(PHP_EOL); } $stmt->close(); @@ -143,14 +143,11 @@ if(isset($_GET['action'])){ $stmt->bind_result($uuid, $typeid, $ident, $mts); while($stmt->fetch()){ - $uuid = strtolower($uuid); - echo('["'); - echo(getFormedUUIDString($uuid)); - echo('","'); - echo(getFormedUUIDString($typeid)); - echo('","'); - echo($ident); - echo('"]'); + echoNode( + getFormedUUIDString($uuid), + getFormedUUIDString($typeid), + $ident + ); echo(' @' . $mts); echo("
"); } @@ -184,29 +181,59 @@ if(isset($_GET['action'])){ exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db)); } $stmt->close(); - exitWithResponseCode("CEA95615-649C-4837-9E24-0C968FA57647", "OK"); - } else if(strcmp($action, 'saytest') == 0){ - /* - //for Mac OSX say command. - system("say " . escapeshellarg("sayのテストをしています。")); - */ + exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK"); + } else if(strcmp($action, 'updatenode') == 0){ + if(isset($_GET['nid'])){ + $nodeid = $_GET['nid']; + } else{ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "nodeid needed."); + } + if(isset($_GET['tid'])){ + $typeid = $_GET['tid']; + } else{ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "typeid needed."); + } + if(isset($_GET['ident'])){ + $ident = $_GET['ident']; + } else{ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", "ident needed."); + } + + $stmt = $db->prepare(QUERY_UPDATE_Node); + $mts = getTimeStampMs(); + $stmt->bind_param(QUERY_UPDATE_Node_TYPES, $typeid, $ident, $mts, $nodeid); + $stmt->execute(); + // + $stmt->store_result(); + if($stmt->errno != 0){ + exitWithResponseCode("A0518702-C90C-4785-B5EA-1A213DD0205B", mysqli_error($db)); + } + $stmt->close(); + exitWithResponseCode("cea95615-649c-4837-9e24-0c968fa57647", "OK"); } } //NOP error -exitWithResponseCode("B539657C-0FA6-49C2-AFB0-13AF5C7866ED"); +exitWithResponseCode("b539657c-0fa6-49c2-afb0-13af5c7866ed"); function exitWithResponseCode($errid, $description = "") { - die('["' . $errid .'","' . $description . '"]'); + echoNode("1eeb6d3d-751f-444f-91c8-ed940e65f8bd", $errid, $description); + exit(); } function echoMemoryDBNetworkTimestamp() { - echo('["a2560a9c-dcf7-4746-ac14-347188518cf2","e3346fd4-ac17-41c3-b3c7-e04972e5c014","'); - echo(getTimeStampMs()); - echo('"]'); - echo(PHP_EOL); + echoNode( + "a2560a9c-dcf7-4746-ac14-347188518cf2", + "e3346fd4-ac17-41c3-b3c7-e04972e5c014", + getTimeStampMs() + ); +} + +function echoNode($nid, $tid, $ident) +{ + echo('["' . $nid .'","' . $tid .'","' . $ident . '"]'); } function connectDB() diff --git a/mgcanvas/index.html b/mgcanvas/index.html index fdc3dc8..931bd65 100755 --- a/mgcanvas/index.html +++ b/mgcanvas/index.html @@ -43,7 +43,7 @@ onload = function() { identifier:
- + \ No newline at end of file diff --git a/mgcanvas/mgcanvas.js b/mgcanvas/mgcanvas.js index e3cae90..b80639d 100755 --- a/mgcanvas/mgcanvas.js +++ b/mgcanvas/mgcanvas.js @@ -85,10 +85,18 @@ MGCanvas.prototype = { setSourceMemoryDB: function(mdb){ var that = this; this.srcMemoryDB = mdb; - mdb.callback_refreshedNode = function(t){ + mdb.callback_updatedNode = function(t){ var n; - n = new MGNode(that, t.identifier); - that.nodeList.push(n); + n = that.nodeList.isIncluded(t.nodeid, function(a, b){ return a.nodeid == b; }); + if(!n){ + // 新規追加 + n = new MGNode(that, t.identifier); + n.nodeid = t.nodeid; + that.nodeList.push(n); + } else{ + // 更新 + n.identifier = t.identifier; + } } }, bringToCenter: function(){ @@ -434,14 +442,23 @@ MGCanvas.prototype = { }, setIdentifierForSelectedNode: function(str){ if(this.selectedNode){ - this.selectedNode.identifier = str; + if(this.srcMemoryDB){ + this.srcMemoryDB.updateNode(str, this.selectedNode.typeid, this.selectedNode.nodeid); + } else{ + this.selectedNode.identifier = str; + } } }, } function MGNode(env, identifier){ this.env = env; + // this.identifier = identifier; + // + this.nodeid = undefined; + this.typeid = undefined; + // this.position = new Point2D(Math.random() * 32 - 16, Math.random() * 32 - 16); this.size = 10; //ランダムな初期ベクトルをもつ。