]> sjero.net Git - linphone/blob - p2pproxy/src/org/linphone/p2pproxy/core/P2pProxyAccountManagement.java
117fac451c98c8893461b08d789e8556ae10f291
[linphone] / p2pproxy / src / org / linphone / p2pproxy / core / P2pProxyAccountManagement.java
1 /*
2 p2pproxy
3 Copyright (C) 2007  Jehan Monnier ()
4
5 P2pProxyAccountManagement.java - .
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */
21 package org.linphone.p2pproxy.core;
22
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import net.jxta.discovery.DiscoveryService;
28 import net.jxta.document.Advertisement;
29 import net.jxta.document.AdvertisementFactory;
30 import net.jxta.id.ID;
31 import net.jxta.id.IDFactory;
32
33 import org.apache.log4j.Logger;
34 import org.linphone.p2pproxy.api.P2pProxyException;
35 import org.linphone.p2pproxy.api.P2pProxyUserNotFoundException;
36
37 import org.linphone.p2pproxy.api.P2pProxyUserAlreadyExistException;
38
39 public class P2pProxyAccountManagement implements ServiceProvider, P2pProxyAccountManagementMBean {
40    private final static Logger mLog = Logger.getLogger(P2pProxyAccountManagement.class);
41    protected final JxtaNetworkManager mJxtaNetworkManager;
42    
43    public P2pProxyAccountManagement() {
44       mJxtaNetworkManager = null;
45    }
46    /**
47     * @param jxtaNetworkManager
48     */
49    public P2pProxyAccountManagement(final JxtaNetworkManager jxtaNetworkManager) {
50       super();
51       mJxtaNetworkManager = jxtaNetworkManager;
52    }
53
54    public void start(long aTimeOut) throws P2pProxyException {
55      mLog.info("P2pProxyAccountManagementMBean started");
56    }
57
58    public void stop() {
59       mLog.info("P2pProxyAccountManagementMBean stopped");
60    }
61
62    public void createAccount(String aUserName) throws P2pProxyException, P2pProxyUserAlreadyExistException {
63       // 1 check if already exist
64       if (isValidAccount(aUserName) == false) {
65          
66          // 2 creates and remote publish
67          P2pUserProfileAdvertisement lP2pUserProfileAdvertisement = (P2pUserProfileAdvertisement) AdvertisementFactory.newAdvertisement(P2pUserProfileAdvertisement.getAdvertisementType());
68          
69          lP2pUserProfileAdvertisement.setID(IDFactory.newCodatID(mJxtaNetworkManager.getPeerGroup().getPeerGroupID()));
70          lP2pUserProfileAdvertisement.setUserName(aUserName);
71          try {
72             mJxtaNetworkManager.getPeerGroup().getDiscoveryService().publish(lP2pUserProfileAdvertisement,DiscoveryService.INFINITE_LIFETIME,DiscoveryService.DEFAULT_EXPIRATION);
73          } catch (IOException e1) {
74             throw new P2pProxyException(e1);
75          }
76          mJxtaNetworkManager.getPeerGroup().getDiscoveryService().remotePublish(lP2pUserProfileAdvertisement, DiscoveryService.NO_EXPIRATION);
77          mLog.debug("publishing P2pUserProfileAdvertisement :"+lP2pUserProfileAdvertisement);
78       } else {
79          throw new P2pProxyUserAlreadyExistException(aUserName);
80       }
81       
82    }
83
84    public void deleteAccount(String aUserName) throws P2pProxyException, P2pProxyUserNotFoundException {
85       // 1 check if already exist
86       try {
87          List<? extends Advertisement> lAdvertisements = mJxtaNetworkManager.getAdvertisementList(null, P2pUserProfileAdvertisement.USER_NAME_TAG, aUserName, true);
88          if (lAdvertisements.isEmpty()) throw new P2pProxyUserNotFoundException (aUserName +" not found");
89          
90          // 2 local and remote publish to 0
91          mJxtaNetworkManager.getPeerGroup().getDiscoveryService().flushAdvertisement(lAdvertisements.get(0));
92          mJxtaNetworkManager.getPeerGroup().getDiscoveryService().remotePublish(lAdvertisements.get(0), 0);
93  
94       } catch (P2pProxyAdvertisementNotFoundException e) {
95          throw e;
96      } catch (InterruptedException e) {
97          throw new P2pProxyException(e);
98       } catch (IOException e) {
99          throw new P2pProxyException(e);
100       }
101       
102    }
103
104    public boolean isValidAccount(String aUserName) throws P2pProxyException {
105       boolean lStatus = false;
106       try {
107          if (mJxtaNetworkManager.getAdvertisementList(null, P2pUserProfileAdvertisement.USER_NAME_TAG, aUserName, true).size() >0 ) {
108             lStatus = true;;
109          }
110       } catch (P2pProxyAdvertisementNotFoundException e) {
111          lStatus = false;
112       }catch (Exception e) {
113          mLog.error("cannot check acount",e);
114       }
115       return lStatus;
116    }
117
118  
119
120 }