PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/apps/bpm/models/request_info.php

https://github.com/jplante815/meican
PHP | 345 lines | 251 code | 81 blank | 13 comment | 42 complexity | 4de71ced8e2b4b814a15e9f05421ba43 MD5 | raw file
  1. <?php
  2. include_once 'libs/Model/resource_model.php';
  3. include_once 'libs/auth.php';
  4. class request_info extends Resource_Model {
  5. function request_info() {
  6. $this->setTableName("request_info");
  7. // Add all table attributes
  8. $this->addAttribute('loc_id', "INTEGER", true, false, false);
  9. $this->addAttribute("req_id", "INTEGER");
  10. $this->addAttribute("src_ode_ip", "VARCHAR");
  11. $this->addAttribute("src_usr", "INTEGER");
  12. $this->addAttribute("dst_ode_ip", "VARCHAR");
  13. $this->addAttribute("resource_type", "VARCHAR");
  14. $this->addAttribute("resource_id", "INTEGER");
  15. $this->addAttribute('answerable', "VARCHAR");
  16. $this->addAttribute("status", "VARCHAR");
  17. $this->addAttribute("response", "VARCHAR");
  18. $this->addAttribute("message", "VARCHAR");
  19. $this->addAttribute("crr_ode_ip", "VARCHAR");
  20. $this->addAttribute("response_user", "INTEGER");
  21. $this->addAttribute("start_time", "FLOAT");
  22. $this->addAttribute("finish_time", "FLOAT");
  23. }
  24. public function setDom($dom_src, $arg_ip) {
  25. $domain = new domain_info();
  26. $domain->dom_ip = $arg_ip;
  27. if ($result = $domain->fetch(FALSE)) {
  28. $this->{$dom_src} = $result[0]->dom_id;
  29. } else {
  30. //domain nao existe ira adicionar
  31. $obj = $domain->insert();
  32. $this->{$dom_src} = $obj->dom_id;
  33. }
  34. }
  35. public function setDomIp($dom_src_ip, $arg_id) {
  36. $domain = new domain_info();
  37. $domain->dom_id = $arg_id;
  38. if ($result = $domain->fetch(FALSE)) {
  39. $this->{$dom_src_ip} = $result[0]->dom_ip;
  40. }
  41. }
  42. function getRequestInfo($getReqInfo = FALSE, $getFlowInfo = FALSE, $getTimerInfo = FALSE) {
  43. //Log::write("debug", "Get request info:\n" . print_r($this, TRUE));
  44. $domain_info = new domain_info();
  45. $domain_info->ode_ip = $this->src_ode_ip;
  46. $return_request = new stdClass();
  47. $return_request->loc_id = $this->loc_id;
  48. $return_request->req_id = $this->req_id;
  49. $return_request->status = $this->status;
  50. $return_request->response = $this->response;
  51. $return_request->message = $this->message;
  52. $return_request->resc_descr = _("Unknown");
  53. $return_request->resc_type = _("Unknown");
  54. if ($domain_result = $domain_info->fetch(FALSE)) {
  55. // request information was found in this MEICAN domain, WS is NOT required
  56. Log::write("circuits", "Request is local");
  57. $return_request->src_domain = $domain_result[0]->dom_descr;
  58. $user_info = new user_info();
  59. $user_info->usr_id = $this->src_usr;
  60. if ($user = $user_info->fetch(FALSE))
  61. $return_request->src_user = $user[0]->usr_login;
  62. else
  63. $return_request->src_user = $this->src_usr;
  64. $domain_info = new domain_info();
  65. $domain_info->ode_ip = $this->dst_ode_ip;
  66. if ($dst_domain = $domain_info->fetch(FALSE))
  67. $return_request->dst_domain = $dst_domain[0]->dom_descr;
  68. else {
  69. // try to call a WS to get domain description
  70. try {
  71. $ODEendpoint = "http://$this->src_ode_ip}/getMeicanData";
  72. $requestSOAP = array('ode_ip' => $this->dst_ode_ip);
  73. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  74. $domain = $client->getDomains($requestSOAP);
  75. $return_request->dst_domain = $domain['dom_descr'];
  76. } catch (Exception $e) {
  77. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  78. $return_request->dst_domain = $this->dst_ode_ip;
  79. }
  80. }
  81. if ($getReqInfo) {
  82. $req_tmp = new request_info();
  83. $req_tmp->req_id = $this->req_id;
  84. $req_tmp->src_ode_ip = $this->src_ode_ip;
  85. $req_tmp->answerable = 'no';
  86. if ($req_result = $req_tmp->fetch(FALSE)) {
  87. $resourceReq = $req_result[0];
  88. if ($resourceReq->resource_type == "reservation_info") {
  89. $res_info = new reservation_info();
  90. $res_info->res_id = $resourceReq->resource_id;
  91. if ($reservation = $res_info->fetch(false)) {
  92. $return_request->resc_id = $resourceReq->resource_id;
  93. $return_request->resc_descr = $reservation[0]->res_name;
  94. $return_request->resc_type = $resourceReq->resource_type;
  95. $return_request->bandwidth = $reservation[0]->bandwidth;
  96. if ($getFlowInfo) {
  97. $flow = new flow_info();
  98. $flow->flw_id = $reservation[0]->flw_id;
  99. $return_request->flow_info = $flow->getFlowDetails();
  100. if ($return_request->flow_info->path)
  101. $return_request->flow_info->path = MeicanTopology::getWaypoints($return_request->flow_info->path);
  102. $return_request->flow_info->source->domain = $return_request->src_domain;
  103. $return_request->flow_info->dest->domain = $return_request->dst_domain;
  104. } else
  105. $return_request->flow_info = NULL;
  106. if ($getTimerInfo) {
  107. $timer = new timer_info();
  108. $timer->tmr_id = $reservation[0]->tmr_id;
  109. $return_request->timer_info = $timer->getTimerDetails();
  110. } else
  111. $return_request->timer_info = NULL;
  112. }
  113. } else {
  114. $resource = new $resourceReq->resource_type();
  115. $pk = $resource->getPrimaryKey();
  116. $resource->{$pk} = $resourceReq->resource_id;
  117. if ($result = $resource->fetch(FALSE)) {
  118. $return_request->resc_descr = $result[0]->{$resource->displayField};
  119. $return_request->resc_type = $resourceReq->resource_type;
  120. }
  121. $return_request->flow_info = NULL;
  122. $return_request->timer_info = NULL;
  123. }
  124. }
  125. }
  126. } else {
  127. // request information was NOT found in this MEICAN domain, trying WS to get data
  128. Log::write("circuits", "Request is remote");
  129. $ODEendpoint = "http://$this->src_ode_ip}/getMeicanData";
  130. // get source domain
  131. try {
  132. $requestSOAP = array('ode_ip' => $this->src_ode_ip);
  133. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  134. $domain = $client->getDomains($requestSOAP);
  135. $return_request->src_domain = $domain['dom_descr'];
  136. } catch (Exception $e) {
  137. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  138. $return_request->src_domain = $this->src_ode_ip;
  139. }
  140. // get destination domain
  141. try {
  142. $requestSOAP = array('ode_ip' => $this->dst_ode_ip);
  143. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  144. $domain = $client->getDomains($requestSOAP);
  145. $return_request->dst_domain = $domain['dom_descr'];
  146. } catch (Exception $e) {
  147. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  148. $return_request->dst_domain = $this->dst_ode_ip;
  149. }
  150. //get source user
  151. try {
  152. $requestSOAP = array('usr_id' => $this->src_usr);
  153. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  154. $user = $client->getUsers($requestSOAP);
  155. $return_request->src_user = $user['usr_name'];
  156. } catch (Exception $e) {
  157. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  158. $return_request->src_user = $this->src_usr;
  159. }
  160. // get request info
  161. if ($getReqInfo) {
  162. $return_request->resc_type = $this->resource_type;
  163. if ($this->resource_type == "reservation_info") {
  164. try {
  165. $requestSOAP = array('res_id' => $this->resource_id);
  166. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  167. $reservation = $client->getReservations($requestSOAP);
  168. $return_request->resc_descr = $reservation['res_name'];
  169. } catch (Exception $e) {
  170. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  171. }
  172. if ($getFlowInfo) {
  173. try {
  174. $requestSOAP = array('res_id' => $this->resource_id);
  175. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  176. $flow = $client->getFlowInfo($requestSOAP);
  177. $return_request->flow_info = $flow;
  178. } catch (Exception $e) {
  179. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  180. }
  181. }
  182. if ($getTimerInfo) {
  183. try {
  184. $requestSOAP = array('res_id' => $this->resource_id);
  185. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  186. $timer = $client->getTimerInfo($requestSOAP);
  187. $return_request->timer_info = $timer;
  188. } catch (Exception $e) {
  189. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  190. }
  191. }
  192. } else {
  193. $return_request->flow_info = NULL;
  194. $return_request->timer_info = NULL;
  195. try {
  196. $requestSOAP = array('req_id' => $this->resource_id);
  197. $client = new SoapClient($ODEendpoint, array('cache_wsdl' => 0));
  198. $request = $client->getRequestInfo($requestSOAP);
  199. $return_request->resc_descr = $request['resc_descr'];
  200. } catch (Exception $e) {
  201. Log::write("error", "Caught exception while trying to call getMeicanData from ODE: " . print_r($e->getMessage()));
  202. }
  203. }
  204. }
  205. }
  206. Log::write("debug", "Request info return:\n" . print_r($return_request, TRUE));
  207. return $return_request;
  208. }
  209. public function response() {
  210. $message = $this->message;
  211. $response = $this->response;
  212. $this->message = "";
  213. $this->response = "";
  214. $now = microtime(true);
  215. $usr = AuthSystem::getUserId();
  216. $res = $this->fetch(FALSE);
  217. $toResponse = $res[0];
  218. if (!$toResponse->response) {
  219. //$local = $this->updateTo(array('response' => $response, 'message' => $message, 'status' => 'ANSWERED', 'finish_time' => $now, 'response_user' => $usr), FALSE);
  220. $responseSOAP = array(
  221. 'req_id' => $toResponse->req_id,
  222. 'src_ode_ip' => $toResponse->src_ode_ip,
  223. 'response' => $response,
  224. 'message' => $message);
  225. $dom = new domain_info();
  226. $dom->ode_ip = $toResponse->crr_ode_ip;
  227. $res_domain = $dom->fetch(FALSE);
  228. $domain = $res_domain[0];
  229. if ($domain->ode_wsdl_path && $domain->ode_response) {
  230. Log::write("circuits", "Sending response:\n" . print_r($responseSOAP, TRUE));
  231. try {
  232. $client = new SoapClient($domain->ode_wsdl_path, array('cache_wsdl' => WSDL_CACHE_NONE));
  233. if ($client->{$domain->ode_response}($responseSOAP)) {
  234. $local = $this->updateTo(array('finish_time' => $now, 'response_user' => $usr), FALSE);
  235. }
  236. //$client->__soapCall($domain->ode_response, $responseSOAP);
  237. // wait for 2 seconds while ODE call saveResponse, then it will update de DB
  238. sleep(2);
  239. return TRUE;
  240. } catch (Exception $e) {
  241. Log::write("error", "Caught exception while trying to connect to ODE:\n" . print_r($e->getMessage()));
  242. return FALSE;
  243. }
  244. } else {
  245. Log::write("error", 'ODE not confired correctly');
  246. return FALSE;
  247. }
  248. } else {
  249. Log::write('warning', "Request already answered:\n" . print_r($this, TRUE));
  250. return FALSE;
  251. }
  252. }
  253. public function checkRequests() {
  254. $noReq = 0;
  255. $result = $this->fetch();
  256. foreach ($result as $t) {
  257. if ($t->answerable == 'yes')
  258. if (!$t->response)
  259. $noReq++;
  260. }
  261. return $noReq;
  262. }
  263. }
  264. ?>