PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/azureus-4.7.0.2/org/gudy/azureus2/ui/swt/nat/NatTestWindow.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 413 lines | 317 code | 71 blank | 25 comment | 50 complexity | 946089cfe7a2670a3e172890ea1e6785 MD5 | raw file
  1. /*
  2. * Created on 14 avr. 2005
  3. * Created by Olivier Chalouhi
  4. *
  5. * Copyright (C) 2004, 2005, 2006 Aelitis SAS, All rights Reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details ( see the LICENSE file ).
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. * AELITIS, SAS au capital de 46,603.30 euros,
  21. * 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
  22. */
  23. package org.gudy.azureus2.ui.swt.nat;
  24. import java.net.InetAddress;
  25. import org.eclipse.swt.SWT;
  26. import org.eclipse.swt.custom.StyledText;
  27. import org.eclipse.swt.layout.GridData;
  28. import org.eclipse.swt.layout.GridLayout;
  29. import org.eclipse.swt.widgets.*;
  30. import org.gudy.azureus2.core3.config.COConfigurationManager;
  31. import org.gudy.azureus2.core3.internat.MessageText;
  32. import org.gudy.azureus2.core3.ipchecker.natchecker.NatChecker;
  33. import org.gudy.azureus2.core3.util.AERunnable;
  34. import org.gudy.azureus2.core3.util.AEThread;
  35. import org.gudy.azureus2.core3.util.Debug;
  36. import org.gudy.azureus2.ui.swt.Messages;
  37. import org.gudy.azureus2.ui.swt.Utils;
  38. import org.gudy.azureus2.ui.swt.components.shell.ShellFactory;
  39. import org.gudy.azureus2.ui.swt.shells.CoreWaiterSWT;
  40. import org.gudy.azureus2.ui.swt.shells.CoreWaiterSWT.TriggerInThread;
  41. import com.aelitis.azureus.core.AzureusCore;
  42. import com.aelitis.azureus.core.AzureusCoreRunningListener;
  43. import com.aelitis.azureus.core.AzureusCoreFactory;
  44. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdmin;
  45. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdminProgressListener;
  46. import com.aelitis.azureus.core.networkmanager.admin.NetworkAdminProtocol;
  47. public class NatTestWindow {
  48. Display display;
  49. Button bTestTCP,bTestUDP,bApply,bCancel;
  50. StyledText textResults;
  51. int serverTCPListenPort;
  52. int serverUDPListenPort;
  53. public class CheckerTCP extends AEThread {
  54. private int TCPListenPort;
  55. public CheckerTCP(int tcp_listen_port) {
  56. super("NAT Checker TCP");
  57. this.TCPListenPort = tcp_listen_port;
  58. }
  59. public void
  60. runSupport()
  61. {
  62. try{
  63. printMessage(MessageText.getString("configureWizard.nat.testing") + " TCP " + TCPListenPort + " ... ");
  64. NatChecker checker = new NatChecker(AzureusCoreFactory.getSingleton(), NetworkAdmin.getSingleton().getMultiHomedServiceBindAddresses(true)[0], TCPListenPort, false);
  65. switch (checker.getResult()) {
  66. case NatChecker.NAT_OK :
  67. printMessage( "\n" + MessageText.getString("configureWizard.nat.ok") + "\n" + checker.getAdditionalInfo());
  68. break;
  69. case NatChecker.NAT_KO :
  70. printMessage( "\n" + MessageText.getString("configureWizard.nat.ko") + " - " + checker.getAdditionalInfo()+".\n");
  71. break;
  72. default :
  73. printMessage( "\n" + MessageText.getString("configureWizard.nat.unable") + ". \n(" + checker.getAdditionalInfo()+").\n");
  74. break;
  75. }
  76. }finally{
  77. if (display.isDisposed()) {return;}
  78. display.asyncExec(new AERunnable() {
  79. public void runSupport() {
  80. if(bTestTCP != null && ! bTestTCP.isDisposed())
  81. bTestTCP.setEnabled(true);
  82. if(bTestUDP != null && ! bTestUDP.isDisposed())
  83. bTestUDP.setEnabled(true);
  84. if(bApply != null && ! bApply.isDisposed())
  85. bApply.setEnabled(true);
  86. }
  87. });
  88. }
  89. }
  90. }
  91. public class CheckerUDP extends AEThread {
  92. private AzureusCore core;
  93. private int udp_port;
  94. public CheckerUDP(AzureusCore _core, int _udp_port ){
  95. super("NAT Checker UDP");
  96. core = _core;
  97. udp_port = _udp_port;
  98. }
  99. public void
  100. runSupport()
  101. {
  102. try{
  103. final NetworkAdmin admin = NetworkAdmin.getSingleton();
  104. NetworkAdminProtocol[] inbound_protocols = admin.getInboundProtocols(core);
  105. NetworkAdminProtocol selected = null;
  106. for ( NetworkAdminProtocol p: inbound_protocols ){
  107. if ( p.getType() == NetworkAdminProtocol.PT_UDP && p.getPort() == udp_port ){
  108. selected = p;
  109. break;
  110. }
  111. }
  112. if ( selected == null ){
  113. selected = admin.createInboundProtocol( core, NetworkAdminProtocol.PT_UDP, udp_port );
  114. }
  115. if ( selected == null ){
  116. printMessage( "\n" + MessageText.getString("configureWizard.nat.ko") + ". \n( No UDP protocols enabled ).\n");
  117. }else{
  118. printMessage(MessageText.getString("configureWizard.nat.testing") + " UDP " + udp_port + " ... ");
  119. try{
  120. selected.test(
  121. null,
  122. true,
  123. new NetworkAdminProgressListener()
  124. {
  125. public void
  126. reportProgress(
  127. String task )
  128. {
  129. printMessage( "\n " + task );
  130. }
  131. });
  132. printMessage( "\n" + MessageText.getString("configureWizard.nat.ok"));
  133. }catch( Throwable e ){
  134. printMessage( "\n" + MessageText.getString("configureWizard.nat.ko") + ". " + Debug.getNestedExceptionMessage(e)+".\n");
  135. }
  136. }
  137. }finally{
  138. if (display.isDisposed()) {return;}
  139. display.asyncExec(new AERunnable() {
  140. public void runSupport() {
  141. if(bTestTCP != null && ! bTestTCP.isDisposed())
  142. bTestTCP.setEnabled(true);
  143. if(bTestUDP != null && ! bTestUDP.isDisposed())
  144. bTestUDP.setEnabled(true);
  145. if(bApply != null && ! bApply.isDisposed())
  146. bApply.setEnabled(true);
  147. }
  148. });
  149. }
  150. }
  151. }
  152. public NatTestWindow() {
  153. serverTCPListenPort = COConfigurationManager.getIntParameter( "TCP.Listen.Port" );
  154. serverUDPListenPort = COConfigurationManager.getIntParameter( "UDP.Listen.Port" );
  155. final Shell shell = ShellFactory.createMainShell(SWT.BORDER | SWT.TITLE | SWT.CLOSE);
  156. shell.setText(MessageText.getString("configureWizard.nat.title"));
  157. Utils.setShellIcon(shell);
  158. display = shell.getDisplay();
  159. GridLayout layout = new GridLayout();
  160. layout.numColumns = 1;
  161. shell.setLayout(layout);
  162. Composite panel = new Composite(shell, SWT.NULL);
  163. GridData gridData = new GridData(GridData.VERTICAL_ALIGN_CENTER | GridData.FILL_HORIZONTAL);
  164. panel.setLayoutData(gridData);
  165. layout = new GridLayout();
  166. layout.numColumns = 3;
  167. panel.setLayout(layout);
  168. Label label = new Label(panel, SWT.WRAP);
  169. gridData = new GridData();
  170. gridData.horizontalSpan = 3;
  171. gridData.widthHint = 400;
  172. label.setLayoutData(gridData);
  173. Messages.setLanguageText(label, "configureWizard.nat.message");
  174. label = new Label(panel, SWT.NULL);
  175. label = new Label(panel, SWT.NULL);
  176. label = new Label(panel, SWT.NULL);
  177. label = new Label(panel, SWT.NULL);
  178. // TCP
  179. Messages.setLanguageText(label, "configureWizard.nat.server.tcp_listen_port");
  180. final Text textServerTCPListen = new Text(panel, SWT.BORDER);
  181. gridData = new GridData();
  182. gridData.grabExcessHorizontalSpace = true;
  183. gridData.horizontalAlignment = SWT.FILL;
  184. textServerTCPListen.setLayoutData(gridData);
  185. textServerTCPListen.setText("" + serverTCPListenPort);
  186. textServerTCPListen.addListener(SWT.Verify, new Listener() {
  187. public void handleEvent(Event e) {
  188. String text = e.text;
  189. char[] chars = new char[text.length()];
  190. text.getChars(0, chars.length, chars, 0);
  191. for (int i = 0; i < chars.length; i++) {
  192. if (!('0' <= chars[i] && chars[i] <= '9')) {
  193. e.doit = false;
  194. return;
  195. }
  196. }
  197. }
  198. });
  199. textServerTCPListen.addListener(SWT.Modify, new Listener() {
  200. public void handleEvent(Event e) {
  201. final int TCPListenPort = Integer.parseInt(textServerTCPListen.getText());
  202. serverTCPListenPort = TCPListenPort;
  203. }
  204. });
  205. bTestTCP = new Button(panel, SWT.PUSH);
  206. Messages.setLanguageText(bTestTCP, "configureWizard.nat.test");
  207. gridData = new GridData();
  208. gridData.widthHint = 70;
  209. bTestTCP.setLayoutData(gridData);
  210. label = new Label(panel, SWT.NULL);
  211. // UDP
  212. Messages.setLanguageText(label, "configureWizard.nat.server.udp_listen_port");
  213. final Text textServerUDPListen = new Text(panel, SWT.BORDER);
  214. gridData = new GridData();
  215. gridData.grabExcessHorizontalSpace = true;
  216. gridData.horizontalAlignment = SWT.FILL;
  217. textServerUDPListen.setLayoutData(gridData);
  218. textServerUDPListen.setText("" + serverUDPListenPort);
  219. textServerUDPListen.addListener(SWT.Verify, new Listener() {
  220. public void handleEvent(Event e) {
  221. String text = e.text;
  222. char[] chars = new char[text.length()];
  223. text.getChars(0, chars.length, chars, 0);
  224. for (int i = 0; i < chars.length; i++) {
  225. if (!('0' <= chars[i] && chars[i] <= '9')) {
  226. e.doit = false;
  227. return;
  228. }
  229. }
  230. }
  231. });
  232. textServerUDPListen.addListener(SWT.Modify, new Listener() {
  233. public void handleEvent(Event e) {
  234. final int UDPListenPort = Integer.parseInt(textServerUDPListen.getText());
  235. serverUDPListenPort =UDPListenPort;
  236. }
  237. });
  238. bTestUDP = new Button(panel, SWT.PUSH);
  239. Messages.setLanguageText(bTestUDP, "configureWizard.nat.test");
  240. gridData = new GridData();
  241. gridData.widthHint = 70;
  242. bTestUDP.setLayoutData(gridData);
  243. // results
  244. textResults = new StyledText(panel, SWT.BORDER | SWT.MULTI | SWT.V_SCROLL | SWT.WRAP );
  245. gridData = new GridData();
  246. gridData.widthHint = 400;
  247. gridData.heightHint = 100;
  248. gridData.grabExcessVerticalSpace = true;
  249. gridData.verticalAlignment = SWT.FILL;
  250. gridData.horizontalSpan = 3;
  251. textResults.setLayoutData(gridData);
  252. textResults.setBackground(panel.getDisplay().getSystemColor(SWT.COLOR_WHITE));
  253. bTestTCP.addListener(SWT.Selection, new Listener() {
  254. public void handleEvent(Event event) {
  255. bTestUDP.setEnabled(false);
  256. bTestTCP.setEnabled(false);
  257. bApply.setEnabled(false);
  258. textResults.setText("");
  259. CoreWaiterSWT.waitForCore(TriggerInThread.ANY_THREAD,
  260. new AzureusCoreRunningListener() {
  261. public void azureusCoreRunning(AzureusCore core) {
  262. CheckerTCP checker = new CheckerTCP(serverTCPListenPort);
  263. checker.start();
  264. }
  265. });
  266. }
  267. });
  268. bTestUDP.addListener(SWT.Selection, new Listener() {
  269. public void handleEvent(Event event) {
  270. bTestUDP.setEnabled(false);
  271. bTestTCP.setEnabled(false);
  272. bApply.setEnabled(false);
  273. textResults.setText("");
  274. CoreWaiterSWT.waitForCore(TriggerInThread.ANY_THREAD,
  275. new AzureusCoreRunningListener() {
  276. public void azureusCoreRunning(AzureusCore core) {
  277. CheckerUDP checker = new CheckerUDP(core,serverUDPListenPort);
  278. checker.start();
  279. }
  280. });
  281. }
  282. });
  283. bApply = new Button(panel,SWT.PUSH);
  284. bApply.setText(MessageText.getString("Button.apply"));
  285. gridData = new GridData();
  286. gridData.widthHint = 70;
  287. gridData.grabExcessHorizontalSpace = true;
  288. gridData.horizontalAlignment = SWT.RIGHT;
  289. gridData.horizontalSpan = 2;
  290. bApply.setLayoutData(gridData);
  291. bApply.addListener(SWT.Selection, new Listener() {
  292. public void handleEvent(Event event) {
  293. int old_tcp = COConfigurationManager.getIntParameter( "TCP.Listen.Port" );
  294. int old_udp = COConfigurationManager.getIntParameter( "UDP.Listen.Port" );
  295. int old_udp2 = COConfigurationManager.getIntParameter( "UDP.NonData.Listen.Port" );
  296. if ( old_tcp != serverTCPListenPort ){
  297. COConfigurationManager.setParameter("TCP.Listen.Port",serverTCPListenPort);
  298. }
  299. if ( old_udp != serverUDPListenPort ){
  300. COConfigurationManager.setParameter("UDP.Listen.Port",serverUDPListenPort);
  301. if ( old_udp == old_udp2 ){
  302. COConfigurationManager.setParameter("UDP.NonData.Listen.Port",serverUDPListenPort);
  303. }
  304. }
  305. COConfigurationManager.save();
  306. shell.close();
  307. }
  308. });
  309. bCancel = new Button(panel,SWT.PUSH);
  310. bCancel.setText(MessageText.getString("Button.cancel"));
  311. gridData = new GridData();
  312. gridData.widthHint = 70;
  313. bCancel.setLayoutData(gridData);
  314. bCancel.addListener(SWT.Selection, new Listener() {
  315. public void handleEvent(Event event) {
  316. shell.close();
  317. }
  318. });
  319. shell.setDefaultButton( bApply );
  320. shell.addListener(SWT.Traverse, new Listener() {
  321. public void handleEvent(Event e) {
  322. if ( e.character == SWT.ESC){
  323. shell.close();
  324. }
  325. }
  326. });
  327. shell.pack();
  328. Utils.centreWindow(shell);
  329. shell.open();
  330. }
  331. public void printMessage(final String message) {
  332. if (display == null || display.isDisposed())
  333. return;
  334. display.asyncExec(new AERunnable() {
  335. public void runSupport() {
  336. if (textResults == null || textResults.isDisposed())
  337. return;
  338. textResults.append(message);
  339. }
  340. });
  341. }
  342. }