Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2018
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.33 KB | None | 0 0
  1. #include <cpprest/http_listener.h>
  2. #include <cpprest/http_client.h>
  3. #include <thread>
  4. #include <Windows.h>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <memory>
  8. #include <vector>
  9. #include <map>
  10. #include <chrono>
  11. #include <string.h>
  12. #include <atomic>
  13. #include <future>
  14.  
  15. namespace http = web::http;
  16. namespace server_listener = http::experimental::listener;
  17. std::atomic_bool flag{ false };
  18.  
  19. enum class TransactionType : uint16_t {
  20.     Debit,
  21.     Credit
  22. };
  23.  
  24. enum class AccountType : uint16_t {
  25.     Savings,
  26.     Current,
  27.     FixedDeposit
  28. };
  29.  
  30. struct DateTime {
  31.     SYSTEMTIME time_of_day;
  32.     uint16_t year;
  33.     uint16_t month;
  34.     uint16_t day;
  35. };
  36.  
  37. void obsfucate( std::string & s )
  38. {
  39.     for ( auto &t : s ) t = ~t;
  40. }
  41.  
  42. void decode( std::string & s )
  43. {
  44.     for ( auto &t : s ) t = ~t;
  45. }
  46.  
  47. struct StatementOfAccount {
  48.     uint64_t amount;
  49.     DateTime date_time;
  50.     TransactionType trans_type;
  51.     std::string remarks;
  52. };
  53.  
  54. struct UserInfo {
  55.  
  56.     struct Header {
  57.         uint16_t transaction_len;
  58.         uint16_t remarks_len;
  59.         uint16_t fullname_len;
  60.     } header;
  61.  
  62.     unsigned int balance;
  63.     unsigned int pin;
  64.     unsigned int account_number;
  65.     std::string full_name;
  66.     std::vector<StatementOfAccount> transactions;
  67. };
  68.  
  69. #define WRITE(object,mem_function,s) (fwrite( &(object.mem_function), sizeof(s), 1, file ))
  70.  
  71. void WriteToFileFromMap( FILE *file, std::map<unsigned int, UserInfo> const & map )
  72. {
  73.     for ( auto & m : map ) {
  74.         UserInfo info = m.second;
  75.  
  76.         WRITE( info.header, transaction_len, uint16_t );
  77.         WRITE( info.header, remarks_len, uint16_t );
  78.         WRITE( info.header, fullname_len, uint16_t );
  79.         WRITE( info, balance, unsigned int );
  80.         WRITE( info, pin, unsigned int );
  81.         WRITE( info, account_number, unsigned int );
  82.  
  83.         obsfucate( info.full_name );
  84.         fwrite( info.full_name.c_str(), info.header.fullname_len, 1, file );
  85.  
  86.         for ( uint16_t x = 0; x != info.header.transaction_len; ++x ){
  87.             auto &d = info.transactions[ x ];
  88.  
  89.             WRITE( d, amount, uint64_t );
  90.             DateTime dt = d.date_time;
  91.             WRITE( dt, year, uint16_t );
  92.             WRITE( dt, month, uint16_t );
  93.             WRITE( dt, day, uint16_t );
  94.             WRITE( d, trans_type, uint16_t );
  95.  
  96.             obsfucate( d.remarks );
  97.             fwrite( d.remarks.c_str(), info.header.remarks_len, 1, file );
  98.         }
  99.     }
  100. }
  101. #undef WRITE
  102.  
  103. #define READ(object,mem_function,s) (fread(&(object.mem_function), sizeof(s), 1, file ))
  104.  
  105. void ReadFromFileToMap( FILE *file, std::map<unsigned int, UserInfo> & map )
  106. {
  107.     UserInfo info;
  108.  
  109.     READ( info.header, transaction_len, uint16_t );
  110.     READ( info.header, remarks_len, uint16_t );
  111.     READ( info.header, fullname_len, uint16_t );
  112.     READ( info, balance, unsigned int );
  113.     READ( info, pin, unsigned int );
  114.     READ( info, account_number, unsigned int );
  115.  
  116.     info.full_name.resize( info.header.fullname_len );
  117.     fread( &info.full_name[ 0 ], info.header.fullname_len, 1, file );
  118.     decode( info.full_name );
  119.  
  120.     std::vector<StatementOfAccount> statement( info.header.transaction_len, StatementOfAccount() );
  121.  
  122.     for ( auto & s : statement ){
  123.         READ( s, amount, uint64_t );
  124.         DateTime &dt = s.date_time;
  125.         READ( dt, year, uint16_t );
  126.         READ( dt, month, uint16_t );
  127.         READ( dt, day, uint16_t );
  128.  
  129.         READ( s, trans_type, uint16_t );
  130.  
  131.         s.remarks.resize( info.header.remarks_len );
  132.         READ( s, remarks[ 0 ], s.remarks.size() );
  133.     }
  134.     map.emplace( info.account_number, info );
  135. }
  136.  
  137. #undef READ
  138.  
  139. void foo( std::map<unsigned int, UserInfo> & m_data )
  140. {
  141.     std::map<unsigned int, UserInfo> map{};
  142.  
  143.     FILE *file = NULL;
  144.     auto t = fopen_s( &file, "file.db", "rb" );
  145.     if ( !file ){
  146.         auto c = reinterpret_cast< uintptr_t >( ReadFromFileToMap );
  147.         FILE *file = NULL;
  148.         auto q = fopen_s( &file, "file.db", "wb" );
  149.         if ( file ){
  150.             SYSTEMTIME t;
  151.             GetSystemTime( &t );
  152.  
  153.             for ( uint16_t i = 0; i != 200; ++i ){
  154.                 UserInfo info;
  155.                 info.header.transaction_len = i + 1;
  156.                 info.header.remarks_len = 26;
  157.                 info.header.fullname_len = 6;
  158.                 info.pin = rand() % 9999 + 1000;
  159.                 info.balance = ( i + 1 ) * ( rand() % 99999 + 1000 );
  160.                 info.account_number = c + ( i * 12 );
  161.                 info.full_name = "Joshua";
  162.  
  163.                 std::vector<StatementOfAccount> vec_account{};
  164.                 for ( int x = 0; x != ( i + 1 ); ++x ){
  165.                     vec_account.push_back( StatementOfAccount{
  166.                         info.balance + 1000, DateTime{ t, 2016, 06, 10 },
  167.                         TransactionType::Debit, "enum class TransactionType"
  168.                     } );
  169.                 }
  170.                 info.transactions = std::move( vec_account );
  171.                 map.insert( { info.account_number, info } );
  172.             }
  173.         }
  174.         std::cout << "Done creating...now writing" << std::endl;
  175.         WriteToFileFromMap( file, map );
  176.         fclose( file );
  177.  
  178.         fopen_s( &file, "file.db", "rb" );
  179.         ReadFromFileToMap( file, m_data );
  180.     }
  181.     else {
  182.         ReadFromFileToMap( file, m_data );
  183.     }
  184.  
  185.     bool lflag = false;
  186.     while ( !flag.compare_exchange_weak( lflag, true ) );
  187.  
  188.     std::cout << "Compare exchange weak....done.\n";
  189. }
  190.  
  191. struct ServerThread
  192. {
  193. private:
  194.  
  195.     static std::map<unsigned int, UserInfo> data;
  196.     static std::map<unsigned int, utility::string_t> loggedin_users;
  197. public:
  198.     static void processGetter( http::http_request request )
  199.     {
  200.         if ( !flag.load() ){
  201.             request.reply( http::status_codes::PartialContent );
  202.         }
  203.         else {
  204.             request.reply( http::status_codes::OK );
  205.         }
  206.     }
  207.  
  208.     static void processPost( http::http_request request )
  209.     {
  210.         if ( !flag.load() ){
  211.             request.reply( http::status_codes::PartialContent );
  212.         }
  213.         else {
  214.             std::wcout << request.absolute_uri().path() << std::endl;
  215.             if ( request.absolute_uri().path() == U( "/login" ) ){
  216.                 std::wcout << L"We are being asked for permission to login\n";
  217.                 request.extract_json().then( [ = ] ( pplx::task<web::json::value> jsonValue ){
  218.  
  219.                     web::json::value login_info{};
  220.                     int username{};
  221.                     utility::string_t password;
  222.  
  223.                     try {
  224.                         login_info = jsonValue.get();
  225.                         username = login_info[ U( "username" ) ].as_integer();
  226.                         password = login_info[ U( "pin" ) ].as_string();
  227.                     }
  228.                     catch ( http::http_exception except ){
  229.                         std::wcerr << "Conversion error: " << except.what() << std::endl;
  230.                     }
  231.                     catch ( ... ) {
  232.                         std::wcerr << "We are unable to identify what went wrong" << std::endl;
  233.                     }
  234.  
  235.                     auto search_result = data.find( username );
  236.                     if ( search_result == data.end() ){
  237.                         web::json::value error{};
  238.                         error[ U( "Error" ) ] = web::json::value( U( "User not found." ) );
  239.                         std::wcerr << "We are done here\n";
  240.                         request.reply( http::status_codes::Unauthorized, error );
  241.                     }
  242.                     else {
  243.                         GUID session_id;
  244.                         CoCreateGuid( &session_id );
  245.  
  246.                         web::json::value ack_message{};
  247.                         uint32_t guid_array[ 3 ] { session_id.Data1, session_id.Data2, session_id.Data3 };
  248.                         auto first_byte = web::json::value( guid_array[ 0 ] ).as_string(),
  249.                             second_byte = web::json::value( guid_array[ 1 ] ).as_string(),
  250.                             third_byte = web::json::value( guid_array[ 2 ] ).as_string();
  251.  
  252.                         auto guid_session_id = first_byte + second_byte + third_byte;
  253.                         loggedin_users.insert( { username, guid_session_id } );
  254.                         ack_message[ U( "Status" ) ] = web::json::value( U( "Success" ) );
  255.                         ack_message[ U( "session_id" ) ] = web::json::value( guid_session_id );
  256.                         ack_message[ U( "More" ) ] = web::json::value( U( "Successfully logged in." ) );
  257.  
  258.                         request.reply( http::status_codes::Accepted, ack_message );
  259.                     }
  260.                 } ).wait();
  261.             }
  262.             request.reply( http::status_codes::Unauthorized );
  263.         }
  264.     }
  265.  
  266.     static void processPut( http::http_request request )
  267.     {
  268.  
  269.     }
  270.  
  271.     static void processLogin( http::http_request request )
  272.     {
  273.  
  274.     }
  275.  
  276.     void operator()()
  277.     {
  278.         std::thread long_computation{ foo, data };
  279.         server_listener::http_listener server{ U( "http://localhost:3000" ) };
  280.  
  281.         server.support( http::methods::GET, processGetter );
  282.         server.support( http::methods::PUT, processPut );
  283.         server.support( http::methods::POST, processPost ); // for accepting ID/password
  284.         server.support( http::methods::CONNECT, processLogin );
  285.  
  286.         try {
  287.             server.open()
  288.                 .then( [ &long_computation ]
  289.             {
  290.             } ).wait();
  291.             while ( true );
  292.         }
  293.         catch ( std::exception const & error_message ) {
  294.             std::cerr << error_message.what() << "\n";
  295.         }
  296.     }
  297. };
  298.  
  299. std::map<unsigned int, UserInfo> ServerThread::data{};
  300. std::map<unsigned int, utility::string_t> ServerThread::loggedin_users{};
  301.  
  302. int main()
  303. {
  304.  
  305.     std::thread server_thread{ ServerThread{} };
  306.  
  307.     http::client::http_client client{ U( "http://localhost:3000" ) };
  308.     server_thread.detach();
  309.  
  310.     auto task = client.request( http::methods::GET ).then( [ &client ] ( http::http_response response ){
  311.  
  312.         if ( response.status_code() != http::status_codes::OK ){
  313.             throw std::runtime_error{ "We don't know what happened but we can't continue" };
  314.         }
  315.  
  316.         http::uri_builder uri_builder{ U( "login" ) };
  317.         web::json::value login_credentials{};
  318.         login_credentials[ U( "username" ) ] = web::json::value::number( 12345678 );
  319.         login_credentials[ U( "pin" ) ] = web::json::value::string( U( "1490" ) );
  320.  
  321.         return client.request( http::methods::POST, uri_builder.to_string(), login_credentials );
  322.     } ).then( [ &client ] ( http::http_response response ) {
  323.         if ( response.status_code() != http::status_codes::Accepted ){
  324.             response.extract_json().then( [] ( pplx::task<web::json::value> jsonValue ){
  325.                 auto reason = jsonValue.get().as_object()[ U( "Error" ) ].as_string();
  326.                 std::wcerr << reason << std::endl;
  327.             } ).wait();
  328.         }
  329.         else {
  330.             auto session_id = std::make_shared<utility::string_t>();
  331.             response.extract_json().then( [ &client, &session_id ] ( pplx::task<web::json::value> jsonValue ){
  332.                 web::json::value value{};
  333.                 try {
  334.                     value = jsonValue.get();
  335.                 }
  336.                 catch ( http::http_exception error ){
  337.                     std::wcerr << error.what() << std::endl;
  338.                 }
  339.                 auto result = value.as_object();
  340.                 std::wcerr << result[ U( "Status" ) ].as_string() << std::endl;
  341.                 std::wcerr << result[ U( "More" ) ].as_string() << std::endl;
  342.                 *session_id = result[ U( "session_id" ) ].as_string();
  343.  
  344.                 web::uri_builder uri_builder{ U( "/home" ) };
  345.                 uri_builder.append_query( U( "username" ), U( "1234" ) );
  346.                 uri_builder.append_query( U( "session_id" ), *session_id );
  347.                 uri_builder.append_query( U( "utility" ), U( "balance" ) );
  348.                 client.request( http::methods::GET, uri_builder.to_string() );
  349.             } ).wait();
  350.         }
  351.     } );
  352.  
  353.     try {
  354.         task.wait();
  355.     }
  356.     catch ( std::exception const & except ){
  357.         std::cout << "Error: " << except.what() << std::endl;
  358.     }
  359.     std::cout << "I am done jare" << std::endl;
  360.     return 0;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement