Tuesday, October 31, 2017

How to add C/C++ language service to a Visual Studio-based application?

Leave a Comment

I am trying to create a domain specific IDE based on Visual Studio shell. I followed the walk-through to create a basic Isolated Visual Studio Shell Application.

Then I started the basic shell application and drag a C file into it. There's no syntax highlighting because no language service is included yet.

I found the GUIDs for various Visual Studio feature packages. I was expecting some kind of language feature package for C/C++ listed there so I can just add it into my IDE's feature list. But there's not.

So how can I make my IDE recognize the C/C++ language?

Or put it another way, how can I customize Visual Studio to include only features I need?

Some References

There are many extension types I can do with Visual Studio. What interests me most is the Visual Studio-based application. I will put the findings here as my research goes on.

Walkthrough: Creating a Basic Isolated Shell Application

Package GUIDs of Visual Studio Features

Legacy Language Service Extensibility

New Editor and Language Service Extensions

Visual Studio Isolated Shell

Visual Studio Integrated Shell says this:

No programming language is included. However, the integrated shell does provide a framework that allows you to add programming languages.

0 Answers

Read More

How to convert a file to UTF-8 in php?

Leave a Comment

Is it possible to convert a file into UTF-8 on my end?

If I have an access on the file after the submission with

$_FILES['file']['tmp_name'] 

Note: The user can upload a CSV file with any kind of charset, I usually encounter an unknown 8-bit charset.

I try

$row = array(); $datas = file($_FILES['file']['tmp_name']); foreach($datas as $data) {     $data = mb_convert_encoding($data, 'UTF-8');     $row[] = explode(',', $data); } 

But the problem is, this code remove special characters like single quote.

My first question is htmlspecialchars remove the value inside the array?

I put it for additional information. Thanks for those who can help!

4 Answers

Answers 1

Try this out.
The example I have used was something I was doing in a test environment, you might need to change the code slightly.

I had a text file with the following data in:

test café áÁÁÁááá žžœš¥± ÆÆÖÖÖasØØ ß 

Then I had a form which took a file input in and performed the following code:

function neatify_files(&$files) {     $tmp = array();     for ($i = 0; $i < count($_FILES); $i++) {         for ($j = 0; $j < count($_FILES[array_keys($_FILES)[$i]]["name"]); $j++) {             $tmp[array_keys($_FILES)[$i]][$j]["name"] = $_FILES[array_keys($_FILES)[$i]]["name"][$j];             $tmp[array_keys($_FILES)[$i]][$j]["type"] = $_FILES[array_keys($_FILES)[$i]]["type"][$j];             $tmp[array_keys($_FILES)[$i]][$j]["tmp_name"] = $_FILES[array_keys($_FILES)[$i]]["tmp_name"][$j];             $tmp[array_keys($_FILES)[$i]][$j]["error"] = $_FILES[array_keys($_FILES)[$i]]["error"][$j];             $tmp[array_keys($_FILES)[$i]][$j]["size"] = $_FILES[array_keys($_FILES)[$i]]["size"][$j];         }     }     return $files = $tmp; }  if (isset($_POST["submit"])) {     neatify_files($_FILES);     $file = $_FILES["file"][0];      $handle = fopen($file["tmp_name"], "r");     while ($line = fgets($handle)) {         $enc = mb_detect_encoding($line, "UTF-8", true);         if (strtolower($enc) != "utf-8") {             echo "<p>" . (iconv($enc, "UTF-8", $line)) . "</p>";         } else {             echo "<p>$line</p>";         }     } } ?> <form action="<?= $_SERVER["PHP_SELF"]; ?>" method="POST" enctype="multipart/form-data">     <input type="file" name="file[]" />     <input type="submit" name="submit" value="Submit" /> </form> 

The function neatify_files is something I wrote to make the $_FILES array more logical in its layout.

The form is a standard form that simply POSTs the data to the server.
Note: Using $_SERVER["PHP_SELF"] is a security risk, see here for more.

When the data is posted I store the file in a variable. Obviously, if you are using the multiple attribute your code won't look quite like this.

$handle stores the entire contents of the text file, in a read-only format; hence the "r" argument.

$enc uses the mb_detect_encoding function to detect the encoding (duh).
At first I was having trouble with obtaining the correct encoding. Setting the encoding_list to use only UTF-8, and setting strict to be true.

If the encoding is UTF-8 then I simply print the line, if it didn't I converted it to UTF-8 using the iconv function.

Answers 2

before you can convert it to utf-8, you need to know what characterset it is. if you can't figure that out, you can't in any sane way convert it to utf8.. however, an insane way to convert it to utf-8, if the encoding cannot be determined, is to simply strip any bytes that doesn't happen to be valid in utf-8, you might be able to use that as a fallback...

warning, untested code (im suddenly in a hurry), but may look something like this:

foreach ( $datas as $data ) {     $encoding = guess_encoding ( $data );     if (empty ( $encoding )) {         // encoding cannot be determined...         // as a fallback, we simply strip any bytes that isnt valid utf-8...         // obviously this isn't a reliable conversion scheme.         // also this could probably be improved         $data = iconv ( "ASCII", "UTF-8//TRANSLIT//IGNORE", $text );     } else {         $data = mb_convert_encoding ( $data, 'UTF-8', $encoding );     }     $row [] = explode ( ',', $data ); } function guess_encoding(string $str): string {     $blacklist = array (             'pass',             'auto',             'wchar',             'byte2be',             'byte2le',             'byte4be',             'byte4le',             'BASE64',             'UUENCODE',             'HTML-ENTITIES',             '7bit',             '8bit'      );     $encodings = array_flip ( mb_list_encodings () );     foreach ( $blacklist as $tmp ) {         unset ( $encodings [$tmp] );     }     $encodings = array_keys ( $encodings );     $detected = mb_detect_encoding ( $str, $encodings, true );     return ( string ) $detected; } 

Answers 3

you can convert the file text into binary data by using the following

FUNCTION bin2text($bin_str)  {      $text_str = '';      $chars = EXPLODE("\n", CHUNK_SPLIT(STR_REPLACE("\n", '', $bin_str), 8));      $_I = COUNT($chars);      FOR($i = 0; $i < $_I; $text_str .= CHR(BINDEC($chars[$i])), $i  );      RETURN $text_str;  }   FUNCTION text2bin($txt_str)  {      $len = STRLEN($txt_str);      $bin = '';      FOR($i = 0; $i < $len; $i  )      {          $bin .= STRLEN(DECBIN(ORD($txt_str[$i]))) < 8 ? STR_PAD(DECBIN(ORD($txt_str[$i])), 8, 0, STR_PAD_LEFT) : DECBIN(ORD($txt_str[$i]));      }      RETURN $bin;  } 

after converting the data into binary you simply change the text to php method mb_convert_encoding($fileText, "UTF-8");

Answers 4

Let's try this:

function encode_utf8($data) {     if ($data === null || $data === '') {         return $data;     }     if (!mb_check_encoding($data, 'UTF-8')) {         return mb_convert_encoding($data, 'UTF-8');     } else {         return $data;     } } 

Usage:

$content = file_get_contents($_FILES['file']['tmp_name']); $content = encode_utf8($content);  $rows = explode("\n", $content); foreach ($rows as $row) {     print_r($row); } 
Read More

Flexdashboard/plotly interaction results in odd scroll bar behavior

Leave a Comment

I have a bizarre and very frustrating problem. When I build plotly graphs within storyboards (from the flexdashboard package), I get a very annoying and totally unnecessary scroll bar in my legend. When someone tries to click one of the dots on or off, the scroll bar twitches and its practically impossible to click the thing. This scroll bar only appears when the tab with the plotly graph is not immediately visible during the load of the page - i.e. if the page loads with some other tab selected.

I can make the same graph outside of the storyboard, with no problems either in RStudio, or saving it as an htmlwidget and loading it in Chrome. But when I load my storyboard, either in RStudio or in Chrome, I get this annoying scrollbar. The scrollbar exists whether it's a vertical or horizontal legend.

ggplotly objects do not have this problem.

Here's an example of the unnecessary scroll bar. The ggplotly graph is fine, and the plotly one has the scrollbar.

--- title: "Untitled" output:    flexdashboard::flex_dashboard:     storyboard: true ---  ```{r setup, include=FALSE} library(flexdashboard) ```  ### ggplot  ```{r}  library(plotly)  carggplot <- ggplot(mtcars, aes(hp, mpg, fill = as.factor(carb))) +     geom_point() +     theme_bw()  ggplotly(carggplot) ```   ### plotly  ```{r} carsplot <- plot_ly(     data = mtcars,     x = ~hp,     y = ~mpg,     color = ~as.factor(carb),     type = "scatter",     mode = "markers"     )  carsplot ``` 

I have been unable to find any documentation on this issue, although I found a similar problem posted by someone using the python interface to plotly.

I'm looking for a way to either turn off the scroll bar completely (while keeping the legend), or some explanation of the scroll bar's twitchy behavior.

flexdashboard is 0.5, plotly is 4.7.1, R is 64 bit 3.4.1, Windows 7.

1 Answers

Answers 1

There are quite a lot of moving parts going on to answer your question. I won't cover each in detail but touch on them only briefly.

Background

  1. The plotly.js chart is behaving as designed when the length of the legend gets longer, it automatically inserts the scrollbar.

  2. This is not happening with the ggplotly chart because all the visual styling is coming from the ggplot object.

  3. flexdashboard is the culprit in this case because of how it is dynamically fitting the available space and informing plotly.js on how to render. It is worth noting this appears as SVG in the source html.

  4. The solution is then to manipulate the DOM in order to hide/remove/alter the problematic element.

Potential Solution

The solution I offer below is therefore a bit of a hack. A more permanent solution may be to lodge an issue with the good people at RStudio to see if any change could be made to the package, which addresses your problem.

If you add runtime: shiny to your YAML header, you can then make use of the excellent shinyjs package by Dean Attali. While I'm not an expert in this space, I've added a few lines to your MRE that remove <rect> elements from the SVG of class=scrollbar. Important to note you may need to alter the javascript I offer to be more specific and not remove elements you may wish to retain.

Update to MRE

Here is the Rmd code with comments where I've made changes.

--- title: "Untitled" output:    flexdashboard::flex_dashboard:     storyboard: true   runtime: shiny ---  ```{r setup, include=FALSE} library(shinyjs)        # add package, note runtime option above in YAML useShinyjs(rmd = TRUE)  # function needs to initialise js  library(flexdashboard) library(plotly) ```  ### ggplot  ```{r}  library(plotly)  carggplot <- ggplot(mtcars, aes(hp, mpg, fill = as.factor(carb))) +     geom_point() +     theme_bw()  ggplotly(carggplot) ```   ### plotly  ```{r} carsplot <- plot_ly(     data = mtcars,     x = ~hp,     y = ~mpg,     color = ~as.factor(carb),     type = "scatter",     mode = "markers"     )  carsplot  runjs("$svg.selectAll('rect[class=scrollbar]').remove();") # run plain js to remove elements ``` 

N.B. As I post this, I have had unreliable results and will dig in further.

Read More

RequireJS effecting an embedded jquery-ui widget

Leave a Comment

I have a jQuery widget that are partner is trying to embed. The problem we are getting is the partner is using requireJS and its effecting the widget.

The widget is in an anonymous function and requires jquery-ui within. After debugging we have found that jQuery UI is being removed after the noConflict call. Here is the code from the widget.

(function () {      // Localize jQuery variable     var jQueryWidget;      /******** Load jQuery if not present *********/     if (window.jQuery === undefined || window.jQuery.fn.jquery !== '3.2.1') {         var script_tag = document.createElement('script');         script_tag.setAttribute("type", "text/javascript");         script_tag.setAttribute("src", "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js");         script_tag.onload = scriptLoadHandler;         script_tag.onreadystatechange = function () { // Same thing but for IE             if (this.readyState == 'complete' || this.readyState == 'loaded') {                 scriptLoadHandler();             }         };         (document.getElementsByTagName("head")[0] || document.documentElement).appendChild(script_tag);     } else {         loadJQueryUi();     }      function scriptLoadHandler() {         loadJQueryUi();         }      function loadJQueryUi() {     /******* Load UI *******/         jQuery.getScript('https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js', function () {           jQueryWidget = jQuery.noConflict(true);           setHandlers(jQueryWidget);         });           /******* Load UI CSS *******/         var css_link = jQuery("<link>", {             rel: "stylesheet",             type: "text/css",             href: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css"         });         css_link.appendTo('head');     }      function setHandlers($) {         $(document).on('focus', '#start-date, #end-date', function(){        $('#start-date').datepicker({         dateFormat: "M dd, yy",         minDate: 'D',         numberOfMonths: 1,       });              $('#end-date').datepicker({                 dateFormat: "M dd, yy",                 minDate:'+1D',                 numberOfMonths:1,             });     } })(); 

Using chrome debugger we can see that when the getScript is called it correctly adds jquery-ui to the loaded version. Its straight after we call the noConflict that it restores the previous jQuery but are version no longer has jQueryUI.

Testing the widget on others sites without requireJS works correctly.

Has anyone came across this before? Unfortunately we have not worked with RequireJS before but cant see why it would effect are anonymous function.

Any help would be really appreciated.

2 Answers

Answers 1

The problem is that what you are trying to do is unsafe. There are two factors that, combined, work against you:

  1. Scripts are loaded asynchronously. The only thing you control is the relative order in which your widget loads jQuery and jQueryUI. However, the page in which your widget operates also load its own version of jQuery. Your code cannot coerce the order in which scripts loaded by the partner code are going to load.

  2. jQuery is not a well-behaved AMD module. A well-behaved AMD module calls define to gets its dependencies and it does not leak anything into the global space. Unfortunately, jQuery does leak $ and jQuery into the global space.

With these two factors combined, you are facing a race condition depending on which order the two versions of jQuery are loaded: it is generally impossible to know which version of jQuery the global symbols $ and jQuery are referring to. Consider your code:

jQuery.getScript('https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js', function () {   jQueryWidget = jQuery.noConflict(true);   setHandlers(jQueryWidget); }); 

You cannot know whether jQuery refers the version you asked be loaded or to the version that the partner code wanted to load. The only thing .getScript guarantees is that the callback will be called after the script is loaded, but it does not prevent other scripts from loading between the script that .getScript loads and the time the callback is called. The browser is absolutely free to load the script you give to .getScript, load some other script that was requested through RequireJS, and then call your callback.

If you want your widget to be something that can be plopped into a page without having to change any of the existing code, then there's no simple fix. You cannot just change the logic you show in your question, nor can you just add RequireJS to your widget. RequireJS cannot by itself fix this. Contrarily to what the other answer suggest, the context configuration option for RequireJS is not a fix. It would be a fix if there were no scripts that try to access jQuery through the global $ or jQuery, but there are a dozens of plugins for jQuery that do just that. You cannot ensure that the partner code does not use them.

And beware of proposed fixes that seem to fix the problem. You can try a fix, and it seems to work, and you think the problem is solved but really the problem is not manifesting itself because, well, it is a race condition. Everything is fine, until one month later, another partner loads your widget and boom: their page creates just the right conditions to cause things to load in an order that screws up your code.

There is an additional complication which you may not have run into yet but is bound to happen from time to time. (Again, you are dealing with race conditions, so...) You code is loading jQuery and jQuery UI through script elements. However, they both check whether define is available, and if so, they will call define. This can cause a variety of problems depending on the order in which everything happens, but one possible issue is that if RequireJS is present before your widget loads, jQuery UI will call define from a script element and this will give rise to a mismatched anonymous define error. (There's a different issue with jQuery, which is more complicated, and not worth getting into.)

The only way I can see to get your widget to load without interference from the partner code, and without requiring the partner to change their code would be to use something like Webpack to build your code into a single bundle in which define should be forced to be falsy in your bundle so that any code that tests for the presence of define is not triggered. (See import-loader, which can be used for this.) If you load your code as a single bundle, then it can initialize itself in a synchronous manner, and you can be sure that $ and jQuery refer to the jQuery you've included in your bundle.


If you are going to follow my advice here is a nice example, that takes full advantage of Webpack, includes correct minification, and eliminates some artifacts from your code that are no longer needed with this approach (for instance the IIFE, and some of the functions you had). It is runnable locally by saving the files, running:

  1. npm install webpack jquery jquery-ui imports-loader lite-server
  2. ./node_modules/.bin/webpack
  3. ./node_modules/.bin/lite-server

And there's something I did not realize when I first wrote my explanation but that I noticed now. It is not necessary to call noConflict when you wrap your code with Webpack because when it is wrapped by Webpack, jQuery detects a CommonJS environment with a DOM and turns on a noGlobal flag internally which prevents leaking into the global space.

webpack.conf.js:

const webpack = require('webpack'); module.exports = {     entry: {         main: "./widget.js",         "main.min": "./widget.js",     },     module: {         rules: [{             test: /widget\.js$/,             loader: "imports-loader?define=>false",         }],     },     // Check the options for this and use what suits you best.     devtool: "source-map",     output: {         path: __dirname + "/build",         filename: "[name].js",         sourceMapFilename: "[name].map.js",     },     plugins: [         new webpack.optimize.UglifyJsPlugin({             sourceMap: true,             include: /\.min\.js$/,         }),     ], }; 

Your widget as widget.js:

var $ = require("jquery"); require("jquery-ui/ui/widgets/datepicker");  var css_link = $("<link>", {     rel: "stylesheet",     type: "text/css",     href: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css" }); css_link.appendTo("head");  $(document).ready(function() {     console.log("jQuery compare (we want this false)", $ === window.$);     console.log("jQuery version in widget",  $.fn.jquery);     console.log("jQuery UI version in widget", $.ui.version);     $("#end-date").datepicker(); }); 

index.html:

<!DOCTYPE html> <html>   <head>     <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script>     <script>       require.config({         paths: {           jquery: "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min",           "jquery-ui": "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui"         }       });       require(["jquery", "jquery-ui"], function(myJQuery) {         console.log("jQuery compare (we want this true)", myJQuery === $);          console.log("jQuery version main", $.fn.jquery);         console.log("jQuery ui version main", $.ui.version);       })     </script>   </head>   <body>     <input id="end-date">     <script src="build/main.min.js"></script>      <!-- The following also works: -->     <!--     <script>       require(["build/main.min.js"]);     </script>     -->   </body> </html> 

Answers 2

I think problem is with jQueryWidget = jQuery.noConflict(true);

true means remove all jQuery variables from the global scope.

jQuery.noConflict( [removeAll ] ) removeAll Type: Boolean A Boolean indicating whether to remove all jQuery variables from the global scope (including jQuery itself).

[noconflict][1]

One thing we can try removing the true boolean parameter, Let know if it helps.

UPDATE 2: Below approach should not require any partner code changes

<!DOCTYPE html>         <html>          <head>             <title></title>             <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script>             <script type="text/javascript">             (function() {                  // Localize jQuery variable                 var jQueryWidget;                 /*                 *                 *                 *                     This is plugin's require config. Only used by plugin and                     will not affect partner config.                 *                 *                 */                 var requireForPlugin = require.config({                     context: "pluginversion",                     paths: {                         "jquery": "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min",                          "jquery.ui.widget": "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min"                      }                 });                 requireForPlugin(["require", "jquery", "jquery.ui.widget"], function() {                     /******** Load jQuery if not present *********/                     if (window.jQuery === undefined || window.jQuery.fn.jquery !== '3.2.1') {                         scriptLoadHandler();                     } else {                         loadJQueryUi();                     }                      function scriptLoadHandler() {                         loadJQueryUi();                     }                      function loadJQueryUi() {                         jQueryWidget = jQuery.noConflict(true);                         setHandlers(jQueryWidget);                         var css_link = jQueryWidget("<link>", {                             rel: "stylesheet",                             type: "text/css",                             href: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css"                         });                         css_link.appendTo('head');                     }                      function setHandlers($) {                         $('#end-date').on('click', function() {                             alert('JQUERY--' + $().jquery);                             alert('JQUERY UI--' + $.ui.version);                             $('#end-date').datepicker({                                 dateFormat: "M dd, yy",                                 minDate: '+1D',                                 numberOfMonths: 1,                             });                         });                     }                 });             })();             </script>             <script>             //SAMPLE PARTNER APPLICATION CODE:              /*             *             *             *                 This is partner's require config                 which uses diffrent version of jquery ui and                 jquery             *             *             */             require.config({                 paths: {                     "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min",                      "jquery.ui.widget": "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui"                  }             });             require(["jquery", "jquery.ui.widget"], function() {                 $('#btn').on('click', function() {                     alert('JQUERY--' + $().jquery);                     alert('JQUERY UI--' + $.ui.version);                 });             })             </script>         </head>          <body>             <div><span>FOCUS</span>                 <input type="text" name="" id="end-date" />             </div>             <button id="btn" style="width: 100px; height:50px; margin:10px">click me</button>         </body>          </html> 

I have modified the plugin code, so that it uses its own jquery and jquery ui version(I am making use of the requirejs here).

Also for demo I have added a sample partner script which gives alert on button click. you can see without modifying the partner code and requirejs config, plugin can work now.

Both plugin and partner code have there independent jquery and jquery ui versions.

Hope this helps.

References: Using Multiple Versions of jQuery with Require.js and http://requirejs.org/docs/api.html#multiversion

UPDATE3: using webpack and imports loader We can use webpack to bundle the plugin js code in that case plugin will have its own version of jquery, we have to change the way the plugin is being build.

Install webpack, jquery, imports loader and jquery-ui using npm and build it, below is the sample code:

main.js used imports loader to make define as false

require('imports-loader?define=>false!./app.js'); 

app.js which includes the plugin code and it adds the required dependencies

 (function() {       var $ = require('jquery');      require('jquery-ui');      require('jquery-ui/ui/widgets/datepicker.js');       function scriptLoadHandler() {          loadJQueryUi();      }       $(document).ready(function() {          scriptLoadHandler();      });       function loadJQueryUi() {          setHandlers();          var css_link = $("<link>", {              rel: "stylesheet",              type: "text/css",              href: "https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css"          });          css_link.appendTo('head');      }       function setHandlers() {          $('#end-date').on('click', function() {              alert('JQUERY--' + $().jquery);              alert('JQUERY UI--' + $.ui.version);              $('#end-date').datepicker({                  dateFormat: "M dd, yy",                  minDate: '+1D',                  numberOfMonths: 1,              });          });      }  })(); 

webpack.config.js

  var webpack = require('webpack');    module.exports = {   entry: "./main.js",   output: {   filename: "main.min.js"      }   }; 

sample.html

<!DOCTYPE html> <html>  <head>     <title></title>     <script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.5/require.js"></script>      <script src="main.min.js"></script>      <script>     //SAMPLE PARTNER APPLICATION CODE:      /*     *     *     *         This is partner's require config         which uses diffrent version of jquery ui and         jquery     *     *     */     require.config({         paths: {             "jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min",              "jquery.ui.widget": "https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.0/jquery-ui"          }     });     require(["jquery", "jquery.ui.widget"], function() {         $('#btn').on('click', function() {             alert('JQUERY--' + $().jquery);             alert('JQUERY UI--' + $.ui.version);         });     })     </script> </head>  <body>     <div><span>FOCUS</span>         <input type="text" name="" id="end-date" />     </div>     <button id="btn" style="width: 100px;"></button> </body>  </html> 

after doing a webpack it will generate a main.min.js file which is included in the sample.html file

Read More

Starting Kivy service on bootup (Android)

Leave a Comment

I'm trying to start my kivy app's service on bootup.

I'm sure that my service is ok because it works when I start my app. But on bootup I have a problem.

I've read this article and tried to make it:

package net.saband.myapp;  import android.content.BroadcastReceiver; import android.content.Intent; import android.content.Context; import org.kivy.android.PythonActivity;  public class MyBroadcastReceiver extends BroadcastReceiver {     public void onReceive(Context context, Intent intent) {         Intent ix = new Intent(context, PythonActivity.class);         ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         context.startActivity(ix);     } } 

It works but starts the app but not the service. So I've studied some questions on StackOverflow and changed my code for this:

package net.saband.myapp;  import android.content.BroadcastReceiver; import android.content.Intent; import android.content.Context; import net.saband.myapp.ServiceMyservice;  public class MyBroadcastReceiver extends BroadcastReceiver {     public void onReceive(Context context, Intent intent) {         Intent ix = new Intent(context, ServiceMyservice.class);         ix.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);         context.startService(ix);     } } 

... and got an error:

10-21 19:16:44.784  1513  1569 I ActivityManager: Start proc 6334:net.saband.myapp:service_myservice/u0a116 for service net.saband.myapp/.ServiceMyservice 10-21 19:16:44.786  6334  6334 I art     : Late-enabling -Xcheck:jni 10-21 19:16:44.885  6334  6334 D AndroidRuntime: Shutting down VM 10-21 19:16:44.888  6334  6334 E AndroidRuntime: FATAL EXCEPTION: main 10-21 19:16:44.888  6334  6334 E AndroidRuntime: Process: net.saband.myapp:service_myservice, PID: 6334 10-21 19:16:44.888  6334  6334 E AndroidRuntime: Theme: themes:{} 10-21 19:16:44.888  6334  6334 E AndroidRuntime: java.lang.RuntimeException: Unable to start service net.saband.myapp.ServiceMyservice@8c96929 with Intent { cmp=net.saband.myapp/.ServiceMyservice }: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference 

Can you please explain me what's wrong and what should I do to start the service? Thanks!

UPDATED

By request of @Juggernaut I add my service code:

from time import sleep  if __name__ == '__main__':     while True:         print "myapp service"         sleep(5) 

It works when I run app because app calls the service:

def __start_service(self):     if platform == 'android':         service = autoclass('net.saband.myapp.ServiceMyservice')         mActivity = autoclass('org.kivy.android.PythonActivity').mActivity         argument = ''         service.start(mActivity, argument) 

1 Answers

Answers 1

It seems that Service or Activity that you try to start is not initialized. You didn't paste your AndroidManifest, so I'll write what it's supposed to have.

The first thing is that you should have permission added to run on boot:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/> 

The second thing is that you should define your receiver in Manifest:

    <receiver android:name=".MyBroadcastReceiver"               android:enabled="false">         <intent-filter>             <action android:name="android.intent.action.BOOT_COMPLETED"/>         </intent-filter>     </receiver> 

The third thing is that you should define your service or activity you try to start, e.g:

    <activity android:name=".PythonActivity" />     <service android:name=".ServiceMyservice" android:exported="false"/> 
Read More

Swift websockets not accepting client certificate

Leave a Comment

I am working on a project that requires client certificate support with websockets. I am currently using Starscream, however unfortunately from reading the documentation, it does not seem to have any information regarding support for this. I have looked around at few other swift web socket libraries, but none of them mention support for this

Does anyone know of any libraries that support such functionality?

Any information would be much appreciated!!

Edit:

So I am currently using Starscream to try this. I have got the certificate setup. here is the code I am trying so far

public struct IdentityAndTrust {     public var identityRef:SecIdentity     public var trust:SecTrust     public var certData : Data }      var socket = WebSocket(url: URL(string: "\(ConstantKeys.ipAddress)")!, protocols: [])     var identityTest : IdentityAndTrust?   func createTrust() {     do     {         let urlPath     = Bundle.main.path(forResource: "client", ofType: "p12")         let url         = NSURL.fileURL(withPath: urlPath!)         let certificateData = try Data(contentsOf: url)          identityTest = extractTrustAndIdentity(certData: certificateData, certPassword: ConstantKeys.password)     }     catch     {         print(error)     } }  func extractTrustAndIdentity(certData:Data, certPassword:String) -> IdentityAndTrust {     var identityAndTrust:IdentityAndTrust!     var securityError:OSStatus = errSecSuccess      var items: CFArray?     let certOptions: Dictionary = [ kSecImportExportPassphrase as String : certPassword ];     // import certificate to read its entries     securityError = SecPKCS12Import(certData as CFData, certOptions as CFDictionary, &items);     if securityError == errSecSuccess {          let certItems:CFArray = items as CFArray!;         let certItemsArray:Array = certItems as Array         let dict:AnyObject? = certItemsArray.first;          if let certEntry:Dictionary = dict as? Dictionary<String, AnyObject> {              // grab the identity             let identityPointer:AnyObject? = certEntry["identity"];             let secIdentityRef:SecIdentity = identityPointer as! SecIdentity!;              // grab the trust             let trustPointer:AnyObject? = certEntry["trust"];             let trustRef:SecTrust = trustPointer as! SecTrust;              // grab the certificate chain             var certRef: SecCertificate?             SecIdentityCopyCertificate(secIdentityRef, &certRef);             let certArray:NSMutableArray = NSMutableArray();             certArray.add(certRef as SecCertificate!);              identityAndTrust = IdentityAndTrust(identityRef: secIdentityRef, trust: trustRef, certData : certData);         }     }     return identityAndTrust } 

I then connect socket like so

let key = SecTrustCopyPublicKey(identityTest!.trust)!;     let ssl =  SSLCert(key: key)      socket.security = SSLSecurity(certs: [ssl], usePublicKeys: false)     socket.enabledSSLCipherSuites = [TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384]     socket.delegate = self     socket.connect() 

But I got the following error message

CFNetwork SSLHandshake failed (-9807)

TCP Conn 0x604000173980 SSLHandshake failed (-9807) websocket is disconnected: The operation couldn’t be completed. (OSStatus error -9807.)

I know the certificate is valid as I use it to make https requests and it works fine. So does anyone know why it is not working? Or does anyone know of another socket library that would help with this issue?

2 Answers

Answers 1

You can do the SSL pinning by simply using NSURLSession (URLSession) without using any third party library but if you still want to use one, SocketRocket, AFNetworking have support for it.

The links below should help you:

http://www.yeradis.com/swift-authentication-challenge

http://www.indelible.org/ink/trusted-ssl-certificates/

https://jetforme.org/2013/05/validating-a-self-signed-ssl-certificate-in-ios-and-os-x-against-a-changing-host-name/enter link description here

Any methods you choose (third party or URLSession), I suggest that you do read this security issue:

https://github.com/facebook/SocketRocket/pull/534

https://www.synopsys.com/blogs/software-security/ineffective-certificate-pinning-implementations/enter link description here

Answers 2

self.urlSession?.dataTaskWithURL(NSURL(string:self.urlTextField.text!)!, completionHandler: { (NSData data, NSURLResponse response, NSError error) Void in // response management code }).resume()

Read More

Lost context in object

Leave a Comment

I have this code:

/// global context function Outer(){     /// Outer context     this.print = function(){          console.log("1: "+this)          inside(); /// "this" is bound to the global object. Why not bound to the Outer object?         function inside(){              console.log("2: "+this)          }     };  }  function print(){     console.log("3: "+this); } var obj = new Outer;  obj.print(); /// "this" is bound to the Outer object. print(); /// "this" is bound to the global object. 

Why inside the method call, this has a global object? Can anyone explain this?

6 Answers

Answers 1

That's because, its obeying following 2 JS rules

  • Rule 1. In Javascript, new function = new scope - that’s the rule.
  • Rule 2. When a function is called as a method of an object, this is set to the object the method is called on.

Explanation of the code:

  • For rule 2, this.print() is a feature of Outer object. So this will refer to the Outer object.

So the console.log 1 will print 1: [object Object]

  • For rule 1, the function inside() in Outer is not a feature of Outer object. So new scope will created and this assigned to window. And that's also same for the last function 'print()'

So console.log 2 will print 2: [object Window]

And console.log 3 will print 3: [object Window] as well

Hope Helps,

Run the code: https://es6console.com/j9bxeati/

Reference: https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/#function-scope

Answers 2

As the reference states,

In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which do provide their own this binding (it remains the this value of the enclosing lexical context).

<...>

Inside a function, the value of this depends on how the function is called.

<...>

Since the following code is not in strict mode, and because the value of this is not set by the call, this will default to the global object

<...>

So, in strict mode, if this was not defined by the execution context, it remains undefined.

The posted code is not executed in strict mode, so this equals to global variable (window) inside the function when it's called like print().

If this is not the desirable behaviour, and print is expected to be called separately from the object it originally was defined on (e.g. when being passed as a callback), the function can be bound to its lexical this with arrow in ES6:

function Outer(){     this.print = () => { ... };  }  

In ES5 bind or self = this recipe can be used:

function Outer(){     this.print = (function(){ ... }).bind(this); }  function Outer(){     var self = this;     this.print = function(){       self;       ...     }; } 

If the function isn't expected to be called with another context, it can be called with another context in-place:

print.call(this); 

Or:

print.bind(this)(); 

Answers 3

If it is not obvious for you, that probably means you confuse scope and context of the function. Scope is what vars function has access to during invocation. Context (this) is determined by how function is invoked.

Now look on how you invoke your function 'inside' and answer the question. Does it vividly owned by any object? That is why you have global

Answers 4

The value of this is determined by how a function is called.

Your "inside" function was called inside "print" function, which has an object reference, but that object did not have "inside" function as a property or that object did not invoke "inside" function.

"this" refers to the object which called it, not the scope in which it is called. Hence, the global object

Answers 5

A function's this keyword behaves a little differently in JavaScript compared to other languages. It also has some differences between strict mode and non-strict mode.

In most cases, the value of this is determined by how a function is called. It can't be set by assignment during execution, and it may be different each time the function is called. ES5 introduced the bind method to set the value of a function's this regardless of how it's called, and ES2015 introduced arrow functions which do provide their own this binding (it remains the this value of the enclosing lexical context).

// An object can be passed as the first argument to call or apply and this will be bound to it.  var obj = {a: 'Custom'};    // This property is set on the global object  var a = 'Global';    function whatsThis(arg) {    return this.a;  // The value of this is dependent on how the function is called  }    whatsThis();          // 'Global'  whatsThis.call(obj);  // 'Custom'  whatsThis.apply(obj); // 'Custom'

Answers 6

the value of this is depends on how you are making a call to the intended function and generally leading parent object plays important role in it.

what does it mean by leading parent object?:

sampleObj.getDetails(); // here 'sampleObj' is the leading parent object for 'getDetails()' function 

lets try to clear the things using some examples of making call to the function using below sample code snippet.

function globalFunction(){     console.log('global this: ', this); } var simpleObj = {   name : 'john'  }; var complexObj = {   outer: function(){     console.log('outer this: ', this);   } } globalFunction(); // will log global object as no leading parent object is available  complexObj.outer(); // will log 'complexObj' object as leading parent object is 'complexObj'  complexObj.outer = complexObj.outer.bind(simpleObj); complexObj.outer(); // will log 'simpleObj' object as we have set preference as 'simpleObj' for this **  complexObj.outer.call(); // will log global object as we arent setting any preference for 'this' ***  complexObj.outer.call(simpleObj); // will log simpleObj object as we have set preference as simpleObj for 'this' ***  complexObj.outer.apply(); // will log global object as we arent setting any preference for 'this' ****  complexObj.outer.apply(simpleObj); // will log simpleObj object as we have set preference as simpleObj for 'this' **** 

hopefully it will help you!

** what is bind: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind

*** what is call: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call

**** what is apply: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply

Read More

Cannot build for android using react-native

Leave a Comment

I'm trying to run react-native run-android but I continuously get this error.I am running on emulator . I started the app using react-native init.

PS: I am running on proxy.

When I am trying to run

react-native run-android

I get:

FAILURE: Build failed with an exception.  *  What went wrong:  A problem occurred configuring root project 'AwesomeProject'.  > Could not resolve all dependencies for configuration ':classpath'.  > Could not resolve com.android.tools.build:gradle:2.2.3.  Required by:      :AwesomeProject:unspecified   > Could not resolve com.android.tools.build:gradle:2.2.3.      > Could not get resource 'https://jcenter.bintray.com/com/android/tool /build/gradle/2.2.3/gradle-2.2.3.pom'.         > Could not GET 'https://jcenter.bintray.com/com/android/tools/buil  /gradle/2.2.3/gradle-2.2.3.pom'. Received status code 407 from server:   Proxy Authentication Required 

When I am trying to run

directly in Android Studio

I get:

Error:Could not GET  'https://jcenter.bintray.com/com/android/tools/build/gradle/2.2.3/gradle- 2.2.3.pom'. Received status code 407 from server: Proxy Authentication   Required <a href="toggle.offline.mode">Enable Gradle 'offline mode' and sync  project</a> 

I have my node_modules installed and I'm not sure what else to try, I've been googling for the last 4 hours and have come up with nothing.

4 Answers

Answers 1

your problem is not related to npm and node. you have some gradle problems.check if you can access the URL from browser?

https://jcenter.bintray.com/com/android/tools/build/gradle/2.2.3/gradle- 2.2.3.pom

Answers 2

set your proxy details Inside sdk manager

AndroidStudio->tools->Android->SDKManager->system settings->Http Proxy

Answers 3

It clearly says <a href="toggle.offline.mode">Enable Gradle 'offline mode' and sync project</a>. Have you tried enabling offline mode? If not, try this:

Go to File -> Settings.

And open the 'Build,Execution,Deployment',Then open the 'Build Tools' -> 'Gradle'.

Then uncheck "Offline work" on the right.

Click the 'OK' button.

Then Rebuild the Project.

For Mac go to AndroidStudio -> Preferences, rest is same.

ref.: https://stackoverflow.com/a/30794890/2895571

Answers 4

Try adding these in your gradle.properties:

systemProp.http.proxyHost=proxyURL systemProp.http.proxyPort=proxyPort systemProp.http.proxyUser=USER systemProp.http.proxyPassword=PASSWORD systemProp.https.proxyHost=proxyUrl  systemProp.https.proxyPort=proxyPort systemProp.https.proxyUser=USER systemProp.https.proxyPassword=PASSWORD 
Read More

Monday, October 30, 2017

What is the difference between isset() and __isset()?

Leave a Comment

I need to know about magic function __isset() and normal function isset(). Actually what is the real difference between php language construct isset() and php magic method __isset() ? When I google it they told that __isset() is a magic function. What are difference between common php functions and magic functions in php?

7 Answers

Answers 1

isset()

It is a language construct that checks the initialization of variables or class properties:

$a = 10;  isset($a);     // true isset($a, $b); // false  class Test {     public $prop = 10; }  $obj = new Test; isset($obj->prop); // true 

__isset()

It is a magic method that is invoked when isset() or empty() check non-existent or inaccessible class property:

class Test {     public function __isset($name) {         echo "Non-existent property '$name'";     } }  $obj = new Test; isset($obj->prop); // prints "Non-existent property 'prop'" and return false 

Difference:

           isset()                               __isset() 
Language construct                    | Magic method                                       | Always return bool                    | Result depends on custom logic*                                       | Must be invoked in code               | Called automatically by event                                       | Unlimited number of parameters        | Has only one parameter                                       | Can be used in any scope              | Must be defined as method**                                       | Is a reserved keyword                 | Not a reserved keyword                                       | Can't be redefined (Parse error)      | Can be redefined in extended class***

__isset() result anyway will be automatically casted as bool.

Actually you can define custom function __isset() but it has nothing to do with the magic method.

See this example.


Magic Methods

Unlike common functions can be defined only in class scope and invoked automatically on specific events such as: inaccessible method invocation, class serialization, when unset() used on inaccessible properties and so on. See also this official documentation: Overloading.

Answers 2

__isset is the magic method. Magic methods are the methods called internally.

Consider following code

<?php // Declare a simple class class TestClass {     public $foo;      public function __construct($foo)     {         $this->foo = $foo;     }      public function __toString()     {         return $this->foo;     } }  $class = new TestClass('Hello'); echo $class; ?> 

here _toString is magic method but you will not call that. When the line echo $class; is executed. PHP knows that now I should treat $class object as a string and to treat any object as the string call_toString method, if implemented in that class.

All magic methods called like this in indirect way.

Another example as follow

<?php class CallableClass {     public function __invoke($x)     {         var_dump($x);     } } $obj = new CallableClass; $obj(5); var_dump(is_callable($obj)); ?> 

Similarly, in above code , var_dump(is_callable($obj)); invokes __invoke magic method indirectly.

Answers 3

First of all let me tell you what isset() function does. The isset() function checks whether the value has been set or is it null. The _isset() function is a magic method in PHP. Any function with a " _ " in the beginning is a magic method in PHP. Now, __isset() is invoked by calling isset() or empty() on inaccessible properties, by that I mean those properties that have not been defined in the class and are being explicitly defined at the run time. Here is a piece of code that should make you understand it better:

<?php class PropertyTest {     /**  Location for overloaded data.  */     private $data = array();      /**  Overloading not used on declared properties.  */     public $declared = 1;      /**  Overloading only used on this when accessed outside the class.  */     private $hidden = 2;      public function __set($name, $value)     {         echo "Setting '$name' to '$value'\n";         $this->data[$name] = $value;     }      public function __get($name)     {         echo "Getting '$name'\n";         if (array_key_exists($name, $this->data)) {             return $this->data[$name];         }          $trace = debug_backtrace();         trigger_error(             'Undefined property via __get(): ' . $name .             ' in ' . $trace[0]['file'] .             ' on line ' . $trace[0]['line'],             E_USER_NOTICE);         return null;     }      /**  As of PHP 5.1.0  */     public function __isset($name)     {         echo "Is '$name' set?\n";         return isset($this->data[$name]);     }      /**  As of PHP 5.1.0  */     public function __unset($name)     {         echo "Unsetting '$name'\n";         unset($this->data[$name]);     }      /**  Not a magic method, just here for example.  */     public function getHidden()     {         return $this->hidden;     } }   echo "<pre>\n";  $obj = new PropertyTest;  $obj->a = 1; echo $obj->a . "\n\n";  var_dump(isset($obj->a)); unset($obj->a); var_dump(isset($obj->a)); echo "\n";  echo $obj->declared . "\n\n";  echo "Let's experiment with the private property named 'hidden':\n"; echo "Privates are visible inside the class, so __get() not used...\n"; echo $obj->getHidden() . "\n"; echo "Privates not visible outside of class, so __get() is used...\n"; echo $obj->hidden . "\n"; ?> 

Answers 4

in simple words, __isset() helps isset() to work over protected/private vars in class .

Example:

class test {     public $x = array(); } 

in the above class you can do this isset($test->x['key']) as the $x is public

but here

class test {     protected $x = array();      function __isset($key)     {         return isset($this->x[$key]);     } } 

$x is protected and you cannot access it, so we created __isset() to help us use isset($x['key'])

you can say that __isset() is just a bridge for isset()

Answers 5

Magic functions are automatically invoked (triggered) when something happens. Normal functions have to be specifically invoked by your php code.

In your case: __isset() will be automatically invoked when you have an isset() which is trying to get a non-accessible property.

Example:

root@folgore:/tmp/php# cat a.php  <?php class a {     private $att1;     public $att2;      function __isset($field) {         echo "__isset invoked for $field\n";     } }  $obj=new a();  // __isset will be triggered: isset($obj->att1);  // __isset will not be triggered: isset($obj->att2);  root@folgore:/tmp/php# php a.php  __isset invoked for att1 

Answers 6

What are difference between common php functions and magic functions in php?

Common PHP function are declared and accessible with expected inputs and results but they should be called. In contrast, magic functions are defined in PHP but when defined in a class then they will be called automatically. For example the isset() is a PHP function

Determine if a variable is set and is not NULL

But __isset() is a Property overloading of a class.

Overloading in PHP provides means to dynamically "create" properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types. The overloading methods are invoked when interacting with properties or methods that have not been declared or are not visible in the current scope.

It will be called magically behind the scene as described above if declared in the class. Let's experiment the PHP Class Property overloading.

<?php     class PropertyTest         {             /**  Location for overloaded data.  */             private $data = array();              /**  Overloading not used on declared properties.  */             public $declared = 1;              /**  Overloading only used on this when accessed outside the class.  */             private $hidden = 2;              public function __set($name, $value)             {                 echo "Setting '$name' to '$value'\n";                 $this->data[$name] = $value;             }              public function __get($name)             {                 echo "Getting '$name'\n";                 if (array_key_exists($name, $this->data)) {                     return $this->data[$name];                 }                  $trace = debug_backtrace();                 trigger_error(                     'Undefined property via __get(): ' . $name .                     ' in ' . $trace[0]['file'] .                     ' on line ' . $trace[0]['line'],                     E_USER_NOTICE);                 return null;             }              /*  As of PHP 5.1.0  */              public function __isset($name)               {                   echo "Is '$name' set?\n";                   return isset($this->data[$name]);               }               /**  As of PHP 5.1.0 */               public function __unset($name)             {                 echo "Unsetting '$name'\n";                 unset($this->data[$name]);             }              /**  Not a magic method, just here for example.  */             public function getHidden()             {                 return $this->hidden;             }         }           echo "<pre>\n";          $obj = new PropertyTest;          //__set() is called when 'a' property is not visible outside of class         $obj->a = 1;         //__get() is called when 'a' property is not visible outside of class         echo "a: ".$obj->a . "\n\n";         //__isset() is called when 'a' property is not visible outside of class         var_dump(isset($obj->a));         unset($obj->a);         //__isset() is called when 'a' property is not visible outside of class         var_dump(isset($obj->a));         echo "\n";         //__isset() is not called as 'declared' property is visible outside of class         var_dump(isset($obj->declared));         //__get() is not called as 'declared' property is visible outside of class                     echo "declared: ". $obj->declared . "\n\n";         //__set() is not called as 'declared' property is visible outside of class         $obj->declared = 3;         //__get() is not called as 'declared' property is visible outside of class          echo "declared: ". $obj->declared . "\n\n";          //__isset() is called as 'hidden' property is not visible outside of class          var_dump(isset($obj->hidden));          echo "Let's experiment with the private property named 'hidden':\n";         echo "Privates are visible inside the class, so __get() not used...\n";         echo $obj->getHidden() . "\n";         echo "Privates not visible outside of class, so __get() is used...\n";         var_dump($obj->hidden);   ?> 

The above code will output

    Setting 'a' to '1'     Getting 'a'     a: 1      Is 'a' set?     bool(true)     Unsetting 'a'     Is 'a' set?     bool(false)      bool(true)     declared: 1      declared: 3      Is 'hidden' set?     bool(false)     Let's experiment with the private property named 'hidden':     Privates are visible inside the class, so __get() not used...     2     Privates not visible outside of class, so __get() is used...     Getting 'hidden'     NULL 

It says the 'hidden' property is not set and shows bool(false) but echos out value '2' later because the 'hidden' property is not visible outside of the class and it calls __isset() magic function but it also is not set in 'data' so it returns bool(false). In getHidden() function though it returns the object private property 'hidden' which is visible to object internal functions. In last var_dump($obj->hidden) it calls __get() method and it returns NULL. Because in __get() method it looks for data['hidden'] which is NULL.

Note: the example here is from PHP Manuel: Overloading with some modifications.

Hope this helps!

Answers 7

isset() is for variables and __isset() is for properties of a class.

Read More

GLSL: Can I combine MRT, ssbo and imageAtomic operations in the same shader (pass)?

Leave a Comment

A 2-pass rendering system in OpenGL is using an MRT shader that is bound to 2 framebuffer textures tex1 and tex2. The goal of the mrt pass is to compute the overdraw in the scene and render it out in a gather pass. I use the framebuffer textures to pass on the result.

It also has a working ssbo buffer that is quite large (and using a fixed screen resolution) and takes ages to link, but I can use it to do atomicAdds. What I am trying to accomplish is to replace this with imageAtomicAdd operations on a uiimage2D, just like with the mrt passes.

The problem is that the result of imageAtomicAdd is always zero, where I expect it to go up just like atomicAdd does at that point..

#version 440 core  layout(early_fragment_tests) in;  // this works fine layout (location = 0) out vec4 tex1; layout (location = 1) out vec4 tex2;  // this works fine layout(std430, binding = 3) buffer ssbo_data {         uint v[1024*768]; };  // this does not work at all. uniform volatile layout(r32ui) uimage2D imgCounter;  out vec4 frag_colour;  void main ()  {            ivec2 coords = ivec2(gl_FragCoord.xy);     uint addValue = 1u;      uint countOverdraw1 = atomicAdd(v[coords.x + coords.y * 1024], 1u);     uint countOverdraw2 = imageAtomicAdd(imgCounter, ivec2(0,0), 1u);       memoryBarrier();      // supports 256 levels of overdraw..     float overdrawDepth = 256.0;     vec3 c1 = vec3(float(countOverdraw1+1)/overdrawDepth ,0,1);     vec3 c2 = vec3(float(countOverdraw2+1)/overdrawDepth ,0,1);      tex1 = vec4(c1,1);       tex2 = vec4(c2,1);       frag_colour = vec4(1,1,1,1); } 

From the khronos website on image atomic operations I gather that..

Atomic operations to any texel that is outside of the boundaries of the bound image will return 0 and do nothing.

.. but the coordinate ivec2(0,0) would be well within the bounds of the texture size (1024 x 768).

Maybe the texture is not set up correctly? This is how I construct the uiimage2D (pieced together from the pipeline flow):

EDIT: I update the code as per suggestion by the answer from Nicol Bolas: texture parameters are set instead of sampler parameters

char data[1024*768*4]; glGenTextures(1, &m_Handle);  m_Target = GL_TEXTURE_2D;  glActiveTexture(GL_TEXTURE0+6); glBindTexture(m_Target,m_Handle);  // updated : a sampler object was bound to the texture, but is now removed  glTexParameteri(m_Target, GL_TEXTURE_MAG_FILTER, GL_NEAREST);  // updated glTexParameteri(m_Target, GL_TEXTURE_MIN_FILTER, GL_NEAREST);  // updated glTexParameteri(m_Target, GL_TEXTURE_WRAP_R, GL_REPEAT);       // updated glTexParameteri(m_Target, GL_TEXTURE_WRAP_S, GL_REPEAT);       // updated glTexParameteri(m_Target, GL_TEXTURE_WRAP_T, GL_REPEAT);       // updated glTexImage2D(m_Target, 0, R32UI, 1024, 768, 0, GL_RED_INTEGER, GL_UNSIGNED_INT,  data);      

If I run it through gDEBugger GL, I see that "Texture data is not available at this time" and while the 'Texture 4' parameters of the texture are filled in and correct, none of the 'Texture Parameters' and 'Level 0 parameters' are shown (N/A). Trapping the debugger at that point throws a whole number of problems that do not appear outside of gDEBugger. Here are the first few:

GL_INVALID_OPERATION error generated. The required buffer is missing. GL_INVALID_ENUM error generated. <pname> requires feature(s) disabled in the current profile.  GL_INVALID_OPERATION error generated. <index> exceeds the maximum number of supported texture units.  GL_INVALID_ENUM error generated. or require feature(s) disabled in the current profile.   GL_INVALID_OPERATION error generated. Can't mix integer and non-integer data ... 

I'm explicitly forcing GL 4.4 or GL4.4 'core' profile so I'm a bit puzzled what may be the problem with the required buffer that is missing. Could it be that it is mistakingly seeing the imgCounter as part of the MRT setup for the framebuffer?

1 Answers

Answers 1

That texture is incomplete.

See, when you bind a texture for use with image load/store operations, you don't bind a sampler along with it. So all of those glSamplerParameter calls are meaningless to the texture's completeness status.

The texture is incomplete because the filtering parameters are GL_LINEAR, but the texture is an unsigned integer format. When creating integer format textures, you should always set the texture's parameters to valid values.

Read More

Unable to select Bootstrap dropdown in Java Selenium

Leave a Comment

I'm having a very difficult time selecting bootstrap dropdown in Selenium.

I'm new to Selenium so any suggestion and guidance would be very helpful, as what I'd love to do is just select the dropdown, type "email", and press enter during the instance.

I've gone through dozens of solutions, but none of them have worked for this specific issue.

What can I do? Please help.

Salenium

package newPackage;      import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.WebElement;  import org.openqa.selenium.chrome.ChromeDriver;  import org.openqa.selenium.interactions.Actions;      public class importLeads {    	public static void main(String args[]) throws Exception {  		  		        System.setProperty("webdriver.chrome.driver", "C:\\Users\\David\\Downloads\\Notes\\WebDriver\\chromedriver.exe");      WebDriver driver = new ChromeDriver();        //login to site      driver.get("https://demos5.softaculous.com/Mautic/s/contacts/import/new");      driver.manage().window().maximize();      driver.findElement(By.id("username")).sendKeys("admin");      driver.findElement(By.id("password")).sendKeys("password");      driver.findElement(By.className("btn")).click();      Thread.sleep(2000);        //importing Data      WebElement uploadBox = driver.findElement(By.id("lead_import_file"));      uploadBox.sendKeys("C:\\Users\\David\\Downloads\\data_file.csv");      driver.findElement(By.id("lead_import_start")).click();        //Select from dropdown      WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_address')"));      Actions cursor = new Actions(driver);      cursor.moveToElement(dropdownToggle);      cursor.click();      cursor.perform();      Thread.sleep(1000);      WebElement weh = driver.findElement(By.id("lead_field_import_email_address_chosen"));        Actions cursor2 = new Actions(driver);      cursor2.moveToElement(weh);      cursor2.click();    	}  }


Here is the link to the data file ufile.io/vy2ws

In order for the upload of the .CSV file to work, you must be running a local version of this software. Download can be found here: https://www.mautic.org/m/asset/54:mautic-2100

Once uploaded, it should look like this where it asks to choose from the drop-down.

enter image description here

HTML

<div class="col-sm-4">     <div class="row">        <div class="form-group col-xs-12 ">           <label class="control-label" for="lead_field_import_email_address">Email address</label>                   <div class="choice-wrapper">              <select id="lead_field_import_email_address" name="lead_field_import[email_address]" class="form-control" autocomplete="false" style="display: none;">                 <option value=""></option>                 <optgroup label="Contact">                    <option value="email">Email</option>                 </optgroup>              </select>              <div class="chosen-container chosen-container-single chosen-with-drop chosen-container-active" style="width: 100%;" title="" id="lead_field_import_email_address_chosen">                 <a class="chosen-single chosen-default">                    <span>Choose one...</span>                    <div><b></b></div>                 </a>                 <div class="chosen-drop">                    <div class="chosen-search"><input type="text" autocomplete="off"></div>                    <ul class="chosen-results">                       <li class="group-result">Contact</li>                       <li class="active-result group-option" data-option-array-index="9" style="">Email</li>                    </ul>                 </div>              </div>           </div>        </div>     </div>  </div>


Replacing :

WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_add‌​ress')"));  

to this :

WebElement dropdownToggle = driver.findElement(By.id("lead_field_import_email_address"))‌​; 

resulted in:

Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 35535 Only local connections are allowed. Oct 22, 2017 1:20:49 AM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"lead_field_import_email_address"}   (Session info: chrome=61.0.3163.100)   (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.14393 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z' System info: host: 'DAVID-PC', ip: '192.235.0.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\David\AppData\Local\Temp\scoped_dir4068_28713}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}] Session ID: ee90469095e7b1121dc2e387d8e485e6 *** Element info: {Using=id, value=lead_field_import_email_address}     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)     at java.lang.reflect.Constructor.newInstance(Unknown Source)     at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)     at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)     at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)     at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)     at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)     at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)     at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)     at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:402)     at org.openqa.selenium.By$ById.findElement(By.java:218)     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)     at newPackage.importLeads.main(importLeads.java:31) 

Replacing

WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_address')"));     Actions cursor = new Actions(driver); 

with

WebElement element =  driver.findElement(By.id("lead_field_import_email_address"));     Select select = new Select(element); 

resulted in:

Starting ChromeDriver 2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f) on port 32443 Only local connections are allowed. Oct 23, 2017 1:36:09 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: OSS Exception in thread "main" org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"lead_field_import_email_address"}   (Session info: chrome=61.0.3163.100)   (Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.15063 x86_64) (WARNING: The server did not provide any stacktrace information) Command duration or timeout: 0 milliseconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:26.402Z' System info: host: 'HOME-PC', ip: '192.235.0.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_151' Driver info: org.openqa.selenium.chrome.ChromeDriver Capabilities [{mobileEmulationEnabled=false, hasTouchScreen=false, platform=XP, acceptSslCerts=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, setWindowRect=true, unexpectedAlertBehaviour=, applicationCacheEnabled=false, rotatable=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f), userDataDir=C:\Users\David\AppData\Local\Temp\1\scoped_dir5416_25737}, takesHeapSnapshot=true, pageLoadStrategy=normal, unhandledPromptBehavior=, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, browserConnectionEnabled=false, nativeEvents=true, locationContextEnabled=true, cssSelectorsEnabled=true}] Session ID: 40cde314a5a76400aceff8b625b38e3c *** Element info: {Using=id, value=lead_field_import_email_address}     at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)     at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)     at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)     at java.lang.reflect.Constructor.newInstance(Unknown Source)     at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:214)     at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)     at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)     at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:82)     at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:45)     at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)     at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)     at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:586)     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:356)     at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:402)     at org.openqa.selenium.By$ById.findElement(By.java:218)     at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)     at newAutomation.importLeads.main(importLeads.java:33) 

2 Answers

Answers 1

I tried to replicate manually the steps of your code but when I load the data_file.csv and I click the Upload button:

enter image description here

nothing happens.

And, in the html, there isn't the element that you try to find:

WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_address')")); 

That's why I wrote this comment.

I can imagine that should open something. From the informations:

  • Limit
  • Delimiter
  • Enclosure
  • Escape

I can suppose that you have to upload a file with a particular format.

EDIT

Trying locally it works. I don't understand well this part of your code:

        //Select from dropdown         WebElement dropdownToggle = driver.findElement(By.xpath("id('lead_field_import_email_address')"));         Actions cursor = new Actions(driver);         cursor.moveToElement(dropdownToggle);         cursor.click();         cursor.perform();         Thread.sleep(1000);         WebElement weh = driver.findElement(By.id("lead_field_import_email_address_chosen"));          Actions cursor2 = new Actions(driver);         cursor2.moveToElement(weh);         cursor2.click(); 

If you want to choose from the email drop-down, you could use the xpath:

WebElement we = driver.findElement(By.xpath("//div[@class='choice-wrapper']//div[@id='lead_field_import_email_address_chosen']")); 

or simply the id:

WebElement we= driver.findElement(By.id("lead_field_import_email_address_chosen")); 

and execute the interested operation.

So, for example:

    WebElement we= driver.findElement(By.id("lead_field_import_email_address_chosen"));     we.click();     we.sendKeys("email");     we.sendKeys(Keys.ENTER); 

EDIT 2

Your problem is that you use Thread.sleep(XXXX); in order to wait the elements. This is not deterministic. You must use an explicit wait.

From Explicit Waits:

An explicit wait is a code you define to wait for a certain condition to occur before proceeding further in the code. The extreme case of this is time.sleep(), which sets the condition to an exact time period to wait. There are some convenience methods provided that help you write code that will wait only as long as required. WebDriverWait in combination with ExpectedCondition is one way this can be accomplished.

So, in java you need:

WebDriverWait wait = new WebDriverWait(driver, 15); WebElement we=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_field_import_email_address_chosen"))); 

Always from the previous link:

This waits up to 15 seconds before throwing a TimeoutException unless it finds the element to return within 15 seconds. WebDriverWait by default calls the ExpectedCondition every 500 milliseconds until it returns successfully. A successful return is for ExpectedCondition type is Boolean return true or not null return value for all other ExpectedCondition types.

Finally, the entire code:

        String geckoDriver = System.getProperty("pathTo/geckodriver";         System.setProperty("webdriver.gecko.driver", geckoDriver);         WebDriver driver= new FirefoxDriver();         driver.get("http://localhost:8888/2.10.0/s/contacts/import/new");         driver.findElement(By.id("username")).sendKeys("admin");         driver.findElement(By.id("password")).sendKeys("password");         driver.findElement(By.className("btn")).click();          WebDriverWait wait = new WebDriverWait(driver, 15);          WebElement uploadBox=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_import_file")));         uploadBox.sendKeys("/pathTo/data_file.csv");         driver.findElement(By.id("lead_import_start")).click();          //Select from dropdown         WebElement we=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_field_import_email_address_chosen")));         we.click();         we.sendKeys("email");         we.sendKeys(Keys.ENTER); 

EDIT 3

With Firefox, the above code (in my tests) is ok. I noticed that you use Chrome. With Chrome, I have this problem. The solution works for me:

        //Select from dropdown         WebElement we=wait.until(ExpectedConditions.elementToBeClickable(By.id("lead_field_import_email_address_chosen")));         /*we.click();         we.sendKeys("email");*/          Actions actions = new Actions(driver);         actions.moveToElement(we);         actions.click();         actions.sendKeys("email");         actions.sendKeys(Keys.ENTER);         actions.build().perform(); 

Answers 2

Looking at the HTML you have provided, the WebElement with id="lead_field_import_email_address" is with in a Select tag. So instead of using Actions Class in this case we should try to use the Select Class instead as follows:

WebElement element =  driver.findElement(By.id("lead_field_import_email_address")); Select select = new Select(element); 

Next, we can select any of the options by invoking either selectByIndex(n), selectByValue("value") or selectByVisibleText("visible_text") method.

Read More

Running TensorFlow on multicore devices

Leave a Comment

I have a basic Android TensorFlowInference example that runs fine in a single thread.

public class InferenceExample {      private static final String MODEL_FILE = "file:///android_asset/model.pb";     private static final String INPUT_NODE = "intput_node0";     private static final String OUTPUT_NODE = "output_node0";      private static final int[] INPUT_SIZE = {1, 8000, 1};     public static final int CHUNK_SIZE = 8000;     public static final int STRIDE = 4;     private static final int NUM_OUTPUT_STATES = 5;      private static TensorFlowInferenceInterface inferenceInterface;      public InferenceExample(final Context context) {         inferenceInterface = new TensorFlowInferenceInterface(context.getAssets(), MODEL_FILE);     }      public float[] run(float[] data) {          float[] res = new float[CHUNK_SIZE / STRIDE * NUM_OUTPUT_STATES];          inferenceInterface.feed(INPUT_NODE, data, INPUT_SIZE[0], INPUT_SIZE[1], INPUT_SIZE[2]);         inferenceInterface.run(new String[]{OUTPUT_NODE});         inferenceInterface.fetch(OUTPUT_NODE, res);          return res;     } } 

The example crashes with various exceptions including java.lang.ArrayIndexOutOfBoundsException and java.lang.NullPointerException when running in a ThreadPool as per the below example so I guess it's not thread safe.

InferenceExample inference = new InferenceExample(context);  ExecutorService executor = Executors.newFixedThreadPool(NUMBER_OF_CORES);     Collection<Future<?>> futures = new LinkedList<Future<?>>();  for (int i = 1; i <= 100; i++) {     Future<?> result = executor.submit(new Runnable() {         public void run() {            inference.call(randomData);         }     });     futures.add(result); }  for (Future<?> future:futures) {     try { future.get(); }     catch(ExecutionException | InterruptedException e) {         Log.e("TF", e.getMessage());     } } 

Is it possible to leverage multicore Android devices with TensorFlowInferenceInterface?

2 Answers

Answers 1

To make the InferenceExample thread safe I changed the TensorFlowInferenceInterface from static and made the run method synchronized:

private TensorFlowInferenceInterface inferenceInterface;  public InferenceExample(final Context context) {     inferenceInterface = new TensorFlowInferenceInterface(assets, model); }  public synchronized float[] run(float[] data) { ... } 

Then I round robin a list of InterferenceExample instance across numThreads.

for (int i = 1; i <= 100; i++) {     final int id = i % numThreads;     Future<?> result = executor.submit(new Runnable() {         public void run() {             list.get(id).run(data);         }     });     futures.add(result); } 

This does increase performance however on a 8 core device this peaks at numThreads of 2 and only shows ~50% CPU usage in Android Studio Monitor.

Answers 2

The TensorFlowInferenceInterface class is not thread-safe (as it keeps state between calls to feed, run, fetch etc.

However, it is built on top of the TensorFlow Java API where objects of the Session class are thread-safe.

So you might want to use the underlying Java API directly, TensorFlowInferenceInterface's constructor creates a Session and sets it up with a Graph loaded from the AssetManager (code).

Hope that helps.

Read More