LoginSignup
2
2

More than 5 years have passed since last update.

Processingでproxyサーバを介してwebのリソースを利用する

Last updated at Posted at 2013-04-16

手軽にビジュアルプログラミングを体験できるProcessingではスケッチ上にwebのリソースを利用することもできる。

例えば日本語版WikipediaのProcessingのページに掲載されている日本地図を塗り分けるサンプルコードでは

PShape japan;
int square_len = 512;
void setup() {
  japan = loadShape("http://upload.wikimedia.org/wikipedia/commons/5/56/Blank_map_of_Japan.svg");
  size(square_len,square_len);
  smooth();
  noLoop();
}

と定義することで、指定したURLのリソースをスケッチ内に利用できる。

ところがproxyサーバを介して外部ネットワーク通信が行われる環境下にてこのサンプルコードを実行すると、

java.net.UnknownHostException: upload.wikimedia.org
...

このエラーがコンソールに表示されスケッチの実行ができない。

これは通信にproxyサーバを介している事によりDNSの名前解決ができないために起こっており、エラーを解消するにはproxyサーバの場所をスケッチに定義する必要がある。

対処その1:スケッチのsetupメソッド内でSystemのプロパティ設定を行う

スケッチ実行時、一度だけ実行されるsetupメソッドにて、Systemプロパティにproxyサーバの場所を定義する。

import java.util.Properties;
PShape japan;
int square_len = 512;
void setup() {
  Properties systemSettings = System.getProperties();
  systemSettings.put("http.proxyHost", "my.proxy.example.com");
  systemSettings.put("http.proxyPort", "8080");
  System.setProperties(systemSettings); 

  japan = loadShape("http://upload.wikimedia.org/wikipedia/commons/5/56/Blank_map_of_Japan.svg");
  size(square_len,square_len);
  smooth();
  noLoop();
}

参考: http://www.processing.org/discourse/beta/num_1274721911.html

対処その2:preferences.txt にproxyサーバの場所を設定する。

その1の方法ではスケッチ毎のsetupメソッドに設定を行う必要がある。常にproxyサーバ設定を有効にするためにProcessingの環境設定ファイル:preferences.txtに設定を行う事もできる。

  • ProcessingのIDEより[File]-[Preferences]と選択しPreferencesダイアログを表示
  • Preferencesダイアログ下部の "More preferences can be edited directly in the file..." の行で示されたパス位置の preferences.txt を開く。
  • 一旦Processingを終了する
  • preferences.txt を開き、プロパティ"run.options"の値を編集する。
run.options=-DproxyHost=my.proxy.example.com -DproxyPort=8080
  • preferences.txtを保存して、Processingを再起動する

参考:http://mayoi.net/archives/2005/05/10-1116_539.php

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2