~kadamandiya-dev/+junk/kadamandiya-dev

« back to all changes in this revision

Viewing changes to gui/Kadamandiya/src/com/test/CMS_App.java

  • Committer: Dileepa Jayakody
  • Date: 2011-09-14 18:53:41 UTC
  • Revision ID: dileepajayakody@gmail.com-20110914185341-5pc3zzuctm9ecftt
added files for feedback logic

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
package com.test;
2
 
 
3
 
import java.io.*;
4
 
import java.awt.*;
5
 
import java.awt.event.*;
6
 
import javax.swing.*;
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.*;
12
 
import net.jxta.id.*;
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;
19
 
import java.net.URL;
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;
27
 
 
28
 
public class CMS_App extends JFrame {
29
 
 
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;
37
 
 
38
 
    private final static        MimeMediaType XMLMIMETYPE = new MimeMediaType("text/xml"); 
39
 
 
40
 
    public static void main(String args[]) {
41
 
        CMS_App myapp = new CMS_App();
42
 
 
43
 
        myapp.addWindowListener (
44
 
            new WindowAdapter() {
45
 
            public void windowClosing(WindowEvent e) {
46
 
              System.exit(0);
47
 
            }
48
 
          }
49
 
        );
50
 
 
51
 
        myapp.launchJXTA();
52
 
        myapp.getServices();
53
 
        myapp.run();
54
 
    }
55
 
 
56
 
 
57
 
    public CMS_App() { 
58
 
      super("client");
59
 
 
60
 
      Container c = getContentPane();
61
 
      
62
 
      sendButton = new JButton("Send Search");
63
 
      sendButton.addActionListener(
64
 
        new ActionListener() {
65
 
          public void actionPerformed(ActionEvent e) {
66
 
            sendData();
67
 
          } 
68
 
        }
69
 
      );
70
 
      c.add(sendButton, BorderLayout.NORTH);
71
 
 
72
 
      displayArea = new JTextArea();
73
 
      c.add(new JScrollPane(displayArea), BorderLayout.CENTER);
74
 
 
75
 
      setSize(300,150);
76
 
      show();
77
 
    }
78
 
 
79
 
    public void run() {
80
 
      displayArea.append("Click on Button to send data...\n");
81
 
 
82
 
        try {
83
 
         //myCms.getContentManager().share(new File("image.png"));
84
 
                //myCms.getContentManager().share(new File("test.txt"));
85
 
         ContentMetadata[] data = new ContentMetadata[2];
86
 
         
87
 
         ContentMetadata cm1 = new ContentMetadata() {
88
 
                
89
 
                @Override
90
 
                public String getValue() {
91
 
                        // TODO Auto-generated method stub
92
 
                        return "test1";
93
 
                }
94
 
        };
95
 
ContentMetadata cm2 = new ContentMetadata() {
96
 
                
97
 
                @Override
98
 
                public String getValue() {
99
 
                        // TODO Auto-generated method stub
100
 
                        return "test2";
101
 
                }
102
 
        };
103
 
        data[0] = cm1;
104
 
        data[1] = cm2;
105
 
        
106
 
        //create the MetaDataObjects for the content
107
 
        //ContentMetadataFactory.newInstance();
108
 
        
109
 
        //myCms.getContentManager().share(new File("image.png"), "", "", data);
110
 
         myCms.getContentManager().share(new File("image.png"), "", "", "");
111
 
 
112
 
                
113
 
        } catch (IOException ex) {
114
 
                
115
 
          System.out.println("Share command failed.");
116
 
          ex.printStackTrace();
117
 
        }       
118
 
    }
119
 
 
120
 
    private void launchJXTA() {
121
 
        displayArea.append("Launching Peer into JXTA Network...\n");
122
 
        try {
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");
132
 
            e.printStackTrace();
133
 
            System.exit(1);
134
 
        }  
135
 
    }
136
 
 
137
 
    private void getServices() {
138
 
      displayArea.append("Getting Services...\n");
139
 
      myDiscoveryService = netPeerGroup.getDiscoveryService();
140
 
      myPipeService = netPeerGroup.getPipeService();
141
 
 
142
 
      try {
143
 
        myCms = new CMS();
144
 
        myCms.init(netPeerGroup, null, null);
145
 
        
146
 
                     
147
 
        if(myCms.startApp(new File("client")) == -1) {
148
 
           System.out.println("CMS initialization failed");
149
 
           System.exit(-1);
150
 
        }
151
 
      } catch (Exception e) {
152
 
        System.out.println("CMS init failure");
153
 
        e.printStackTrace();
154
 
        System.exit(-1);
155
 
      }
156
 
    }
157
 
 
158
 
    public interface ContentListener {
159
 
      public void finishedRetrieve(String url);
160
 
    }
161
 
 
162
 
    class GetRequestor extends GetContentRequest {
163
 
      private ContentAdvertisement searchResult = null;
164
 
      private String url = null;
165
 
      private ContentListener listener;
166
 
 
167
 
      public GetRequestor( PeerGroup pg, ContentAdvertisement res, File tmpFile, ContentListener listener )                                 
168
 
                                                   throws InvocationTargetException {
169
 
        super( pg, res, tmpFile );
170
 
        searchResult = res;
171
 
 
172
 
//CMS Adv has ContentMetadata objects ...
173
 
        url = tmpFile.getAbsolutePath();
174
 
        this.listener = listener;
175
 
      }
176
 
 
177
 
      public ContentAdvertisement getContentAdvertisement() {
178
 
        return searchResult;
179
 
      }
180
 
 
181
 
      public void notifyDone() {
182
 
        listener.finishedRetrieve( url );
183
 
      }
184
 
    }
185
 
 
186
 
    class ListRequestor extends CachedListContentRequest {
187
 
 
188
 
      boolean gotOne = false;
189
 
      public ListRequestor( PeerGroup group, String inSubStr ) {
190
 
        super( group, inSubStr );
191
 
      }
192
 
 
193
 
      public void notifyMoreResults() {
194
 
        System.out.println("Search Done");
195
 
 
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");
202
 
 
203
 
            if (!gotOne) {
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");
209
 
                 }
210
 
              };
211
 
 
212
 
              try {
213
 
                GetRequestor request = new GetRequestor( netPeerGroup, result[i], tmpFile, myListener );
214
 
              } catch ( InvocationTargetException e ) {
215
 
                        e.printStackTrace();
216
 
              }
217
 
              gotOne = true;
218
 
            }
219
 
          }
220
 
        }
221
 
        else
222
 
          System.out.println("No results");
223
 
      }
224
 
    }
225
 
  
226
 
    private void sendData() {
227
 
 
228
 
      myListRequestor =  new ListRequestor( netPeerGroup, "png" );
229
 
      myListRequestor.activateRequest();
230
 
    }
231
 
}
232