Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5c759b472129f44f8a61c35f04af3576666fb19e (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 * Copyright (c) 2006-2012 Eike Stepper (Loehne, 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.tests.signal;

import org.eclipse.net4j.connector.IConnector;
import org.eclipse.net4j.protocol.IProtocol2;
import org.eclipse.net4j.signal.SignalProtocol;
import org.eclipse.net4j.signal.SignalReactor;
import org.eclipse.net4j.util.factory.ProductCreationException;

import org.eclipse.spi.net4j.ServerProtocolFactory;

import java.io.IOException;
import java.rmi.AlreadyBoundException;

/**
 * @author Eike Stepper
 */
public class TestSignalProtocol extends SignalProtocol<Object>
{
  public static final String PROTOCOL_NAME = "signal.protocol"; //$NON-NLS-1$

  public static final short SIGNAL_INT = 1;

  public static final short SIGNAL_INT_FAIL = 2;

  public static final short SIGNAL_ARRAY = 3;

  public static final short SIGNAL_STRING = 4;

  public static final short SIGNAL_ASYNC = 5;

  public static final short SIGNAL_EXCEPTION = 6;

  public static final String SIMULATED_EXCEPTION = "Simulated exception"; //$NON-NLS-1$

  private int version = super.getVersion();

  public TestSignalProtocol(IConnector connector, int version)
  {
    this();
    this.version = version;
    open(connector);
  }

  public TestSignalProtocol(IConnector connector)
  {
    this();
    open(connector);
  }

  public TestSignalProtocol()
  {
    super(PROTOCOL_NAME);
  }

  @Override
  public int getVersion()
  {
    return version;
  }

  public void setVersion(int version)
  {
    this.version = version;
  }

  @Override
  protected SignalReactor createSignalReactor(short signalID)
  {
    switch (signalID)
    {
    case SIGNAL_INT:
      return new IntIndication(this);

    case SIGNAL_INT_FAIL:
      return new IntFailIndication(this);

    case SIGNAL_ARRAY:
      return new ArrayIndication(this);

    case SIGNAL_STRING:
      return new StringIndication(this);

    case SIGNAL_ASYNC:
      return new AsyncIndication(this);

    case SIGNAL_EXCEPTION:
      return new ExceptionIndication(this);

    default:
      return super.createSignalReactor(signalID);
    }
  }

  public void throwException(boolean ioProblem) throws Exception
  {
    if (ioProblem)
    {
      throw new IOException(SIMULATED_EXCEPTION);
    }

    try
    {
      throwNestedException();
    }
    catch (Exception ex)
    {
      throw new ClassNotFoundException(SIMULATED_EXCEPTION, ex);
    }
  }

  public void throwNestedException() throws Exception
  {
    throw new AlreadyBoundException(SIMULATED_EXCEPTION);
  }

  /**
   * @author Eike Stepper
   */
  public static class Factory extends ServerProtocolFactory
  {
    private int version = IProtocol2.UNSPECIFIED_VERSION;

    public Factory(int version)
    {
      this();
      this.version = version;
    }

    public Factory()
    {
      super(PROTOCOL_NAME);
    }

    public TestSignalProtocol create(String description) throws ProductCreationException
    {
      TestSignalProtocol protocol = new TestSignalProtocol();
      protocol.setVersion(version);
      return protocol;
    }
  }
}

Back to the top