Newer
Older
1001
1002
1003
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
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
struct link_t* l = &(linkList->links[i]);
cJSON* item = cJSON_GetArrayItem(linkListArray, i);
// Get the link Id (uuid)
cJSON* linkIdObj = cJSON_GetObjectItem(item, "link_Id");
if (cJSON_IsString(linkIdObj)) {
duplicate_string(l->linkId, linkIdObj->valuestring);
//DEBUG_PC("Link (%d) -- Id: %s (uuid)", i + 1, l->linkId);
}
// Get the link endpoints (assumed to be p2p)
cJSON* endPointsLinkObj = cJSON_GetObjectItem(item, "link_endpoint_ids");
if (cJSON_IsArray(endPointsLinkObj)) {
parse_json_link_endpoints_array(endPointsLinkObj, l);
}
// get the fowarding direction
cJSON* fwdDirObj = cJSON_GetObjectItem(item, "forwarding_direction");
if (cJSON_IsNumber(fwdDirObj)) {
l->forwarding_direction = (guint)(fwdDirObj->valuedouble);
print_link_forwarding_direction(l->forwarding_direction);
}
// total potential capacity
cJSON* totalPotentialCapacityObj = cJSON_GetObjectItem(item, "total-potential-capacity");
if (cJSON_IsObject(totalPotentialCapacityObj))
{
parse_capacity_object(totalPotentialCapacityObj, &l->potential_capacity);
//DEBUG_PC("Link (%d) -- Potential Capacity: %f", i + 1, l->potential_capacity.value);
print_capacity_unit(l->potential_capacity.unit);
}
// total available capacity
cJSON* availableCapacityObj = cJSON_GetObjectItem(item, "available-capacity");
if (cJSON_IsObject(availableCapacityObj))
{
parse_capacity_object(availableCapacityObj, &l->available_capacity);
//DEBUG_PC("Link (%d) -- Available Capacity: %f", i + 1, l->available_capacity.value);
print_capacity_unit(l->available_capacity.unit);
}
// Cost Characteristics
cJSON* costCharacObj = cJSON_GetObjectItem(item, "cost-characteristics");
if (cJSON_IsObject(costCharacObj)) {
// Cost Name
cJSON* costNameObj = cJSON_GetObjectItem(costCharacObj, "cost-name");
if (cJSON_IsString(costNameObj)) {
duplicate_string(l->cost_characteristics.cost_name, costNameObj->valuestring);
//DEBUG_PC("Link (%d) -- Cost Name: %s", i + 1, l->cost_characteristics.cost_name);
}
// Cost value
cJSON* costValueObj = cJSON_GetObjectItem(costCharacObj, "cost-value");
if (cJSON_IsString(costValueObj)) {
char* endpr;
l->cost_characteristics.cost_value = (gdouble)(strtod(costValueObj->valuestring, &endpr));
//DEBUG_PC("Link (%d) -- Cost Value: %f", i + 1, l->cost_characteristics.cost_value);
}
// Cost Algorithm
cJSON* costAlgObj = cJSON_GetObjectItem(costCharacObj, "cost-algorithm");
if (cJSON_IsString(costAlgObj)) {
char* endpr;
l->cost_characteristics.cost_algorithm = (gdouble)(strtod(costAlgObj->valuestring, &endpr));
//DEBUG_PC("Link (%d) -- Cost Algorithm: %f", i + 1, l->cost_characteristics.cost_algorithm);
}
}
// Latency Characteristics
cJSON* latencyCharacObj = cJSON_GetObjectItem(item, "latency-characteristics");
if (cJSON_IsObject(latencyCharacObj)) {
cJSON* fixedLatencyCharacObj = cJSON_GetObjectItem(latencyCharacObj, "fixed-latency-characteristic");
if (cJSON_IsString(fixedLatencyCharacObj)) {
char* endpr;
l->latency_characteristics.fixed_latency = (gdouble)(strtod(fixedLatencyCharacObj->valuestring, &endpr));
//DEBUG_PC("Link (%d) -- Latency: %f", i + 1, l->latency_characteristics.fixed_latency);
}
}
}
return;
}
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
///////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to generate the reverse (unidirecitonal) link from those being learnt
* from the received context
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
////////////////////////////////////////////////////////////////////////////////////////
void generate_reverse_linkList() {
DEBUG_PC("Starting the Creation of the Reverse Links [current: %d]", linkList->numLinks);
gint numLinks = linkList->numLinks;
for (gint i = 0; i < numLinks; i++) {
struct link_t* refLink = &(linkList->links[i]);
struct link_t* newLink = &(linkList->links[numLinks + i]);
linkList->numLinks++;
// Copy the linkId + appending "_rev"
duplicate_string(newLink->linkId, refLink->linkId);
strcat(newLink->linkId, "_rev");
//DEBUG_PC("refLink: %s // newLink: %s", refLink->linkId, newLink->linkId);
// Assumption: p2p links. The newLink endpoints are the reversed ones form the reference Link (refLink)
// i.e., refLink A->B, then newLink B->A
#if 0
if (refLink->numLinkEndPointIds != 2) {
DEBUG_PC("To construct the new Link from ref, 2 EndPoints are a MUST");
exit(-1);
}
#endif
//DEBUG_PC("Number of Endpoints in Link: %d", refLink->numLinkEndPointIds);
for (gint j = refLink->numLinkEndPointIds - 1, m = 0; j >= 0; j--, m++) {
struct link_endpointId_t* refEndPId = &(refLink->linkEndPointId[j]);
struct link_endpointId_t* newEndPId = &(newLink->linkEndPointId[m]);
// Duplicate the topologyId information, i.e., contextId and topology_uuid
duplicate_string(newEndPId->topology_id.contextId, refEndPId->topology_id.contextId);
duplicate_string(newEndPId->topology_id.topology_uuid, refEndPId->topology_id.topology_uuid);
//duplicate the deviceId and endPoint_uuid
duplicate_string(newEndPId->deviceId, refEndPId->deviceId);
duplicate_string(newEndPId->endPointId, refEndPId->endPointId);
//DEBUG_PC("refLink Endpoint[%d]: %s(%s)", j, refEndPId->deviceId, refEndPId->endPointId);
//DEBUG_PC("newLink Endpoint[%d]: %s(%s)", m, newEndPId->deviceId, newEndPId->endPointId);
newLink->numLinkEndPointIds++;
}
// duplicate forwarding direction
newLink->forwarding_direction = refLink->forwarding_direction;
// duplicate capacity attributes
memcpy(&newLink->potential_capacity.value, &refLink->potential_capacity.value, sizeof(gdouble));
newLink->potential_capacity.unit = refLink->potential_capacity.unit;
memcpy(&newLink->available_capacity.value, &refLink->available_capacity.value, sizeof(gdouble));
newLink->available_capacity.unit = refLink->available_capacity.unit;
// duplicate cost characteristics
memcpy(&newLink->cost_characteristics.cost_value, &refLink->cost_characteristics.cost_value, sizeof(gdouble));
memcpy(&newLink->cost_characteristics.cost_algorithm, &refLink->cost_characteristics.cost_algorithm, sizeof(gdouble));
duplicate_string(newLink->cost_characteristics.cost_name, refLink->cost_characteristics.cost_name);
// duplicate latency characteristics
memcpy(&newLink->latency_characteristics.fixed_latency, &refLink->latency_characteristics.fixed_latency, sizeof(gdouble));
}
DEBUG_PC("Terminating Reverse Links [total: %d]", linkList->numLinks);
return;
}
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
///////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to parse the JSON object/s for the PATH COMP request (i.e. service
* requests, device and links)
*
* @param root
* @param source
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void parsing_json_obj_pathComp_request(cJSON * root, GIOChannel * source)
{
//DEBUG_PC("**");
if (deviceList == NULL){
DEBUG_PC ("Device List does not exist ... STOP");
exit(-1);
}
if (linkList == NULL) {
DEBUG_PC("Link List does not exist ... STOP")
}
if (serviceList == NULL)
{
DEBUG_PC ("Service List does not exist ... STOP");
exit(-1);
}
// Set of services to seek their path and resource selection
cJSON* serviceListArray = cJSON_GetObjectItem(root, "serviceList");
if (cJSON_IsArray(serviceListArray)) {
parsing_json_serviceList_array(serviceListArray);
}
// Get the deviceList
cJSON* deviceListArray = cJSON_GetObjectItem(root, "deviceList");
if (cJSON_IsArray(deviceListArray)) {
parsing_json_deviceList_array(deviceListArray);
}
// Get the linkList
cJSON* linkListArray = cJSON_GetObjectItem(root, "linkList");
if (cJSON_IsArray(linkListArray)) {
parsing_json_linkList_array(linkListArray);
// In the context information, if solely the list of links are passed for a single direction,
// the reverse direction MUST be created sythetically
generate_reverse_linkList();
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
}
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used parse the JSON object/s
*
* @param data
* @param source
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint parsing_json_obj (guchar *data, GIOChannel *source) {
cJSON * root = cJSON_Parse((const char *)data);
char * print = cJSON_Print(root);
DEBUG_PC("STARTING PARSING JSON CONTENTS");
parsing_json_obj_pathComp_request (root, source);
// Release the root JSON object variable
cJSON_free (root);
g_free(print);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Create new tcp client connected to PATH COMP
*
* @param channel_client, GIOChannel
* @param fd
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
struct pathComp_client * RESTapi_client_create (GIOChannel * channel_client, gint fd) {
/** check values */
g_assert(channel_client != NULL);
struct pathComp_client* client = g_malloc0 (sizeof (struct pathComp_client));
if (client == NULL )
{
DEBUG_PC ("Malloc for the client failed");
exit(-1);
}
/** Make client input/output buffer. */
client->channel = channel_client;
client->obuf = stream_new(MAXLENGTH);
client->ibuf = stream_new(MAXLENGTH);
client->fd = fd;
// Clients connected to the PATH COMP SERVER
CLIENT_ID++;
client->type = CLIENT_ID;
//DEBUG_PC ("Client Id: %u is created (%p)", client->type, client);
//DEBUG_PC ("Client ibuf: %p || obuf: %p", client->ibuf, client->obuf);
// Add the tcp client to the list
RESTapi_tcp_client_list = g_list_append (RESTapi_tcp_client_list, client);
//DEBUG_PC ("Num of TCP Clients: %d", g_list_length (RESTapi_tcp_client_list));
return client;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Close the tcp client, removing from the rapi_tcp_client_list
*
* @param client
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void RESTapi_client_close (struct pathComp_client* client)
{
//DEBUG_PC("Closing the client (Id: %d) %p", client->type, client);
//DEBUG_PC("Client ibuf: %p || obuf: %p", client->ibuf, client->obuf);
if (client->ibuf != NULL)
{
//DEBUG_PC("Client ibuf: %p", client->ibuf);
stream_free(client->ibuf);
client->ibuf = NULL;
}
if (client->obuf != NULL)
{
//DEBUG_PC("Client obuf: %p", client->obuf);
stream_free(client->obuf);
client->obuf = NULL;
}
// Remove from the list
RESTapi_tcp_client_list = g_list_remove (RESTapi_tcp_client_list, client);
//DEBUG_PC ("TCP Client List: %d", g_list_length(RESTapi_tcp_client_list));
g_free (client);
client = NULL;
DEBUG_PC ("client has been removed ...");
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Close operations over the passed tcp channel
*
* @param source
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void RESTapi_close_operations (GIOChannel * source)
{
gint fd = g_io_channel_unix_get_fd (source);
//DEBUG_PC ("Stop all the operations over the fd: %d", fd);
g_io_channel_flush(source, NULL);
GError *error = NULL;
g_io_channel_shutdown (source, TRUE, &error);
if(error)
{
DEBUG_PC ("An error occurred ...");
}
g_io_channel_unref (source);
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Remove the client and close operations over the TCP connection
*
* @param client
* @param source
* @param fd
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void RESTapi_stop (struct pathComp_client* client, GIOChannel * source, gint fd)
{
DEBUG_PC("Client Socket: %d is Stopped", fd);
// remove client
RESTapi_client_close(client);
// Stop operations over that channel
RESTapi_close_operations(source);
close (fd);
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used read the different lines ending up in \r\n
*
* @param s
* @param buf
* @param size
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint RESTapi_get_line (GIOChannel *channel, gchar *buf, gint size)
{
gint i = 0;
//DEBUG_PC ("\n");
//DEBUG_PC ("----- Read REST API Line(\r\n) ------");
gint n = 0;
guchar c = '\0'; // END OF FILE
gboolean cr = FALSE;
while (i < size - 1)
{
n = read_channel (channel, &c, 1);
if (n == -1)
{
//DEBUG_PC ("Close the channel and eliminate the client");
return -1;
}
if (n > 0)
{
//DEBUG_PC ("%c", c);
buf[i] = c;
i++;
if (c == '\r')
{
cr = TRUE;
}
if ((c == '\n') && (cr == TRUE))
{
break;
}
}
else
{
c = '\n';
buf[i] = c;
i++;
break;
}
}
buf[i] = '\0';
//DEBUG_PC ("Line (size: %d) buf: %s", i, buf);
return i;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used read the HTTP method
*
* @param buf
* @param j
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
guint RESTapi_get_method (gchar *buf, gint *j)
{
guint RestApiMethod = 0;
gchar method[255];
gint i = 0;
while (!ISspace(buf[*j]) && (i < sizeof(method) - 1))
{
method[i] = buf[*j];
i++;
*j = *j + 1;
}
method[i] = '\0';
DEBUG_PC ("REST API METHOD: %s", method);
// Check that the methods are GET, POST or PUT
if (strcasecmp((const char *)method, "GET") && strcasecmp((const char *)method, "POST") &&
strcasecmp ((const char *)method, "HTTP/1.1") && strcasecmp ((const char *)method, "PUT"))
{
DEBUG_PC ("The method: %s is not currently supported ...", method);
return RestApiMethod;
}
// Method selector
if (strncmp ((const char*)method, "GET", 3) == 0)
{
RestApiMethod = REST_API_METHOD_GET;
}
else if (strncmp ((const char*)method, "POST", 4) == 0)
{
RestApiMethod = REST_API_METHOD_POST;
}
else if (strncmp ((const char *)method, "HTTP/1.1", 8) == 0)
{
RestApiMethod = REST_API_METHOD_HTTP;
}
else if (strncmp ((const char *)method, "PUT", 3) == 0)
{
RestApiMethod = REST_API_METHOD_PUT;
}
return RestApiMethod;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used read the url
*
* @param buf
* @param j
* @param url
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint get_url (gchar *buf, gint *j, gchar *url)
{
// Skip space char
while (ISspace(buf[*j]) && (*j < strlen(buf))) {
*j = *j + 1;
}
//DEBUG_PC ("buf[%d]: %c", *j, buf[*j]);
int result = isspace (buf[*j]);
*buf = *buf + *j;
gint numChar = 0;
gint initChar = *j;
result = 0;
while (result == 0) {
*j = *j + 1;
result = isspace (buf[*j]);
numChar++;
}
//DEBUG_PC ("numChar: %d", numChar);
memcpy (url, buf + initChar, numChar);
url[numChar] = '\0';
//DEBUG_PC ("url: %s", url);
return numChar;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used read the version
*
* @param buf
* @param j
* @param version
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint get_version (gchar *buf, gint *j, gchar *version) {
// Skip space char
while (ISspace(buf[*j]) && (*j < strlen(buf)))
{
*j = *j + 1;
}
//DEBUG_PC ("buf[%d]: %c", *j, buf[*j]);
int result = isspace (buf[*j]);
*buf = *buf + *j;
gint numChar = 0;
gint initChar = *j;
result = 0;
while (result == 0) {
*j = *j + 1;
result = isspace (buf[*j]);
numChar++;
}
//DEBUG_PC ("numChar: %d", numChar);
memcpy (version, buf + initChar, numChar);
version[numChar] = '\0';
//DEBUG_PC ("version: %s", version);
return numChar;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to trigger the route computation for the network connectivity service
* List and retrieve the result
*
* @param compRouteList
* @param raId
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gint triggering_routeComp (struct compRouteOutputList_t *compRouteList, gchar *algId) {
g_assert (compRouteList);
gint httpCode = HTTP_RETURN_CODE_OK;
DEBUG_PC("Requested Algorithm: %s", algId);
//////////////////// Algorithm Selector (RAId)//////////////////////////////////////
// Connectivity Service Abstraction (CSA)
if (strncmp ((const char*)algId, "KSP", 3) == 0)
{
DEBUG_PC ("Alg Id: KSP");
httpCode = pathComp_ksp_alg(compRouteList);
}
#if 0
// Infrastructure Abstraction (InA)
else if (strncmp ((const char*)raId, "InA", 3) == 0)
{
//DEBUG_PC ("RA: InA");
httpCode = ra_InA_alg (compRouteList);
}
// Global Concurrent Optimization (GCO): Resoration / Re-Allocation / Re-Optimization
else if (strncmp ((const char*)raId, "GCO", 3) == 0)
{
//DEBUG_PC ("RA: GCO");
httpCode = ra_GCO_alg (compRouteList);
}
#endif
return httpCode;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to process the REST API commands
*
* @param source
* @param cond
* @param data
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gboolean RESTapi_activity(GIOChannel *source, GIOCondition cond, gpointer data)
{
/** some checks */
g_assert(source != NULL);
g_assert(data != NULL);
gchar buf[1024];
gchar version[255];
gchar http_result[255];
gint body_length = 0;
struct pathComp_client *client = (struct pathComp_client*)(data);
DEBUG_PC (" ************************************************************************** ");
DEBUG_PC (" REST API ACTIVITY Triggered ");
DEBUG_PC (" ************************************************************************** ");
gint fd = g_io_channel_unix_get_fd (source);
DEBUG_PC ("fd: %d, cond: %d", fd, cond);
if (cond != G_IO_IN)
{
DEBUG_PC ("Something happening with the channel and fd ... (cond: %d)", cond);
RESTapi_stop(client, source, fd);
return FALSE;
}
/** Clear input buffer. */
stream_reset (client->ibuf);
// get line
gint nbytes = RESTapi_get_line (source, buf, sizeof (buf));
if (nbytes == -1)
{
DEBUG_PC ("nbytes -1 ... CLOSE CLIENT FD and eliminate CLIENT");
RESTapi_stop(client, source, fd);
return FALSE;
}
if ((buf[0] == '\n') && (nbytes == 1))
{
//DEBUG_PC (" -- buf[0] = newline --");
RESTapi_stop(client, source, fd);
return FALSE;
}
gint i = 0, j = 0;
// Get the REST Method
guint RestApiMethod = RESTapi_get_method (buf, &j);
if (RestApiMethod == 0)
{
DEBUG_PC ("The method is NOT supported ...");
RESTapi_unimplemented (source);
RESTapi_stop(client, source, fd);
return FALSE;
}
// get the REST url
gchar url[255];
i = get_url (buf, &j, url);
url[i] = '\0';
// for method POST, PUT check that the url is "/pathComp"
if (strncmp((const char*) url, "/pathComp/api/v1/compRoute", 26) != 0)
{
DEBUG_PC ("Unknown url: %s", url);
RESTapi_stop(client, source, fd);
exit (-1);
}
// get the version
i = get_version (buf, &j, version);
version[i] = '\0';
// Assume HTTP/1.1, then there is Host Header
memset(buf, '\0', sizeof(buf));
nbytes = RESTapi_get_line(source, buf, sizeof (buf));
if (nbytes == -1)
{
DEBUG_PC ("nbytes -1 ... then close the fd and eliminate associated client");
RESTapi_stop(client, source, fd);
return FALSE;
}
//DEBUG_PC ("Header: %s", buf);
// Headers --- The Header Fields ends up with a void line (i.e., \r\n)
while ((nbytes > 0) && (strcmp ("\r\n", (const char *)buf) != 0))
{
/* read & discard headers */
memset(buf, '\0', sizeof(buf));
nbytes = RESTapi_get_line (source, buf, sizeof (buf));
if (nbytes == -1)
{
DEBUG_PC ("nbytes -1 ... then close the fd and eliminate associated client");
RESTapi_stop(client, source, fd);
return FALSE;
}
//DEBUG_PC ("Header: %s", buf);
if (strncmp ((const char *)buf, "Content-Length:", 15) == 0)
{
//DEBUG_PC ("Header Content-Length Found");
gchar str[20];
gint i = 15, k = 0; // "Content-Length:" We skip the first 16 characters to directly retrieve the length in bytes of the Body of Request
gchar contentLength[255];
memset (contentLength, '\0', sizeof (contentLength));
while (buf[i] != '\r')
{
//DEBUG_PC ("%c", buf[i]);
str[k] = buf[i];
k++, i++;
}
str[k] = '\0';
j = 0, i = 0;
while (ISspace(str[j]) && (j < strlen(str)))
{
j++;
}
while (j < strlen(str))
{
contentLength[i] = str[j];
i++; j++;
}
contentLength[i] = '\0';
body_length = atoi (contentLength);
//DEBUG_PC ("Body length: %d (%s) in Bytes", body_length, contentLength);
}
}
//DEBUG_PC("Read Entire HTTP Header");
if (body_length == 0)
{
DEBUG_PC ("--- NO REST API Body length (length = %d) ---", body_length);
return TRUE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Processing Body of the Request
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DEBUG_PC ("REST API Request - Body -");
nbytes = read_channel (source, (guchar *)(client->ibuf->data + client->ibuf->putp), body_length);
if ((nbytes < 0) && (body_length > 0))
{
DEBUG_PC ("nbytes: %d; body_length: %d", nbytes, body_length);
exit (-1);
}
client->ibuf->putp += nbytes;
client->ibuf->endp += nbytes;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Parsing the contents of the Request
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
// build the device list
deviceList = create_device_list();
// build the link list
linkList = create_link_list();
// Create the network connectivity service list
serviceList = create_service_list();
// Process the json contents and store relevant information at Device, Link,
// and network connectivity service
gint ret = parsing_json_obj (client->ibuf->data, source);
if (ret == -1) {
DEBUG_PC ("Something wrong with the JSON Objects ... ");
RESTapi_stop(client, source, fd);
return FALSE;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Trigger the path computation
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//DEBUG_PC ("Triggering the computation");
struct compRouteOutputList_t *compRouteOutputList = create_route_list ();
gint httpCode = triggering_routeComp (compRouteOutputList, algId);
// Send the response to the REST API Client
if (httpCode != HTTP_RETURN_CODE_OK)
{
DEBUG_PC ("HTTP CODE: %d -- NO OK", httpCode);
rapi_response (source, httpCode);
}
else
{
DEBUG_PC ("HTTP CODE: %d -- OK", httpCode);
rapi_response_ok (source, httpCode, compRouteOutputList);
}
// Release the variables
g_free (compRouteOutputList);
g_free(linkList);
g_free(deviceList);
g_free(serviceList);
return TRUE;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Function used to accept a new connection and add the client to list of clients
*
* @param source, GIOChannel
* @param cond, GIOCondition
* @param data, gpointer
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
gboolean RESTapi_tcp_new_connection(GIOChannel *source, GIOCondition cond, gpointer data)
{
DEBUG_PC (" ****** New TCP Connection (REST API) ******");
/** get size of client_addre structure */
struct sockaddr_in client_addr;
socklen_t client = sizeof(client_addr);
if ((cond == G_IO_HUP) || (cond == G_IO_ERR) || (G_IO_NVAL))
{
//DEBUG_PC ("Something happening with the channel and fd ... cond: %d", cond);
// Find the associated client (by the fd) and remove from PATH COMP client list.
// Stop all the operations over that PATH COMP client bound channel
struct pathComp_client *pathComp_client = NULL;
gint fd = g_io_channel_unix_get_fd (source);
GList *found = g_list_find_custom (RESTapi_tcp_client_list, &fd, find_rl_client_by_fd);
if (found != NULL)
{
pathComp_client = (struct pathComp_client*)(found->data);
// remove client
RESTapi_client_close(pathComp_client);
// Stop operations over that channel
RESTapi_close_operations(source);
close (fd);
return FALSE;
}
}
if (cond == G_IO_IN)
{
gint new = accept(g_io_channel_unix_get_fd(source), (struct sockaddr*)&client_addr, &client);
if (new < 0)
{
//DEBUG_PC ("Unable to accept new connection");
return FALSE;
}
/** new channel */
GIOChannel * new_channel = g_io_channel_unix_new (new);
//DEBUG_PC ("TCP Connection (REST API) is UP; (socket: %d)", new);
/** create pathComp client */
struct pathComp_client *new_client = RESTapi_client_create (new_channel, new);
/**
* force binary encoding with NULL
*/
GError *error = NULL;
if ( g_io_channel_set_encoding (new_channel, NULL, &error) != G_IO_STATUS_NORMAL)
{
DEBUG_PC ("Error: %s", error->message);
exit (-1);
}
g_io_channel_set_close_on_unref (new_channel, TRUE);
// On unbuffered channels, it is safe to mix read
// & write calls from the new and old APIs.
g_io_channel_set_buffered (new_channel, FALSE);
if (g_io_channel_set_flags (new_channel, G_IO_FLAG_NONBLOCK, &error) != G_IO_STATUS_NORMAL )
{
DEBUG_PC ("Error: %s", error->message);
exit (-1);
}
//Adds the new channel into the main event loop.
g_io_add_watch (new_channel, G_IO_IN, RESTapi_activity, new_client);
}
return TRUE;
}
///////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief enabling the reuse of the addr for the Server TCP
*
* @param sock
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void RESTapi_tcp_enable_reuseaddr (gint sock)
{
gint tmp = 1;
if (sock < 0)
{
DEBUG_PC (" socket: %d !!!",sock);
exit (-1);
}
if (setsockopt (sock, SOL_SOCKET, SO_REUSEADDR, (gchar *)&tmp, sizeof (tmp)) == -1)
{
DEBUG_PC ("bad setsockopt ...");
exit (-1);
}
return;
}
////////////////////////////////////////////////////////////////////////////////////////
/**
* @file pathComp_RESTapi.c
* @brief Main function for the creating / maintaining TCP session for the REST API
*
* @ port
*
* @author Ricardo Martínez <ricardo.martinez@cttc.es>
* @date 2022
*/
/////////////////////////////////////////////////////////////////////////////////////////
void RESTapi_init(gint port)
{
DEBUG_PC ("REST API PORT (listening): %d", port);
// File Descriptor - FD - for the socket
gint s = socket (AF_INET, SOCK_STREAM, 0);
if (s == -1)
{
DEBUG_PC ("Socket creation: FAILED!!");
exit (-1);
}
DEBUG_PC (" CREATED TCP Connection [@fd: %d]", s);
// Re-bind
RESTapi_tcp_enable_reuseaddr(s);
struct sockaddr_in addr;
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
addr.sin_port = htons ((u_short)port);
addr.sin_addr.s_addr = INADDR_ANY;
// Associate IP address and Port to the created socket
if (bind (s, (struct sockaddr *)&addr, sizeof(addr)) == -1)
{
close (s);
DEBUG_PC ("Socket bind: FAILED!!");
exit (-1);
}
DEBUG_PC ("Bind to Fd: %d DONE!!", s);
/** Set up queue for incoming connections */
if (listen (s, 10) == -1)
{
close (s);
DEBUG_PC ("Socket listen: FAILED!!");
exit (-1);
}
//DEBUG_PC ("Listen (up to 10) to Fd: %d Done", s);
/** Create NEW channel to handle the socket operations*/
GIOChannel *channel = g_io_channel_unix_new (s);
gsize buffersize = g_io_channel_get_buffer_size (channel);
//DEBUG_PC ("GIOChannel with Buffer Size: %d", (gint)buffersize);
gsize newBufferSize = MAX_GIO_CHANNEL_BUFFER_SIZE;
g_io_channel_set_buffer_size (channel, newBufferSize);
buffersize = g_io_channel_get_buffer_size (channel);
//DEBUG_PC ("GIOChannel with Buffer Size: %d", (gint)buffersize);
//DEBUG_PC ("Channel associated to fd: %d is created", s);
// Adds the new channel into the main event loop.
g_io_add_watch (channel, G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL, RESTapi_tcp_new_connection, NULL);
return;
}