Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • 02:59
    solaim opened #597
  • Jul 03 12:12
    bjornstromberg commented #68
  • Jul 03 11:30
    bjornstromberg opened #68
  • Jul 02 14:39
    kekxv opened #596
  • Jun 29 10:32
    Bycob edited #595
  • Jun 29 10:21
    Bycob opened #595
  • Jun 28 13:46
    ankitgit edited #594
  • Jun 28 13:45
    ankitgit opened #594
  • Jun 25 19:34
    falko-strenzke closed #593
  • Jun 25 19:34
    falko-strenzke commented #593
  • Jun 23 14:22
    falko-strenzke commented #593
  • Jun 20 14:04
    hw-dwalter opened #67
  • Jun 20 13:37
    hw-dwalter commented #593
  • Jun 20 13:34
    hw-dwalter commented #593
  • Jun 20 10:34
    Alberto-Izquierdo commented #531
  • Jun 16 18:18
    falko-strenzke commented #433
  • Jun 16 12:59
    falko-strenzke opened #593
  • Jun 16 10:08
    Alberto-Izquierdo commented #531
  • Jun 16 08:44
    Alberto-Izquierdo commented #531
  • Jun 16 08:04
    wjr20138216 opened #592
oat++
@oatpp_io_twitter

Hey @shijiantouzouyiqie ,

Hello,after refreshing the browser, the program crashes. How to optimize the error point whenwsinstancelistener::onaftercreate uses std::threadto actively send data?
Or doesstd::threadnot cause the program to crash!

You can see the error, right? Try to use try - catch

terminate called after throwing an instance of 'std::runtime_error'
  what():  [oatpp::web::protocol::websocket::WebSocket::sendOneFrameText()]: Unknown error while writing to socket.
Also, I wouldn't recommend using my example "as is". It's better to at least pool your threads.

Hey @Maboulmagd ,

Hi I was wondering how to deserialize the following using oatpp::DTO:

"changes": [
    [
      "buy",
      "9122.04",
      "0.00121425"
    ],
    ...,
    [
      "sell",
      "9122.07",
      "0.98942292"
    ]
    ...
  ],

Vector<Vector<String>>>

 "trades": [
      {
          "type": "trade",
          "symbol": "BTCUSD",
          "eventid": 169841458,
          "timestamp": 1560976400428,
          "price": "9122.04",
          "quantity": "0.0073173",
          "side": "sell"
      },
      ...
  ],
Vector<Object<MyObj>>
oat++
@oatpp_io_twitter

On another note, what is the advantage of using oatpp DTO's over nlohmann::json?

One should use whatever serves the need better.

Mo Aboulmagd
@Maboulmagd
@oatpp_io_twitter Makes sense, and thank you, it seems as though I was just not seeing that the data I was getting was mostly strings, as opposed to ints/floats, so my custom DTO's were incorrect.
That said, I guess its worth benchmarking nlohmann::json vs mapper_->readFromString<T>, to see which approach is more performant?
@acidtonic I'm currently working for a trading firm, but this is just a side project for me to see how far I can go, and to learn more...That said, Bloomberg is actually a place I wouldn't mind working at, since its C++, though I do hear that its very team-dependent as to whether you use modern C++, or pre C++11. How was it working there, if you don't mind me asking? Also thanks for the database tip.
2 replies
Zach
@acidtonic
@Maboulmagd if you want to pm me I'll share some opinions.
1 reply
mmn
@mmn:matrix.org
[m]
For prices and quantities... the problem is that they are decimal values that don't necessarily directly map into floats/doubles.
There is an arbitrary-precision type NUMERIC in PostgreSQL, which is a good fit for this type of data. It is apparently not directly supported in oatpp-postgresql, but you can always pass strings to PostgreSQL and it will convert them to the correct types.
mmn
@mmn:matrix.org
[m]
(Well, NUMERIC is AFAIK in standard SQL, the problem is that standard SQL assumes that you specify a fixed decimal scale (PostgreSQL does not)... which might be a problem if you want to have price and quantity data for pairs like Shiba Inu/BTC on one hand, and BTC/Shiba Inu on the other.)
Nikola Radovanovic
@nikoladsp
hi all, short architectural question: say I want to create model as a shared library and use it in multiple micro-services (one service is one process), is it then possible to use single swagger page for all micro-services?
oat++
@oatpp_io_twitter

@nikoladsp ,

You can do that.

  • You can have a proxy in front of all your services which proxies requests + has the swagger-ui for all upstream services.
  • Or, you can serve swagger-ui in a separate service. Just make sure that you have the correct server address in swagger-ui and that all your services are accessible at that host-name
Nikola Radovanovic
@nikoladsp
@oatpp_io_twitter , many thanks. I will probably go with second approach
shijiantouzouyiqie
@shijiantouzouyiqie
The browser establishes a link with the server websocket and actively sends data. The browser refreshes the page and closes the page for the first time. The server runs normally; The browser establishes a link with the server websocket and actively sends data. When the browser closes the page for the second time, it will send 1001 status code to the server, and the program crashes;
@oatpp_io_twitter Hello, the following is the current code. Is there a better optimization method?
void* StartThread(void* arg);
class WSInstanceListener : public oatpp::websocket::ConnectionHandler::SocketInstanceListener {
public:

.....
public:

  pthread_t m_pid;
  bool m_bQuitThread;
  oatpp::websocket::WebSocket* m_Socket;

};

WSInstanceListener::WSInstanceListener() {
    m_pid = -1;
    m_bQuitThread = true;
    m_Socket = NULL;
}

void WSInstanceListener::onAfterCreate(
        const oatpp::websocket::WebSocket &socket,
        const std::shared_ptr<const ParameterMap> &params) {
    SOCKETS++;
    OATPP_LOGD(TAG, "New Incoming Connection. Connection count=%d",
            SOCKETS.load());

    socket.setListener(std::make_shared<WSListener>());

    m_Socket = (oatpp::websocket::WebSocket*) &socket;

    pthread_create(&m_pid, NULL, StartThread, (void*) this);
    pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
    pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
    pthread_detach(m_pid);

}

void WSInstanceListener::onBeforeDestroy(
        const oatpp::websocket::WebSocket &socket) {
    SOCKETS--;
    OATPP_LOGD(TAG, "Connection closed. Connection count=%d", SOCKETS.load());

    m_bQuitThread = false;
    pthread_cancel(m_pid);
    pthread_join(m_pid,NULL);
    this->m_Socket->stopListening();

}

void* StartThread(void *arg) {
    WSInstanceListener *lis = (WSInstanceListener*) arg;

    lis->m_bQuitThread = true;

    while (lis->m_bQuitThread) {
        pthread_testcancel();
        lis->m_Socket->sendOneFrameText("hello");
    }

    return (void*) 0;
}

 I |2021-12-15 19:46:53 1639568813189617| MyApp:Server running on port 8000
 D |2021-12-15 19:47:00 1639568820630942| Server_WSInstanceListener:New Incoming Connection. Connection count=1
 D |2021-12-15 19:47:06 1639568826556318| Server_WSListener:onClose code=1001
 D |2021-12-15 19:47:06 1639568826556353| Server_WSInstanceListener:Connection closed. Connection count=0
 D |2021-12-15 19:47:16 1639568836406451| Server_WSInstanceListener:New Incoming Connection. Connection count=1
 D |2021-12-15 19:47:21 1639568841210745| Server_WSListener:onClose code=1001
 terminate called after throwing an instance of 'std::runtime_error'
 D |2021-12-15 19:47:21 1639568841210782| Server_WSInstanceListener:Connection closed. Connection count=0
 terminate called recursively
AlexandreHURDYK
@AlexandreHURDYK

Hello.

I've got an annoying issue I'm trying to fix on my oat++ client application : if my server shuts down before it gets the time to send a response to my client after a client request, it causes my client to crash. This is probably a common problem. Any way to fix this client side ?

I can provide an example API call from my client if necessary.
There's probably something I forgot to do.
AlexandreHURDYK
@AlexandreHURDYK
Most basic example : if I call an API function while the server is not up, the client crashes.
As simple as that.
I can try to make a minimal working example.
Is there a way to make my request time out or something ?
Ok nevermind I fixed the problem and I'm stupid : the simple solution was just to surround the API calls with a try/catch.
Nikola Radovanovic
@nikoladsp
hi all, advice needed.
for simplicity, say that there is a DTO User class with id (primary key, auto inc, etc), salutation (mr,mrs, default null) and full name (string).
  1. for all crud methods apart create, id is required field. how is it possible to reuse one DTO in such way that swagger know that id is not present at all while creating user object (excluded from schema) and in all other cases is required? or do I need DTO per crud operation. Additionally, during update, id is the only required field, all others are optional, while reading for example I expect id and fullname to be present and salutation is optional.
  2. is it possible to use some pattern (decorator maybe?) to decouple DTO classes from "business" logic classes. I am interested for this especially in cases of relationships (composed objects, where one holds many others)
    thanks in advance
mmn
@mmn:matrix.org
[m]
Is there some easy way to determine the reason why oatpp::websocket::WebSocket::listen() returned? It catches the relevant exceptions and does not return any value, which makes it a bit hard.
Okay, maybe I should abandon listen() and "reimplement" it using iterateFrame().
oat++
@oatpp_io_twitter

Hey @nikoladsp ,

In an ideal world you would have to decouple the DB model and REST DTOs. The way you present data to service users and the way you store data may differ (and they do differ in most cases). Also, it's not recommended to expose DB ids - since they do not describe the actual resource in most cases.

Treat CRUD service as an adaptor of REST interface to DB model.

So the answer is - create two sets of DTOs - one for REST, second for DB.
Read from DB - do whatever transformations/compositions you need - join data in memory if needed - form response DTO and return it on the endpoint.

About the 'ID' problem - follow this issue - oatpp/oatpp#485
We may introduce hidden (from swagger docs) fields in future
oat++
@oatpp_io_twitter

Hey @mmn:matrix.org ,

if the WebSocket connection was closed gracefully - you'll receive the onClose method call in WS listener first, then the listen method will return.
If there was any kind of network issue - listen method will just return - no onClose event triggered.
Here is the implementation of listen - https://github.com/oatpp/oatpp-websocket/blob/master/src/oatpp-websocket/WebSocket.cpp#L279

1 reply
Please note that closed connection - is a normal situation for WebSocket - your application has to have the re-connecting functionality.
Also, make sure to respond with pong messages for all pings
Nikola Radovanovic
@nikoladsp
hi @oatpp_io_twitter many thanks, now to compile some stuff! :D
oat++
@oatpp_io_twitter

The browser establishes a link with the server websocket and actively sends data. The browser refreshes the page and closes the page for the first time. The server runs normally; The browser establishes a link with the server websocket and actively sends data. When the browser closes the page for the second time, it will send 1001 status code to the server, and the program crashes;
@oatpp_io_twitter Hello, the following is the current code. Is there a better optimization method?

Hey @shijiantouzouyiqie ,

Your program is missing a lot of things - and it has critical bugs:

  • please use std::thread instead of the pthread - just for simplicity and portability
  • synchronize with mutex access to m_bQuitThread
  • make sure that the socket is never used after m_bQuitThread is set to true
  • use sendOneFrameText call inside of try - catch block.
shijiantouzouyiqie
@shijiantouzouyiqie
@oatpp_io_twitter Thank you. I'm trying. I mainly use fewer threads. Set std::thread as a class member or the global does not understand the use of initialization. When I disconnect from the browser, the thread will be disconnected.
shijiantouzouyiqie
@shijiantouzouyiqie
@oatpp_io_twitter Thank you.
Using std::thread, the server can be used normally.
mmn
@mmn:matrix.org
[m]
So for the time being, I'm just deriving WebSocket with my own listen() (and stopListening(); can't use m_listening because it's private) which passes the exceptions through.
shijiantouzouyiqie
@shijiantouzouyiqie
Hello, theoatpp:: websocket:: websocket * m_socket; used here is initialized in the structure as a member variable. It is understood that it should be initialized bym_socket = new oatpp:: websocket:: websocket();. If an error is reported, I don't know how to solve this problem....
oat++
@oatpp_io_twitter
Hey @shijiantouzouyiqie , so what is the question?
1 reply
SSE4
@SSE4
how do I select from PostgreSQL ENUM type into the DTO with oatpp ENUM?
Leonid Stryzhevskyi
@lganzzzo
Hello @SSE4 , just creare a field in DTO with enum. Also, make sure to use enum as string or as a number - depending on your application
You can see an example in exaple-crud
That example is for SQLite, but there is no difference
SSE4
@SSE4
thank for the help. I made it work, I had to add a cast to my SELECT QUERY: CAST({0} as text)
otherwise, I was getting an exception: [oatpp::postgresql::mapping::Deserializer::deserializeString()]: Error. Unknown OID.
Zach
@acidtonic

I keep fighting some odd linker errors about missing vtable/constructors for DTO files even though they are correctly defined when linking across a few projects. But since the DTO's are only headers the static library for the DTO's are 8 bytes and basically empty.

I link everywhere else but on windows I get errors about the Vtable for the API-Client DTOs

Not sure what I am missing. I actually have two projects and one links but the other doesnt. Both use the same API library and dtos, just trying to track down why
Leonid Stryzhevskyi
@lganzzzo
Hey @acidtonic , I had before some errors with header only libraries. I ended up just adding an empty .cpp file. Just didnt want to spend time on that
Zach
@acidtonic
I have unfortunately a large multi-part project structure and I'm not sure if it's my fault or not :)