Lines Matching refs:query

60 /* The packet is a query. */
62 /* The packet is a response to a query. */
94 /* Handshake query.
95 * This query is sent to SDK controller service as part of the connection
182 * header, with query data immediately following this header.
185 /* Data packet header for this query. */
187 /* A unique query identifier. This ID is used to track the query in the
197 * When query descriptors are allocated by this API, they are allocated large
198 * enough to contain this header, and query data to send to the service,
206 /* Next query in the list of active queries. */
208 /* A timer to run time out on this query after it has been sent. */
210 /* Absolute time for this query's deadline. This is the value that query's
211 * timer is set to after query has been transmitted to the service. */
213 /* SDK controller socket that owns the query. */
215 /* A callback to invoke on query state changes. */
217 /* An opaque pointer associated with this query. */
219 /* Points to an address of a buffer where to save query response. */
222 * in), or actual query response size (when query is completed). */
224 /* Internal response buffer, allocated if query creator didn't provide its
228 /* Internal response buffer size used if query creator didn't provide its
232 /* Number of outstanding references to the query. */
235 /* Common query header. Query data immediately follows this header, so it
242 * All replies to a query, sent and received via SDK controller socket begin with
243 * this header, with query reply data immediately following this header.
249 /* An identifier for the query that is addressed with this reply. */
263 /* Data packet header for this query. */
301 /* I/O dispatcher expects query response header. */
303 /* I/O dispatcher expects query response data. */
317 /* Header for a query packet. */
321 /* Header for a query response packet. */
326 /* A query for which a reply is currently being received. */
352 /* Head of the active query list. */
354 /* Tail of the active query list. */
356 /* Query ID generator that gets incremented for each new query. */
446 * SDKCtlSocket query list management
449 /* Adds a query to the list of active queries.
451 * sdkctl - SDKCtlSocket instance for the query.
452 * query - Query to add to the list.
455 _sdkctl_socket_add_query(SDKCtlQuery* query)
457 SDKCtlSocket* const sdkctl = query->sdkctl;
460 sdkctl->query_head = sdkctl->query_tail = query;
462 sdkctl->query_tail->next = query;
463 sdkctl->query_tail = query;
466 /* Keep the query referenced while it's in the list. */
467 sdkctl_query_reference(query);
470 /* Removes a query from the list of active queries.
472 * query - Query to remove from the list of active queries. If query has been
474 * that wad made when the query has been added to the list.
476 * Boolean: 1 if query has been removed, or 0 if query has not been found in the
480 _sdkctl_socket_remove_query(SDKCtlQuery* query)
482 SDKCtlSocket* const sdkctl = query->sdkctl;
486 /* Quick check: the query could be currently handled by the dispatcher. */
487 if (sdkctl->io_dispatcher.current_query == query) {
488 /* Release the query from dispatcher. */
490 sdkctl_query_release(query);
494 /* Remove query from the list. */
495 while (head != NULL && query != head) {
501 sdkctl->service_name, query);
507 assert(query == sdkctl->query_head);
508 sdkctl->query_head = query->next;
511 assert(query != sdkctl->query_head);
512 prev->next = query->next;
514 if (sdkctl->query_tail == query) {
516 assert(query->next == NULL);
519 query->next = NULL;
521 /* Release query that is now removed from the list. Note that query
524 sdkctl_query_release(query);
529 /* Removes a query (based on query ID) from the list of active queries.
531 * sdkctl - SDKCtlSocket instance that owns the query.
532 * query_id - Identifies the query to remove.
534 * A query removed from the list of active queries, or NULL if query with the
535 * given ID has not been found in the list. Note that query returned from this
536 * routine still holds the reference made when the query has been added to the
542 SDKCtlQuery* query = NULL;
546 /* Quick check: the query could be currently handled by dispatcher. */
549 /* Release the query from dispatcher. */
550 query = sdkctl->io_dispatcher.current_query;
552 return query;
555 /* Remove query from the list. */
567 query = head;
570 assert(query == sdkctl->query_head);
571 sdkctl->query_head = query->next;
574 assert(query != sdkctl->query_head);
575 prev->next = query->next;
577 if (sdkctl->query_tail == query) {
579 assert(query->next == NULL);
582 query->next = NULL;
584 return query;
587 /* Pulls the first query from the list of active queries.
589 * sdkctl - SDKCtlSocket instance that owns the query.
591 * A query removed from the head of the list of active queries, or NULL if query
597 SDKCtlQuery* const query = sdkctl->query_head;
599 if (query != NULL) {
600 sdkctl->query_head = query->next;
605 return query;
608 /* Generates new query ID for the given SDKCtl. */
697 /* Lets see what's going on with query transmission. */
827 /* Lets see what's going on with query transmission. */
950 /* Frees query descriptor. */
952 _sdkctl_query_free(SDKCtlQuery* query)
954 if (query != NULL) {
955 SDKCtlSocket* const sdkctl = query->sdkctl;
956 if (query->internal_resp_buffer != NULL &&
957 (query->response_buffer == NULL ||
958 query->response_buffer == &query->internal_resp_buffer)) {
959 /* This query used its internal buffer to receive the response.
961 free(query->internal_resp_buffer);
964 loopTimer_done(query->timer);
967 _sdkctl_socket_free_recycler(sdkctl, query);
969 T("SDKCtl %s: Query %p is freed.", sdkctl->service_name, query);
971 /* Release socket that owned this query. */
976 /* Cancels timeout for the query.
978 * For the simplicity of implementation, the dispatcher will cancel query timer
979 * when query response data begins to flow in. If we let the timer to expire at
984 _sdkctl_query_cancel_timeout(SDKCtlQuery* query)
986 loopTimer_stop(query->timer);
989 query->sdkctl->service_name, query, query->header.query_id, query->deadline);
996 /* Callback that is invoked by the I/O dispatcher when query is successfuly
997 * completed (i.e. response to the query is received).
1000 _on_sdkctl_query_completed(SDKCtlQuery* query)
1003 query->sdkctl->service_name, query, query->header.query_id);
1005 /* Cancel deadline, and inform the client about query completion. */
1006 _sdkctl_query_cancel_timeout(query);
1007 query->query_cb(query->query_opaque, query, ASIO_STATE_SUCCEEDED);
1010 /* A callback that is invoked on query cancellation. */
1012 _on_sdkctl_query_cancelled(SDKCtlQuery* query)
1018 * the client about query cancellation.
1021 /* Cancel deadline, and inform the client about query cancellation. */
1022 _sdkctl_query_cancel_timeout(query);
1023 query->query_cb(query->query_opaque, query, ASIO_STATE_CANCELLED);
1026 /* A timer callback that is invoked on query timeout.
1033 SDKCtlQuery* const query = (SDKCtlQuery*)opaque;
1036 query->sdkctl->service_name, query, query->header.query_id,
1037 query->deadline, async_socket_deadline(query->sdkctl->as, 0));
1039 /* Reference the query while we're in this callback. */
1040 sdkctl_query_reference(query);
1043 * extend the deadline, and retry the query. */
1045 query->query_cb(query->query_opaque, query, ASIO_STATE_TIMED_OUT);
1047 /* For actions other than retry we will destroy the query. */
1049 _sdkctl_socket_remove_query(query);
1052 sdkctl_query_release(query);
1055 /* A callback that is invoked when query has been sent to the SDK controller. */
1057 _on_sdkctl_query_sent(SDKCtlQuery* query)
1059 T("SDKCtl %s: Sent %d bytes of query %p ID %d of type %d",
1060 query->sdkctl->service_name, query->header.packet.size, query,
1061 query->header.query_id, query->header.query_type);
1064 query->query_cb(query->query_opaque, query, ASIO_STATE_CONTINUES);
1066 /* Set a timer to expire at query's deadline, and let the response to come
1068 loopTimer_startAbsolute(query->timer, query->deadline);
1071 /* An I/O callback invoked on query transmission.
1073 * io_opaque SDKCtlQuery instance of the query that's being sent with this I/O.
1082 SDKCtlQuery* const query = (SDKCtlQuery*)io_opaque;
1085 /* Reference the query while we're in this callback. */
1086 sdkctl_query_reference(query);
1088 /* Lets see what's going on with query transmission. */
1092 _on_sdkctl_query_sent(query);
1097 query->sdkctl->service_name, query, query->header.query_id);
1098 /* Remove the query from the list of active queries. */
1099 _sdkctl_socket_remove_query(query);
1100 _on_sdkctl_query_cancelled(query);
1105 query->sdkctl->service_name, query, query->header.query_id,
1106 query->deadline, async_socket_deadline(query->sdkctl->as, 0));
1107 /* Invoke query's callback. */
1108 action = query->query_cb(query->query_opaque, query, status);
1109 /* For actions other than retry we need to stop the query. */
1111 _sdkctl_socket_remove_query(query);
1117 query->sdkctl->service_name, query, query->header.query_id,
1119 /* Invoke query's callback. Note that we will let the client to
1121 action = query->query_cb(query->query_opaque, query, status);
1122 /* For actions other than retry we need to stop the query. */
1124 _sdkctl_socket_remove_query(query);
1130 sdkctl_query_release(query);
1138 sdkctl_query_release(query);
1150 SDKCtlQuery* const query =
1152 query->next = NULL;
1153 query->sdkctl = sdkctl;
1154 query->response_buffer = NULL;
1155 query->response_size = NULL;
1156 query->internal_resp_buffer = NULL;
1157 query->internal_resp_size = 0;
1158 query->query_cb = NULL;
1159 query->query_opaque = NULL;
1160 query->deadline = DURATION_INFINITE;
1161 query->ref_count = 1;
1162 query->header.packet.signature = _sdkctl_packet_sig;
1163 query->header.packet.size = sizeof(SDKCtlQueryHeader) + in_data_size;
1164 query->header.packet.type = SDKCTL_PACKET_QUERY;
1165 query->header.query_id = _sdkctl_socket_next_query_id(sdkctl);
1166 query->header.query_type = query_type;
1168 /* Initialize timer to fire up on query deadline expiration. */
1169 loopTimer_init(query->timer, sdkctl->looper, _on_skdctl_query_timeout, query);
1171 /* Reference socket that owns this query. */
1175 query->sdkctl->service_name, query, query->header.query_id,
1178 return query;
1191 SDKCtlQuery* const query = sdkctl_query_new(sdkctl, query_type, in_data_size);
1193 query->response_buffer = response_buffer;
1194 if (query->response_buffer == NULL) {
1196 query->response_buffer = &query->internal_resp_buffer;
1198 query->response_size = response_size;
1199 if (query->response_size == NULL) {
1202 query->response_size = &query->internal_resp_size;
1204 query->query_cb = query_cb;
1205 query->query_opaque = query_opaque;
1206 /* Init query's input buffer. */
1208 memcpy(query + 1, in_data, in_data_size);
1211 return query;
1215 sdkctl_query_send(SDKCtlQuery* query, int to)
1217 SDKCtlSocket* const sdkctl = query->sdkctl;
1220 query->deadline = async_socket_deadline(query->sdkctl->as, to);
1222 /* List the query in the list of active queries. */
1223 _sdkctl_socket_add_query(query);
1225 /* Reference query associated with write I/O. */
1226 sdkctl_query_reference(query);
1228 assert(query->header.packet.signature == _sdkctl_packet_sig);
1229 /* Transmit the query to SDK controller. */
1230 async_socket_write_abs(sdkctl->as, &query->header, query->header.packet.size,
1231 _on_sdkctl_query_send_io, query, query->deadline);
1234 query->sdkctl->service_name, query, query->header.query_id,
1235 query->header.query_type, query->deadline);
1249 SDKCtlQuery* const query =
1253 sdkctl_query_send(query, to);
1254 return query;
1258 sdkctl_query_reference(SDKCtlQuery* query)
1260 assert(query->ref_count > 0);
1261 query->ref_count++;
1262 return query->ref_count;
1266 sdkctl_query_release(SDKCtlQuery* query)
1268 assert(query->ref_count > 0);
1269 query->ref_count--;
1270 if (query->ref_count == 0) {
1272 _sdkctl_query_free(query);
1275 return query->ref_count;
1279 sdkctl_query_get_buffer_in(SDKCtlQuery* query)
1282 return query + 1;
1286 sdkctl_query_get_buffer_out(SDKCtlQuery* query)
1288 return query->response_buffer != NULL ? *query->response_buffer :
1289 query->internal_resp_buffer;
1297 * Note that we expect the packet to be a message, since queries, and query
1382 /* Cancel current query. */
1384 SDKCtlQuery* const query = dispatcher->current_query;
1386 _on_sdkctl_query_cancelled(query);
1387 sdkctl_query_release(query);
1434 /* If we're in the middle of receiving query reply we need to cancel the
1435 * query. */
1437 SDKCtlQuery* const query = dispatcher->current_query;
1439 _on_sdkctl_query_cancelled(query);
1440 sdkctl_query_release(query);
1476 * - Response to a query that has been sent to SDK controller,
1477 * - A query from SDK controller.
1482 /* This is a response to the query. Before receiving response data we
1483 * need to locate the relevant query, and use its response buffer to read
1484 * the data. For that we need to obtain query ID firts. So, initiate
1529 /* A query reply header has been received by I/O dispatcher. */
1535 SDKCtlQuery* query;
1537 T("SDKCtl %s: Query reply header is received for query ID %d",
1540 /* Pull the query out of the list of active queries. It's the dispatcher that
1541 * owns this query now. */
1544 query = dispatcher->current_query;
1549 if (query == NULL) {
1559 /* Copy query reply info to the packet. */
1565 /* Prepare to receive query reply. For the simplicity sake, cancel query
1567 * receiving query's reply. */
1568 _sdkctl_query_cancel_timeout(query);
1570 if (*query->response_size < query_data_size) {
1571 *query->response_buffer = malloc(query_data_size);
1572 if (*query->response_buffer == NULL) {
1573 APANIC("%s: Unable to allocate %d bytes for query response",
1577 /* Save the actual query response size. */
1578 *query->response_size = query_data_size;
1580 /* Start reading query response. */
1581 async_socket_read_rel(sdkctl->as, *query->response_buffer,
1582 *query->response_size, _on_sdkctl_io_dispatcher_io,
1589 /* A query reply header has been received by I/O dispatcher. */
1594 SDKCtlQuery* const query = dispatcher->current_query;
1597 if (query != NULL) {
1598 _ANDROID_ASSERT(query->header.query_id == dispatcher->query_reply_header.query_id,
1601 T("SDKCtl %s: Query reply is received for query %p ID %d. Reply size is %d",
1602 dispatcher->sdkctl->service_name, query, query->header.query_id,
1603 *query->response_size);
1605 /* Complete the query, and release it from the dispatcher. */
1606 _on_sdkctl_query_completed(query);
1607 sdkctl_query_release(query);
1609 /* This was "read up in the air" for a cancelled query. Just discard the
1707 /* Query reply is received. Complete the query. */
1736 SDKCtlQuery* query;
1738 /* Cancel query that is being completed in dispatcher. */
1740 SDKCtlQuery* const query = dispatcher->current_query;
1742 _on_sdkctl_query_cancelled(query);
1743 sdkctl_query_release(query);
1746 /* One by one empty query list cancelling pulled queries. */
1747 query = _sdkctl_socket_pull_first_query(sdkctl);
1748 while (query != NULL) {
1749 _sdkctl_query_cancel_timeout(query);
1750 query->query_cb(query->query_opaque, query, ASIO_STATE_CANCELLED);
1751 sdkctl_query_release(query);
1752 query = _sdkctl_socket_pull_first_query(sdkctl);
1820 /* Initiates handshake query when SDK controller socket is connected. */
2073 * Handshake query
2086 /* Handshake has failed due to unknown query. */
2092 SDKCtlQuery* query,
2098 const int* res = (const int*)(*query->response_buffer);
2120 D("SDKCtl %s: Handshake failed: unknown query.",
2160 D("SDKCtl %s: Sending handshake query...", sdkctl->service_name);
2161 SDKCtlQuery* query =
2166 sdkctl_query_release(query);