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
|
// **********************************************************************
//
// Copyright (c) 2003-2014 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import Glacier2.CannotCreateSessionException;
import Glacier2.SessionControlPrx;
import Glacier2.SessionPrx;
import Glacier2._PermissionsVerifierDisp;
import Glacier2._SessionManagerDisp;
import Ice.Current;
import Ice.LocalException;
import Ice.NotRegisteredException;
import Ice.ObjectNotExistException;
import Ice.StringHolder;
import Voip.Callback_Control_incomingCall;
import Voip.ControlPrx;
import Voip.SessionPrxHelper;
import Voip._SessionDisp;
public class Server extends Ice.Application
{
private final ScheduledExecutorService timer = new ScheduledThreadPoolExecutor(1);
class SessionI extends _SessionDisp
{
private ControlPrx _ctrl;
private long _timestamp = System.currentTimeMillis();
public void setControl(ControlPrx ctrl, Current current)
{
_ctrl = ctrl;
}
public void simulateCall(int delay, Current current)
{
timer.schedule(new Runnable()
{
public void run()
{
if(_ctrl != null)
{
System.out.println("calling incoming call");
_ctrl.begin_incomingCall(new Callback_Control_incomingCall()
{
public void exception(LocalException ex)
{
System.out.println("incoming call failed");
ex.printStackTrace();
}
public void response()
{
System.out.println("incoming call succeeded");
}
});
}
}
}, delay, TimeUnit.MILLISECONDS);
}
public void destroy(Current current)
{
try
{
current.adapter.remove(current.id);
}
catch(NotRegisteredException ex)
{
// Ignore.
}
}
public void refresh(Current current)
{
_timestamp = System.currentTimeMillis();
}
long getTimestamp()
{
return _timestamp;
}
}
class PermissionsVerifierI extends _PermissionsVerifierDisp
{
public boolean checkPermissions(String userId, String password, StringHolder reason, Current current)
{
return true;
}
}
class SessionManagerI extends _SessionManagerDisp
{
public SessionPrx create(String userId, SessionControlPrx control,
Current current) throws CannotCreateSessionException
{
// The configured timeout must be greater than 600. This is 601 * 2.
final long sessionTimeout = 1202;
final SessionI session = new SessionI();
final SessionPrx proxy = SessionPrxHelper.uncheckedCast(current.adapter.addWithUUID(session));
timer.scheduleWithFixedDelay(new Runnable()
{
public void run()
{
// If the session has already been destroyed the ONE will
// fall out of run canceling the task.
if (System.currentTimeMillis() - session.getTimestamp() > (sessionTimeout * 1000L * 2))
{
proxy.destroy();
throw new ObjectNotExistException();
}
}
}, sessionTimeout, sessionTimeout, TimeUnit.SECONDS);
return proxy;
}
};
public int
run(String[] args)
{
if(args.length > 0)
{
System.err.println(appName() + ": too many arguments");
return 1;
}
Ice.ObjectAdapter adapter = communicator().createObjectAdapter("VoipServer");
adapter.add(new PermissionsVerifierI(), communicator().stringToIdentity("VoipVerifier"));
adapter.add(new SessionManagerI(), communicator().stringToIdentity("VoipSessionManager"));
adapter.activate();
communicator().waitForShutdown();
return 0;
}
public static void
main(String[] args)
{
Server app = new Server();
int status = app.main("Server", args, "config.server");
System.exit(status);
}
}
|