From d44e0ec9b412bb1f2c9a98bc485967040fb3ae7b Mon Sep 17 00:00:00 2001 From: borysr Date: Wed, 22 May 2024 19:33:42 +0200 Subject: [PATCH] kod - v1 --- cpp/._rvmain.cpp.swp | Bin 0 -> 12288 bytes cpp/Make.rules | 81 +++++++++++++++++++++ cpp/Makefile | 24 +++++++ cpp/_crt0.S | 43 +++++++++++ cpp/_rv-diss | 1 + cpp/_rv-readelf | 1 + cpp/_rvmain.cpp | 65 +++++++++++++++++ cpp/gdb/z.py | 17 +++++ cpp/gdb/zero.py | 5 ++ cpp/myfunc.cpp | 35 +++++++++ cpp/myfunc.h | 9 +++ cpp/myfuncOOP.cpp | 38 ++++++++++ cpp/myfuncOOP.hpp | 22 ++++++ cpp/myfuncStruct.cpp | 29 ++++++++ cpp/myfuncStruct.h | 15 ++++ cpp/myfuncStructOOP.cpp | 44 ++++++++++++ cpp/myfuncStructOOP.h | 21 ++++++ cpp/tracked_files.txt | 5 ++ cpp/try/3.py | 16 +++++ cpp/try/__os-build | 1 + cpp/try/__os-run | 1 + cpp/try/main.cpp | 155 ++++++++++++++++++++++++++++++++++++++++ cpp/try/uri.cpp | 53 ++++++++++++++ 23 files changed, 681 insertions(+) create mode 100644 cpp/._rvmain.cpp.swp create mode 100644 cpp/Make.rules create mode 100644 cpp/Makefile create mode 100644 cpp/_crt0.S create mode 100644 cpp/_rv-diss create mode 100644 cpp/_rv-readelf create mode 100644 cpp/_rvmain.cpp create mode 100644 cpp/gdb/z.py create mode 100644 cpp/gdb/zero.py create mode 100644 cpp/myfunc.cpp create mode 100644 cpp/myfunc.h create mode 100644 cpp/myfuncOOP.cpp create mode 100644 cpp/myfuncOOP.hpp create mode 100644 cpp/myfuncStruct.cpp create mode 100644 cpp/myfuncStruct.h create mode 100644 cpp/myfuncStructOOP.cpp create mode 100644 cpp/myfuncStructOOP.h create mode 100644 cpp/tracked_files.txt create mode 100644 cpp/try/3.py create mode 100644 cpp/try/__os-build create mode 100644 cpp/try/__os-run create mode 100644 cpp/try/main.cpp create mode 100644 cpp/try/uri.cpp diff --git a/cpp/._rvmain.cpp.swp b/cpp/._rvmain.cpp.swp new file mode 100644 index 0000000000000000000000000000000000000000..e6a5d76642f44fb5aa19862644f39b8a97850836 GIT binary patch literal 12288 zcmeI2&u`pB6vrn>v_ep*Xs$&siAcP0ySqtKN}6oi2q6?HG(uH-V2KjPJKIdxv0dAn zCA&(09IGHCH~n)T^rv35v^b}AFy{eSJuxb()W^3>$S%IW=Luj_=B zPL#ANfwxle<~18m@>G?2x8;sz>z#a2ISuYJ#4?IT~<02^QfY=8~00XDz} z*Z><~18jf|uz{D*fY%V>)z^g>cmu`b|Nq6`|2uC9u?>C$_rX1I2Yd;>09#-Uw7@4| z0h|HvfqwAlO(FgUe}Lb?18^Vw1bzfRfV&_8%itq$7Pz1U954d@IU>Yk@C*13d<(t- zx4=yhg9yxm^PnI6i?#d(eg!{+@4?sLE3gG_gC6LDb6^HkK?Rh-5YRr}0ms2%@FyJY zfNk&)aAN~(fDNz#Hoykh02^QfFSdcr12*wmBC~c9(%2LhNN8Sg#YwZIglGNeuX+iM zqJwHQIM<|(7vd|uvO$Rqyi7$Qg%PFgBu-Q+DQaSVFo?M-%t(LX*C|nt#HPu$nWxlg z^A3Hdn;*3iUZd)iFwgcEQbj&FW)$`89m>m!$1rI)t-G$jxI4Z{GC(c8|4z`aaNXS~ z)g&ja`-?Py_kKQJrQu;Jo<}~bj-c+m+-8)JgXO%xl#y?W#AUiha1zMSNlR1YyYAC) zQ<*6l7S2P47y(NiNOK&o6v%aqiO(G0O(eAlMv?T=DK{ z2nl&65a@*TE=|~7*9pzD*{pnGPvlBXM!i2eqY(Jcd8T}OZ0*}>`D;-PYTg@A6j^^- z;vyo%P(wCVDEAaTRz)i;byig%$r(*u7sp`~rwV2E^1a5P@Z*hQw@=tQvQnYgTGRs5 u-Ln`M2eU{W>xG(AXpGv^j<;cwUaCENdk@u+3jLtnkTjiU4aAgJXT%e8a&7DY literal 0 HcmV?d00001 diff --git a/cpp/Make.rules b/cpp/Make.rules new file mode 100644 index 0000000..2bd12e9 --- /dev/null +++ b/cpp/Make.rules @@ -0,0 +1,81 @@ + +ARCH=riscv64-unknown-elf +GNU_DIR=$(HOME)/riscv/riscv/ +GNU_BIN=$(GNU_DIR)/bin + + +CC=$(GNU_BIN)/$(ARCH)-gcc +CXX=$(GNU_BIN)/$(ARCH)-g++ +AS=$(GNU_BIN)/$(ARCH)-as +LD=$(GNU_BIN)/$(ARCH)-ld +OBJCOPY=$(GNU_BIN)/$(ARCH)-objcopy +OBJDUMP=$(GNU_BIN)/$(ARCH)-objdump +SIZE=$(GNU_BIN)/$(ARCH)-size +AR=$(GNU_BIN)/$(ARCH)-ar +RANLIB=$(GNU_BIN)/$(ARCH)-ranlib + + +CFLAGS+=-ffreestanding +CFLAGS+=-fno-pic +CFLAGS+=-march=rv32i -mabi=ilp32 +CFLAGS+= -g + + +LDFLAGS+=-nostdlib +LDFLAGS+=-Wl,-Ttext=0x00000000 + +# see: https://github.com/riscv/riscv-gcc/issues/120 +#LDFLAGS+=-Wl,--no-relax + + + +ASFLAGS+=$(CFLAGS) +CXXFLAGS+=$(CFLAGS) + +CLEAN_DIRS=$(SUBDIRS:%=clean-%) +ALL_DIRS=$(SUBDIRS:%=all-%) + +OBJDUMPFLAGS+=-Mnumeric,no-aliases + +.PHONY: all clean world $(CLEAN_DIRS) $(ALL_DIRS) + + +%.bin : % + $(OBJCOPY) $< -O binary $@ + +%.lst : % + $(OBJDUMP) $(OBJDUMPFLAGS) -dr --disassemble-all $< > $<.lst + +% : %.o + $(LINK.cc) $(LDFLAGS) -o $@ $^ $(LDLIBS) + $(SIZE) -x -A $@ + +%.s: %.c + $(COMPILE.c) -S -o $@ $< + +%.s: %.cc + $(COMPILE.cc) -S -o $@ $< + +%.o: %.c + $(COMPILE.c) -o $@ $< + +%.o: %.cc + $(COMPILE.cc) -o $@ $< + +%.srec: % + $(OBJCOPY) $< -O srec $@ + + + + +all:: $(ALL_DIRS) + +clean:: $(CLEAN_DIRS) + +$(ALL_DIRS):: + $(MAKE) -C $(@:all-%=%) all + +$(CLEAN_DIRS):: + $(MAKE) -C $(@:clean-%=%) clean + +world:: clean all diff --git a/cpp/Makefile b/cpp/Makefile new file mode 100644 index 0000000..e677ff3 --- /dev/null +++ b/cpp/Makefile @@ -0,0 +1,24 @@ +TOP=./ +include $(TOP)/Make.rules + +LDLIBS= +CFLAGS+=-O0 -g + +LDFLAGS+=-Wl,--no-relax +LDFLAGS+=-Wl,-Tdata=0x10000 + +PROGS=prog prog.bin prog.lst + +all:: $(PROGS) + +prog: _crt0.o _rvmain.o myfunc.o myfuncStruct.o myfuncStructOOP.o myfuncOOP.o + $(LINK.cc) -o $@ $^ $(LDLIBS) + $(SIZE) -A -x $@ + +clean:: + rm -f $(PROGS) *.o *.s *.lst *.bin *.srec *.dis + +.PHONY: run +run: prog.bin + ../../../src/rvddt -l0x3000 -f prog.bin + diff --git a/cpp/_crt0.S b/cpp/_crt0.S new file mode 100644 index 0000000..bc6a10a --- /dev/null +++ b/cpp/_crt0.S @@ -0,0 +1,43 @@ + .text + .global _start + .type _start, @function + +_start: + # Initialize global pointer + .option push + .option norelax + la gp, __global_pointer$ + .option pop + + li sp, 0x1fff0 + + # Clear the bss segment + la a0, __bss_start + la a1, __BSS_END__ + + j finish_bss +clear_bss: + bgeu a0, a1, finish_bss + sb x0, 0(a0) + addi a0, a0, 1 + beq x0, x0, clear_bss +finish_bss: + + nop //! + + call main + + nop //! + + # abort execution here + ebreak + + .section .rodata +alfabet: + .string "abcdefghijklmnopqrstuwxyz" +slowo: + + .section .data +wynik: + .string "mpabi" + .space 26 # rezerwuje 26 bajtów dla wyniku, zainicjowane na 0 diff --git a/cpp/_rv-diss b/cpp/_rv-diss new file mode 100644 index 0000000..9a22230 --- /dev/null +++ b/cpp/_rv-diss @@ -0,0 +1 @@ +riscv64-unknown-elf-objdump -S --disassemble prog > prog.dis diff --git a/cpp/_rv-readelf b/cpp/_rv-readelf new file mode 100644 index 0000000..940c525 --- /dev/null +++ b/cpp/_rv-readelf @@ -0,0 +1 @@ +readelf -S -W prog diff --git a/cpp/_rvmain.cpp b/cpp/_rvmain.cpp new file mode 100644 index 0000000..53c49d3 --- /dev/null +++ b/cpp/_rvmain.cpp @@ -0,0 +1,65 @@ +#include + +int strlen(char *s) { + char *p = s; + while (*p != '\0') + p++; + return p - s; +} + +void strcpy(char *s, char *t) +{ + while (*s++ = *t++); +} +#define ALLOCSIZE 10000 + +static char allocbuf[ALLOCSIZE]; +static char *allocp = allocbuf; + +char *alloc(int n) +{ + + if (n % 4 != 0) { + n += 4 - (n % 4); + } + + + if (allocbuf + ALLOCSIZE - allocp >= n) { + allocp += n; + return allocp - n; + } else + return 0; +} + + +//funkcja lokuje pamiec dla kazdego slowa. +// +//wskaznik p1 wskazuje na pierwsza litera slowa, +//liczy ilosc slowa i alokuje pamiec dla niego. + + +void alg (char *s) { + + char count; + + for (int8_t c = 0; c <= strlen(s); c++) { + + if (s[c] != 0x20 && s[c] != '\0') { + count++; + } else { + char *p1 = &s[++c]; + alloc (count); + count = 0; + } + + } +} +int main() { + + char *str = "If wantered relation no surprise of all"; + + alg(str); + + + return 1; +} diff --git a/cpp/gdb/z.py b/cpp/gdb/z.py new file mode 100644 index 0000000..98c6a26 --- /dev/null +++ b/cpp/gdb/z.py @@ -0,0 +1,17 @@ +# set args 0x1FF80 0x80 0x30 +# source gdb/z.py + +import gdb +import sys + +# Parse arguments from the GDB command +args = gdb.string_to_argv(gdb.parameter("args")) +if len(args) != 3: + print("Usage: source gdb/zero_with_params.py ") +else: + start_address = int(args[0], 16) # Convert start address from hex to int + num_bytes = int(args[1], 16) # Convert number of bytes from hex to int + pattern = int(args[2], 16) # Convert pattern from hex to int + + for i in range(num_bytes): + gdb.execute("set *((char*)%x + %x) = %x" % (start_address, i, pattern)) diff --git a/cpp/gdb/zero.py b/cpp/gdb/zero.py new file mode 100644 index 0000000..0095bd7 --- /dev/null +++ b/cpp/gdb/zero.py @@ -0,0 +1,5 @@ +#source gdb/z.py + +import gdb +for i in range(0, 128): # 128 bajtów + gdb.execute("set *((char*)(0x1FF80 + %x)) = 0xaa" % i) diff --git a/cpp/myfunc.cpp b/cpp/myfunc.cpp new file mode 100644 index 0000000..b621842 --- /dev/null +++ b/cpp/myfunc.cpp @@ -0,0 +1,35 @@ +#include "myfunc.h" + +// Lokalna deklaracja funkcji my_strlen +static int my_strlen(const char *str) { + int length = 0; + while (str[length] != '\0') { + length++; + } + return length; +} + +void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik) { + // Użycie my_strlen do obliczenia długości alfabetu + int alfabet_length = my_strlen(alfabet); + + for (int i = 0; i < alfabet_length; ++i) { + wynik[i] = 0; + } + + for (int i = 0; i < alfabet_length; ++i) { + for (int j = 0; slowo[j] != '\0'; ++j) { + if (alfabet[i] == slowo[j]) { + wynik[i]++; + } + } + } +} + +// #include "myfunc.h" + +// const char* alfabet = "abcdefghijklmnopqrstuwxyz"; +// const char* slowo = "mpabi"; +// int wynik[26] = {0}; // Inicjalizacja tablicy wyników na 0 + +// count_characters(alfabet, slowo, wynik); diff --git a/cpp/myfunc.h b/cpp/myfunc.h new file mode 100644 index 0000000..99a5984 --- /dev/null +++ b/cpp/myfunc.h @@ -0,0 +1,9 @@ +#ifndef myfunc_H +#define myfunc_H + +#include + + +void count_characters(const char* alfabet, const char* slowo, uint8_t* wynik); + +#endif diff --git a/cpp/myfuncOOP.cpp b/cpp/myfuncOOP.cpp new file mode 100644 index 0000000..02e929d --- /dev/null +++ b/cpp/myfuncOOP.cpp @@ -0,0 +1,38 @@ +#include "myfuncOOP.hpp" + +MyfuncOOP::MyfuncOOP(const char* alfabet, const char* slowo, uint8_t* wynik) + : alfabet(alfabet), slowo(slowo), wynik(wynik) { +} + +void MyfuncOOP::countCharacters() { + int alfabet_length = myStrlen(alfabet); + + for (int i = 0; i < alfabet_length; ++i) { + wynik[i] = 0; // Initialize counts to zero + } + + for (int i = 0; i < alfabet_length; ++i) { + for (int j = 0; slowo[j] != '\0'; ++j) { + if (alfabet[i] == slowo[j]) { + wynik[i]++; + } + } + } +} + +const uint8_t* MyfuncOOP::getResults() const { + return wynik; +} + +int MyfuncOOP::myStrlen(const char* str) { + int length = 0; + while (str[length] != '\0') { + ++length; + } + return length; +} + + +// MyfuncOOP zliczacz(alfabet, slowo, wynik); +// zliczacz.countCharacters(); +// const uint8_t* results = zliczacz.getResults(); \ No newline at end of file diff --git a/cpp/myfuncOOP.hpp b/cpp/myfuncOOP.hpp new file mode 100644 index 0000000..277e918 --- /dev/null +++ b/cpp/myfuncOOP.hpp @@ -0,0 +1,22 @@ +#ifndef MYFUNC_OOP_HPP +#define MYFUNC_OOP_HPP + +#include + +class MyfuncOOP { +public: + MyfuncOOP(const char* alfabet, const char* slowo, uint8_t* wynik); + + void countCharacters(); + + const uint8_t* getResults() const; + +private: + const char* alfabet; + const char* slowo; + uint8_t* wynik; + + static int myStrlen(const char* str); +}; + +#endif // MYFUNC_OOP_HPP diff --git a/cpp/myfuncStruct.cpp b/cpp/myfuncStruct.cpp new file mode 100644 index 0000000..47e307f --- /dev/null +++ b/cpp/myfuncStruct.cpp @@ -0,0 +1,29 @@ +#include "myfuncStruct.h" + +// Static function for string length calculation +static int my_strlen(const char *str) { + int length = 0; + while (str[length] != '\0') { + length++; + } + return length; +} + +// Function to count occurrences of each character in 'alfabet' within 'slowo' +void count_charactersStruct(ZliczaczStruct* zliczacz) { + int alfabet_length = my_strlen(zliczacz->alfabet); + + // Initialize the result array to zero + for (int i = 0; i < alfabet_length; ++i) { + zliczacz->wynik[i] = 0; + } + + // Count occurrences + for (int i = 0; i < alfabet_length; ++i) { + for (int j = 0; zliczacz->slowo[j] != '\0'; ++j) { + if (zliczacz->alfabet[i] == zliczacz->slowo[j]) { + zliczacz->wynik[i]++; + } + } + } +} diff --git a/cpp/myfuncStruct.h b/cpp/myfuncStruct.h new file mode 100644 index 0000000..2671140 --- /dev/null +++ b/cpp/myfuncStruct.h @@ -0,0 +1,15 @@ +#ifndef MYFUNCSTRUCT_H +#define MYFUNCSTRUCT_H + +#include + +typedef struct { + const char* alfabet; + const char* slowo; + uint8_t* wynik; // Pointer to an array for results +} ZliczaczStruct; + +// Function declaration for counting character occurrences +void count_charactersStruct(ZliczaczStruct* zliczacz); + +#endif // MYFUNCSTRUCT_H diff --git a/cpp/myfuncStructOOP.cpp b/cpp/myfuncStructOOP.cpp new file mode 100644 index 0000000..8138dc3 --- /dev/null +++ b/cpp/myfuncStructOOP.cpp @@ -0,0 +1,44 @@ +#include "myfuncStructOOP.h" + +// Static function for string length calculation +static int my_strlen(const char *str) { + int length = 0; + while (str[length] != '\0') { + length++; + } + return length; +} + +// 'Method' to count occurrences of each character +static void count_charactersStructOOP(ZliczaczStructOOP* zliczacz) { + int alfabet_length = my_strlen(zliczacz->alfabet); + + // Initialize the result array to zero + for (int i = 0; i < alfabet_length; ++i) { + zliczacz->wynik[i] = 0; + } + + // Count occurrences + for (int i = 0; i < alfabet_length; ++i) { + for (int j = 0; zliczacz->slowo[j] != '\0'; ++j) { + if (zliczacz->alfabet[i] == zliczacz->slowo[j]) { + zliczacz->wynik[i]++; + } + } + } +} + +// Constructor-like function to initialize a ZliczaczStructOOP +void initializeZliczaczStructOOP(ZliczaczStructOOP* zliczacz, const char* alfabet, const char* slowo, uint8_t* wynik) { + zliczacz->alfabet = alfabet; + zliczacz->slowo = slowo; + zliczacz->wynik = wynik; + zliczacz->count_characters = count_charactersStructOOP; +} + +// #include "myfuncStructOOP.h" + +// ZliczaczStructOOP zliczacz; +// initializeZliczaczStructOOP(&zliczacz, alfabet, slowo, wynik); + +// zliczacz.count_characters(&zliczacz); diff --git a/cpp/myfuncStructOOP.h b/cpp/myfuncStructOOP.h new file mode 100644 index 0000000..dd3e7e4 --- /dev/null +++ b/cpp/myfuncStructOOP.h @@ -0,0 +1,21 @@ +#ifndef MYFUNCSTRUCTOOP_H +#define MYFUNCSTRUCTOOP_H + +#include + +typedef struct ZliczaczStructOOP ZliczaczStructOOP; + +struct ZliczaczStructOOP { + // Data members + const char* alfabet; + const char* slowo; + uint8_t* wynik; // Pointer to an array for results + + // Function pointers, acting as 'methods' + void (*count_characters)(ZliczaczStructOOP*); +}; + +void initializeZliczaczStructOOP(ZliczaczStructOOP* zliczacz, const char* alfabet, const char* slowo, uint8_t* wynik); + + +#endif // MYFUNCSTRUCTOOP_H diff --git a/cpp/tracked_files.txt b/cpp/tracked_files.txt new file mode 100644 index 0000000..fb2f94e --- /dev/null +++ b/cpp/tracked_files.txt @@ -0,0 +1,5 @@ +_crt0.S +main.cpp +myfunc.cpp +myfunc.h +_rvmain.cc diff --git a/cpp/try/3.py b/cpp/try/3.py new file mode 100644 index 0000000..d12e3b0 --- /dev/null +++ b/cpp/try/3.py @@ -0,0 +1,16 @@ +import asyncio +import websockets + +async def consume_ws_latest_records(): + uri = "ws://172.24.0.3:3333/ws_latest_records" + + async with websockets.connect(uri) as websocket: + while True: + message = input("Enter a message to send: ") + await websocket.send(message) + + response = await websocket.recv() + print(f"Received from server: {response}") + +if __name__ == "__main__": + asyncio.get_event_loop().run_until_complete(consume_ws_latest_records()) diff --git a/cpp/try/__os-build b/cpp/try/__os-build new file mode 100644 index 0000000..025913d --- /dev/null +++ b/cpp/try/__os-build @@ -0,0 +1 @@ +g++ -std=c++17 -o websocket-client-sync main.cpp myfunc.cpp -lboost_system -lboost_thread -lboost_coroutine -ljsoncpp diff --git a/cpp/try/__os-run b/cpp/try/__os-run new file mode 100644 index 0000000..b500c13 --- /dev/null +++ b/cpp/try/__os-run @@ -0,0 +1 @@ +./websocket-client-sync ws://localhost:3333/ws diff --git a/cpp/try/main.cpp b/cpp/try/main.cpp new file mode 100644 index 0000000..9f7dea1 --- /dev/null +++ b/cpp/try/main.cpp @@ -0,0 +1,155 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace beast = boost::beast; +namespace websocket = beast::websocket; +namespace net = boost::asio; +using tcp = boost::asio::ip::tcp; + +struct Person { + std::string name; + int age; + long long timestamp; +}; + +Json::Value serializePerson(const Person& person) { + Json::Value jsonPerson; + jsonPerson["name"] = person.name; + jsonPerson["age"] = person.age; + jsonPerson["timestamp"] = static_cast(person.timestamp); + return jsonPerson; +} + +std::tuple parseURI(const std::string& uri) { + std::regex uriRegex(R"(^ws://([^:/]+):(\d+)(/.+)$)"); + std::smatch match; + + if (std::regex_match(uri, match, uriRegex)) { + return std::make_tuple(match[1].str(), match[2].str(), match[3].str()); + } else { + throw std::invalid_argument("Nieprawidłowe URI"); + } +} + +template +T getInput(const std::string& prompt) { + T value; + while (true) { + std::cout << prompt; + std::cin >> value; + + if (std::cin.fail()) { + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(), '\n'); + std::cerr << "Błąd! Spróbuj ponownie." << std::endl; + } else { + std::cin.ignore(std::numeric_limits::max(), '\n'); + break; + } + } + + return value; +} + +void iterateCharBuffer(const char* charBuffer, std::size_t bufferSize) { + for (std::size_t i = 0; i < bufferSize; ++i) { + std::cout << charBuffer[i]; + } + std::cout << std::endl; + + // Example: Call the function you want to apply to the char buffer +} + +int main(int argc, char** argv) { + try { + if (argc != 2) { + std::cerr << "Sposób użycia: " << argv[0] << " \n"; + return EXIT_FAILURE; + } + + std::string uri = argv[1]; + auto uriParts = parseURI(uri); + std::string host, port, endpoint; + std::tie(host, port, endpoint) = uriParts; + + net::io_context io_context; + + websocket::stream ws(io_context); + tcp::resolver resolver(io_context); + auto endpoints = resolver.resolve(host, port); + net::connect(ws.next_layer(), endpoints); + + ws.handshake(host, endpoint); + + while (true) { + std::cout << "Menu:\n"; + std::cout << "1. Dodaj rekord\n"; + std::cout << "2. Zwróć ostatnie rekordy\n"; + std::cout << "3. Wyjście\n"; + + int choice = getInput("Wybierz opcję: "); + + if (choice == 1) { + + std::string name = getInput("Podaj imię: "); + int age = getInput("Podaj wiek: "); + + Person personToSend{name, age, std::chrono::duration_cast( + std::chrono::system_clock::now().time_since_epoch()).count()}; + + Json::Value jsonPerson = serializePerson(personToSend); + + ws.write(net::buffer(Json::writeString(Json::StreamWriterBuilder(), jsonPerson))); + + } else if (choice == 2) { + + ws.write(net::buffer("get_latest_records")); + + beast::flat_buffer buffer; + ws.read(buffer); + + std::cout << "Otrzymano: " << beast::make_printable(buffer.data()) << std::endl; + + const char* bufferData = boost::asio::buffer_cast(buffer.data()); + std::size_t bufferSize = boost::asio::buffer_size(buffer.data()); + + char* charBuffer = new char[bufferSize + 1]; + std::memcpy(charBuffer, bufferData, bufferSize); + charBuffer[bufferSize] = '\0'; + + iterateCharBuffer(charBuffer, bufferSize); + + delete[] charBuffer; + + buffer.consume(buffer.size()); + + } else if (choice == 3) { + + std::cout << "Zamykanie programu...\n"; + break; + + } else { + + std::cout << "Nieprawidłowy wybór. Spróbuj ponownie.\n"; + + } + + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } + + } catch (std::exception const& e) { + std::cerr << "Błąd: " << e.what() << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/cpp/try/uri.cpp b/cpp/try/uri.cpp new file mode 100644 index 0000000..cff7149 --- /dev/null +++ b/cpp/try/uri.cpp @@ -0,0 +1,53 @@ +#include +#include +#include + +#include +#include +#include + +namespace beast = boost::beast; +namespace websocket = beast::websocket; +namespace net = boost::asio; +using tcp = boost::asio::ip::tcp; + +std::tuple parseURI(const std::string& uri) { + std::regex uriRegex(R"(^ws://([^:/]+):(\d+)(/.+)$)"); + std::smatch match; + + if (std::regex_match(uri, match, uriRegex)) { + return std::make_tuple(match[1].str(), match[2].str(), match[3].str()); + } else { + throw std::invalid_argument("Nieprawidłowe URI"); + } +} + +int main() { + std::string uri = "ws://172.24.0.3:3333"; + std::cout << "Dostarczone URI: " << uri << std::endl; + + try { + auto uriParts = parseURI(uri); + std::string host, port, endpoint; + std::tie(host, port, endpoint) = uriParts; + + net::io_context io_context; + + // Utwórz obiekt WebSocket + websocket::stream ws(io_context); + + // Połącz z serwerem WebSocket + tcp::resolver resolver(io_context); + auto endpoints = resolver.resolve(host, port); + net::connect(ws.next_layer(), endpoints); + + // Wysyłanie danych na serwer WebSocket + ws.handshake(host, endpoint); + + } catch (const std::exception& e) { + std::cerr << "Błąd: " << e.what() << std::endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +}