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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
|
/*
* 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 YalpClients.SwtClient;
import YalpInterfaces.*;
import YalpClients.*;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.*;
import java.net.Inet4Address;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
import java.sql.SQLException;
import java.util.ArrayList;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
/*
* Class Model
*
* <em>Implementiert die Schnittstellenfunktionen zur Steuerung des Servers
* (Datenbankinhalte schreiben, lesen; Streaming steuern</em>
*
* @author Volker Dahnke / Manuel Traut
*
* @version 0.6 14-12-2005<br>
*
* @see client.GUI
*/
public class Model {
public ServerControlInterface srvCon;
private Process vlcPlayer;
private Output actualStream = new Output();
private Output streamInfo = new Output();
private ArrayList<Media> findMedia = new ArrayList<Media>();
private ArrayList<Media> editMedia = new ArrayList<Media>();
private ArrayList<Media> toPlaylist = new ArrayList<Media>();
private ArrayList<Media> playList = new ArrayList<Media>();
private ArrayList<YalpUser> userList = new ArrayList<YalpUser>();
private ClientConfiguration settings;
private String clientIP;
private String curPasswd;
private AccessRights userGroup=AccessRights.DENY;
private String userName;
private Boolean playback = false;
private Boolean logoff= false;
private Session session = new Session();
/*
* Instances srvConnection to Registry, DbConnection, ServerControl
* and VlcStreamer. Also instances Class variables
*/
public Model(String[] argv) {
this.settings=new ClientConfiguration();
loadConfig("ClientConfiguration.xml");
writeConfig("ClientConfiguration.xml");
try {
ORB orb = ORB.init(argv, null);
org.omg.CORBA.Object objRef =
orb.resolve_initial_references("NameService");
NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
String name = "YALP_Server";
srvCon = ServerControlInterfaceHelper.narrow(ncRef.resolve_str(name));
} catch (Exception e) {// DEBUG
String errorStr="couldn't connect to YALP Server";
MessageBox messageBox = new MessageBox(
new Shell(Display.getDefault(),SWT.MIN), SWT.OK );
System.out.println(e);
messageBox.setMessage(errorStr);
messageBox.open();
System.exit(0);
}
// determine client IP Adress
try {
this.clientIP = Inet4Address.getLocalHost().getHostAddress();
System.out.println(this.clientIP);
} catch (UnknownHostException e) {
String errorStr = "Model.model: determine IP failed";
MessageBox messageBox = new MessageBox(
new Shell(Display.getDefault(),SWT.MIN), SWT.OK );
System.out.println(errorStr);
messageBox.setMessage(errorStr);
messageBox.open();
System.exit(0);
}
this.userList = new ArrayList<YalpUser>();
this.playList = new ArrayList<Media>();
this.toPlaylist = new ArrayList<Media>();
}
/*
* write Configuration to XML File
*
* @param fileName
* @return boolean: true if succesful
*/
public boolean writeConfig(String fileName) {
try {
FileOutputStream srvConfigFile = new FileOutputStream(fileName);
XMLEncoder srvConfigWriter = new XMLEncoder(srvConfigFile);
srvConfigWriter.writeObject(settings.serverIP);
srvConfigWriter.writeObject(settings.classServerPort);
srvConfigWriter.writeObject(settings.registryPort);
srvConfigWriter.writeObject(settings.vlcCommand);
srvConfigWriter.close();
} catch (FileNotFoundException fnfe) {
return false;
}
return true;
}
/*
* load Configuration from XML File if possible
*
* @param fileName
* @return boolean: true if succesful
*/
public boolean loadConfig(String fileName){
try{
FileInputStream srvConfigFile = new FileInputStream(fileName);
XMLDecoder srvConfigLoader = new XMLDecoder(srvConfigFile);
System.out.println(srvConfigFile);
settings.serverIP = (String)srvConfigLoader.readObject();
settings.classServerPort= (Integer)srvConfigLoader.readObject();
settings.registryPort = (Integer)srvConfigLoader.readObject();
settings.vlcCommand = (String)srvConfigLoader.readObject();
srvConfigLoader.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;
}
/*
* get acutal srvConfig object
*
* @return ClientConfiguration: aktuelle Konfiguration
*/
public ClientConfiguration getConfig(){
return this.settings;
}
/*
* setzen und speichern eines ClientConfiguration Files
*
* @param set
* ClientConfiguration.xml
* @return boolean: true if succesful written Configuration
*/
public boolean setConfig(ClientConfiguration set){
this.settings = set;
return this.writeConfig("ClientConfiguration.xml");
}
/*
* Sends YalpUsername and passwd to Server and returns YalpUsergroup
*
* @param username
* @param passwd
* @return AccessRights
*/
public AccessRights userVerify(String username,String passwd){
this.userName=username;
this.curPasswd = passwd;
YalpErrorHolder err = new YalpErrorHolder();
SessionHolder sess = new SessionHolder();
try{
// log on server
this.srvCon.clientLogon(this.userName,passwd,this.clientIP,sess,err);
this.session = sess.value;
this.userGroup = sess.value.me.level;
/*
} catch (RemoteException e) {
System.out.println("client.Model: Logon on Server failed 1"+ e);
e.printStackTrace();
System.out.println("client.Model: Logon on Server failed 1");
} catch (MalformedURLException e) {
System.out.println("client.Model: Logon on Server failed 2");
} catch (SQLException e) {
System.out.println("client.Model: Logon on Server failed 3");
} catch (NotBoundException e) {
System.out.println("client.Model: Logon on Server failed 4");
*/
}catch(NullPointerException e){
System.out.println("YALP Server couldn't be reached");
this.userGroup = AccessRights.NO_YALP_SERVER;
}
return this.userGroup;
}
/*
* Gets a list with all YalpUsers from the server
*
* @return ArrayList<YalpUser> all registered YalpUsers
*/
public ArrayList<YalpUser> getUser(){
YalpErrorHolder err = new YalpErrorHolder();
try{
UsersHolder usr = new UsersHolder();
this.srvCon.getUser(usr, err);
this.userList = new ArrayList<YalpUser>(usr.value.length);
for(int i = 0; i < usr.value.length; i++)
this.userList.add(usr.value[i]);
}catch(Exception e){
System.out.println("Exceoption in Model userList "+ e);
}
return userList;
}
/*
* Sends User change request to server
*
* @param user
* Configuration with changes on specific YalpUser
*/
public void changeUser(YalpUser user, String passwd, Action action) {
YalpErrorHolder err = new YalpErrorHolder();
try{
this.srvCon.changeUser( user,
passwd,
action,
err );
System.out.println(err.value.descr);
}catch(Exception e){
System.out.println("Exceoption in Model changeUser "+ e);
}
}
/*
* Sends Media change request to server and refreshes Model data
*
* @param media
* to change
* @return boolean: succesful changed media
*/
public Boolean changeMedia(Media media, Action action){
YalpErrorHolder err = new YalpErrorHolder();
try{
this.srvCon.changeMedia(media, action, err);
if(err.value.code == YalpErrorCode.OK){
Media result;
for (int i=0;i<this.editMedia.size();i++){
result=this.editMedia.get(i);
/* t.b.d. alter to new db desgin
if (result.id==media.id){
result.type=media.type;
result.title=media.title;
result.author=media.author;
result.album=media.album;
result.category=media.category;
result.year=media.year;
result.duration=media.duration;
result.aBitrate=media.aBitrate;
result.vBitrate=media.vBitrate;
result.resolution=media.resolution;
result.ownerId=media.ownerId;
result.lastEdit=media.lastEdit;
result.path=media.path;
result.name=media.name;
return true;
}
*/
}
}
} catch(Exception e) {
System.out.println("Exceoption in Model changeUser "+ e);
}
return false;
}
/*
* adds Media with given id to Playlist
*
* @param id
*/
public void add2PlayList(int id){
Boolean notfound=true;
for (int i=0;(i<this.findMedia.size())&& (notfound);i++){
Media result=this.findMedia.get(i);
if (result.id==id){
String name = result.name;
String path = result.path;
if(playback){
this.add2Stream(name, path);
}
this.playList.add(result);
this.toPlaylist.add(result);
notfound=false;
}
}
}
/*
* Removes Media with given id from Playlist
*
* @param id
*/
public void removeFromPlayList(int id){
/* t.b.d.
this.playList.remItem(id);
this.toPlaylist.remove(id);
*/
}
/*
* Returns List with items to play
*
* @return ArrayList<Media>: ArrayList to create Playlist from
*/
public ArrayList<Media> getToPlaylist(){
return this.toPlaylist;
}
/*
* Creates new Stream with actual playlist
*
* @return Streaminfo: about new created Stream, Client should use this,
* information to get the Information howto receive the stream
*/
public Output createOutput(){
/* t.b.d. former createStream */
streamInfo.outputAction = Action.CREATE;
Media[] a = {};
streamInfo.playlist = this.playList.toArray(a);
return this.streamInfo;
}
/*
* Sends search request to server and refreshes Model data
*
* @param request
* Search Request
* @param kind
* 1 - search in first Window; other ints: search in other window
* (admins edit view)
* @return int: number of results
*/
public int search(String request, ArrayList<MediaType> types, int kind){
YalpErrorHolder err = new YalpErrorHolder();
MediasHolder result = new MediasHolder();
try{
MediaType[] a = {};
if (kind == 1){
srvCon.search(request, types.toArray(a), result, err);
for(int i = 0; i < result.value.length; i++)
findMedia.add(result.value[i]);
return findMedia.size();
} else {
srvCon.search(request, types.toArray(a), result, err);
for(int i = 0; i < result.value.length; i++)
editMedia.add(result.value[i]);
return editMedia.size();
}
} catch(Exception e){
e.printStackTrace();
System.out.println("Exceoption in Model search "+ e);
return 0;
}
}
/*
* Returns the search result
*
* @param kind
* 1 - for searchMedia of mainwindow; 2 - for searchMedia of editView
* @return ArrayList<Media>: last found medias
*/
public ArrayList<Media> getMedia(int kind){
if (kind==1) return this.findMedia;
else return this.editMedia;
}
/*
* Retuns search result with given id
*
* @param id
* of the result
* @param kind
* 1 - for mainWindow, 2 for admins editView
* @return Media: searchMedia, with specified ID
*/
public Media getMediaWithId(int id,int kind){
ArrayList<Media> resultList;
if (kind==1)resultList= this.findMedia;
else resultList=this.editMedia;
for (int i=0;(i<resultList.size());i++){
Media result=resultList.get(i);
if (result.id==id){
return result;
}
}
return null;
}
/*
* Retuns id of YalpUser with given username
*
* @param username
* @return int: useriD
*/
public int getOwnerid(String username){
ArrayList<YalpUser> ownerList =this.userList;
for (YalpUser owner:ownerList){
if (owner.name.equals(username)){
return owner.id;
}
}
return 0;
}
/*
* Returns MediaType as string
*
* @param mediatype
* @return string
*/
public String getTypeName(MediaType type)
{
switch(type.value())
{
case MediaType._IMAGE:
return "image";
case MediaType._SOUND:
return "sound";
case MediaType._VIDEO:
return "video";
default:
return "unknown";
}
}
/*
* Returns AccessRights as string
*
* @param mediatype
* @return string
*/
public String accessRightsName(AccessRights type)
{
switch(type.value())
{
case AccessRights._USER:
return "user";
case AccessRights._ADMIN:
return "admin";
case AccessRights._DENY:
return "no login";
default:
return "unknown";
}
}
/*
* Sets class variable actualStream to given value
*
* @param aStream
* stream to srvControl via play, pause, etc
*/
public void setActualStream(Output aStream){
actualStream = aStream;
}
/*
* Starts Video Lan Player
*
* @return boolean: true if vlc can be started and a stream is actualStream
*/
private boolean startVLC(){
String vlcCmd = settings.vlcCommand +
" --reset-srvConfig --video-on-top -I telnet --telnet-port 12345 ";
/*
switch (this.actualStream.getAccess()){
case UDP:
vlcCmd += "udp:@";
break;
case HTTP:
vlcCmd += "http://";
break;
default:
System.out.println(
"client.Model.startVLC: playback this type is not supported" );
}
*/
vlcCmd += actualStream.info.params;
System.out.println(vlcCmd);
try {
this.vlcPlayer = Runtime.getRuntime().exec(vlcCmd);
} catch (IOException e) {
System.out.println("client.Model.startVLC: starting vlcPlayer failed");
return false;
}
return true;
}
/*
* Stops Video Lan Player
*
*/
private void stopVLC(){
playback = false;
if(vlcPlayer != null)
vlcPlayer.destroy();
}
/*
* Starts playback of actual playlist
*
* @return void
*/
public boolean play(){
playback = true;
actualStream.outputAction = Action.CREATE;
actualStream.info = new AccessInfo( "bam",
"bam",
"vlc",
"params",
AccessType.STREAM );
Media[] a = new Media[playList.size()];
actualStream.playlist = playList.toArray(a);
actualStream.destIp = clientIP;
YalpErrorHolder err = new YalpErrorHolder();
OutputHolder tmp = new OutputHolder();
tmp.value = actualStream;
srvCon.control(tmp, err);
actualStream = tmp.value;
actualStream.outputAction = Action.START;
OutputHolder tmp2 = new OutputHolder(actualStream);
srvCon.control(tmp2, err);
actualStream = tmp2.value;
startVLC();
return true;
}
/*
* Adds a new Media to Playlist. Only during playback
*
* @param name
* @param path
* @return boolean: true if succesful added
*/
public boolean add2Stream(String name, String path){
YalpErrorHolder err = new YalpErrorHolder();
Media[] empty = {};
this.actualStream.playlist = empty;
Media media = new Media();
media.path = path;
media.fileName = name;
this.actualStream.playlist[this.actualStream.playlist.length] = media;
this.actualStream.outputAction = Action.CREATE;
OutputHolder tmp = new OutputHolder(this.actualStream);
this.srvCon.control(tmp, err);
this.actualStream = tmp.value;
return true;
}
/*
* Stops playback
*
* @return boolean: false - if srvConnection to streamer failed
*/
public boolean stop(){
YalpErrorHolder err = new YalpErrorHolder();
this.actualStream.outputAction = Action.STOP;
OutputHolder tmp = new OutputHolder(this.actualStream);
this.srvCon.control(tmp,err);
this.actualStream = tmp.value;
this.stopVLC();
return true;
}
/*
* jumps to next media in playlist
*
* @return boolean: false - if srvConnection to streamer failed
*/
public boolean next(){
YalpErrorHolder err = new YalpErrorHolder();
this.actualStream.outputAction = Action.FORWARD;
OutputHolder tmp = new OutputHolder(this.actualStream);
this.srvCon.control(tmp,err);
this.actualStream = tmp.value;
return true;
}
/*
* Pauses playback
*
* @return boolean: false - if srvConnection to streamer failed
*/
public boolean pause(){
YalpErrorHolder err = new YalpErrorHolder();
this.actualStream.outputAction = Action.PAUSE;
OutputHolder tmp = new OutputHolder(this.actualStream);
this.srvCon.control(tmp,err);
this.actualStream = tmp.value;
return true;
}
/*
* Causes the server to clean up and shutdown
*
*/
public void serverShutdown(){
if (this.playback)
this.stop();
this.srvCon.serverShutdown();
}
/*
* Checks if the server is still alive if not Client shutdown
*
*/
public void serverStillAlive(){
// try{
// srvCon.ping();
/* }catch (RemoteException e){
if (this.playback)
this.stopVLC();
String errorStr="Server shutdown - Client is going down too";
MessageBox messageBox = new MessageBox(
new Shell(Display.getDefault(),SWT.MIN), SWT.OK);
System.out.println(errorStr);
messageBox.setMessage(errorStr);
messageBox.open();
System.exit(0);
} */
}
/*
* Returns client IP address
*
* @return String: ipAddress
*/
public String getClientIp(){
return this.clientIP;
}
/*
* Returns username of logged in user
*
* @return String: username
*/
public String getYalpUserName(){
return this.userName;
}
/*
* Returns indication if playback is runing
*
* @return boolean: true - if is play'in
*/
public Boolean getPlayback(){
return this.playback;
}
/*
* Get Total amount of streams played
*
* @return int: number of Streams totally played
*
public int getAllStreamNum(){
try{
return this.srvCon.getStreamCounter().getAllStreams();
}catch (Exception e){
System.out.println("Exception in Model.getAllStreamNum"+e);
}
return -1;
}
*/
/*
* Gets amount of Streams actual playing
*
* @return int: number of Streams play'in at the moment
*
public int getActualStreamNum(){
try{
return this.srvCon.getStreamCounter().getActualStreams();
}catch (Exception e){
System.out.println("Exception in Model.getActualStreamNum"+e);
}
return -1;
}
*/
/*
* Sets class variable logoff to true and logs off from server
*/
public void logoff(){
YalpErrorHolder err = new YalpErrorHolder();
try{
this.logoff=true;
this.srvCon.clientLogoff(this.session, err);
}catch(Exception e) {// DEBUG
System.out.println("Model Exeptions in logoff");
e.printStackTrace();
}
}
/*
* Returns indication if client is logging of
*
* @return Boolean: true - if is logging of
*/
public Boolean getLogoff(){
return this.logoff;
}
}
|