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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/*
* Copyright (c) 2006 Manuel Traut and Volker Dahnke
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors: Manuel Traut and Volker Dahnke
*
*/
package YalpServer;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.util.ArrayList;
import java.util.Properties;
import java.security.*;
import java.sql.SQLException;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;
import YalpInterfaces.*;
/*
* Class InitServer
*
* <em>Establishes DBConnection and creates VlcStreamer waits for
* further Instructions via Interfaces</em>
*
* @author Volker Dahnke / Manuel Traut
*
* @version 0.1 20-11-2005<br>
*
* @see Server
*
*/
public class InitServer {
private ServerSettings settings = new ServerSettings();
private ServerControlImpl srvCon;
private ServerControlInterface srv;
private ORB orb;
private String[] orbArgs;
private POA poa;
private String serverIP;
private ArrayList<FileBrowser> browseList;
/*
* Constructor: starts Server initialization
*/
public InitServer(String[] _orbArgs) {
loadConfig("ServerSettings.xml");
writeConfig("ServerSettings.xml");
this.browseList = new ArrayList<FileBrowser>();
this.orbArgs = _orbArgs; // t.b.d. read orbargs from config xml
try {
this.serverIP = Inet4Address.getLocalHost().getHostAddress();
} catch(UnknownHostException e) {
/* t.b.d. error handling */
System.out.println("couldn't resolve hostname");
}
/* bind ServerControl to ORB and NamingService */
this.orb = ORB.init(orbArgs, null);
try {
this.poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
} catch(org.omg.CORBA.ORBPackage.InvalidName e) {
/* t.b.d. error handling */
System.out.println("couldn't get name ref of root poa");
}
try {
poa.the_POAManager().activate();
} catch(org.omg.PortableServer.POAManagerPackage.AdapterInactive e) {
/* t.b.d. error handling */
System.out.println("poa inactive");
}
this.srvCon = new ServerControlImpl();
this.srvCon.setORB(this.orb);
try {
org.omg.CORBA.Object ref = poa.servant_to_reference(this.srvCon);
this.srv = ServerControlInterfaceHelper.narrow(ref);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "YALP_Server";
NameComponent path[] = ncRef.to_name(name);
ncRef.rebind(path, this.srv);
System.out.println("YALP Server ready");
this.orb.run();
} catch( org.omg.CosNaming.NamingContextPackage.InvalidName e) {
/* t.b.d. error handling */
System.out.println("couldn't narrow ref to path");
} catch( org.omg.CosNaming.NamingContextPackage.NotFound e) {
/* t.b.d. error handling */
System.out.println("naming context not found, couldn't bind server ctl");
} catch(org.omg.PortableServer.POAPackage.ServantNotActive e) {
/* t.b.d. error handling */
System.out.println("couldn't get name ref of root poa");
} catch (org.omg.CORBA.ORBPackage.InvalidName e) {
/* t.b.d. error handling */
System.out.println("couldn't get NameService");
} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) {
/* t.b.d. error handling */
System.out.println("policies not set correctly");
} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
/* t.b.d. error handling */
System.out.println("rebind failed, cannot proceed");
}
}
/*
* write Configuration to XML File
*
* @param fileName
* where to write the configuration file
* @return boolean
* false - if failed
*/
public boolean writeConfig(String fileName) {
try{
FileOutputStream configFile = new FileOutputStream(fileName);
XMLEncoder configWriter = new XMLEncoder(configFile);
configWriter.writeObject(settings.imageDir);
configWriter.writeObject(settings.videoDir);
configWriter.writeObject(settings.soundDir);
configWriter.close();
} catch (FileNotFoundException fnfe){
return false;
}
return true;
}
/*
* tries to load ServerSettings from XML File
* @param fileName
* Configuration file
* @return boolean
* false - if loading failed
*/
public boolean loadConfig(String fileName) {
try{
FileInputStream configFile = new FileInputStream(fileName);
XMLDecoder configLoader = new XMLDecoder(configFile);
settings.imageDir = (String)configLoader.readObject();
settings.videoDir = (String)configLoader.readObject();
settings.soundDir = (String)configLoader.readObject();
configLoader.close();
} catch(FileNotFoundException fnfe) {
System.out.println("Configuration not found, loading defaults...");
return false;
} catch(ClassCastException cce) {
System.out.println("Errors in Configuration, loading defaults...");
return false;
}
return true;
}
/*
* returns actual configuration of the server
* @return ServerSettings
* actual configuration
*/
public ServerSettings getConfig() {
return this.settings;
}
/*
* sets and saves a new ServerConfiguration
*
* @param set
* new ServerSettings
* @return boolean
* true if succesfully saved
*/
public boolean setConfig(ServerSettings set){
this.settings = set;
return this.writeConfig("ServerConfiguration.xml");
}
/*
* t.b.d. session management
*
* Client logon (give him,his fileBrowser)
*
* @param ipAdress
* of the Client
* @param userName
* using the Client
*
public void newClient(String ipAdress,String userName) {
this.browseList.add( new FileBrowser( this.settings.startDir,
ipAdress,
userName ) );
}
*/
/*
* Client logoff (free memory of his fileBrowser
*
* @param ipAdress
* of the Client
* @param userName
* using the Client
*/
public void remClient(String ipAdress, String userName) {
for( FileBrowser aBrowser : this.browseList ) {
if( aBrowser.getOwner().equals(ipAdress) &&
aBrowser.getUserName().equals(userName) ) {
this.browseList.remove(aBrowser);
break;
}
}
}
/*
* change Directory in fileBrowser
*
* @param ip
* of the Client
* @param dir
* to which should be changed
* @return ArrayList<YalpFile>
* content of the directory changed to
*/
public ArrayList<YalpFile> changeDir(String ip, String dir) {
FileBrowser aBrowser = null;
for(FileBrowser oneMoreBrowser : this.browseList) {
if( oneMoreBrowser.getOwner().equals(ip) ) {
oneMoreBrowser.changeDir(dir);
aBrowser = oneMoreBrowser;
break;
}
}
return aBrowser.getFiles();
}
/*
* Server Shutdown
*/
public void serverShutdown() {
System.out.println("Server shutdown - Server is going down");
System.exit(0);
}
}
|