Skip to main content
summaryrefslogtreecommitdiffstats
blob: 14ce6cd7a6baae95ab311595304b26a9b883ad42 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (c) 2009-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.emf.internal.cdo.session.remote;

import org.eclipse.emf.cdo.session.remote.CDORemoteSession;
import org.eclipse.emf.cdo.session.remote.CDORemoteSessionMessage;

import org.eclipse.emf.spi.cdo.InternalCDORemoteSession;
import org.eclipse.emf.spi.cdo.InternalCDORemoteSessionManager;

import java.text.MessageFormat;

/**
 * @author Eike Stepper
 */
public class CDORemoteSessionImpl implements InternalCDORemoteSession
{
  private InternalCDORemoteSessionManager manager;

  private int sessionID;

  private String userID;

  private boolean subscribed;

  public CDORemoteSessionImpl(InternalCDORemoteSessionManager manager, int sessionID, String userID)
  {
    this.manager = manager;
    this.sessionID = sessionID;
    this.userID = userID;
  }

  public InternalCDORemoteSessionManager getManager()
  {
    return manager;
  }

  public int getSessionID()
  {
    return sessionID;
  }

  public String getUserID()
  {
    return userID;
  }

  public boolean isSubscribed()
  {
    return subscribed;
  }

  public void setSubscribed(boolean subscribed)
  {
    this.subscribed = subscribed;
  }

  public boolean sendMessage(CDORemoteSessionMessage message)
  {
    if (!isSubscribed())
    {
      return false;
    }

    return manager.sendMessage(message, this).size() == 1;
  }

  public int compareTo(CDORemoteSession obj)
  {
    int result = userID.compareTo(obj.getUserID());
    if (result == 0)
    {
      result = Integer.valueOf(sessionID).compareTo(obj.getSessionID());
    }

    return result;
  }

  @Override
  public boolean equals(Object obj)
  {
    if (obj == this)
    {
      return true;
    }

    if (obj instanceof CDORemoteSession)
    {
      CDORemoteSession that = (CDORemoteSession)obj;
      return manager == that.getManager() && sessionID == that.getSessionID();
    }

    return false;
  }

  @Override
  public int hashCode()
  {
    return manager.hashCode() ^ sessionID;
  }

  @Override
  public String toString()
  {
    String repo = manager.getLocalSession().getRepositoryInfo().getName();
    String user = userID == null ? repo : userID + "@" + repo;
    return MessageFormat.format("{0} ({1})", user, sessionID); //$NON-NLS-1$
  }
}

Back to the top