package server;
import java.util.*;
import java.io.*;
import net.jxta.id.IDFactory;
import net.jxta.pipe.InputPipe;
import net.jxta.pipe.PipeMsgEvent;
import net.jxta.pipe.PipeMsgListener;
import net.jxta.credential.AuthenticationCredential;
import net.jxta.document.StructuredDocument;
import net.jxta.document.Document;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.MimeMediaType;
import net.jxta.membership.Authenticator;
import net.jxta.membership.MembershipService;
import net.jxta.peergroup.PeerGroup;
import net.jxta.peergroup.PeerGroupFactory;
import net.jxta.exception.PeerGroupException;
import net.jxta.exception.ProtocolNotSupportedException;
import net.jxta.document.AdvertisementFactory;
import net.jxta.document.MimeMediaType;
import net.jxta.discovery.DiscoveryService;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.pipe.PipeService;
import net.jxta.pipe.OutputPipe;
import net.jxta.pipe.OutputPipeListener;
import net.jxta.pipe.OutputPipeEvent;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PipeAdvertisement;
import net.jxta.protocol.PeerGroupAdvertisement;
import net.jxta.protocol.ModuleImplAdvertisement;
import net.jxta.protocol.PeerAdvertisement;
import net.jxta.protocol.ResolverQueryMsg;
import net.jxta.protocol.ResolverResponseMsg;
import net.jxta.resolver.ResolverService;
import net.jxta.resolver.QueryHandler;
import net.jxta.endpoint.Message;
import net.jxta.endpoint.MessageElement;
import net.jxta.document.Advertisement;
import net.jxta.id.ID;
import net.jxta.impl.protocol.ResolverResponse;
import net.jxta.impl.protocol.ResolverQuery;
/**
* Encapsulates an Agent framework that handles communication between many
* Student peers. Sends a series of questions to Student peers, and
* processes their answers. Once completed, this Teacher peer will
* recommend additional resources to the student for improvement
* depending on the exam result.
*/
public class Teacher {
public static final String DISTANCE_LEARNING = "DL:";
public static final String TEACHER_PIPE = "teacher_pipe";
public static final String ACTION_TAG = "tag";
public static final String CLIENT_NAME = "client";
public static final String COURSE_NAME = "course";
private PeerGroup netPeerGroup;
private DiscoveryService discoverySvc;
private PipeAdvertisement pipeAdv;
private InputPipe inputPipe;
private AgentFactory agentFactory = AgentFactory.getInstance();
// This Hashtable is used to store the agents used for individual students.
private Hashtable agentTable = new Hashtable();
private void sendMessage(String clientName, Map map)
throws IOException {
for (;;) {
Enumeration enum = discoverySvc.getLocalAdvertisements(
DiscoveryService.ADV, "Name",
Teacher.DISTANCE_LEARNING +
clientName);
while (enum.hasMoreElements()) {
PipeAdvertisement pipeAdv = (PipeAdvertisement) enum.nextElement();
PipeService pipeSvc = netPeerGroup.getPipeService();
OutputPipe outputPipe = null;
try {
outputPipe = pipeSvc.createOutputPipe(pipeAdv, 1000);
Message message = pipeSvc.createMessage();
Set keySet = map.keySet();
Iterator iterator = keySet.iterator();
while (iterator.hasNext()) {
String key = (String) iterator.next();
String value = (String) map.get(key);
MessageElement element = message.newMessageElement(key,
new MimeMediaType(
"text/plain"),
new ByteArrayInputStream(
value.getBytes()));
message.addElement(element);
}
outputPipe.send(message);
System.out.println("Sent message");
return;
} catch (IOException io) {
io.printStackTrace();
} finally {
if (outputPipe != null) {
outputPipe.close();
}
}
}
// Need to look remotely.
discoverySvc.getRemoteAdvertisements(null, DiscoveryService.ADV,
"Name",
Teacher.DISTANCE_LEARNING +
clientName, 10, null);
try {
Thread.sleep(5 * 1000); // 5 sec too short if this is truly distant
System.out.print(".");
} catch (Exception ignore) {
}
}
}
/**
* Publish our pipe advertisement.
*/
public void publishPipeAdv() throws IOException {
PipeAdvertisement pipeAdv = null;
String advType = PipeAdvertisement.getAdvertisementType();
pipeAdv = (PipeAdvertisement) AdvertisementFactory.newAdvertisement(
advType);
pipeAdv.setPipeID(IDFactory.newPipeID(netPeerGroup.getPeerGroupID()));
pipeAdv.setName(DISTANCE_LEARNING + TEACHER_PIPE);
pipeAdv.setType(PipeService.UnicastType);
discoverySvc.publish(pipeAdv, DiscoveryService.ADV);
discoverySvc.remotePublish(pipeAdv, DiscoveryService.ADV);
this.pipeAdv = pipeAdv;
}
/**
* Create an input pipe for inbound messages.
*/
public void createInputPipe() throws IOException {
PipeService pipeSvc = netPeerGroup.getPipeService();
PipeMsgListener listener = new InputPipeMsgListener();
this.inputPipe = pipeSvc.createInputPipe(this.pipeAdv, listener);
}
/**
* Start up the Net Peer group & get the Discovery Service for that group.
*/
public void startJxta() throws PeerGroupException, IOException {
this.netPeerGroup = PeerGroupFactory.newNetPeerGroup();
this.discoverySvc = netPeerGroup.getDiscoveryService();
// Flush all local cache information. We want to start from scratch.
discoverySvc.flushAdvertisements(null, DiscoveryService.ADV);
discoverySvc.flushAdvertisements(null, DiscoveryService.PEER);
discoverySvc.flushAdvertisements(null, DiscoveryService.GROUP);
}
/**
* Create the Teacher and initiate the JXTA services.
*/
public static void main(String[] argc) throws Exception {
Teacher teacher = new Teacher();
teacher.startJxta();
teacher.publishPipeAdv();
teacher.createInputPipe();
System.out.println("Ready to teach");
}
/**
* Handles the Student Teacher interactions.
*/
class InputPipeMsgListener implements PipeMsgListener {
/**
* Utility method to extract the value of a provided key
*
* @param message The Message to parse.
* @param key The key to search for.
*/
private String getElementValue(Message message, String key) {
if (!message.hasElement(key)) {
return null;
}
MessageElement element = message.getElement(key);
InputStream stream = element.getStream();
try {
byte[] bytes = new byte[stre
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
jxta远程教学p2p技术.rar (46个子文件)
ch22
DistanceLearning
runStudent.bat 223B
teacher
pse
etc
passwd 41B
root
peer-root.pem 2KB
client
peer-service.pem 3KB
peer.phrase 92B
cm
jxta-NetGroup
scheduler.db 2KB
public
private
Groups
Peers
uuid-59616261646162614A7874615032503312B421D07C674B359AAFBD09851F662803 2KB
tmp
Adv
uuid-59616261646162614E50472050325033188FFFA9BF904D17A30FB5D263F57A6604 300B
info-jxta-NetGroup
PeerInfoService
scheduler.db 0B
public
private
tmp
jxta-WorldGroup
scheduler.db 749B
public
private
Groups
jxta-NetGroup 281B
Peers
uuid-59616261646162614A787461503250331D80EDA081734F32A1E7A1919061AC3D03 2KB
uuid-59616261646162614A7874615032503312B421D07C674B359AAFBD09851F662803 1KB
tmp
Adv
cm459584383 472B
cm-115408536 4KB
cm-106035347 6KB
info-jxta-WorldGroup
PeerInfoService
scheduler.db 0B
public
private
tmp
PlatformConfig 2KB
runTeacher.bat 223B
student01
runStudent.bat 223B
source
server
AgentFactory.java 647B
Agent.java 4KB
client
Student.java 12KB
runTeacher.bat 223B
student02
runStudent.bat 223B
build
server
Agent.class 3KB
AgentFactory.class 625B
Teacher$InputPipeMsgListener.class 4KB
Teacher.class 6KB
Agent$Problem.class 883B
client
Student.class 9KB
lib
jxtasecurity.jar 60KB
cryptix-asn1.jar 70KB
beepcore.jar 79KB
jxtaptls.jar 196KB
minimalBC.jar 101KB
jxta.jar 631KB
log4j.jar 155KB
cryptix32.jar 473KB
exams
history101
answers.xml 1KB
exam.xml 1KB
AnswersTemplate.xml 1KB
ExamTemplate.xml 876B
build.xml 2KB
Teacher.java 12KB
www.pudn.com.txt 218B
共 46 条
- 1
资源评论
- gfghgi2012-05-16跟想象的有一定差距,,用的旧版本的
cctc_joe
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功