Write two C++ files, producer.cpp and consumer.cpp. producer.cpp generates market data of three different structs, while consumer.cpp writes the market data into four CSV files using multi-threading. All stock market data is serialized using protobuf, and the two files communicate with each other through nanomsg

2023-04-07by@biteasquirrel-h3jPZ&@cpp.Model: gpt-3.5
biteasquirrel-h3jPZ

You are a C++Coder Expert,I have something to ask you.

Avatar
biteasquirrel-h3jPZ

Write two C++ files, producer.cpp and consumer.cpp. producer.cpp generates market data of three different structs, while consumer.cpp writes the market data into four CSV files using multi-threading. All stock market data is serialized using protobuf, and the two files communicate with each other through nanomsg

Avatar
Avatar

Here are the producer.cpp and consumer.cpp files as per the requirements:

producer.cpp:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
#include "market_data.pb.h"

using namespace std;
using namespace market;

int main()
{
    // Initialize nanomsg socket
    int sock = nn_socket(AF_SP, NN_PAIR);
    nn_bind(sock, "ipc:///tmp/market_data.ipc");

    // Generate market data
    vector<StockQuote> quotes = {{"AAPL", 100.50, 1000},
                                 {"GOOG", 1500.75, 500},
                                 {"TSLA", 800.25, 750}};

    vector<IndexQuote> indices = {{"DOW", 30000},
                                  {"NASDAQ", 12000},
                                  {"S&P 500", 3700}};

    vector<FuturesQuote> futures = {{"ES", 3700.50, 1},
                                    {"NQ", 13000.75, 2},
                                    {"YM", 31000.25, 1}};

    // Serialize market data using protobuf
    MarketData market_data;
    for (auto& quote : quotes)
    {
        StockQuote* stock_quote = market_data.add_stock_quotes();
        stock_quote->set_ticker(quote.ticker);
        stock_quote->set_price(quote.price);
        stock_quote->set_volume(quote.volume);
    }

    for (auto& index : indices)
    {
        IndexQuote* index_quote = market_data.add_index_quotes();
        index_quote->set_ticker(index.ticker);
        index_quote->set_value(index.value);
    }

    for (auto& future : futures)
    {
        FuturesQuote* futures_quote = market_data.add_futures_quotes();
        futures_quote->set_ticker(future.ticker);
        futures_quote->set_price(future.price);
        futures_quote->set_contract_size(future.contract_size);
    }

    // Send market data to consumer
    string serialized_data = market_data.SerializeAsString();
    nn_send(sock, serialized_data.c_str(), serialized_data.size(), 0);

    // Cleanup
    nn_shutdown(sock, 0);

    return 0;
}

consumer.cpp:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <chrono>
#include <thread>
#include <nanomsg/nn.h>
#include <nanomsg/pair.h>
#include "market_data.pb.h"

using namespace std;
using namespace market;

// Function to write market data to CSV file
void write_to_csv(string filename, vector<string> headers, vector<vector<string>> data)
{
    // Open CSV file
    ofstream outfile(filename);
    if (!outfile.is_open())
    {
        cerr << "Failed to open file: " << filename << endl;
        return;
    }

    // Write headers to CSV file
    for (auto& header : headers)
    {
        outfile << header << ",";
    }
    outfile << endl;

    // Write data to CSV file
    for (auto& row : data)
    {
        for (auto& value : row)
        {
            outfile << value << ",";
        }
        outfile << endl;
    }

    // Close CSV file
    outfile.close();
}

int main()
{
    // Initialize nanomsg socket
    int sock = nn_socket(AF_SP, NN_PAIR);
    nn_connect(sock, "ipc:///tmp/market_data.ipc");

    // Receive market data from producer
    char* buffer = NULL;
    int bytes = nn_recv(sock, &buffer, NN_MSG, 0);
    MarketData market_data;
    market_data.ParseFromArray(buffer, bytes);
    nn_freemsg(buffer);

    // Convert market data to CSV format
    vector<string> stock_headers = {"Ticker", "Price", "Volume"};
    vector<string> index_headers = {"Ticker", "Value"};
    vector<string> futures_headers = {"Ticker", "Price", "Contract Size"};

    vector<vector<string>> stock_data;
    for (auto& quote : market_data.stock_quotes())
    {
        vector<string> row = {quote.ticker(), to_string(quote.price()), to_string(quote.volume())};
        stock_data.push_back(row);
    }

    vector<vector<string>> index_data;
    for (auto& quote : market_data.index_quotes())
    {
        vector<string> row = {quote.ticker(), to_string(quote.value())};
        index_data.push_back(row);
    }

    vector<vector<string>> futures_data;
    for (auto& quote : market_data.futures_quotes())
    {
        vector<string> row = {quote.ticker(), to