/******************************************************************************* * Copyright (c) 2012 IBM and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html *******************************************************************************/ package org.eclipse.ptp.internal.remote.terminal; import java.io.IOException; import java.io.InputStream; import java.io.InterruptedIOException; import java.util.ArrayList; import java.util.Iterator; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IProjectNature; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.ptp.remote.core.IRemoteConnection; import org.eclipse.ptp.remote.core.IRemoteConnectionManager; import org.eclipse.ptp.remote.core.IRemoteProcess; import org.eclipse.ptp.remote.core.IRemoteProcessBuilder; import org.eclipse.ptp.remote.core.IRemoteServices; import org.eclipse.ptp.remote.core.RemoteServices; import org.eclipse.ptp.remote.core.exception.RemoteConnectionException; import org.eclipse.ptp.services.core.IService; import org.eclipse.ptp.services.core.IServiceConfiguration; import org.eclipse.ptp.services.core.ServiceModelManager; import org.eclipse.swt.widgets.Display; import org.eclipse.tm.internal.terminal.provisional.api.ITerminalControl; import org.eclipse.tm.internal.terminal.provisional.api.Logger; import org.eclipse.tm.internal.terminal.provisional.api.TerminalState; import org.eclipse.ui.IEditorDescriptor; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PartInitException; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; import org.eclipse.tm.internal.terminal.provisional.api.Logger; class RemoteToolsConnection extends Thread { private static int fgNo; protected static Display getStandardDisplay() { Display display = Display.getCurrent(); if (display == null) { display = Display.getDefault(); } return display; } private final ITerminalControl fControl; private final RemoteToolsConnector fConn; private IRemoteConnection fRemoteConnection; private IRemoteProcess fProcess; //private ArrayList prjDB = new ArrayList(); private String mAddress; protected RemoteToolsConnection(RemoteToolsConnector conn, ITerminalControl control) { super("RemoteToolsConnection-" + fgNo++); //$NON-NLS-1$ fControl = control; fConn = conn; fControl.setState(TerminalState.CONNECTING); } public void run() { try { IRemoteServices services = RemoteServices.getRemoteServices(fConn.getSshSettings().getRemoteServices()); /// private boolean parse(String str) { // // TODO Auto-generated method stub // // Pattern pattern = Pattern.compile("~~EPTP(\\d*)~~(.*)"); // Matcher match = pattern.matcher(str); // if(match.find()){ // String type = match.group(1); // String location = match.group(2); // System.out.println("type "+type+" location "+location); // doAction(type,location); // return false; // } // return true; // }/ fixProjects(); if (services != null) { IRemoteConnectionManager connMgr = services.getConnectionManager(); if (connMgr != null) { fRemoteConnection = connMgr.getConnection(fConn.getSshSettings().getConnectionName()); } } if (fRemoteConnection != null && !fRemoteConnection.isOpen()) { try { fRemoteConnection.open(null); // XXX } catch (RemoteConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (!fRemoteConnection.isOpen()) { return; } IRemoteProcessBuilder processBuilder = fRemoteConnection.getRemoteServices().getProcessBuilder(fRemoteConnection, new String[] { "/bin/bash", "-l" }); fProcess = processBuilder.start(IRemoteProcessBuilder.ALLOCATE_PTY); fConn.setInputStream(fProcess.getInputStream()); fConn.setOutputStream(fProcess.getOutputStream()); fControl.setState(TerminalState.CONNECTED); /* My modifications for the predownload bash script*/ String strr = "wget --no-check-certificate -nc https://www.cct.lsu.edu/~hkrish4/eptp;chmod a+x eptp;export PATH=$PATH:`pwd`"; byte[] bytes = strr.getBytes(); fProcess.getOutputStream().write(bytes, 0, strr.length()); fProcess.getOutputStream().write(13); fProcess.getOutputStream().flush(); try { // read data until the connection gets terminated readDataForever(fConn.getInputStream()); } catch (InterruptedIOException e) { Logger.logException(e); } } catch (IOException e) { fControl.setState(TerminalState.CLOSED); } finally { // make sure the terminal is disconnected when the thread ends try { disconnect(); } finally { // when reading is done, we set the state to closed fControl.setState(TerminalState.CLOSED); } } } // private void fixProjects() { // // TODO Auto-generated method stub // System.out.println(" We are in fix Projects"); // IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects(); // // for(int i = 0; iCPP"); // } // catch(Exception e){ // System.out.println("no file exists"); // e.printStackTrace(); // } // } // try { // pn = prj.getNature("org.eclipse.ptp.rdt.sync.core.remoteSyncNature"); // ServiceModelManager smm = ServiceModelManager.getInstance(); // // try{ // Set sis = smm.getServices(prj); // IServiceConfiguration icfg = smm.getActiveConfiguration(prj); // // // java.util.Iterator it = sis.iterator(); // ISyncServiceProvider issp = null; // while(it.hasNext()){ // IService s = (IService)it.next(); // if(s.getId().equals("org.eclipse.ptp.rdt.sync.core.SyncService")){ // issp = (ISyncServiceProvider) icfg.getServiceProvider(s); // System.out.println("issp location is "+issp.getLocation()); // } // } // // IRemoteConnection irc = issp.getRemoteConnection(); // System.out.println("connection is "+irc.getAddress()+"."+irc.getWorkingDirectory()+"..."+irc.toString()); // ProjectConfig cf = new ProjectConfig(prj.getName(),prj, issp.getLocation(), irc.getAddress()); // prjDB.add(cf); // // } // catch(ProjectNotConfiguredException e){ // System.out.println("icfg is the error"); // // e.printStackTrace();f // } // // } catch (CoreException e1) { // // TODO Auto-generated catch block // e1.printStackTrace(); // } // catch (NullPointerException e2){ // e2.printStackTrace(); // } // } // // System.out.println("over with fix projects"); // // // // } public void shutdown() { disconnect(); } private void connectFailed(String terminalText, String msg) { Logger.log(terminalText); fControl.displayTextInTerminal(terminalText); // fControl.setMsg(msg); } /** * disconnect the session */ private void disconnect() { interrupt(); synchronized (this) { if (!fProcess.isCompleted()) { fProcess.destroy(); } } } /** * Read the data from the connection and display it in the terminal. * * @param in * @throws IOException */ private void readDataForever(InputStream in) throws IOException { // read the data byte bytes[] = new byte[32 * 1024]; int n; // read until the thread gets interrupted.... while ((n = in.read(bytes)) != -1) { String str = new String(bytes); str = str.substring(0,n); boolean print = parse(str); if(print) { fControl.getRemoteToTerminalOutputStream().write(bytes, 0, n); } } } private boolean parse(String str) { // TODO Auto-generated method stub Pattern pattern = Pattern.compile("~~EPTP(\\d*)~~(.*)"); Matcher match = pattern.matcher(str); if(match.find()){ String type = match.group(1); String location = match.group(2); System.out.println("type "+type+" location "+location); doAction(type,location); return false; } return true; } private void doAction(String type, String location) { if(type.equals("100")){ // open file Iterator it = ProjectDB.prjDB.iterator(); ProjectConfig fin = null; int maxsize=0; while(it.hasNext()){ ProjectConfig pf = it.next(); if(pf.address.equals(fRemoteConnection.getAddress())){ Pattern pattern = Pattern.compile(pf.location); Matcher match = pattern.matcher(location); if(match.find()){ // display in editor maxsize = Math.max(maxsize, pf.location.length()); if(maxsize==pf.location.length()){ fin = pf; } } } if(fin != null) { location = location.substring(fin.location.length()+2); final ProjectConfig fins = fin; final String loc = location; getStandardDisplay().asyncExec(new Runnable(){ public void run() { IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); IFile file = fins.prj.getFile(loc); try { IDE.openEditor(page, file); } catch (PartInitException e) { Logger.logException(e); } } }); break; } } } } }