#pragma once #include <Ice/Identity.ice> // ++ namespace module Remote { // - ++ vector<double> sequence<double> Measurement; // interface - - () interface CallbackReceiver { // - progress-bar void Callback(int num); // - void SendData(Measurement m); }; // interface CallbackSender { // void AddClient(Ice::Identity ident); }; };
//Remote::CallbackSender Ice class ImplCallback: public Remote::CallbackSender { public: ImplCallback(const Ice::CommunicatorPtr& c) : communicator { c } { /* */ th = std::thread([this]() { int count =0; constexpr int sizeMeasurement=30; /*typedef ::std::vector< ::Ice::Double> Measurement; - */ Measurement measurement(sizeMeasurement); std::random_device r; std::default_random_engine e1(r()); std::uniform_real_distribution<double> uniform_dist(-10, 10); while(true) { std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::lock_guard<std::mutex> lk(mut); auto it = clients.begin(); auto itend=clients.end(); for(;it!=itend;) { try { /* - progress-bar*/ (*it)->Callback(++count); for(auto& m:measurement) m=uniform_dist(e1); /* - */ (*it)->SendData(measurement); ++it; } catch(const std::exception& ex) { /* - !*/ clients.erase(it++); } } } }); th.detach(); } /* */ virtual void AddClient(const Ice::Identity& ident, const Ice::Current& current = ::Ice::Current()) override { cout << "adding client `" << communicator->identityToString(ident) << "'" << endl; std::lock_guard<std::mutex> lk(mut); /* . */ CallbackReceiverPrx c = CallbackReceiverPrx::uncheckedCast(current.con->createProxy(ident)); clients.insert(c); } private: /* */ std::set<Remote::CallbackReceiverPrx> clients; Ice::CommunicatorPtr communicator; std::mutex mut; std::thread th; };
void ServerFun() { Ice::CommunicatorPtr ic; try { /* Ice*/ ic = Ice::initialize(); /* WebSocket 20002*/ /* - */ Ice::ObjectAdapterPtr adapter2 = ic->createObjectAdapterWithEndpoints("Callback.Server", "ws -p 20002"); /* ImplCallback sender*/ adapter2->add(new ImplCallback(ic), ic->stringToIdentity("sender")); /* - !*/ adapter2->activate(); while (true) { std::this_thread::sleep_for(std::chrono::seconds(1)); } ic->shutdown(); ic->destroy(); } catch (const std::exception& ex) { cout << ex.what() << endl; if (ic) { try { ic->destroy(); } catch (const Ice::Exception& ex2) { cout << ex2 << endl; } } } }
"use strict" var app = angular.module('webApp', []); // angular app.controller('webController', function myController($scope) { // 1- 2- 3- $scope.mode = 1; //progress-bar 0 100 $scope.valuenow = 0; // - radio html $scope.mode1 = function() { $scope.mode = 1; } var communicator = Ice.initialize(); // var CallbackReceiverI = Ice.Class(Remote.CallbackReceiver, { // progress-bar Callback : function(num, current) { $scope.valuenow = num % 100; $scope.$apply(); }, // SendData: function(measurement){ var data, graph; var container = document.getElementById('container'); data = []; for (var i = 0; i <measurement.length; ++i) { data.push([ i, measurement[i] ]); } // flotr2 . if ($scope.mode == 1) { graph = Flotr.draw(container, [ data ], { colors : [ '#C0D800' ], yaxis : { max : 12, min : -12 } }); } //else ... } }); var proxy2 = communicator.stringToProxy("sender:ws -h localhost -p 20002"); // AddClient Remote.CallbackSenderPrx.checkedCast(proxy2).then(function(pr2) { communicator.createObjectAdapter("").then(function(adapter) { var r = adapter.addWithUUID(new CallbackReceiverI()); proxy2.ice_getCachedConnection().setAdapter(adapter); pr2.AddClient(r.ice_getIdentity()); // Heartbeat proxy2.ice_getCachedConnection().setACM(undefined, undefined, Ice.ACMHeartbeat.HeartbeatAlways); }); }); });
Source: https://habr.com/ru/post/325942/
All Articles