Partial socket connection (client still TODO)
parent
3cfefba694
commit
f6537c4cd0
|
@ -0,0 +1,77 @@
|
|||
#include <arpa/inet.h>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int makeSocket() {
|
||||
int sockfd;
|
||||
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
perror("socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
void bindSocketPort(int server_fd, int port) {
|
||||
struct sockaddr_in localAddr;
|
||||
localAddr.sin_family = AF_INET;
|
||||
localAddr.sin_addr.s_addr = INADDR_ANY;
|
||||
localAddr.sin_port = htons(port);
|
||||
|
||||
if (bind(server_fd, (struct sockaddr *)&localAddr, sizeof(localAddr)) < 0) {
|
||||
perror("bind failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("FD %d bound to port %d\n", server_fd, port);
|
||||
}
|
||||
|
||||
void startListening(int server_fd) {
|
||||
if (listen(server_fd, 3) < 0) {
|
||||
perror("listen");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("FD %d listening to new connections\n", server_fd);
|
||||
}
|
||||
|
||||
int acceptConnection(int server_fd) {
|
||||
int client_fd;
|
||||
struct sockaddr_in remoteAddr;
|
||||
size_t addrlen = sizeof(remoteAddr);
|
||||
if ((client_fd = accept(server_fd, (struct sockaddr *)&remoteAddr, (socklen_t *)&addrlen)) < 0) {
|
||||
perror("accept");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("Connection from host %s, port %d, FD %d\n", inet_ntoa(remoteAddr.sin_addr), ntohs(remoteAddr.sin_port), client_fd);
|
||||
return client_fd;
|
||||
}
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
if (argc != 2) {
|
||||
printf("Usage: %s portNumber \n", argv[0]);
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
int port = atoi(argv[1]);
|
||||
printf("Start socket port %d\n", port);
|
||||
|
||||
int server_fd = makeSocket();
|
||||
bindSocketPort(server_fd, port);
|
||||
startListening(server_fd);
|
||||
int client_fd = acceptConnection(server_fd);
|
||||
|
||||
while (true) {
|
||||
uint32_t word;
|
||||
ssize_t bytes = read(client_fd, &word, 4);
|
||||
if (bytes != 4) {
|
||||
perror("Receive failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
printf("[RICEVUTO]\t0x%x", word);
|
||||
}
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,36 @@
|
|||
#include <cstdint>
|
||||
|
||||
#define START_OF_HEADER_MARKER 0xee1234ee
|
||||
|
||||
typedef struct Header {
|
||||
uint32_t startOfHeaderMarker = START_OF_HEADER_MARKER;
|
||||
uint32_t headerSize;
|
||||
uint32_t fragmentSize;
|
||||
uint32_t runNumber;
|
||||
uint32_t detectorEventNumber;
|
||||
uint32_t numberOfStatusElements;
|
||||
uint32_t *statusElementsArray;
|
||||
/*friend std::ostream& operator <<(std::ostream& os, Header const& header)
|
||||
{
|
||||
return os << std::setw(8) << std::setfill('0') << header.startOfHeaderMarker << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.headerSize << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.fragmentSize << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.runNumber << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.detectorEventNumber << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.numberOfStatusElements << '\n';
|
||||
}*/
|
||||
} Header;
|
||||
|
||||
typedef struct Fragment {
|
||||
Header header;
|
||||
uint32_t *payloadElements;
|
||||
} Fragment;
|
||||
|
||||
enum ERROR_CODES {
|
||||
INCORRECT_ERROR = (1 << 0),
|
||||
CORRUPTED_ERROR = (1 << 1),
|
||||
MISSING_DATA_ERROR = (1 << 2),
|
||||
TIMEOUT_ERROR = (1 << 3)
|
||||
};
|
||||
|
||||
|
69
provider.cxx
69
provider.cxx
|
@ -1,50 +1,24 @@
|
|||
#include <algorithm>
|
||||
#include <arpa/inet.h>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <netinet/in.h>
|
||||
#include <random>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
#define START_OF_HEADER_MARKER 0xee1234ee
|
||||
#include "fragment_dataformat.h"
|
||||
|
||||
#define MIN_FRAGMENTS 0
|
||||
#define MAX_FRAGMENTS 100
|
||||
|
||||
typedef struct Header {
|
||||
uint32_t startOfHeaderMarker;
|
||||
uint32_t headerSize;
|
||||
uint32_t fragmentSize;
|
||||
uint32_t runNumber;
|
||||
uint32_t detectorEventNumber;
|
||||
uint32_t numberOfStatusElements;
|
||||
uint32_t *statusElementsArray;
|
||||
friend std::ostream& operator <<(std::ostream& os, Header const& header)
|
||||
{
|
||||
return os << std::setw(8) << std::setfill('0') << header.startOfHeaderMarker << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.headerSize << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.fragmentSize << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.runNumber << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.detectorEventNumber << '\n'
|
||||
<< std::setw(8) << std::setfill('0') << header.numberOfStatusElements << '\n';
|
||||
}
|
||||
} Header;
|
||||
|
||||
typedef struct Fragment {
|
||||
Header header;
|
||||
uint32_t *payloadElements;
|
||||
} Fragment;
|
||||
|
||||
enum ERROR_CODES {
|
||||
INCORRECT_ERROR = (1 << 0),
|
||||
CORRUPTED_ERROR = (1 << 1),
|
||||
MISSING_DATA_ERROR = (1 << 2),
|
||||
TIMEOUT_ERROR = (1 << 3)
|
||||
};
|
||||
|
||||
/*
|
||||
int rndError(float p){
|
||||
|
@ -53,6 +27,28 @@ int rndError(float p){
|
|||
}*/
|
||||
|
||||
|
||||
int makeSocket() {
|
||||
int sockfd;
|
||||
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
|
||||
perror("socket failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return sockfd;
|
||||
}
|
||||
|
||||
void connectTo(int sock, const char* host, int port) {
|
||||
struct sockaddr_in serv_addr;
|
||||
serv_addr.sin_family = AF_INET;
|
||||
serv_addr.sin_addr.s_addr = inet_addr(host);
|
||||
serv_addr.sin_port = htons(port);
|
||||
|
||||
if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
|
||||
perror("Connection failed");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
printf("Connected to %s: %d\n", host, port);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
|
@ -77,7 +73,6 @@ int main(int argc, char* argv[]) {
|
|||
|
||||
while (true) {
|
||||
Header header;
|
||||
header.startOfHeaderMarker = START_OF_HEADER_MARKER;
|
||||
header.runNumber = run_number;
|
||||
header.detectorEventNumber = event_number;
|
||||
event_number++;
|
||||
|
@ -112,8 +107,16 @@ int main(int argc, char* argv[]) {
|
|||
fragment.payloadElements[i] = unif_int_distib(generator);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
for (uint32_t i = 0; i < header.fragmentSize; i++) {
|
||||
|
||||
}
|
||||
|
||||
std::cout << std::hex << std::setw(8) << std::setfill('0');
|
||||
std::cout << fragment.header;
|
||||
//std::cout << fragment.header;
|
||||
for (uint8_t i = 0; i < fragment.header.numberOfStatusElements; i++) {
|
||||
std::cout << "status element " << std::setw(8) << std::setfill('0') << fragment.header.statusElementsArray[i] << std::endl;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue