4 * Created on March 29, 2008, 10:45 PM
6 * To change this template, choose Tools | Template Manager
7 * and open the template in the editor.
10 package javaapplication1;
14 import javax.swing.JOptionPane;
20 public class PanelFiller {
22 /** Creates a new instance of PanelFiller */
23 public PanelFiller(JPanel[] panelList, JLabel statusLabel) {
24 panel1 = panelList[0];
25 panel2 = panelList[1];
26 panel3 = panelList[2];
27 panel4 = panelList[3];
28 panel5 = panelList[4];
29 this.statusLabel = statusLabel;
32 rtime = Runtime.getRuntime();
33 child = rtime.exec("/bin/bash");
34 outCommand = new BufferedWriter(new OutputStreamWriter(child.getOutputStream()));
36 JOptionPane.showMessageDialog(null, "The program cannot start bash.\n");
40 displayHandle = new DisplayHandle(outCommand);
43 public void handleOperation(String opname){
45 JPanel curpanel = null;
46 String type = record.getType();
47 if (type.equals("user"))
49 else if (type.equals("slice"))
51 else if (type.equals("node"))
53 else if (type.equals("SA")){
57 else if (type.equals("MA")){
62 if (opname.equals("register")){
63 String params = displayHandle.PanelToString(type, curpanel);
64 String rest = params.substring(12);
65 if (rest.charAt(0)=='}')
66 params = "'g_params':{'hrn':'"+record.getHrn()+"', 'type':'"+record.getType()+"'"+rest;
68 params = "'g_params':{'hrn':'"+record.getHrn()+"', 'type':'"+record.getType()+"', "+rest;
69 String message = "{'opname':'"+opname+"', "+params+"}";
70 String result = SendToClientStub(message);
71 String[] res_arr = result.split("\n");
73 for (int i=0; i<res_arr.length; i++)
74 send_str += res_arr[i];
75 statusLabel.setText(send_str);
77 else if (opname.equals("remove")){
78 String gparams = "{'hrn':'"+record.getHrn()+"', 'type':'"+record.getType()+"'}";
79 String pparams = "{}";
80 String message = "{'opname':'"+opname+"', 'g_params':"+gparams+", 'p_params':"+pparams+"}";
81 String result = SendToClientStub(message);
82 String[] res_arr = result.split("\n");
84 for (int i=0; i<res_arr.length; i++)
85 send_str += res_arr[i];
86 statusLabel.setText(send_str);
88 else if (opname.equals("update")){
89 String params = displayHandle.PanelToString(type, curpanel);
90 String rest = params.substring(12);
91 if (rest.charAt(0)=='}')
92 params = "'g_params':{'hrn':'"+record.getHrn()+"', 'type':'"+record.getType()+"'"+rest;
94 params = "'g_params':{'hrn':'"+record.getHrn()+"', 'type':'"+record.getType()+"', "+rest;
95 String message = "{'opname':'"+opname+"', "+params+"}";
96 String result = SendToClientStub(message);
97 String[] res_arr = result.split("\n");
99 for (int i=0; i<res_arr.length; i++)
100 send_str += res_arr[i];
101 statusLabel.setText(send_str);
103 else if (opname.equals("lookup")){
104 String gparams = "{'hrn':'"+record.getHrn()+"', 'type':'"+record.getType()+"'}";
105 String pparams = "{}";
106 String message = "{'opname':'"+opname+"', 'g_params':"+gparams+", 'p_params':"+pparams+"}";
107 String result = SendToClientStub(message);
108 String[] res_arr = result.split("\n");
109 String[] send_arr = null;
112 if (res_arr!=null && res_arr.length > 0){
114 String status_str = "";
115 for (int i=0; i<res_arr.length; i++){
116 if (!res_arr[i].equals("{'geni':{"))
117 status_str += res_arr[i];
121 statusLabel.setText(status_str);
125 for(i=0; i<res_arr.length && !res_arr[i].equals("{'geni':{"); i++)
127 if (i<res_arr.length && res_arr[i].equals("{'geni':{")){
129 send_arr = new String[res_arr.length-i-3];
131 while(!res_arr[i].equals("}")){
132 send_arr[p++] = res_arr[i];
136 while(!res_arr[i].equals("}}")){
137 send_arr[p++] = res_arr[i];
140 displayHandle.FillComponentList(type, send_arr);
141 if (type.equals("user"))
142 displayHandle.displayComponentList("user",panel1, panel2);
143 else if (type.equals("slice"))
144 displayHandle.displayComponentList("slice",panel1, panel3);
145 else if (type.equals("node"))
146 displayHandle.displayComponentList("node",panel1, panel4);
147 else if (type.equals("SA/MA"))
148 displayHandle.displayComponentList("SA/MA",panel1, panel5);
154 private String SendToClientStub(String message){
156 //do the call to the python client
157 String curdir = System.getProperty("user.dir");
158 String client_folder = user.getDirectory();
162 //cd to the client folder
163 outCommand.write("cd "+client_folder+"\n");
166 FileWriter fstream = new FileWriter(client_folder+"/tmp_input.txt");
167 BufferedWriter out = new BufferedWriter(fstream);
168 out.write(user.getHrn()+" "+user.getType()+"\n");
170 //Close the output stream
172 outCommand.write("./clientstub.py\n");
175 //get the result of the call from the python client
176 File file = new File(client_folder+"/tmp_output.txt");
178 while(!file.exists() && i<5){
181 Thread.currentThread().sleep(200);
182 } catch (InterruptedException ex) {
183 ex.printStackTrace();
184 result = "Problem occured in client stub.\n";
190 Thread.currentThread().sleep(200);
191 } catch (InterruptedException ex) {
192 ex.printStackTrace();
193 result = "Problem occured in client stub.\n";
197 BufferedReader in = new BufferedReader(new FileReader(client_folder+"/tmp_output.txt"));
199 String tmp = in.readLine();
200 while(tmp!=null && tmp.length() > 0){
206 outCommand.write("rm tmp_input.txt tmp_output.txt\n");
207 outCommand.write("cd "+curdir+"\n");
209 }catch (Exception e){//Catch exception if any
210 System.err.println("Error: " + e.getMessage());
211 result = "Problem occured in client stub.\n";
217 public void initializePanels(){
218 displayHandle.displayComponentList("user",panel1, panel2);
219 displayHandle.displayComponentList("slice",panel1, panel3);
220 displayHandle.displayComponentList("node",panel1, panel4);
221 displayHandle.displayComponentList("SA/MA",panel1, panel5);
224 public void setUser(User user){
227 public void setRecord(RecordInfo record){
228 this.record = record;
232 private RecordInfo record;
233 private javax.swing.JPanel panel1;
234 private javax.swing.JPanel panel2;
235 private javax.swing.JPanel panel3;
236 private javax.swing.JPanel panel4;
237 private javax.swing.JPanel panel5;
238 private javax.swing.JLabel statusLabel;
240 private BufferedWriter outCommand;
241 private Runtime rtime;
242 private Process child;
244 private DisplayHandle displayHandle;