Guest User

Untitled

a guest
Mar 4th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.58 KB | None | 0 0
  1. package Main;
  2.  
  3. import com.jcraft.jsch.*;
  4. import java.awt.*;
  5. import javax.swing.*;
  6. import java.io.*;
  7. import java.nio.channels.FileChannel;
  8. import java.nio.file.Files;
  9. import java.nio.file.Paths;
  10. import java.nio.file.StandardOpenOption;
  11. import java.util.HashMap;
  12.  
  13. public class Start {
  14.  
  15. public static void main(String[] arg) {
  16. /*
  17. * 1-read configuration file and fill hashMap with values
  18. * 2-using hashmap start server thread on local machine by change directory to server folder on desktop, compiling and running using make file
  19. * send to server number of readers and number of writers
  20. * 3-use ssh to log in other hosts as specified in config file, change directory to client folder on desktop, compile and run using make file
  21. */
  22.  
  23. Process p;
  24. String command, s, args;
  25. int rdCount, wrCount;
  26.  
  27. //read configuration
  28. ConfigFileHandler fh = new ConfigFileHandler();
  29. HashMap<String, String> props = fh.readConfiguration();
  30. printConfigFile(props);
  31.  
  32. //run server
  33. //cd to server folder on desktop
  34. makeScript(true); //true server
  35. //append server args
  36. try {
  37. args = "";
  38. args += " ";
  39. args += props.get("srvPort");
  40. args += " ";
  41. args += props.get("rdCount");
  42. args += " ";
  43. args += props.get("wrCount");
  44.  
  45. Files.write(Paths.get("srv_script"), args.getBytes(), StandardOpenOption.APPEND);
  46. }catch (IOException e) {
  47. //exception handling left as an exercise for the reader
  48. }
  49.  
  50.  
  51. ServerThread srvTh = new ServerThread();
  52. Thread myThread = new Thread(srvTh);
  53. myThread.start();
  54. // try {
  55. // Runtime.getRuntime().exec("chmod 755 srv_script");
  56. // p = Runtime.getRuntime().exec("./srv_script");
  57. // BufferedReader stdInput = new BufferedReader(new
  58. // InputStreamReader(p.getInputStream()));
  59. //
  60. // BufferedReader stdError = new BufferedReader(new
  61. // InputStreamReader(p.getErrorStream()));
  62. //
  63. // // read the output from the command
  64. // System.out.println("Here is the standard output of the command:\n");
  65. // while ((s = stdInput.readLine()) != null) {
  66. // System.out.println(s);
  67. // }
  68. //
  69. // // read any errors from the attempted command
  70. // System.out.println("Here is the standard error of the command (if any):\n");
  71. // while ((s = stdError.readLine()) != null) {
  72. // System.out.println(s);
  73. // }
  74. // } catch (IOException e) {
  75. // e.printStackTrace();
  76. // }
  77. System.out.println("Samor");
  78. //execute clients
  79. rdCount = Integer.parseInt(props.get("rdCount"));
  80. wrCount = Integer.parseInt(props.get("wrCount"));
  81.  
  82. for(int i=0;i<rdCount;i++) {
  83. String temp, user, host, password;
  84. temp = props.get("rd"+i);
  85. user = temp.substring(0,temp.indexOf('@'));
  86. host = temp.substring(temp.indexOf('@') + 1);
  87. password = props.get("rdPass" + i);
  88. makeScript(false);
  89. //append args
  90. try {
  91. args = "";
  92. args += " ";
  93. args += props.get("srvIp");
  94. args += " ";
  95. args += props.get("srvPort");
  96. args += " ";
  97. args += i; //TODO: fix id
  98. args += " ";
  99. args += props.get("acCount");
  100. args += " ";
  101. args += "r";
  102.  
  103. Files.write(Paths.get("clt_script"), args.getBytes(), StandardOpenOption.APPEND);
  104. } catch (IOException e) {
  105. //exception handling left as an exercise for the reader
  106. }
  107.  
  108. String commands[] = {"cd $HOME/Desktop/client;pwd; javac *.java;java MyClient"};
  109. args = commands[0];
  110. args += " ";
  111. args += props.get("srvIp");
  112. args += " ";
  113. args += props.get("srvPort");
  114. args += " ";
  115. args += i; //TODO: fix id
  116. args += " ";
  117. args += props.get("acCount");
  118. args += " ";
  119. args += "r";
  120. commands[0] = args;
  121. /*
  122. * transfer client script file to remote in client folder
  123. * cd to client folder
  124. * execute script file
  125. */
  126. createClient(user, host, password, commands);
  127. }
  128. /*
  129. for(int i=0;i<wrCount;i++) {
  130. String temp, user, host, password;
  131. int id = i + rdCount;
  132. temp = props.get("wr"+i);
  133. user = temp.substring(0,temp.indexOf('@'));
  134. host = temp.substring(temp.indexOf('@') + 1);
  135. password = props.get("wrPass" + i);
  136. makeScript(false);
  137. //append args
  138. try {
  139. args = "";
  140. args += " ";
  141. args += props.get("srvIp");
  142. args += " ";
  143. args += props.get("srvPort");
  144. args += " ";
  145. args += i; //TODO: fix id
  146. args += " ";
  147. args += props.get("acCount");
  148. args += " ";
  149. args += "w";
  150.  
  151. Files.write(Paths.get("clt_script"), args.getBytes(), StandardOpenOption.APPEND);
  152. } catch (IOException e) {
  153. //exception handling left as an exercise for the reader
  154. }
  155.  
  156. String commands[] = {"echo magdy", "cd $HOME/workspace/dist1", "pwd", "chmod 755 clt_script", "./clt_script"};
  157. createClient(user, host, password, commands);
  158. }
  159. */
  160.  
  161.  
  162. }
  163.  
  164. private static void createClient(String user, String host, String password, String commands[]) {
  165. Session session = null;
  166. try {
  167. JSch jsch = new JSch();
  168. session = jsch.getSession(user, host, 22);
  169. session.setPassword(password);
  170. java.util.Properties config = new java.util.Properties();
  171. config.put("StrictHostKeyChecking", "no");
  172. session.setConfig(config);
  173. session.connect();
  174. for(int it=0;it<commands.length;it++){
  175. Channel channel = session.openChannel("exec");
  176. ((ChannelExec) channel).setCommand(commands[it]);
  177.  
  178. channel.setInputStream(null);
  179.  
  180. ((ChannelExec) channel).setErrStream(System.err);
  181.  
  182. InputStream in = channel.getInputStream();
  183.  
  184. channel.connect();
  185.  
  186. byte[] tmp = new byte[1024];
  187. while (true) {
  188. while (in.available() > 0) {
  189. int i = in.read(tmp, 0, 1024);
  190. if (i < 0)
  191. break;
  192. System.out.print(new String(tmp, 0, i));
  193. }
  194. if (channel.isClosed()) {
  195. if (in.available() > 0)
  196. continue;
  197. System.out.println("exit-status: "
  198. + channel.getExitStatus());
  199. break;
  200. }
  201. try {
  202. Thread.sleep(1000);
  203. } catch (Exception ee) {
  204. }
  205. }
  206. channel.disconnect();
  207. }
  208. session.disconnect();
  209.  
  210. } catch (Exception e) {
  211.  
  212. }
  213.  
  214. }
  215.  
  216. private static void makeScript(boolean type){
  217. String file1,file2;
  218. if(type) {
  219. file1="srv_temp";
  220. file2="srv_script";
  221. } else {
  222. file1="clt_temp";
  223. file2="clt_script";
  224. }
  225. try{
  226. FileChannel src = new FileInputStream(file1).getChannel();
  227. FileChannel dest = new FileOutputStream(file2).getChannel();
  228. dest.transferFrom(src, 0, src.size());
  229. src.close();
  230. dest.close();
  231. } catch(Exception e) {
  232. e.printStackTrace();
  233. }
  234.  
  235. }
  236.  
  237. private static void printConfigFile(HashMap<String, String> props) {
  238. for(String key: props.keySet()) {
  239. System.out.println(key + " " + props.get(key));
  240. }
  241. }
  242.  
  243. static class ServerThread implements Runnable {
  244. public void run() {
  245. String s;
  246. Process p;
  247. try {
  248. Runtime.getRuntime().exec("chmod 755 srv_script");
  249. p = Runtime.getRuntime().exec("./srv_script");
  250. BufferedReader stdInput = new BufferedReader(new
  251. InputStreamReader(p.getInputStream()));
  252.  
  253. BufferedReader stdError = new BufferedReader(new
  254. InputStreamReader(p.getErrorStream()));
  255.  
  256. // read the output from the command
  257. System.out.println("Here is the standard output of the command:\n");
  258. while ((s = stdInput.readLine()) != null) {
  259. System.out.println(s);
  260. }
  261.  
  262. // read any errors from the attempted command
  263. System.out.println("Here is the standard error of the command (if any):\n");
  264. while ((s = stdError.readLine()) != null) {
  265. System.out.println(s);
  266. }
  267. } catch (IOException e) {
  268. e.printStackTrace();
  269. }
  270. }
  271. }
  272.  
  273.  
  274. private static class ConfigFileHandler {
  275. private static final String CONFIG_FILE = "system.properities";
  276.  
  277. /**
  278. * read configuration file system.properities and sets global variables
  279. */
  280. public HashMap<String, String> readConfiguration() {
  281. HashMap<String, String> props = new HashMap<String, String>();
  282. BufferedReader bf = null;
  283.  
  284. try {
  285. bf = new BufferedReader(new FileReader(CONFIG_FILE));
  286. String sCurrentLine;
  287.  
  288. while ((sCurrentLine = bf.readLine()) != null) {
  289. System.out.println(sCurrentLine);
  290. int splitIndex = sCurrentLine.indexOf('=');
  291. String key = sCurrentLine.substring(0, splitIndex);
  292. String value = sCurrentLine.substring(splitIndex+1);
  293. key = reformulateKeyString(key);
  294. props.put(key, value);
  295. }
  296. bf.close();
  297. } catch (Exception e) {
  298. e.printStackTrace();
  299. }
  300. return props;
  301. }
  302.  
  303. private String reformulateKeyString(String key) {
  304. char num;
  305. String res;
  306. boolean hasNum;
  307. HashMap<String, String> mappingKeys;
  308.  
  309. mappingKeys = getMappingKeys();
  310. hasNum = checkStrHasNo(key);
  311.  
  312. if (hasNum) {
  313. num = key.charAt(key.length() - 1);
  314. res = mappingKeys.get(key.substring(0, key.length() - 1));
  315. res += num;
  316. } else {
  317. res = mappingKeys.get(key);
  318. }
  319. return res;
  320. }
  321.  
  322. private HashMap<String, String> getMappingKeys() {
  323. HashMap<String, String> mappingKeys = new HashMap<String, String>();
  324. mappingKeys.put("RW.server", "srvIp");
  325. mappingKeys.put("RW.server.port", "srvPort");
  326. mappingKeys.put("RW.numberOfReaders", "rdCount");
  327. mappingKeys.put("RW.numberOfWriters", "wrCount");
  328. mappingKeys.put("RW.numberOfAccesses", "acCount");
  329. mappingKeys.put("RW.reader", "rd");
  330. mappingKeys.put("RW.writer", "wr");
  331. mappingKeys.put("RW.password.reader", "rdPass");
  332. mappingKeys.put("RW.password.writer", "wrPass");
  333. return mappingKeys;
  334. }
  335.  
  336. private boolean checkStrHasNo(String txt) {
  337. return txt.matches(".*\\d+.*");
  338. }
  339. }
  340.  
  341.  
  342. }
Add Comment
Please, Sign In to add comment