Buongiorno a tutti, sto facendo un progetto per l'università e finito tutta la prima implementazione sono arrivato nella fase di test.
Allora in pratica si tratta come da titolo di far comunicare in LOCALE un serve ed un client che si scambiano info, con Eclipse provo a fare girare il client da ore ormai ma continua a darmi questo errore:
Exception in thread "main" java.security.AccessControlException: access denied ("java.net.SocketPermission" "127.0.0.1:52365" "connect,resolve")
at java.security.AccessControlContext.checkPermission(Unknown Source)
at java.security.AccessController.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkPermission(Unknown Source)
at java.lang.SecurityManager.checkConnect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at sun.rmi.transport.proxy.RMIDirectSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.proxy.RMIMasterSocketFactory.createSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.createConnection(Unknown Source)
at sun.rmi.transport.tcp.TCPChannel.newConnection(Unknown Source)
at sun.rmi.server.UnicastRef.newCall(Unknown Source)
at sun.rmi.registry.RegistryImpl_Stub.rebind(Unknown Source)
at server.Server.startS(Server.java:177)
at server.Server.main(Server.java:201)
Con questo codice:
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import utilities.*;
import org.openstreetmap.gui.jmapviewer.Coordinate;
import shared.*;
public class Server implements ServInterf {
private ConcurrentMap<ClientInterf, Car> map;
private List<ClientInterf> stubs;
private List<ClientPackage> clients;
private static Park p1 = new Park(new Coordinate(44.430898, 8.767789));
private static List<Park> lp = Collections.synchronizedList(new LinkedList<Park>());
public Server() {
super();
map = new ConcurrentHashMap<ClientInterf, Car>();
stubs = new LinkedList<ClientInterf>();
clients = new LinkedList<ClientPackage>();
}
public double distanza(double lat1, double longit1, double lat2, double longit2) {
double distance = 0;
double dist_long = longit2 - longit1;
double dist_lat = lat2 - lat1;
double pezzo1 = Math.cos(lat2)*Math.sin(dist_long);
double pezzo11 = pezzo1*pezzo1;
double pezzo2 = Math.cos(lat1)*Math.sin(lat2)-Math.sin(lat1)*Math.cos(lat2)
*Math.cos(dist_long);
double pezzo22 = pezzo2*pezzo2;
double pezzo3 = Math.sin(lat1)*Math.sin(lat2)+Math.cos(lat1)*Math.cos(lat2)
*Math.cos(dist_long);
double pezzo4 = Math.atan((Math.sqrt(pezzo11+pezzo22))/pezzo3);
distance = pezzo4*6372;
return distance;
}
@Override
public ServerPackage giveCar(ClientInterf stub, Coordinate c, double range, Cartype t) {
if(lp.isEmpty()) {
System.out.println("Non ho ancora inserito parcheggi");
System.exit(0);
}
ListIterator<Park> lpi = lp.listIterator();
ServerPackage pack;
Park aux = lpi.next();
Car temp = aux.next();
while(lpi.hasNext()) {
if(distanza(c.getLat(), c.getLon(), aux.getPark().getLat(),
aux.getPark().getLon()) <= range) {
while(aux.hasNext()) {
//finché il parcheggio non trova una car di tipo t
if(temp.getType() == t) {
if(aux.removeCar(temp)) {
pack = new ServerPackage(temp.getTarga(), aux.getPark());
map.put(stub, temp);
return pack;
} else {
System.out.println("Error in giveCar");
}
}
else
aux.next(); //controllo la prossima car
}
} else
aux = lpi.next(); //controllo il prossimo parcheggio
}
clients.add(new ClientPackage(stub, c, range, t));
return null; //significa che va in coda il client
}
private void dequeue(Car c, Park p) throws RemoteException {
if(clients.isEmpty()){
return;
}
ListIterator<ClientPackage> iter = clients.listIterator();
ClientPackage aux = iter.next();
while(iter.hasNext()) {
if(aux.getT().equals(c.getType())) {
if(distanza(p.getPark().getLat(), p.getPark().getLon(), aux.getC().getLat(),
aux.getC().getLon()) <= aux.getR()) {
clients.remove(aux);
giveCar(aux.getCi(), aux.getC(), aux.getR(), aux.getT());
notify(aux.getCi(), new ServerPackage(c.getTarga(), p.getPark()));
return;
}
} else
iter.next();
return;
}
}
@Override
public void returnCar(ClientInterf stub, Coordinate p) {
if(lp.isEmpty()) {
System.out.println("Non ho ancora inserito parcheggi");
return;
}
ListIterator<Park> lpi = lp.listIterator();
Park aux = lpi.next();
while(lpi.hasNext() && aux.getPark() != p) {
//controlla che la macchina venga posteggiata in uno dei parcheggi registrati
if(aux.getPark().equals(p)) {
aux.insertCar(map.get(stub));
if(map.remove(stub, map.get(stub))){
try {
dequeue(map.get(stub), aux);
} catch (RemoteException e) {
e.printStackTrace();
}
} else {
System.out.println
(new Error("Il cliente non aveva la macchina da restituire").getMessage());
}
}
}
}
@Override
public void notify(ClientInterf stub, ServerPackage pack) throws RemoteException {
stub.CarFounded(pack);
}
@Override
public synchronized void registerForCallback(ClientInterf stub) throws RemoteException {
if(!stubs.contains(stub)) {
stubs.add(stub);
System.out.println("Client registered for callback");
}
}
@Override
public synchronized void unregisterForCallback(ClientInterf stub) throws RemoteException {
if(stubs.remove(stub))
System.out.println("Client unregistered for callback");
else
System.out.println("Unable to unregister client");
}
public void startS() {
String n = "CallbackServer";
try {
ServInterf stub = (ServInterf) UnicastRemoteObject.exportObject(this, 0);
Registry registry = null;
try {
registry = LocateRegistry.createRegistry(52365);
//registry.list();
} catch(RemoteException e) {
registry = LocateRegistry.getRegistry(52365);
}
registry.rebind(n,stub);
//Client a = new Client(new Coordinate(44.430898, 8.767789), 0.1, Cartype.COMFORT);
//Client b = new Client(new Coordinate(44.535703, 8.99682411999992), 0.1,
// Cartype.ELETTRICO);
//Client c = new Client(new Coordinate(44.430898, 8.767789), 0.1, Cartype.COMFORT);
Car c1 = new Car(Cartype.COMFORT, "AA134BC", p1.getPark());
Car c2 = new Car(Cartype.ELETTRICO, "AC327BC", p1.getPark());
lp.add(p1);
p1.insertCar(c1);
p1.insertCar(c2);
ClientInterf c;
//b.start();
//c.start();
} catch(RemoteException e) {
e.printStackTrace();
System.out.println("Eccezione" +e);
}
}
public static void main(String[] args) {
System.setProperty("java.security.policy", "file:C:\\Perm.policy");
if(System.getSecurityManager() == null) System.setSecurityManager(new SecurityManager());
Server s = new Server();
s.startS();
}
}
Commentando provando e tutto ho visto che il problema è la rebind ma non capisco il perchè, essendo fondamentale per la riuscita del programma.
Vi ringrazio in anticipo della mano!