+// Copyright (c) 2018 Maen Artimy\r
+// \r
+// Licensed under the Apache License, Version 2.0 (the "License");\r
+// you may not use this file except in compliance with the License.\r
+// You may obtain a copy of the License at\r
+// \r
+// http://www.apache.org/licenses/LICENSE-2.0\r
+//\r
+// Unless required by applicable law or agreed to in writing, software\r
+// distributed under the License is distributed on an "AS IS" BASIS,\r
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+// See the License for the specific language governing permissions and\r
+// limitations under the License.\r
+\r
+/**\r
+ * All global objects are here.\r
+ *\r
+ **/\r
+var myGlobalObject = (function () {\r
+ console.log("myGlobalObject");\r
+ var dpid = 1; //current datapath (switch) ID\r
+ var myMasterURL = ""; //"http://localhost:8080";\r
+\r
+ // returns full url for the command sent to the SDN controller\r
+ var url = function(command) {\r
+ var cmd = command.replace("<dpid>",dpid); //dpidStr(dpid));\r
+ return myMasterURL + cmd;\r
+ };\r
+\r
+ // sends asynchronous request to the SDN controller\r
+ function httpGetAsync(command, callback, element) {\r
+ //console.log("httpGetAsync");\r
+ var COMPLETE = 4;\r
+ var OK = 200;\r
+ var NOTFOUND = 404;\r
+ var UNAVAILABLE = 503;\r
+ // this script is for sending a GET request to a server\r
+ var xmlHttp = new XMLHttpRequest();\r
+ xmlHttp.onreadystatechange = function() {\r
+ if (xmlHttp.readyState == COMPLETE) {\r
+ if(xmlHttp.status == OK) {\r
+ callback(xmlHttp.responseText, element);\r
+ } else {\r
+ console.log(xmlHttp.status);\r
+ }\r
+ }\r
+ }\r
+ xmlHttp.open("GET", url(command), true); // true for asynchronous\r
+ xmlHttp.send(null);\r
+ }\r
+\r
+ function httpPostAsync(command, parm, callback, element) {\r
+ //console.log("httpPostAsync");\r
+ var COMPLETE = 4;\r
+ var OK = 200;\r
+ var NOTFOUND = 404;\r
+ var UNAVAILABLE = 503;\r
+ // this script is for sending a GET request to a server\r
+ var xmlHttp = new XMLHttpRequest();\r
+ xmlHttp.onreadystatechange = function() {\r
+ if (xmlHttp.readyState == COMPLETE) {\r
+ if(xmlHttp.status == OK) {\r
+ callback(xmlHttp.responseText, element);\r
+ } else {\r
+ console.log(xmlHttp.status);\r
+ }\r
+ }\r
+ }\r
+ xmlHttp.open("POST", url(command), true); // true for asynchronous\r
+ //xmlHttp.setRequestHeader("Content-Type", "application/json");\r
+ xmlHttp.setRequestHeader("Content-Type", "plain/text");\r
+ xmlHttp.send(parm);\r
+ }\r
+\r
+ function dpidStr(dpid) {\r
+ return ("000000000000000" + dpid.toString(16)).substr(-16);\r
+ }\r
+\r
+ function setDPID(str) {\r
+ dpid = parseInt(str);\r
+ }\r
+\r
+ function getDPID() {\r
+ return dpid;\r
+ }\r
+\r
+ // these are the public objects\r
+ return {\r
+ dpid: getDPID,\r
+ setDPID: setDPID,\r
+ httpGetAsync: httpGetAsync,\r
+ httpPostAsync: httpPostAsync,\r
+ }\r
+})();\r