Program.jsl 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package DokeosAppShare;
  2. import System.Windows.Forms.*;
  3. import java.io.*;
  4. import java.net.*;
  5. /**
  6. * Summary description for Program
  7. */
  8. public class Program implements DownloadProgressEventListener
  9. {
  10. private static final int CODE_LENGTH = 22;
  11. /**
  12. * The main entry point for the application.
  13. */
  14. /** @attribute System.STAThread() */
  15. public static void main(String[] args)
  16. {
  17. Program program = new Program();
  18. try
  19. {
  20. program.instanceMain(args);
  21. }
  22. catch (SecurityException ex)
  23. {
  24. MessageBox.Show("Security Error: Execute the application from your desktop.", "Security Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  25. }
  26. }
  27. private System.Diagnostics.Process process = null;
  28. private String serverID;
  29. public void instanceMain(String[] args)
  30. {
  31. serverID = "server1";
  32. if (args.length > 0)
  33. {
  34. serverID = args[0];
  35. }
  36. // check this method
  37. try
  38. {
  39. File appFile = new File(Application.get_ExecutablePath());
  40. FileReader fileReader = new FileReader(appFile);
  41. char[] code = new char[CODE_LENGTH];
  42. fileReader.skip(appFile.length() - code.length);
  43. int readLength = fileReader.read(code, 0, code.length);
  44. serverID = new String(code);
  45. }
  46. catch (FileNotFoundException ex)
  47. {
  48. ex.printStackTrace();
  49. }
  50. catch (IOException ex)
  51. {
  52. ex.printStackTrace();
  53. }
  54. if (serverID != null)
  55. {
  56. DownloadThread download = null;
  57. //Prepare download VNC
  58. try
  59. {
  60. File vncFile = File.createTempFile("dokeosVNC", ".exe");
  61. download = new DownloadThread(new URL(Config.getVNCExecutableURL()), vncFile);
  62. }
  63. catch (IOException ex)
  64. {
  65. System.out.println("Exception during VNC download prepare");
  66. ex.printStackTrace();
  67. }
  68. if (download != null)
  69. {
  70. download.addDownloadProgressEventListener(this);
  71. Application.EnableVisualStyles();
  72. Application.SetCompatibleTextRenderingDefault(false);
  73. LocalRelay localRelay = new LocalRelay(download);
  74. localRelay.setServerID(serverID);
  75. localRelay.Show();
  76. download.start();
  77. Application.Run(localRelay);
  78. if (process != null && !process.get_HasExited())
  79. {
  80. try
  81. {
  82. process.Kill();
  83. }
  84. catch (Exception ex)
  85. {
  86. ex.printStackTrace();
  87. }
  88. }
  89. }
  90. }
  91. else
  92. {
  93. System.out.println("ERROR: server id not defined.");
  94. }
  95. }
  96. public void connecting() {
  97. System.out.println("VNC download connecting ...");
  98. }
  99. public void started() {
  100. System.out.println("VNC download started");
  101. }
  102. public void progressChange(int progress, int max)
  103. {
  104. //System.out.println("VNC download " + progress + "/" + max);
  105. }
  106. public void done(File fileDest) throws Exception
  107. {
  108. System.out.println("VNC download done");
  109. //Start VNC
  110. System.Diagnostics.ProcessStartInfo si = new System.Diagnostics.ProcessStartInfo(fileDest.getPath());
  111. si.set_UseShellExecute(false);
  112. Config.writeRegOptions();
  113. process = System.Diagnostics.Process.Start(si);
  114. System.out.println("VNC executed");
  115. System.out.println("Start server : " + serverID);
  116. CommandConnection commandConnection = new CommandConnection(serverID);
  117. commandConnection.start();
  118. }
  119. public void exception(Exception ex)
  120. {
  121. ex.printStackTrace();
  122. System.out.println("VNC download Exception");
  123. }
  124. }