embb_mtapi_network.c 34.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

27
#ifdef _WIN32
28
#include <WinSock2.h>
29
#endif
30

31 32 33 34 35 36 37 38 39 40 41 42
#include <embb/mtapi/c/mtapi_network.h>
#include <embb/base/c/memory_allocation.h>
#include <embb/base/c/thread.h>
#include <embb/base/c/atomic.h>
#include <embb/base/c/mutex.h>
#include <embb/base/c/internal/unused.h>
#include <embb_mtapi_network_socket.h>
#include <embb_mtapi_network.h>

#include <embb_mtapi_task_t.h>
#include <embb_mtapi_action_t.h>
#include <embb_mtapi_node_t.h>
43
#include <embb_mtapi_group_t.h>
44 45
#include <mtapi_status_t.h>

46 47
#include <assert.h>

48
int embb_mtapi_network_initialize() {
49
#ifdef _WIN32
50 51 52 53 54 55 56 57 58 59 60 61
  WORD ver_request;
  WSADATA wsa_data;
  int err;

  ver_request = MAKEWORD(2, 2);

  err = WSAStartup(ver_request, &wsa_data);
  if (err != 0) {
    return 0;
  } else {
    return 1;
  }
62 63 64
#else
  return 1;
#endif
65 66 67
}

void embb_mtapi_network_finalize() {
68
#ifdef _WIN32
69
  WSACleanup();
70
#endif
71
}
72 73

enum embb_mtapi_network_operation_enum {
74 75
  EMBB_MTAPI_NETWORK_START_TASK = 0x01AFFE01,
  EMBB_MTAPI_NETWORK_RETURN_RESULT = 0x02AFFE02,
76 77
  EMBB_MTAPI_NETWORK_RETURN_FAILURE = 0x03AFFE03,
  EMBB_MTAPI_NETWORK_CANCEL_TASK = 0x04AFFE04
78 79 80 81 82 83 84 85
};

struct embb_mtapi_network_plugin_struct {
  embb_thread_t thread;
  embb_mtapi_network_socket_t *sockets; // sockets[0] is the listening socket
  int socket_count;
  embb_atomic_int run;
  mtapi_size_t buffer_size;
86 87 88

  embb_mutex_t send_mutex;
  embb_mtapi_network_buffer_t send_buffer;
89 90

  embb_mtapi_network_buffer_t recv_buffer;
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
};

typedef struct embb_mtapi_network_plugin_struct embb_mtapi_network_plugin_t;

static embb_mtapi_network_plugin_t embb_mtapi_network_plugin;

struct embb_mtapi_network_action_struct {
  mtapi_domain_t domain_id;
  mtapi_job_id_t job_id;

  char const * host;
  mtapi_uint16_t port;
  embb_mtapi_network_socket_t socket;

  embb_mutex_t send_mutex;
  embb_mtapi_network_buffer_t send_buffer;
};

typedef struct embb_mtapi_network_action_struct embb_mtapi_network_action_t;

struct embb_mtapi_network_task_struct {
  embb_mtapi_network_socket_t socket;
  int32_t remote_task_id;
  int32_t remote_task_tag;
};

typedef struct embb_mtapi_network_task_struct embb_mtapi_network_task_t;

119 120 121 122 123
static void embb_mtapi_network_return_failure(
  int32_t remote_task_id,
  int32_t remote_task_tag,
  mtapi_status_t status,
  embb_mtapi_network_socket_t * socket,
124
  embb_mtapi_network_buffer_t * buffer) {
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  embb_mtapi_network_buffer_clear(buffer);

  // packet size
  embb_mtapi_network_buffer_push_back_int32(
    buffer, 16);

  // operation
  embb_mtapi_network_buffer_push_back_int32(
    buffer, EMBB_MTAPI_NETWORK_RETURN_FAILURE);

  // task handle
  embb_mtapi_network_buffer_push_back_int32(
    buffer, remote_task_id);
  embb_mtapi_network_buffer_push_back_int32(
    buffer, remote_task_tag);
140

141 142 143 144 145 146
  // status
  embb_mtapi_network_buffer_push_back_int32(
    buffer, (int32_t)status);

  embb_mtapi_network_socket_sendbuffer(
    socket, buffer);
147 148
}

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
static void embb_mtapi_network_task_complete(
  MTAPI_IN mtapi_task_hndl_t task,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();

    if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) {
      embb_mtapi_task_t * local_task =
        embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task);

      if (embb_mtapi_action_pool_is_handle_valid(
        node->action_pool, local_task->action)) {
        /* not needed right now
        embb_mtapi_action_t * local_action =
          embb_mtapi_action_pool_get_storage_for_handle(
          node->action_pool, local_task->action);*/

        embb_mtapi_network_plugin_t * plugin = &embb_mtapi_network_plugin;
        embb_mtapi_network_task_t * network_task =
          (embb_mtapi_network_task_t*)local_task->attributes.user_data;
171
        embb_mtapi_network_buffer_t * send_buf = &plugin->send_buffer;
172

173 174 175 176
        embb_atomic_memory_barrier();
        local_task->attributes.complete_func = NULL;
        embb_atomic_memory_barrier();

177 178 179
        // serialize sending of results
        embb_mutex_lock(&plugin->send_mutex);
        embb_mtapi_network_buffer_clear(send_buf);
180

181 182 183 184 185 186 187 188 189 190 191 192
        if (local_task->error_code == MTAPI_SUCCESS) {
          // actual counts bytes actually put into the buffer
          int actual = 0;
          // expected counts bytes we intended to put into the buffer
          int expected =
            4 +                               // operation
            4 + 4 +                           // remote task handle
            4 +                               // status
            4 + (int)local_task->result_size; // result buffer

                                              // packet size
          actual += embb_mtapi_network_buffer_push_back_int32(
193
            send_buf, expected);
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
          expected += 4;

          // operation is "return result"
          actual += embb_mtapi_network_buffer_push_back_int32(
            send_buf, EMBB_MTAPI_NETWORK_RETURN_RESULT);

          // remote task id
          actual += embb_mtapi_network_buffer_push_back_int32(
            send_buf, network_task->remote_task_id);
          actual += embb_mtapi_network_buffer_push_back_int32(
            send_buf, network_task->remote_task_tag);

          // status
          actual += embb_mtapi_network_buffer_push_back_int32(
            send_buf, local_task->error_code);

          // result size
          actual += embb_mtapi_network_buffer_push_back_int32(
            send_buf, (int32_t)local_task->result_size);
          actual += embb_mtapi_network_buffer_push_back_rawdata(
            send_buf, (int32_t)local_task->result_size,
            local_task->result_buffer);

          if (expected == actual) {
            int sent = embb_mtapi_network_socket_sendbuffer(
              &network_task->socket, send_buf);
            assert(sent == send_buf->size);
221
            EMBB_UNUSED_IN_RELEASE(sent);
222
          } else {
223 224 225 226 227 228 229 230 231 232 233
            embb_mtapi_network_return_failure(
              network_task->remote_task_id,
              network_task->remote_task_tag,
              MTAPI_ERR_UNKNOWN,
              &network_task->socket, send_buf);
          }
        } else {
          embb_mtapi_network_return_failure(
            network_task->remote_task_id,
            network_task->remote_task_tag,
            local_task->error_code,
234 235
            &network_task->socket, send_buf);
        }
236

237 238
        // sending done
        embb_mutex_unlock(&plugin->send_mutex);
239 240 241 242 243

        // we allocated arguments and results on receive, so free them here
        embb_free((void*)local_task->arguments);
        embb_free(local_task->result_buffer);

244 245 246 247 248 249 250 251
        void * data = local_task->attributes.user_data;

        embb_atomic_memory_barrier();
        local_task->attributes.user_data = NULL;
        embb_atomic_memory_barrier();

        embb_free(data);

252 253 254 255 256 257 258 259
        local_status = MTAPI_SUCCESS;
      }
    }
  }

  mtapi_status_set(status, local_status);
}

260 261
static mtapi_status_t embb_mtapi_network_handle_start_task(
  embb_mtapi_network_socket_t * socket,
262 263
  embb_mtapi_network_buffer_t * buffer,
  int packet_size) {
264 265 266 267 268
  int32_t domain_id;
  int32_t job_id;
  int32_t results_size;
  void * results;
  int err;
269
  EMBB_UNUSED_IN_RELEASE(err);
270 271

  int32_t arguments_size;
272 273
  int32_t remote_task_id;
  int32_t remote_task_tag;
274 275 276 277 278 279 280 281
  mtapi_uint_t priority = 0;
  mtapi_job_hndl_t job_hndl;
  mtapi_task_attributes_t task_attr;
  void * arguments;
  mtapi_task_complete_function_t func = embb_mtapi_network_task_complete;
  void * func_void;
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

282 283
  // check if we have at least 28 bytes
  if (packet_size >= 28) {
284 285 286 287 288 289 290 291 292 293 294 295
    // domain id
    err = embb_mtapi_network_buffer_pop_front_int32(buffer, &domain_id);
    assert(err == 4);
    // job id
    err = embb_mtapi_network_buffer_pop_front_int32(buffer, &job_id);
    assert(err == 4);
    // priority
    err = embb_mtapi_network_buffer_pop_front_int32(
      buffer, (int32_t*)&priority);
    assert(err == 4);
    // remote task handle
    err = embb_mtapi_network_buffer_pop_front_int32(
296
      buffer, &remote_task_id);
297 298
    assert(err == 4);
    err = embb_mtapi_network_buffer_pop_front_int32(
299
      buffer, &remote_task_tag);
300 301 302 303 304 305
    assert(err == 4);
    // result size
    err = embb_mtapi_network_buffer_pop_front_int32(buffer,
      &results_size);
    assert(err == 4);
    // arguments size
306
    err = embb_mtapi_network_buffer_pop_front_int32(buffer, &arguments_size);
307 308
    assert(err == 4);

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
    embb_mtapi_network_task_t * network_task =
      (embb_mtapi_network_task_t*)embb_alloc(
        sizeof(embb_mtapi_network_task_t));

    if (network_task == NULL) {
      embb_mtapi_network_return_failure(
        remote_task_id, remote_task_tag, MTAPI_ERR_UNKNOWN,
        socket, buffer);
      return MTAPI_ERR_UNKNOWN;
    }
    network_task->remote_task_id = remote_task_id;
    network_task->remote_task_tag = remote_task_tag;

    // check packet_size again
    if (packet_size == 28 + arguments_size) {
      // allocate buffers
      results = embb_alloc((size_t)results_size);
      if (results == NULL) {
        embb_mtapi_network_return_failure(
          remote_task_id, remote_task_tag, MTAPI_ERR_UNKNOWN,
          socket, buffer);
        return MTAPI_ERR_UNKNOWN;
      }
      arguments = embb_alloc((size_t)arguments_size);
      if (arguments == NULL) {
        embb_free(results);
        embb_mtapi_network_return_failure(
          remote_task_id, remote_task_tag, MTAPI_ERR_UNKNOWN,
          socket, buffer);
        return MTAPI_ERR_UNKNOWN;
      }

      // arguments
      err = embb_mtapi_network_buffer_pop_front_rawdata(
        buffer, arguments_size, arguments);
      assert(err == arguments_size);

      network_task->socket = *socket;
      mtapi_taskattr_init(&task_attr, &local_status);
      assert(local_status == MTAPI_SUCCESS);
      mtapi_taskattr_set(&task_attr, MTAPI_TASK_USER_DATA,
        (void*)network_task, 0, &local_status);
      assert(local_status == MTAPI_SUCCESS);
      mtapi_boolean_t task_detached = MTAPI_TRUE;
      mtapi_taskattr_set(&task_attr, MTAPI_TASK_DETACHED,
        (void*)&task_detached, sizeof(mtapi_boolean_t), &local_status);
      assert(local_status == MTAPI_SUCCESS);
      mtapi_taskattr_set(&task_attr, MTAPI_TASK_PRIORITY,
        (void*)&priority, sizeof(mtapi_uint_t), &local_status);
      assert(local_status == MTAPI_SUCCESS);
      memcpy(&func_void, &func, sizeof(void*));
      mtapi_taskattr_set(&task_attr, MTAPI_TASK_COMPLETE_FUNCTION,
        func_void, 0, &local_status);
      assert(local_status == MTAPI_SUCCESS);
      job_hndl = mtapi_job_get((mtapi_job_id_t)job_id,
        (mtapi_domain_t)domain_id, &local_status);
      if (local_status == MTAPI_SUCCESS) {
        mtapi_task_start(
          MTAPI_TASK_ID_NONE, job_hndl,
          arguments, (mtapi_size_t)arguments_size,
          results, (mtapi_size_t)results_size,
          &task_attr, MTAPI_GROUP_NONE,
          &local_status);
      }
      if (local_status != MTAPI_SUCCESS) {
        embb_free(arguments);
        embb_free(results);
        embb_mtapi_network_return_failure(
          remote_task_id, remote_task_tag, local_status, socket, buffer);
      }
    }
  }
381 382 383 384 385

  return local_status;
}

static mtapi_status_t embb_mtapi_network_handle_return_result(
386 387
  embb_mtapi_network_buffer_t * buffer,
  int packet_size) {
388 389 390
  int32_t task_status;
  int32_t task_id;
  int32_t task_tag;
391 392 393

  int32_t results_size;
  int err;
394
  EMBB_UNUSED_IN_RELEASE(err);
395 396 397 398 399 400
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();
    mtapi_task_hndl_t task;

401 402
    // do we have at least 16 bytes?
    if (packet_size >= 16) {
403 404 405 406 407 408 409 410 411 412 413 414 415 416
      // local task id
      err = embb_mtapi_network_buffer_pop_front_int32(buffer, &task_id);
      assert(err == 4);
      err = embb_mtapi_network_buffer_pop_front_int32(buffer, &task_tag);
      assert(err == 4);
      // task status
      err = embb_mtapi_network_buffer_pop_front_int32(
        buffer, &task_status);
      assert(err == 4);
      // result size
      err = embb_mtapi_network_buffer_pop_front_int32(
        buffer, &results_size);
      assert(err == 4);

417 418
      // check packet_size again
      if (packet_size == 16 + results_size) {
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
        task.id = (mtapi_task_id_t)task_id;
        task.tag = (mtapi_uint_t)task_tag;

        if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) {
          embb_mtapi_task_t * local_task =
            embb_mtapi_task_pool_get_storage_for_handle(
              node->task_pool, task);

          if (embb_mtapi_action_pool_is_handle_valid(
            node->action_pool, local_task->action)) {
            embb_mtapi_action_t * local_action =
              embb_mtapi_action_pool_get_storage_for_handle(
                node->action_pool, local_task->action);

            /* not needed right now
            embb_mtapi_network_action_t * network_action =
            (embb_mtapi_network_action_t*)local_action->plugin_data;*/

            err = embb_mtapi_network_buffer_pop_front_rawdata(
              buffer, results_size, local_task->result_buffer);
            assert(err == results_size);

            local_task->error_code = (mtapi_status_t)task_status;
            embb_atomic_store_int(&local_task->state, MTAPI_TASK_COMPLETED);
            embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1);

            /* is task associated with a group? */
            if (embb_mtapi_group_pool_is_handle_valid(
              node->group_pool, local_task->group)) {
              embb_mtapi_group_t* local_group =
                embb_mtapi_group_pool_get_storage_for_handle(
                  node->group_pool, local_task->group);
              embb_mtapi_task_queue_push(&local_group->queue, local_task);
            }

            local_status = MTAPI_SUCCESS;
          }
        }
      }
    }
  }

461 462 463 464 465 466
  return local_status;
}

static mtapi_status_t embb_mtapi_network_handle_return_failure(
  embb_mtapi_network_buffer_t * buffer,
  int packet_size) {
467 468 469
  int32_t task_status;
  int32_t task_id;
  int32_t task_tag;
470 471

  int err;
472
  EMBB_UNUSED_IN_RELEASE(err);
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();
    mtapi_task_hndl_t task;

    // do we have 12 bytes?
    if (packet_size == 12) {
      // local task id
      err = embb_mtapi_network_buffer_pop_front_int32(buffer, &task_id);
      assert(err == 4);
      err = embb_mtapi_network_buffer_pop_front_int32(buffer, &task_tag);
      assert(err == 4);
      // task status
      err = embb_mtapi_network_buffer_pop_front_int32(
        buffer, &task_status);
      assert(err == 4);

      task.id = (mtapi_task_id_t)task_id;
      task.tag = (mtapi_uint_t)task_tag;

      if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) {
        embb_mtapi_task_t * local_task =
          embb_mtapi_task_pool_get_storage_for_handle(
            node->task_pool, task);

        if (embb_mtapi_action_pool_is_handle_valid(
          node->action_pool, local_task->action)) {
          embb_mtapi_action_t * local_action =
            embb_mtapi_action_pool_get_storage_for_handle(
              node->action_pool, local_task->action);

          embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1);
506 507 508 509 510 511
          local_task->error_code = (mtapi_status_t)task_status;
          if (MTAPI_ERR_ACTION_CANCELLED == task_status) {
            embb_atomic_store_int(&local_task->state, MTAPI_TASK_CANCELLED);
          } else {
            embb_atomic_store_int(&local_task->state, MTAPI_TASK_ERROR);
          }
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

          /* is task associated with a group? */
          if (embb_mtapi_group_pool_is_handle_valid(
            node->group_pool, local_task->group)) {
            embb_mtapi_group_t* local_group =
              embb_mtapi_group_pool_get_storage_for_handle(
                node->group_pool, local_task->group);
            embb_mtapi_task_queue_push(&local_group->queue, local_task);
          }

          local_status = MTAPI_SUCCESS;
        }
      }
    }
  }
527 528 529 530

  return local_status;
}

531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
static mtapi_status_t embb_mtapi_network_handle_cancel_task(
  embb_mtapi_network_buffer_t * buffer,
  int packet_size) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  int32_t remote_task_id;
  int32_t remote_task_tag;
  int err;
  EMBB_UNUSED_IN_RELEASE(err);

  // do we have 8 bytes?
  if (packet_size == 8) {
    // get task handle
    err = embb_mtapi_network_buffer_pop_front_int32(buffer, &remote_task_id);
    assert(err == 4);
    err = embb_mtapi_network_buffer_pop_front_int32(buffer, &remote_task_tag);
    assert(err == 4);

    if (embb_mtapi_node_is_initialized()) {
      embb_mtapi_node_t * node = embb_mtapi_node_get_instance();

      // search for task to cancel
      for (mtapi_uint_t ii = 0; ii < node->attributes.max_tasks; ii++) {
        embb_mtapi_task_t * task = &node->task_pool->storage[ii];
        // is this our task?
555 556
        if (embb_mtapi_network_task_complete ==
            task->attributes.complete_func) {
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
          embb_mtapi_network_task_t * network_task =
            (embb_mtapi_network_task_t*)task->attributes.user_data;
          // is this task the one matching the given remote task?
          if (remote_task_id == network_task->remote_task_id &&
            remote_task_tag == network_task->remote_task_tag) {
            mtapi_task_cancel(task->handle, &local_status);
            break;
          }
        }
      }
    }
  }

  return local_status;
}

573 574
static int embb_mtapi_network_thread(void * args) {
  embb_mtapi_network_plugin_t * plugin = &embb_mtapi_network_plugin;
575
  embb_mtapi_network_buffer_t * buffer = &plugin->recv_buffer;
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
  int err;

  EMBB_UNUSED(args);

  while (embb_atomic_load_int(&plugin->run)) {
    err = embb_mtapi_network_socket_select(
      plugin->sockets, plugin->socket_count, 100);
    if (0 == err) {
      // listening socket, accept connection
      embb_mtapi_network_socket_t accept_socket;
      err = embb_mtapi_network_socket_accept(
        &plugin->sockets[0], &accept_socket);
      if (0 < err) {
        // add socket to socket list
        plugin->sockets[plugin->socket_count] = accept_socket;
        plugin->socket_count++;
      }
    } else if (0 < err) {
594
      int32_t operation;
595
      int32_t packet_size;
596 597
      embb_mtapi_network_socket_t * socket = &plugin->sockets[err];

598
      embb_mtapi_network_buffer_clear(buffer);
599

600
      err = embb_mtapi_network_socket_recvbuffer_sized(
601 602 603 604 605
        socket, buffer, 4);
      if (err == 4) {
        err = embb_mtapi_network_buffer_pop_front_int32(
          buffer, &packet_size);
        assert(err == 4);
606

607
        embb_mtapi_network_buffer_clear(buffer);
608 609 610
        err = embb_mtapi_network_socket_recvbuffer_sized(
          socket, buffer, packet_size);
        if (err == packet_size) {
611
          err = embb_mtapi_network_buffer_pop_front_int32(
612
            buffer, &operation);
613 614
          assert(err == 4);
          packet_size -= 4;
615 616

          switch (operation) {
617
          case EMBB_MTAPI_NETWORK_START_TASK:
618
            embb_mtapi_network_handle_start_task(socket, buffer, packet_size);
619 620
            break;
          case EMBB_MTAPI_NETWORK_RETURN_RESULT:
621 622 623 624
            embb_mtapi_network_handle_return_result(buffer, packet_size);
            break;
          case EMBB_MTAPI_NETWORK_RETURN_FAILURE:
            embb_mtapi_network_handle_return_failure(buffer, packet_size);
625
            break;
626 627 628
          case EMBB_MTAPI_NETWORK_CANCEL_TASK:
            embb_mtapi_network_handle_cancel_task(buffer, packet_size);
            break;
629
          default:
630
            // invalid, ignore
631
            break;
632
          }
633 634
        }
      }
635 636

      embb_mtapi_network_buffer_clear(buffer);
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    }
  }

  return EMBB_SUCCESS;
}

void mtapi_network_plugin_initialize(
  MTAPI_IN char * host,
  MTAPI_IN mtapi_uint16_t port,
  MTAPI_IN mtapi_uint16_t max_connections,
  MTAPI_IN mtapi_size_t buffer_size,
  MTAPI_OUT mtapi_status_t* status) {
  embb_mtapi_network_plugin_t * plugin = &embb_mtapi_network_plugin;
  int err;

652 653 654 655 656 657
  mtapi_status_set(status, MTAPI_ERR_UNKNOWN);

  plugin->socket_count = 0;
  plugin->buffer_size = 0;
  plugin->sockets = NULL;

658
  err = embb_mtapi_network_initialize();
659 660
  if (0 == err) return;

661
  embb_atomic_init_int(&plugin->run, 0);
662

663 664 665
  err = embb_mtapi_network_buffer_initialize(
    &plugin->recv_buffer, (int)buffer_size);
  if (0 == err) {
666
    embb_atomic_destroy_int(&plugin->run);
667 668
    embb_mtapi_network_finalize();
    return;
669 670
  }

671 672 673 674
  err = embb_mtapi_network_buffer_initialize(
    &plugin->send_buffer, (int)buffer_size);
  if (0 == err) {
    embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);
675
    embb_atomic_destroy_int(&plugin->run);
676 677 678 679 680 681 682 683 684 685 686 687 688 689
    embb_mtapi_network_finalize();
    return;
  }

  plugin->buffer_size = buffer_size;

  // 1 listening socket and max_connections connections
  // (2 sockets each if local)
  plugin->sockets = (embb_mtapi_network_socket_t*)embb_alloc(
    sizeof(embb_mtapi_network_socket_t) * (1 + max_connections * 2));
  if (NULL == plugin->sockets) {
    embb_mtapi_network_buffer_finalize(&plugin->send_buffer);
    embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);
    plugin->buffer_size = 0;
690
    embb_atomic_destroy_int(&plugin->run);
691 692 693 694 695 696 697 698 699 700 701
    embb_mtapi_network_finalize();
    return;
  }

  err = embb_mutex_init(&plugin->send_mutex, 0);
  if (EMBB_SUCCESS != err) {
    embb_free(plugin->sockets);
    plugin->sockets = NULL;
    embb_mtapi_network_buffer_finalize(&plugin->send_buffer);
    embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);
    plugin->buffer_size = 0;
702
    embb_atomic_destroy_int(&plugin->run);
703 704 705 706 707 708 709 710 711 712 713 714
    embb_mtapi_network_finalize();
    return;
  }

  err = embb_mtapi_network_socket_initialize(&plugin->sockets[0]);
  if (0 == err) {
    embb_mutex_destroy(&plugin->send_mutex);
    embb_free(plugin->sockets);
    plugin->sockets = NULL;
    embb_mtapi_network_buffer_finalize(&plugin->send_buffer);
    embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);
    plugin->buffer_size = 0;
715
    embb_atomic_destroy_int(&plugin->run);
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    embb_mtapi_network_finalize();
    return;
  }
  plugin->socket_count = 1;

  err = embb_mtapi_network_socket_bind_and_listen(
    &plugin->sockets[0], host, port, max_connections);
  if (0 == err) {
    embb_mtapi_network_socket_finalize(&plugin->sockets[0]);
    plugin->socket_count = 0;
    embb_mutex_destroy(&plugin->send_mutex);
    embb_free(plugin->sockets);
    plugin->sockets = NULL;
    embb_mtapi_network_buffer_finalize(&plugin->send_buffer);
    embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);
    plugin->buffer_size = 0;
732
    embb_atomic_destroy_int(&plugin->run);
733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
    embb_mtapi_network_finalize();
    return;
  }

  embb_atomic_store_int(&plugin->run, 1);

  err = embb_thread_create(
    &plugin->thread, NULL, embb_mtapi_network_thread, NULL);
  if (EMBB_SUCCESS != err) {
    embb_atomic_store_int(&plugin->run, 0);
    embb_mtapi_network_socket_finalize(&plugin->sockets[0]);
    plugin->socket_count = 0;
    embb_mutex_destroy(&plugin->send_mutex);
    embb_free(plugin->sockets);
    plugin->sockets = NULL;
    embb_mtapi_network_buffer_finalize(&plugin->send_buffer);
    embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);
    plugin->buffer_size = 0;
751
    embb_atomic_destroy_int(&plugin->run);
752 753 754 755 756
    embb_mtapi_network_finalize();
    return;
  }

  mtapi_status_set(status, MTAPI_SUCCESS);
757 758 759 760 761 762 763 764 765 766 767
}

void mtapi_network_plugin_finalize(
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_SUCCESS;
  embb_mtapi_network_plugin_t * plugin = &embb_mtapi_network_plugin;
  int err;

  embb_atomic_store_int(&plugin->run, 0);
  embb_thread_join(&plugin->thread, &err);

768 769 770
  embb_mutex_destroy(&plugin->send_mutex);
  embb_mtapi_network_buffer_finalize(&plugin->send_buffer);

771 772
  embb_mtapi_network_buffer_finalize(&plugin->recv_buffer);

773 774
  embb_mtapi_network_socket_finalize(&plugin->sockets[0]);
  embb_free(plugin->sockets);
775 776 777

  embb_atomic_destroy_int(&plugin->run);

778 779 780 781 782 783 784 785
  embb_mtapi_network_finalize();

  mtapi_status_set(status, local_status);
}

static void network_task_start(
  MTAPI_IN mtapi_task_hndl_t task,
  MTAPI_OUT mtapi_status_t* status) {
786 787
  // assume failure
  mtapi_status_set(status, MTAPI_ERR_UNKNOWN);
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();

    if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) {
      embb_mtapi_task_t * local_task =
        embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task);

      if (embb_mtapi_action_pool_is_handle_valid(
        node->action_pool, local_task->action)) {
        embb_mtapi_action_t * local_action =
          embb_mtapi_action_pool_get_storage_for_handle(
          node->action_pool, local_task->action);

        embb_mtapi_network_action_t * network_action =
          (embb_mtapi_network_action_t*)local_action->plugin_data;
        embb_mtapi_network_buffer_t * send_buf = &network_action->send_buffer;

        // serialize sending
        embb_mutex_lock(&network_action->send_mutex);
808 809 810 811 812
        embb_mtapi_network_buffer_clear(send_buf);

        // actual counts bytes actually put into the buffer
        int actual = 0;
        // expected counts bytes we intended to put into the buffer
813
        int expected =
Marcus Winter committed
814 815 816 817 818 819 820
          4 +                                  // operation
          4 +                                  // domain_id
          4 +                                  // job_id
          4 +                                  // priority
          4 + 4 +                              // task handle
          4 +                                  // result_size
          4 + (int)local_task->arguments_size; // arguments buffer
821 822 823 824 825

        // packet size
        actual += embb_mtapi_network_buffer_push_back_int32(
          send_buf, (int32_t)expected);
        expected += 4;
826 827

        // operation is "start task"
828
        actual += embb_mtapi_network_buffer_push_back_int32(
829 830
          send_buf, EMBB_MTAPI_NETWORK_START_TASK);

831
        // domain_id
832
        actual += embb_mtapi_network_buffer_push_back_int32(
833
          send_buf, (int32_t)network_action->domain_id);
834

835
        // job_id
836
        actual += embb_mtapi_network_buffer_push_back_int32(
837
          send_buf, (int32_t)network_action->job_id);
838

839
        // priority
840
        actual += embb_mtapi_network_buffer_push_back_int32(
841
          send_buf, (int32_t)local_task->attributes.priority);
842

843
        // task handle
844
        actual += embb_mtapi_network_buffer_push_back_int32(
845
          send_buf, (int32_t)local_task->handle.id);
846
        actual += embb_mtapi_network_buffer_push_back_int32(
847
          send_buf, (int32_t)local_task->handle.tag);
848

849
        // result size
850
        actual += embb_mtapi_network_buffer_push_back_int32(
851
          send_buf, (int32_t)local_task->result_size);
852

853
        // arguments buffer
854
        actual += embb_mtapi_network_buffer_push_back_int32(
855
          send_buf, (int32_t)local_task->arguments_size);
856
        actual += embb_mtapi_network_buffer_push_back_rawdata(
857
          send_buf, (int32_t)local_task->arguments_size, local_task->arguments);
858 859 860

        // check if everything fit into the buffer
        if (actual == expected) {
861 862
          embb_atomic_fetch_and_add_int(&local_action->num_tasks, 1);
          embb_atomic_store_int(&local_task->state, MTAPI_TASK_RUNNING);
863 864 865 866 867 868 869 870 871
          int sent = embb_mtapi_network_socket_sendbuffer(
            &network_action->socket, send_buf);
          // was everything sent?
          if (sent == send_buf->size) {
            // we've done it, success!
            mtapi_status_set(status, MTAPI_SUCCESS);
          } else {
            // could not send the whole task, this will fail on the remote side,
            // so we can safely assume that the task is in error
872
            embb_atomic_fetch_and_add_int(&local_action->num_tasks, -1);
873 874 875
            embb_atomic_store_int(&local_task->state, MTAPI_TASK_ERROR);
          }
        }
876 877 878 879 880 881 882 883 884 885 886

        embb_mtapi_network_buffer_clear(send_buf);
        embb_mutex_unlock(&network_action->send_mutex);
      }
    }
  }
}

static void network_task_cancel(
  MTAPI_IN mtapi_task_hndl_t task,
  MTAPI_OUT mtapi_status_t* status) {
887 888
  // assume failure
  mtapi_status_set(status, MTAPI_ERR_UNKNOWN);
889

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952
  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();

    if (embb_mtapi_task_pool_is_handle_valid(node->task_pool, task)) {
      embb_mtapi_task_t * local_task =
        embb_mtapi_task_pool_get_storage_for_handle(node->task_pool, task);

      if (embb_mtapi_action_pool_is_handle_valid(
        node->action_pool, local_task->action)) {
        embb_mtapi_action_t * local_action =
          embb_mtapi_action_pool_get_storage_for_handle(
            node->action_pool, local_task->action);

        embb_mtapi_network_action_t * network_action =
          (embb_mtapi_network_action_t*)local_action->plugin_data;
        embb_mtapi_network_buffer_t * send_buf = &network_action->send_buffer;

        // serialize sending
        embb_mutex_lock(&network_action->send_mutex);
        embb_mtapi_network_buffer_clear(send_buf);

        // actual counts bytes actually put into the buffer
        int actual = 0;
        // expected counts bytes we intended to put into the buffer
        int expected =
          4 +    // operation
          4 + 4; // task handle

        // packet size
        actual += embb_mtapi_network_buffer_push_back_int32(
          send_buf, (int32_t)expected);
        expected += 4;

        // operation is "cancel task"
        actual += embb_mtapi_network_buffer_push_back_int32(
          send_buf, EMBB_MTAPI_NETWORK_CANCEL_TASK);

        // task handle
        actual += embb_mtapi_network_buffer_push_back_int32(
          send_buf, (int32_t)local_task->handle.id);
        actual += embb_mtapi_network_buffer_push_back_int32(
          send_buf, (int32_t)local_task->handle.tag);

        // check if everything fit into the buffer
        if (actual == expected) {
          int sent = embb_mtapi_network_socket_sendbuffer(
            &network_action->socket, send_buf);
          // was everything sent?
          if (sent == send_buf->size) {
            // we've done it, success!
            mtapi_status_set(status, MTAPI_SUCCESS);
          } else {
            embb_atomic_store_int(&local_task->state, MTAPI_TASK_ERROR);
          }
        } else {
          embb_atomic_store_int(&local_task->state, MTAPI_TASK_ERROR);
        }

        embb_mtapi_network_buffer_clear(send_buf);
        embb_mutex_unlock(&network_action->send_mutex);
      }
    }
  }
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
}

static void network_action_finalize(
  MTAPI_IN mtapi_action_hndl_t action,
  MTAPI_OUT mtapi_status_t* status
  ) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;

  if (embb_mtapi_node_is_initialized()) {
    embb_mtapi_node_t * node = embb_mtapi_node_get_instance();
    if (embb_mtapi_action_pool_is_handle_valid(node->action_pool, action)) {
      embb_mtapi_action_t * local_action =
        embb_mtapi_action_pool_get_storage_for_handle(
          node->action_pool, action);
      embb_mtapi_network_action_t * network_action =
        (embb_mtapi_network_action_t *)local_action->plugin_data;

      embb_mutex_destroy(&network_action->send_mutex);
      embb_mtapi_network_buffer_finalize(&network_action->send_buffer);
      embb_mtapi_network_socket_finalize(&network_action->socket);

      embb_free(network_action);
      local_status = MTAPI_SUCCESS;
    }
  }

  mtapi_status_set(status, local_status);
}

mtapi_action_hndl_t mtapi_network_action_create(
  MTAPI_IN mtapi_domain_t domain_id,
  MTAPI_IN mtapi_job_id_t local_job_id,
  MTAPI_IN mtapi_job_id_t remote_job_id,
  MTAPI_IN char * host,
  MTAPI_IN mtapi_uint16_t port,
  MTAPI_OUT mtapi_status_t* status) {
  mtapi_status_t local_status = MTAPI_ERR_UNKNOWN;
  embb_mtapi_network_plugin_t * plugin = &embb_mtapi_network_plugin;
  embb_mtapi_network_action_t * action =
    (embb_mtapi_network_action_t*)embb_alloc(
      sizeof(embb_mtapi_network_action_t));
  mtapi_action_hndl_t action_hndl = { 0, 0 };
  int err;

Marcus Winter committed
997 998 999 1000
  if (NULL != action) {
    action->domain_id = domain_id;
    action->job_id = remote_job_id;

1001
    err = embb_mtapi_network_buffer_initialize(
1002
      &action->send_buffer, (int)plugin->buffer_size);
Marcus Winter committed
1003
    if (0 != err) {
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
      err = embb_mutex_init(&action->send_mutex, 0);
      if (EMBB_SUCCESS == err) {
        action->host = host;
        action->port = port;
        embb_mtapi_network_socket_initialize(&action->socket);
        err = embb_mtapi_network_socket_connect(&action->socket, host, port);
        if (0 != err) {
          // store socket for select
          plugin->sockets[plugin->socket_count] = action->socket;
          plugin->socket_count++;

          action_hndl = mtapi_ext_plugin_action_create(
            local_job_id,
            network_task_start,
            network_task_cancel,
            network_action_finalize,
            action,
            NULL, 0, // no node local data obviously
            MTAPI_NULL,
            &local_status);
        } else {
          embb_mutex_destroy(&action->send_mutex);
          embb_mtapi_network_buffer_finalize(&action->send_buffer);
          embb_mtapi_network_socket_finalize(&action->socket);
          embb_free(action);
        }
      }
Marcus Winter committed
1031
    }
1032 1033
  }

Marcus Winter committed
1034
  mtapi_status_set(status, local_status);
1035 1036
  return action_hndl;
}