PageRenderTime 53ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/packages/demo/jpl/Server.java

https://github.com/kraman/RTZen
Java | 340 lines | 224 code | 54 blank | 62 comment | 21 complexity | ef4e13f9908244cc3b737fb2d353ad62 MD5 | raw file
  1. /*
  2. * Copyright (c) 2005 by the University of California, Irvine
  3. * All Rights Reserved.
  4. *
  5. * This software is released under the terms of the RTZen license, which
  6. * you should have received along with this software. If not, you may
  7. * obtain a copy here: http://zen.ece.uci.edu/rtzen/license.php
  8. */
  9. package demo.jpl;
  10. import java.io.BufferedWriter;
  11. import java.io.FileWriter;
  12. import javax.realtime.ImmortalMemory;
  13. import javax.realtime.LTMemory;
  14. import javax.realtime.HeapMemory;
  15. import javax.realtime.PriorityScheduler;
  16. import javax.realtime.RealtimeThread;
  17. import org.omg.CORBA.ORB;
  18. import org.omg.CORBA.Policy;
  19. import org.omg.PortableServer.POA;
  20. import org.omg.PortableServer.POAHelper;
  21. import org.omg.PortableServer.POAManager;
  22. import org.omg.RTCORBA.RTORB;
  23. import org.omg.RTCORBA.RTORBHelper;
  24. import org.omg.RTCORBA.ThreadpoolLane;
  25. import org.omg.RTCORBA.maxPriority;
  26. import org.omg.RTCORBA.minPriority;
  27. /**
  28. * This class implements a simple CORBA Server.
  29. *
  30. * @author Juan Colmenares
  31. * @author Hojjat Jafarpour
  32. * @author Mark Panahi
  33. * @version 1.0
  34. */
  35. public class Server extends RealtimeThread
  36. {
  37. private static final int ITERATION_FACTOR_2 = 1;
  38. private static final int ITERATION_FACTOR_1 = 20;
  39. public String[] args;
  40. private static boolean isClientPropagated = true;
  41. ORB orb;
  42. static short loPrio = minPriority.value;
  43. static short hiPrio = maxPriority.value;
  44. static int staticThreads = 1;
  45. public static void main(String[] args) throws Exception
  46. {
  47. parseCmdLine(args);
  48. Server rt = (Server) ImmortalMemory.instance().newInstance( Server.class );
  49. rt.init( args );
  50. rt.start();
  51. }
  52. public Server(){
  53. super(null,null,null,new LTMemory(3000,300000),null,null);
  54. }
  55. public void init( String args[] ){
  56. this.args = args;
  57. }
  58. public void run()
  59. {
  60. try
  61. {
  62. System.out.println( "[Server] =====================Calling ORB Init in server============================" );
  63. orb = ORB.init(args , null);
  64. System.out.println( "[Server] =====================ORB Init complete in server===========================" );
  65. POA rootPOA = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
  66. System.out.println( "[Server] =================== RootPOA resolved, starting servant_to_ref ==============" );
  67. POAManager poaManager = rootPOA.the_POAManager ();
  68. poaManager.activate();
  69. System.out.println( "[Server] =================== Activated POA Manager ==============" );
  70. // Creating a child poa with a threadpool
  71. RTORB rtorb = RTORBHelper.narrow(orb.resolve_initial_references ("RTORB"));
  72. //System.out.println("Max prio " + PriorityScheduler.instance().getMaxPriority());
  73. //System.out.println("Min prio " + PriorityScheduler.instance().getMinPriority());
  74. //System.out.println("Norm prio " + PriorityScheduler.instance().getNormPriority());
  75. //short priority = (short) (30 + (PriorityScheduler.getNormPriority(RealtimeThread.currentThread()))) ;
  76. //short priority = (short) PriorityScheduler.instance().getMaxPriority();
  77. //System.out.println("Higher priority is: " + priority);
  78. int threadPoolId;
  79. if(isClientPropagated){
  80. System.out.println("[Server] Using client-propagated policy.....");
  81. Policy[] policy = new Policy[2];
  82. policy[0] = rtorb.create_priority_model_policy (
  83. org.omg.RTCORBA.PriorityModel.CLIENT_PROPAGATED,
  84. (short)0);
  85. ThreadpoolLane[] lanes = new ThreadpoolLane[2];
  86. //args are: priority, static threads, dynamic threads
  87. lanes[0] = new ThreadpoolLane(loPrio, staticThreads, 0);
  88. lanes[1] = new ThreadpoolLane(hiPrio, staticThreads, 0);
  89. threadPoolId = rtorb.create_threadpool_with_lanes(10, lanes, false, false, 10, 10);
  90. policy[1] = rtorb.create_threadpool_policy(threadPoolId);
  91. System.out.println("[Server] Creating a child POA");
  92. POA childPOA = rootPOA.create_POA("childPOA", poaManager, policy);
  93. createObj(ITERATION_FACTOR_1, childPOA, "ior1.txt");
  94. createObj(ITERATION_FACTOR_2, childPOA, "ior2.txt");
  95. }else{
  96. System.out.println("[Server] Using server-declared policy.....");
  97. Policy[] loPolicy = new Policy[2];
  98. Policy[] hiPolicy = new Policy[2];
  99. loPolicy[0] = rtorb.create_priority_model_policy (
  100. org.omg.RTCORBA.PriorityModel.SERVER_DECLARED,
  101. loPrio);
  102. hiPolicy[0] = rtorb.create_priority_model_policy (
  103. org.omg.RTCORBA.PriorityModel.SERVER_DECLARED,
  104. hiPrio);
  105. ThreadpoolLane[] lanes = new ThreadpoolLane[2];
  106. //args are: priority, static threads, dynamic threads
  107. lanes[0] = new ThreadpoolLane(loPrio, staticThreads, 0);
  108. lanes[1] = new ThreadpoolLane(hiPrio, staticThreads, 0);
  109. threadPoolId = rtorb.create_threadpool_with_lanes(10, lanes, false, false, 10, 10);
  110. loPolicy[1] = rtorb.create_threadpool_policy(threadPoolId);
  111. hiPolicy[1] = rtorb.create_threadpool_policy(threadPoolId);
  112. System.out.println("[Server] Creating a low priority child POA");
  113. POA lpChildPOA = rootPOA.create_POA("lpChildPOA", poaManager, loPolicy);
  114. System.out.println("[Server] Creating a high priority child POA");
  115. POA hpChildPOA = rootPOA.create_POA("hpChildPOA", poaManager, hiPolicy);
  116. createObj(ITERATION_FACTOR_1, lpChildPOA, "ior1.txt");
  117. createObj(ITERATION_FACTOR_2, hpChildPOA, "ior2.txt");
  118. }
  119. System.out.println( "[Server] RTZen is running ...." );
  120. orb.run();
  121. }
  122. catch (Exception e)
  123. {
  124. e.printStackTrace();
  125. System.exit(-1);
  126. }
  127. }
  128. private void createObj(int iFactor, POA poa, String iorFile){
  129. try{
  130. HelloWorldImpl impl = new HelloWorldImpl(iFactor);
  131. org.omg.CORBA.Object obj = poa.servant_to_reference(impl);
  132. System.out.println( "=================== Servant registered, getting IOR ========================" );
  133. String ior = orb.object_to_string(obj);
  134. writeIOR(ior, iorFile);
  135. }catch(Exception e){
  136. e.printStackTrace();
  137. System.exit(-1);
  138. }
  139. }
  140. /**
  141. * Writes the Server ior to file
  142. */
  143. private void writeIOR(final String ior, final String filename ) {
  144. RealtimeThread rt = new RealtimeThread( null, null, null, HeapMemory.instance(), null, new Runnable() {
  145. public void run() {
  146. try {
  147. BufferedWriter bw = new BufferedWriter( new FileWriter(filename) );
  148. bw.write(ior);
  149. bw.close();
  150. System.out.println( "[Server] " + ior );
  151. }
  152. catch ( java.io.IOException ioe ) {
  153. System.out.println( "Exception writing " + filename );
  154. }
  155. }
  156. } );
  157. rt.start();
  158. try {
  159. rt.join();
  160. }
  161. catch ( InterruptedException e ) {
  162. }
  163. }
  164. private static void parseCmdLine(String[] args) {
  165. int i = 0, j;
  166. String arg;
  167. char flag;
  168. boolean vflag = false;
  169. String outputfile = "";
  170. if(args.length == 0)
  171. printUsage();
  172. while (i < args.length/* && args[i].startsWith("-")*/) {
  173. arg = args[i++];
  174. if (arg.equals("-pm")) {
  175. vflag = true;
  176. if (i < args.length){
  177. if(args[i].equals("sd")){
  178. isClientPropagated = false;
  179. System.out.println("Setting server-declared policy.....");
  180. }else if (args[i].equals("cp")){
  181. isClientPropagated = true;
  182. System.out.println("Setting client-propagated policy.....");
  183. }else{
  184. System.err.println("-pm needs an appropriate priority model");
  185. printUsage();
  186. }
  187. i++;
  188. }else{
  189. System.err.println("-pm needs a priority model");
  190. printUsage();
  191. }
  192. }else if (arg.equals("-lp")) {
  193. vflag = true;
  194. if (i < args.length){
  195. loPrio = (short)Integer.parseInt(args[i]);
  196. if(loPrio < minPriority.value || loPrio > maxPriority.value){
  197. System.err.println("lp priority out of range: " + loPrio);
  198. printUsage();
  199. }
  200. i++;
  201. }else{
  202. System.err.println("-lp needs a value");
  203. printUsage();
  204. }
  205. }else if (arg.equals("-hp")) {
  206. vflag = true;
  207. if (i < args.length){
  208. hiPrio = (short)Integer.parseInt(args[i]);
  209. if(hiPrio < minPriority.value || hiPrio > maxPriority.value){
  210. System.err.println("hp priority out of range: " + hiPrio);
  211. printUsage();
  212. }
  213. i++;
  214. }else{
  215. System.err.println("-hp needs a value");
  216. printUsage();
  217. }
  218. }else if (arg.equals("-st")) {
  219. vflag = true;
  220. if (i < args.length){
  221. staticThreads = Integer.parseInt(args[i]);
  222. if(staticThreads < 1 || staticThreads > 10){
  223. System.err.println("st out of range: " + staticThreads);
  224. printUsage();
  225. }
  226. i++;
  227. }else{
  228. System.err.println("-st needs a value");
  229. printUsage();
  230. }
  231. }
  232. if(!vflag){
  233. System.err.println("Unrecognized option: " + arg);
  234. printUsage();
  235. }
  236. vflag = false;
  237. // use this type of check for "wordy" arguments
  238. /*
  239. if (arg.equals("-verbose")) {
  240. System.out.println("verbose mode on");
  241. vflag = true;
  242. }
  243. // use this type of check for arguments that require arguments
  244. else if (arg.equals("-output")) {
  245. if (i < args.length)
  246. outputfile = args[i++];
  247. else
  248. System.err.println("-output requires a filename");
  249. if (vflag)
  250. System.out.println("output file = " + outputfile);
  251. }
  252. // use this type of check for a series of flag arguments
  253. else {
  254. for (j = 1; j < arg.length(); j++) {
  255. flag = arg.charAt(j);
  256. switch (flag) {
  257. case 'x':
  258. if (vflag) System.out.println("Option x");
  259. break;
  260. case 'n':
  261. if (vflag) System.out.println("Option n");
  262. break;
  263. default:
  264. System.err.println("ParseCmdLine: illegal option " + flag);
  265. break;
  266. }
  267. }
  268. }*/
  269. }
  270. System.err.println("lp priority set to: " + loPrio);
  271. System.err.println("hp priority set to: " + hiPrio);
  272. System.err.println("static threads set to: " + staticThreads);
  273. }
  274. private static void printUsage() {
  275. System.err.println("Please use the following options:");
  276. System.err.println("\t-pm cp|sd\tPriority model(pm) of client-propagated(cp) or server declared(sd).");
  277. System.err.println("\t-lp "+ minPriority.value+"-" + maxPriority.value + "\tPriority of low-priority task. Defaults to CORBA min if not specified.");
  278. System.err.println("\t-hp "+ minPriority.value+"-" + maxPriority.value + "\tPriority of high-priority task. Defaults to CORBA max if not specified.");
  279. System.err.println("\t-st 1-10\tNumber of static threads to use. Defaults to 1.");
  280. System.err.println("\tIMPORTANT: Make sure client priorities match the ones offered by the server!!!");
  281. System.err.println("\t Please see client usage parameters.");
  282. System.exit(-1);
  283. }
  284. }