5
import java.awt.event.*;
7
import java.util.Enumeration;
8
import net.jxta.document.*;
9
import net.jxta.peergroup.*;
10
import net.jxta.exception.*;
11
import net.jxta.impl.peergroup.*;
13
import net.jxta.discovery.*;
14
import net.jxta.pipe.*;
15
import net.jxta.platform.NetworkConfigurator;
16
import net.jxta.platform.NetworkManager;
17
import net.jxta.protocol.*;
18
import java.net.MalformedURLException;
20
import net.jxta.endpoint.*;
21
import net.jxta.discovery.*;
22
import java.lang.reflect.InvocationTargetException;
23
import net.jxta.share.*;
24
import net.jxta.share.client.*;
25
import net.jxta.share.metadata.ContentMetadata;
26
import net.jxta.share.metadata.ContentMetadataFactory;
28
public class CMS_App extends JFrame {
30
static PeerGroup netPeerGroup = null;
31
private DiscoveryService myDiscoveryService = null;
32
private PipeService myPipeService = null;
33
private JTextArea displayArea;
34
private JButton sendButton;
35
private CMS myCms = null;
36
private ListRequestor myListRequestor = null;
38
private final static MimeMediaType XMLMIMETYPE = new MimeMediaType("text/xml");
40
public static void main(String args[]) {
41
CMS_App myapp = new CMS_App();
43
myapp.addWindowListener (
45
public void windowClosing(WindowEvent e) {
60
Container c = getContentPane();
62
sendButton = new JButton("Send Search");
63
sendButton.addActionListener(
64
new ActionListener() {
65
public void actionPerformed(ActionEvent e) {
70
c.add(sendButton, BorderLayout.NORTH);
72
displayArea = new JTextArea();
73
c.add(new JScrollPane(displayArea), BorderLayout.CENTER);
80
displayArea.append("Click on Button to send data...\n");
83
//myCms.getContentManager().share(new File("image.png"));
84
//myCms.getContentManager().share(new File("test.txt"));
85
ContentMetadata[] data = new ContentMetadata[2];
87
ContentMetadata cm1 = new ContentMetadata() {
90
public String getValue() {
91
// TODO Auto-generated method stub
95
ContentMetadata cm2 = new ContentMetadata() {
98
public String getValue() {
99
// TODO Auto-generated method stub
106
//create the MetaDataObjects for the content
107
//ContentMetadataFactory.newInstance();
109
//myCms.getContentManager().share(new File("image.png"), "", "", data);
110
myCms.getContentManager().share(new File("image.png"), "", "", "");
113
} catch (IOException ex) {
115
System.out.println("Share command failed.");
116
ex.printStackTrace();
120
private void launchJXTA() {
121
displayArea.append("Launching Peer into JXTA Network...\n");
123
//netPeerGroup = PeerGroupFactory.newNetPeerGroup();
124
NetworkManager manager = new net.jxta.platform.NetworkManager(
125
NetworkManager.ConfigMode.ADHOC, "CMS", new File(
126
new File(".cache"), "CMS").toURI());
127
NetworkConfigurator netConfig = manager.getConfigurator();
128
manager.startNetwork();
129
this.netPeerGroup = manager.getNetPeerGroup();
130
} catch (Exception e) {
131
System.out.println("Unable to create PeerGroup - Failure");
137
private void getServices() {
138
displayArea.append("Getting Services...\n");
139
myDiscoveryService = netPeerGroup.getDiscoveryService();
140
myPipeService = netPeerGroup.getPipeService();
144
myCms.init(netPeerGroup, null, null);
147
if(myCms.startApp(new File("client")) == -1) {
148
System.out.println("CMS initialization failed");
151
} catch (Exception e) {
152
System.out.println("CMS init failure");
158
public interface ContentListener {
159
public void finishedRetrieve(String url);
162
class GetRequestor extends GetContentRequest {
163
private ContentAdvertisement searchResult = null;
164
private String url = null;
165
private ContentListener listener;
167
public GetRequestor( PeerGroup pg, ContentAdvertisement res, File tmpFile, ContentListener listener )
168
throws InvocationTargetException {
169
super( pg, res, tmpFile );
172
//CMS Adv has ContentMetadata objects ...
173
url = tmpFile.getAbsolutePath();
174
this.listener = listener;
177
public ContentAdvertisement getContentAdvertisement() {
181
public void notifyDone() {
182
listener.finishedRetrieve( url );
186
class ListRequestor extends CachedListContentRequest {
188
boolean gotOne = false;
189
public ListRequestor( PeerGroup group, String inSubStr ) {
190
super( group, inSubStr );
193
public void notifyMoreResults() {
194
System.out.println("Search Done");
196
ContentAdvertisement[] result = myListRequestor.getResults();
197
if ( result != null ) {
198
displayArea.append("Length = " + result.length + "\n");
199
for (int i=0;i<result.length;i++) {
200
ContentAdvertisement myAdv = result[i];
201
displayArea.append(myAdv.getName() + "\n");
204
displayArea.append("Starting Download\n");
205
File tmpFile = new File( "file" + myAdv.getName() );
206
ContentListener myListener = new ContentListener() {
207
public void finishedRetrieve(String url) {
208
displayArea.append("File Download Finished\n");
213
GetRequestor request = new GetRequestor( netPeerGroup, result[i], tmpFile, myListener );
214
} catch ( InvocationTargetException e ) {
222
System.out.println("No results");
226
private void sendData() {
228
myListRequestor = new ListRequestor( netPeerGroup, "png" );
229
myListRequestor.activateRequest();