/
PluginFileTransfer.java
148 lines (119 loc) · 4.54 KB
/
PluginFileTransfer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
PluginFileTransfer.java
This file defines the thread that accepts new plugins and installs them on the Virtual Service.
*/
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import javax.net.ssl.*;
import java.net.URLClassLoader;
import java.net.URL;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
public class PluginFileTransfer extends Thread{//PluginFileTransfer is extending the thread class
private ArrayList<VirtualMachine> machines;//machines is the array list
public PluginFileTransfer(ArrayList<VirtualMachine> m){//this constructor ass the array list m as the parameter
super();//super method
machines = m;//machines is m
}
public void run(){ //Overrides run method.
SSLServerSocket serverSocket = null;//no value at this moment
SSLServerSocketFactory factory= (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
SSLSocket sslsocket = null;
InputStream in = null;
OutputStream out = null;
String filename = "";
String password = "";
ArrayList<Password> passwords = new ArrayList<Password>();
try{
serverSocket = (SSLServerSocket) factory.createServerSocket(9000); //sets up a distinct socket.
}
catch(Exception e){
e.printStackTrace();
}
while(true){
try{
sslsocket = (SSLSocket) serverSocket.accept(); //listens for connection from client.
}
catch(Exception e){
e.printStackTrace();
}
try {
in = sslsocket.getInputStream();
} catch (IOException ex) {
System.out.println("Can't get socket input stream. ");
}
try{ //reads the name of the file, which is the first line of input.
BufferedReader reader = new BufferedReader(new InputStreamReader(sslsocket.getInputStream()));
filename = reader.readLine();
password = reader.readLine();
}
catch(Exception e){
e.printStackTrace();
}
File f = new File("Plugins/" + filename);
//if(filename.endsWith(".class") && (!f.exists() || checkPassword(password.hashCode(), filename, passwords))){
if(filename.endsWith(".class")){
if(!f.exists()) passwords.add(new Password(filename, password.hashCode()));
System.out.println("\033[1m\033[32mNow receiving " + filename + "\033[0m");
filename = filename.replace("class", "txt");
try {
new FileOutputStream("Plugins/" + filename, false).close(); //Create filename
out = new FileOutputStream("Plugins/" + filename);
}
catch (Exception e) {
e.printStackTrace();
}
byte[] bytes = new byte[16*1024];
try{
int count;
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
//Rename to the file's original name
File file = new File("Plugins/" + filename);
filename = filename.replace("txt", "class");
File file2 = new File("Plugins/" + filename);
file.renameTo(file2);
URL[] urls = null;
urls = new URL[] {(new File("./Plugins/").toURI().toURL()), (new File("./").toURI().toURL()) };
URLClassLoader cl = new URLClassLoader(urls);
//Class cls = cl.loadClass(filename.split("\\.")[0], false);
Class cls = Class.forName(filename.split("\\.")[0], true, (ClassLoader) cl);
//Class cls = Class.forName("LightSensorPluginNew", true, cl);
cl.close();
Class[] cArg = new Class[2];
cArg[0] = int.class;
cArg[1] = String.class;
for(int i = 0; i < machines.size(); i++){
VirtualMachine machine = machines.get(i);
System.out.println("got here");
if(machine.getClassName().equals(filename.split("\\.")[0])){
System.out.println("got to here");
IoTDevice currentDevice = machine.getDevice();
IoTDevice newDevice = (IoTDevice) cls.getDeclaredConstructor(cArg).newInstance(currentDevice.getServerPort(), currentDevice.getServerIP());
machine.setDevice(newDevice);
}
}
System.out.println("\033[1m\033[32mSuccessfuly loaded plugin\033[0m");
}
catch(Exception e){
e.printStackTrace();
}
try{
out.close();
in.close();
sslsocket.close();
Thread.sleep(10);
}
catch(Exception e){
e.printStackTrace();
}
}
else{
System.out.println("The file was rejected");
}
}
}
}