-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathNodeServer.java
More file actions
126 lines (94 loc) · 3.64 KB
/
NodeServer.java
File metadata and controls
126 lines (94 loc) · 3.64 KB
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
import Eleccion.*;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
/**
*
* @author Miguel Alejandro
*/
public class NodeServer {
static int uid;
static int numberOfClients;
static public String getParam(String[] args, String arg) {
String result=null;
for (int i=0; i<args.length; i++) {
if (args[i].equals("-"+arg) || args[i].equals("--"+arg)) {
result=args[i+1];
break;
}
}
return result;
}
public static void main (String args []) {
try{
// Extract the -id parameter
uid=Integer.parseInt(getParam(args,"id"));
numberOfClients=Integer.parseInt(getParam(args,"n"));
//crea e inicializa el ORB
ORB orb = ORB.init(args, null);
// consigue la referencia al rootpoa y activa el POAmanager
POA rootpoa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
rootpoa.the_POAManager().activate();
//crea el servant y registra este con el ORB
NodeServant ns = new NodeServant(uid);
rootpoa.activate_object(ns);
//consigue referenciar el objeto del servant
org.omg.CORBA.Object ref = rootpoa.servant_to_reference (ns);
Node href = NodeHelper.narrow(ref);
// consigue el contexto nombrado del root
org.omg.CORBA.Object objRef = orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
// el objeto referenciado en nombre
NameComponent nc = new NameComponent("node"+uid,"");
NameComponent component[] = {nc};
System.out.println("Registering client: "+ uid);
ncRef.rebind(component, href);
String input;
java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
// Wait until all remote objects are activated.
System.out.println("Wait until all clients show this message, and press enter to initialize");
input = in.readLine();
if(uid != 0){
NameComponent nc1 = new NameComponent("node"+(uid-1),"");
NameComponent path[] = {nc1};
org.omg.CORBA.Object objRef1 = ncRef.resolve(path);
Node vecino = NodeHelper.narrow(objRef1);
vecino.register(href,uid,0);
}
if ( uid != (numberOfClients -1) && (uid+1) != (numberOfClients -1) ) {
NameComponent nc2 = new NameComponent("node"+(uid+1),"");
NameComponent path2[] = {nc2};
org.omg.CORBA.Object objRef2 = ncRef.resolve(path2);
Node vecino2 = NodeHelper.narrow(objRef2);
vecino2.register(href, uid,1);
}
if ( uid == numberOfClients - 1) {
NameComponent nc1 = new NameComponent("node0","");
NameComponent path[] = {nc1};
org.omg.CORBA.Object objRef1 = ncRef.resolve(path);
Node vecino = NodeHelper.narrow(objRef1);
vecino.register(href,uid,1);
NameComponent nc2 = new NameComponent("node0","");
NameComponent path2[] = {nc2};
org.omg.CORBA.Object objRef2 = ncRef.resolve(path2);
Node vecino2 = NodeHelper.narrow(objRef2);
href.register(vecino2, 0,0);
NameComponent nc3 = new NameComponent("node"+ (uid-1),"");
NameComponent path3[] = {nc3};
org.omg.CORBA.Object objRef3 = ncRef.resolve(path3);
Node vecino3 = NodeHelper.narrow(objRef3);
href.register(vecino3, uid-1,1);
}
System.out.println("El servidor esta listo y esperando....");
// espere para la invocacion del cliente
orb.run();
}
catch (Exception e) {
System.err.println ("Error: " + e);
e.printStackTrace(System.out);
}
System.out.println("NodeServer Exiting...");
}
}