Skip to content

Commit 040a375

Browse files
committed
adds sample application for Archiving
1 parent 8369ffd commit 040a375

File tree

18 files changed

+633
-68
lines changed

18 files changed

+633
-68
lines changed

sample/Archiving/build.gradle

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apply plugin:'application'
2+
mainClassName = "com.example.ArchivingServer"
3+
4+
repositories {
5+
mavenCentral()
6+
}
7+
8+
dependencies {
9+
compile project(':')
10+
compile group: 'com.github.codingricky', name: 'spark-core-16', version: '1.1'
11+
compile group: 'org.freemarker', name: 'freemarker', version: '2.3.19'
12+
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.7.6'
13+
}
14+
15+
run.systemProperty 'API_KEY', '854511'
16+
run.systemProperty 'API_SECRET', '***REMOVED***'
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Move down content because we have a fixed navbar that is 50px tall */
2+
body {
3+
padding-top: 50px;
4+
padding-bottom: 20px;
5+
background-color: #F2F2F2;
6+
}
7+
8+
/* Responsive: Portrait tablets and up */
9+
@media screen and (min-width: 768px) {
10+
/* Remove padding from wrapping element since we kick in the grid classes here */
11+
.body-content {
12+
padding: 0;
13+
}
14+
}
15+
16+
#subscribers div {
17+
float: left;
18+
}
19+
20+
.bump-me {
21+
padding-top: 40px;
22+
}
20.6 KB
Loading
Loading
Loading
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Initialize an OpenTok Session object
2+
var session = TB.initSession(sessionId);
3+
4+
// Initialize a Publisher, and place it into the element with id="publisher"
5+
var publisher = TB.initPublisher(apiKey, 'publisher');
6+
7+
// Attach event handlers
8+
session.on({
9+
10+
// This function runs when session.connect() asynchronously completes
11+
sessionConnected: function(event) {
12+
// Publish the publisher we initialzed earlier (this will trigger 'streamCreated' on other
13+
// clients)
14+
session.publish(publisher);
15+
},
16+
17+
// This function runs when another client publishes a stream (eg. session.publish())
18+
streamCreated: function(event) {
19+
// Create a container for a new Subscriber, assign it an id using the streamId, put it inside
20+
// the element with id="subscribers"
21+
var subContainer = document.createElement('div');
22+
subContainer.id = 'stream-' + event.stream.streamId;
23+
document.getElementById('subscribers').appendChild(subContainer);
24+
25+
// Subscribe to the stream that caused this event, put it inside the container we just made
26+
session.subscribe(event.stream, subContainer);
27+
}
28+
29+
});
30+
31+
// Connect to the Session using the 'apiKey' of the application and a 'token' for permission
32+
session.connect(apiKey, token);

sample/Archiving/public/js/host.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
var session = OT.initSession(sessionId),
2+
publisher = OT.initPublisher("publisher"),
3+
archiveID = null;
4+
5+
session.connect(apiKey, token, function(err, info) {
6+
if(err) {
7+
alert(err.message || err);
8+
}
9+
session.publish(publisher);
10+
});
11+
12+
session.on('streamCreated', function(event) {
13+
session.subscribe(event.stream, "subscribers", { insertMode: "append" });
14+
});
15+
16+
session.on('archiveStarted', function(event) {
17+
archiveID = event.id;
18+
console.log("ARCHIVE STARTED");
19+
$(".start").hide();
20+
$(".stop").show();
21+
});
22+
23+
session.on('archiveStopped', function(event) {
24+
archiveID = null;
25+
console.log("ARCHIVE STOPPED");
26+
$(".start").show();
27+
$(".stop").hide();
28+
});
29+
30+
$(document).ready(function() {
31+
$(".start").click(function(event){
32+
$.get("start");
33+
}).show();
34+
$(".stop").click(function(event){
35+
$.get("stop/" + archiveID);
36+
}).hide();
37+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var session = OT.initSession(sessionId),
2+
publisher = OT.initPublisher("publisher");
3+
4+
session.connect(apiKey, token, function(err, info) {
5+
if(err) {
6+
alert(err.message || err);
7+
}
8+
session.publish(publisher);
9+
});
10+
11+
session.on('streamCreated', function(event) {
12+
session.subscribe(event.stream, "subscribers", { insertMode : "append" });
13+
});
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
package com.example;
2+
3+
import static spark.Spark.*;
4+
5+
import com.opentok.*;
6+
import com.opentok.OpenTok;
7+
import com.opentok.Role;
8+
import com.opentok.TokenOptions;
9+
import com.opentok.Archive;
10+
import spark.*;
11+
12+
import java.util.Map;
13+
import java.util.HashMap;
14+
import java.util.List;
15+
16+
import com.opentok.exception.OpenTokException;
17+
18+
public class ArchivingServer {
19+
20+
private static final String apiKey = System.getProperty("API_KEY");
21+
private static final String apiSecret = System.getProperty("API_SECRET");
22+
private static final OpenTok opentok = new OpenTok(Integer.parseInt(apiKey), apiSecret);
23+
private static String sessionId;
24+
25+
public static void main(String[] args) throws OpenTokException {
26+
27+
if (apiKey == null || apiKey.isEmpty() || apiSecret == null || apiSecret.isEmpty()) {
28+
System.out.println("You must define API_KEY and API_SECRET system properties in the build.gradle file.");
29+
System.exit(-1);
30+
}
31+
32+
sessionId = opentok.createSession().getSessionId();
33+
34+
externalStaticFileLocation("./public");
35+
36+
get(new FreeMarkerTemplateView("/") {
37+
@Override
38+
public ModelAndView handle(Request request, Response response) {
39+
//Map<String, Object> attributes = new HashMap<String, Object>();
40+
return new ModelAndView(null, "index.ftl");
41+
}
42+
});
43+
44+
get(new FreeMarkerTemplateView("/host") {
45+
@Override
46+
public ModelAndView handle(Request request, Response response) {
47+
48+
String token = null;
49+
try {
50+
token = opentok.generateToken(sessionId, new TokenOptions.Builder()
51+
.role(Role.MODERATOR)
52+
.build());
53+
} catch (OpenTokException e) {
54+
e.printStackTrace();
55+
}
56+
57+
Map<String, Object> attributes = new HashMap<String, Object>();
58+
attributes.put("apiKey", apiKey);
59+
attributes.put("sessionId", sessionId);
60+
attributes.put("token", token);
61+
62+
return new ModelAndView(attributes, "host.ftl");
63+
}
64+
});
65+
66+
get(new FreeMarkerTemplateView("/participant") {
67+
@Override
68+
public ModelAndView handle(Request request, Response response) {
69+
70+
String token = null;
71+
try {
72+
token = opentok.generateToken(sessionId, new TokenOptions.Builder()
73+
.role(Role.MODERATOR)
74+
.build());
75+
} catch (OpenTokException e) {
76+
e.printStackTrace();
77+
}
78+
79+
Map<String, Object> attributes = new HashMap<String, Object>();
80+
attributes.put("apiKey", apiKey);
81+
attributes.put("sessionId", sessionId);
82+
attributes.put("token", token);
83+
84+
return new ModelAndView(attributes, "participant.ftl");
85+
}
86+
});
87+
88+
get(new FreeMarkerTemplateView("/history") {
89+
@Override
90+
public ModelAndView handle(Request request, Response response) {
91+
92+
String pageParam = request.queryParams("page");
93+
int page;
94+
try {
95+
page = Integer.parseInt(pageParam);
96+
} catch (NumberFormatException e) {
97+
page = 1;
98+
}
99+
100+
int offset = (page - 1) * 5;
101+
List<Archive> archives = null;
102+
try {
103+
archives = opentok.listArchives(offset, 5);
104+
} catch (OpenTokException e) {
105+
e.printStackTrace();
106+
}
107+
108+
Map<String, Object> attributes = new HashMap<String, Object>();
109+
attributes.put("archives", archives);
110+
attributes.put("showPrevious", null);
111+
// TODO: we don't have a total count, how do we know if there is a next page?
112+
attributes.put("showNext", "/history?page=" + (page + 1));
113+
114+
if (page > 1) {
115+
attributes.put("showPrevious", "/history?page=" + (page - 1));
116+
}
117+
118+
return new ModelAndView(attributes, "history.ftl");
119+
}
120+
});
121+
122+
get(new Route("/download/:archiveId") {
123+
@Override
124+
public Object handle(Request request, Response response) {
125+
126+
Archive archive = null;
127+
try {
128+
archive = opentok.getArchive(request.params("archiveId"));
129+
} catch (OpenTokException e) {
130+
e.printStackTrace();
131+
return null;
132+
}
133+
134+
response.redirect(archive.getUrl());
135+
return null;
136+
}
137+
});
138+
139+
get(new Route("/start") {
140+
@Override
141+
public Object handle(Request request, Response response) {
142+
143+
Archive archive = null;
144+
try {
145+
archive = opentok.startArchive(sessionId, "Java Archiving Sample App");
146+
} catch (OpenTokException e) {
147+
e.printStackTrace();
148+
return null;
149+
}
150+
return archive.toString();
151+
}
152+
});
153+
154+
get(new Route("/stop/:archiveId") {
155+
@Override
156+
public Object handle(Request request, Response response) {
157+
158+
Archive archive = null;
159+
try {
160+
archive = opentok.stopArchive(request.params("archiveId"));
161+
} catch (OpenTokException e) {
162+
e.printStackTrace();
163+
return null;
164+
}
165+
return archive.toString();
166+
}
167+
});
168+
169+
get(new Route("/delete/:archiveId") {
170+
@Override
171+
public Object handle(Request request, Response response) {
172+
173+
try {
174+
opentok.deleteArchive(request.params("archiveId"));
175+
} catch (OpenTokException e) {
176+
e.printStackTrace();
177+
return null;
178+
}
179+
response.redirect("/history");
180+
return null;
181+
}
182+
});
183+
184+
185+
}
186+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.example;
2+
3+
import freemarker.template.Configuration;
4+
import freemarker.template.Template;
5+
import freemarker.template.TemplateException;
6+
import spark.ModelAndView;
7+
import spark.TemplateViewRoute;
8+
9+
import java.io.IOException;
10+
import java.io.StringWriter;
11+
12+
public abstract class FreeMarkerTemplateView extends TemplateViewRoute {
13+
14+
private Configuration configuration;
15+
16+
protected FreeMarkerTemplateView(String path) {
17+
super(path);
18+
this.configuration = createFreemarkerConfiguration();
19+
}
20+
21+
protected FreeMarkerTemplateView(String path, String acceptType) {
22+
super(path, acceptType);
23+
this.configuration = createFreemarkerConfiguration();
24+
}
25+
26+
@Override
27+
public String render(ModelAndView modelAndView) {
28+
try {
29+
StringWriter stringWriter = new StringWriter();
30+
31+
Template template = configuration.getTemplate(modelAndView.getViewName());
32+
template.process(modelAndView.getModel(), stringWriter);
33+
34+
return stringWriter.toString();
35+
36+
} catch (IOException e) {
37+
throw new IllegalArgumentException(e);
38+
} catch (TemplateException e) {
39+
throw new IllegalArgumentException(e);
40+
}
41+
}
42+
43+
private Configuration createFreemarkerConfiguration() {
44+
Configuration retVal = new Configuration();
45+
retVal.setClassForTemplateLoading(FreeMarkerTemplateView.class, "freemarker");
46+
return retVal;
47+
}
48+
49+
}

0 commit comments

Comments
 (0)