Mohamed Abukar | 3e03815 | 2020-03-04 10:01:45 +0200 | [diff] [blame^] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2020 AT&T Intellectual Property. |
| 4 | Copyright (c) 2020 Nokia |
| 5 | |
| 6 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | you may not use this file except in compliance with the License. |
| 8 | You may obtain a copy of the License at |
| 9 | |
| 10 | http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | |
| 12 | Unless required by applicable law or agreed to in writing, software |
| 13 | distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | See the License for the specific language governing permissions and |
| 16 | limitations under the License. |
| 17 | ================================================================================== |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include "utils.h" |
| 22 | |
| 23 | void * rmrInit(void) { |
| 24 | void* mrc; // msg router context |
| 25 | |
| 26 | if( (mrc = rmr_init("tcp:4588", 1024, RMRFL_NONE)) == NULL ) { |
| 27 | fprintf(stderr, "Unable to initialize RMR\n"); |
| 28 | return NULL; |
| 29 | } |
| 30 | |
| 31 | // Must have a route table before we can send, so wait til RMR is ready |
| 32 | while(!rmr_ready(mrc)) { |
| 33 | fprintf(stderr, "Waiting for RMR to be ready ...\n"); |
| 34 | sleep(1); |
| 35 | } |
| 36 | fprintf(stderr, "RMR is ready now ...\n"); |
| 37 | |
| 38 | return mrc; |
| 39 | } |
| 40 | |
| 41 | int rmrSend(void *mrc, int mtype, void *payload, int payload_len, char *meid) { |
| 42 | int retry_count = 0; |
| 43 | rmr_mbuf_t *sbuf = rmr_alloc_msg(mrc, 1024); |
| 44 | |
| 45 | sbuf->mtype = mtype; |
| 46 | sbuf->sub_id = RMR_VOID_SUBID; |
| 47 | sbuf->state = 0; |
| 48 | sbuf->len = payload_len; |
| 49 | memcpy(sbuf->payload, payload, payload_len); |
| 50 | rmr_str2meid(sbuf, meid); |
| 51 | |
| 52 | do { |
| 53 | sbuf = rmr_send_msg(mrc, sbuf); |
| 54 | if (sbuf == NULL) { |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | if (sbuf->state == RMR_OK) { |
| 59 | break; |
| 60 | } |
| 61 | } while(sbuf->state == RMR_ERR_RETRY && ++retry_count < 10); |
| 62 | |
| 63 | return sbuf->state; |
| 64 | } |
| 65 | |
| 66 | rmr_mbuf_t * rmrRcv(void *mrc) { |
| 67 | while(1) { |
| 68 | rmr_mbuf_t *rbuf = rmr_rcv_msg(mrc, NULL); |
| 69 | if (rbuf != NULL && rbuf->state == RMR_OK) { |
| 70 | return rbuf; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return NULL; |
| 75 | } |
| 76 | |