Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 304c9be517231174b5e6c3d7e98a8d4d827f4cec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Copyright (c) 2004 - 2012 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 */
package org.eclipse.net4j.buddies.internal.server.protocol;

import org.eclipse.net4j.buddies.common.IAccount;
import org.eclipse.net4j.buddies.common.IBuddy;
import org.eclipse.net4j.buddies.common.ISession;
import org.eclipse.net4j.buddies.internal.common.protocol.ProtocolConstants;
import org.eclipse.net4j.buddies.internal.common.protocol.ProtocolUtil;
import org.eclipse.net4j.buddies.internal.server.bundle.OM;
import org.eclipse.net4j.buddies.internal.server.messages.Messages;
import org.eclipse.net4j.buddies.server.IBuddyAdmin;
import org.eclipse.net4j.signal.IndicationWithResponse;
import org.eclipse.net4j.util.io.ExtendedDataInputStream;
import org.eclipse.net4j.util.io.ExtendedDataOutputStream;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Eike Stepper
 */
public class OpenSessionIndication extends IndicationWithResponse
{
  private IAccount account;

  private IBuddy[] buddies;

  /**
   * @since 2.0
   */
  public OpenSessionIndication(BuddiesServerProtocol protocol)
  {
    super(protocol, ProtocolConstants.SIGNAL_OPEN_SESSION);
  }

  @Override
  protected void indicating(ExtendedDataInputStream in) throws Exception
  {
    String userID = in.readString();
    String password = in.readString();
    int size = in.readInt();
    String[] facilityTypes = new String[size];
    for (int i = 0; i < size; i++)
    {
      facilityTypes[i] = in.readString();
    }

    synchronized (IBuddyAdmin.INSTANCE)
    {
      buddies = IBuddyAdmin.INSTANCE.getBuddies();
      ISession session = IBuddyAdmin.INSTANCE.openSession(getProtocol().getChannel(), userID, password, facilityTypes);
      if (session != null)
      {
        account = session.getSelf().getAccount();
      }
      else
      {
        OM.LOG.info(MessageFormat.format(Messages.getString("OpenSessionIndication.0"), userID)); //$NON-NLS-1$
      }
    }
  }

  @Override
  protected void responding(ExtendedDataOutputStream out) throws Exception
  {
    ProtocolUtil.writeAccount(out, account);
    if (account != null)
    {
      List<BuddiesServerProtocol> protocols = new ArrayList<BuddiesServerProtocol>();
      out.writeInt(buddies.length);
      for (IBuddy buddy : buddies)
      {
        out.writeString(buddy.getUserID());
        ISession buddySession = IBuddyAdmin.INSTANCE.getSession(buddy);
        if (buddySession != null)
        {
          protocols.add((BuddiesServerProtocol)buddySession.getProtocol());
        }
      }

      for (BuddiesServerProtocol protocol : protocols)
      {
        try
        {
          new BuddyAddedNotification(protocol, account.getUserID()).sendAsync();
        }
        catch (Exception ex)
        {
          OM.LOG.error(ex);
        }
      }
    }
  }
}

Back to the top