Пример #1
0
 /// <summary>Creates a new <code>Message</code>.</summary>
 /// <param name="channel"><code>Channel</code> to which this <code>Message</code>
 /// belongs.</param>
 /// <param name="msgno">Message number of the BEEP message.</param>
 /// <param name="data"><code>InputDataStream</code> containing the payload of the
 /// message.</param>
 /// <param name="messageType">Message type of the BEEP message.</param>
 /// <seealso cref="InputDataStream"></seealso>
 /// <seealso cref="Channel"></seealso>
 internal MessageImpl(ChannelImpl channel, int msgno, InputDataStream data, core.MessageType messageType)
 {
     this.channel = channel;
     this.msgno = msgno;
     this.ansno = -1;
     this.data = data;
     this.messageType = messageType;
 }
Пример #2
0
 /// <summary>Creates a BEEP message of type ANS</summary>
 /// <param name="channel"><code>Channel</code> to which the message belongs.</param>
 /// <param name="msgno">Message number of the message.</param>
 /// <param name="ansno"></param>
 /// <param name="data"><code>InputDataStream</code> contains the payload of the
 /// message.</param>
 /// <seealso cref="Channel"></seealso>
 /// <seealso cref="InputDataStream"></seealso>
 internal MessageImpl(ChannelImpl channel, int msgno, int ansno, InputDataStream data)
     : this(channel, msgno, data, core.MessageType.MESSAGE_TYPE_ANS)
 {
     this.ansno = ansno;
 }
Пример #3
0
 internal Frame(core.MessageType messageType, ChannelImpl channel, int msgno, bool last, long seqno, int size, int ansno)
 {
     this.messageType = messageType;
     this.channel = channel;
     this.msgno = msgno;
     this.last = last;
     this.seqno = seqno;
     this.size = size;
     this.ansno = ansno;
 }
Пример #4
0
 /// <summary>Creates a <code>InputDataStream</code>.
 /// For use with in <code>Channel</code>.</summary>
 /// <param name="channel">Is notified as BufferSegments are read this allows the
 /// Channel to update the receive window.</param>
 internal InputDataStream(ChannelImpl channel)
 {
     this.channel = channel;
 }
Пример #5
0
		/// <summary>The Initiator Oriented close channel call...but this one is not an
		/// external call, it's invoked from Channel.close();</summary>
		/// <param name="channel"></param>
		/// <param name="code"></param>
		/// <param name="xmlLang"></param>
		/// <exception cref="BEEPException" />
		internal virtual void  closeChannel(ChannelImpl channel, BEEPStatusCode code, string xmlLang)
		{
			
			// Construct Message
			System.Text.StringBuilder closeBuffer = new System.Text.StringBuilder();
			
			closeBuffer.Append("<close number='");
			closeBuffer.Append(channel.NumberAsString);
			closeBuffer.Append("' code='");
			closeBuffer.Append(code);
			
			if ((object) xmlLang != null)
			{
				closeBuffer.Append("' xml:lang='");
				closeBuffer.Append(xmlLang);
			}
			
			closeBuffer.Append("' />");
			
			// Lock necessary because we have to know the msgNo
			// before we send the message, in order to be able
			// to associate the reply with this start request
			CloseReplyListener reply = new CloseReplyListener(this, channel);
			lock (reply)
			{
				OutputDataStream ds = new ByteOutputDataStream(MimeHeaders.BEEP_XML_CONTENT_TYPE, System.Text.Encoding.ASCII.GetBytes(closeBuffer.ToString()));
				
				this.zero.sendMSG(ds, reply);
				try
				{
					System.Threading.Monitor.Wait(reply);
				}
				catch (System.Threading.ThreadInterruptedException e)
				{
					log.error("Error waiting for reply", e);
					throw new BEEPException("Interrupted waiting for reply");
				}
			}
			
			// check the channel state and return the appropriate exception
			if (reply.isError())
			{
				throw reply.getError();
			}
			
			if (channel.getState() != core.ChannelState.STATE_CLOSED)
			{
				throw new BEEPException("Error channel state (" + channel.getState() + ")");
			}
			
			fireChannelClosed(channel);
		}
Пример #6
0
		internal virtual void  sendProfile(string uri, string datum, ChannelImpl ch)
		{
			
			// Send the profile
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			
			sb.Append("<profile uri='");
			sb.Append(uri);
			
			if ((object) datum != null)
			{
				sb.Append("'><![CDATA[");
				sb.Append(datum);
				sb.Append("]]></profile>");
			}
			else
			{
				sb.Append("' />");
			}
			
			OutputDataStream ds = new ByteOutputDataStream(MimeHeaders.BEEP_XML_CONTENT_TYPE, System.Text.Encoding.ASCII.GetBytes(sb.ToString()));
			
			// Store the Channel
			channels[ch.NumberAsString] = ch;
			((IMessageMSG) zero.AppData).sendRPY(ds);
		}
Пример #7
0
		internal virtual IChannel startChannelRequest(System.Collections.ICollection profiles, IRequestHandler handler, bool tuning)
		{
			string channelNumber = NextFreeChannelNumber;
			
			// create the message in a buffer and send it
			System.Text.StringBuilder startBuffer = new System.Text.StringBuilder();
			
			startBuffer.Append("<start number='");
			startBuffer.Append(channelNumber);
			if ((object) serverName != null && !sentServerName)
			{
				startBuffer.Append("' serverName='");
				startBuffer.Append(serverName);
			}
			startBuffer.Append("'>");
			
			foreach(StartChannelProfile p in profiles)
			{
				// @todo maybe we should check these against peerSupportedProfiles
				startBuffer.Append("<profile uri='");
				startBuffer.Append(p.uri);
				startBuffer.Append("' ");
				
				if ((object) p.data == null)
				{
					startBuffer.Append(" />");
				}
				else
				{
					if (p.base64Encoding)
					{
						startBuffer.Append("encoding='base64' ");
					}
					
					startBuffer.Append("><![CDATA[");
					startBuffer.Append(p.data);
					startBuffer.Append("]]></profile>");
				}
			}
			
			startBuffer.Append("</start>");
			
			// @todo handle the data element
			// Create a channel
			ChannelImpl ch = new ChannelImpl(null, channelNumber, handler, false, this);
			
			// Make a message
			OutputDataStream ds = new ByteOutputDataStream(MimeHeaders.BEEP_XML_CONTENT_TYPE, System.Text.Encoding.ASCII.GetBytes(startBuffer.ToString()));
			
			if (tuning)
			{
				this.changeState(core.SessionState.SESSION_STATE_TUNING_PENDING);
				this.changeState(core.SessionState.SESSION_STATE_TUNING);
				this.zero.setState(core.ChannelState.STATE_TUNING);
			}
			
			// Tell Channel Zero to start us up
			StartReplyListener reply = new StartReplyListener(this, ch);
			lock (reply)
			{
				this.zero.sendMSG(ds, reply);
				try
				{
					System.Threading.Monitor.Wait(reply);
				}
				catch (System.Threading.ThreadInterruptedException e)
				{
					log.error("Interrupted waiting for reply", e);
					throw new BEEPException("Interrupted waiting for reply");
				}
			}
			
			// check the channel state and return the appropriate exception
			if (reply.isError())
			{
				throw reply.getError();
			}
			
			if (ch.getState() != core.ChannelState.STATE_ACTIVE)
			{
				throw new BEEPException("Error channel state (" + ch.getState() + ")");
			}
			
			if (tuning)
			{
				ch.setState(core.ChannelState.STATE_TUNING);
			}
			
			if ((object) serverName != null)
			{
				sentServerName = true;
			}
			
			fireChannelStarted(ch);
			return ch;
		}
Пример #8
0
		/// <summary>This method is used to terminate the session when there is an
		/// non-recoverable error in the BEEP protocol (framing error, etc.).</summary>
		/// <param name="reason"></param>
		public virtual void  terminate(string reason)
		{
			log.error(reason);
			
			try
			{
				this.changeState(core.SessionState.SESSION_STATE_ABORTED);
			}
			catch (BEEPException)
			{				
				// Ignore this since we are terminating anyway.
			}
			
			this.disableIO();
			channels.Clear();
			
			zero = null;
			
			fireSessionTerminated();
		}
Пример #9
0
		/// <summary>Closes the <code>Session</code> gracefully. The profiles for
		/// the open channels on the session may veto the close request.</summary>
		/// <exception cref="BEEPException" />
		public virtual void  close()
		{
			if (log.isDebugEnabled())
			{
				log.debug("Closing Session with " + channels.Count + " channels");
			}
			
			changeState(core.SessionState.SESSION_STATE_CLOSE_PENDING);
			
			System.Collections.IEnumerator i = channels.Values.GetEnumerator();
			
			//UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1073_javautilIteratorhasNext"'
			while (i.MoveNext())
			{
				//UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1073_javautilIteratornext"'
				ChannelImpl ch = (ChannelImpl) i.Current;
				
				// if this channel is not zero, call the channel's scl
				if (ch.Number == 0)
				{
					continue;
				}
				
				IStartChannelListener scl = profileRegistry.getStartChannelListener(this.tuningProperties, ch.getProfile());
				
				if (scl == null)
				{
					continue;
				}
				
				// check locally first to see if it is ok to close the channel
				try
				{
					scl.CloseChannel(ch);
				}
				catch (CloseChannelException cce)
				{
					changeState(core.SessionState.SESSION_STATE_ACTIVE);
					// @todo rollback notification
					throw new BEEPException("Close Session rejected by local " + "channel " + ch.getProfile(),cce);
				}
			}
			
			changeState(core.SessionState.SESSION_STATE_CLOSING);
			
			try
			{
				// check with the peer to see if it is ok to close the channel
				zero.close();
			}
			catch (BEEPError e)
			{
				changeState(core.SessionState.SESSION_STATE_ACTIVE);
				throw e;
			}
			catch (BEEPException e)
			{
				//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1043"'
				terminate(e.Message);
				log.error("Error sending close", e);
				throw e;
			}
			
			this.disableIO();
			// @todo close the socket
			
			channels.Clear();
			zero = null;
			
			this.changeState(core.SessionState.SESSION_STATE_CLOSED);
			fireSessionTerminated();
		}
Пример #10
0
		/// <summary>A reentrant version of init() that doesn't block the
		/// first I/O thread waiting on a greeting when it should die
		/// and go away.</summary>
		/// <exception cref="BEEPException" />
		protected internal virtual void  tuningInit()
		{
			log.debug("Session.tuningInit");
			
			this.peerSupportedProfiles = null;
			
			GreetingListener greetingListener = new GreetingListener(this);
			
#if MESSAGELISTENER
			zero = ChannelImpl.createChannelZero(this, greetingListener, new ChannelImpl.MessageListenerAdapter(new ChannelZeroListener(this)));
#else
			zero = ChannelImpl.createChannelZero(this, greetingListener, new ChannelZeroListener(this));
#endif
			channels[CHANNEL_ZERO] = zero;
			
			// send greeting
			sendGreeting();
			changeState(core.SessionState.SESSION_STATE_GREETING_SENT);
			
			// start our listening thread we can now receive a greeting
			this.enableIO();
		}
Пример #11
0
		/// <summary>Initializes the <code>Session</code>.  Initializes Channel Zero and its
		/// listener. Sends a greeting and waits for corresponding greeting.</summary>
		/// <exception cref="BEEPException" />
		protected internal virtual void  init()
		{
			this.peerSupportedProfiles = null;
			
			GreetingListener greetingListener = new GreetingListener(this);
			
#if MESSAGELISTENER
			zero = ChannelImpl.createChannelZero(this, greetingListener, new ChannelImpl.MessageListenerAdapter(new ChannelZeroListener(this)));
#else
			zero = ChannelImpl.createChannelZero(this, greetingListener, new ChannelZeroListener(this));
#endif
			
			channels[CHANNEL_ZERO] = zero;
			
			// send greeting
			sendGreeting();
			changeState(core.SessionState.SESSION_STATE_GREETING_SENT);
			
			// start our listening thread we can now receive a greeting
			this.enableIO();
			
			// blocks until greeting is received or MAX_GREETING_WAIT is reached
			int waitCount = 0;
			
			while ((state < core.SessionState.SESSION_STATE_ACTIVE) && (waitCount < MAX_START_CHANNEL_WAIT))
			{
				try
				{
					lock (greetingListener)
					{
						
						//zero.wait(MAX_START_CHANNEL_INTERVAL);
						System.Threading.Monitor.Wait(greetingListener, TimeSpan.FromMilliseconds(MAX_START_CHANNEL_INTERVAL));
						
						waitCount += MAX_START_CHANNEL_INTERVAL;
					}
				}
				catch (System.Threading.ThreadInterruptedException)
				{
					waitCount += MAX_START_CHANNEL_INTERVAL;
				}
			}
			
			// check the channel state and return the appropriate exception
			if (state != core.SessionState.SESSION_STATE_ACTIVE)
			{
				throw new BEEPException("Greeting exchange failed");
			}
		}
Пример #12
0
			internal CloseReplyListener(SessionImpl enclosingInstance, ChannelImpl channel)
			{
				InitBlock(enclosingInstance);
				this.channel = channel;
				this.error = null;
			}
Пример #13
0
		/// <summary>Listener oriented Start Channel call, a call here means that
		/// we've received a start channel request over the wire.</summary>
		//UPGRADE_ISSUE: Class hierarchy differences between ''java.util.Collection'' and ''SupportClass.CollectionSupport'' may cause compilation errors. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1186"'
		private void  processStartChannel(string channelNumber, System.Collections.ICollection profiles)
		{
			IStartChannelListener scl;
			ChannelImpl ch = null;
			System.Collections.IEnumerator i = profiles.GetEnumerator();
			
			//UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1073_javautilIteratorhasNext"'
			while (i.MoveNext())
			{
				//UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1073_javautilIteratornext"'
				StartChannelProfile p = (StartChannelProfile) i.Current;
				
				scl = profileRegistry.getStartChannelListener(this.tuningProperties, p.uri);
				
				if (scl == null)
				{
					continue;
				}
				
				ch = new ChannelImpl(p.uri, channelNumber, this);
				
				try
				{
					string encoding = p.base64Encoding?"base64":"none";
					
					scl.StartChannel(ch, encoding, p.data);
				}
				catch (StartChannelException e)
				{
					this.enableIO();
					
					try
					{
						((IMessageMSG) zero.AppData).sendERR(e);
					}
					catch (BEEPException)
					{
						terminate("Error sending ERR response to start channel");
					}
					
					return ;
				}
				
				if ((object) p.data != null && (object) ch.StartData == null)
				{
					byte[] data;
					if (p.base64Encoding)
					{
						try
						{
							data = Convert.FromBase64String(p.data);
						}
						catch (System.FormatException)
						{
							ch.abort();
							this.enableIO();
							throw new BEEPError(BEEPStatusCode.REQUESTED_ACTION_ABORTED, "Error parsing piggybacked data.");
						}
					}
					else
					{
						data = System.Text.Encoding.UTF8.GetBytes(p.data);
					}
					
					PiggybackedMSG msg = new PiggybackedMSG(ch, data, p.base64Encoding);
					
					ch.setState(core.ChannelState.STATE_STARTING);
					
					try
					{
						ch.addPiggybackedMSG(msg);
					}
					catch (BEEPException e)
					{
						//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1043"'
						terminate("Error sending profile. " + e.Message);
						
						return ;
					}
				}
				else
				{
					try
					{
						sendProfile(p.uri, ch.StartData, ch);
						ch.setState(core.ChannelState.STATE_ACTIVE);
					}
					catch (BEEPException e)
					{
						//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1043"'
						terminate("Error sending profile. " + e.Message);
						
						return ;
					}
					
					fireChannelStarted(ch);
					
					if ((object) p.data == null && ch.getState() != core.ChannelState.STATE_TUNING)
					{
						this.enableIO();
					}
				}
				
				return ;
			}
			
			this.enableIO();
			
			try
			{
				((IMessageMSG) zero.AppData).sendERR(BEEPStatusCode.REQUESTED_ACTION_NOT_TAKEN2, "all requested profiles are unsupported");
			}
			catch (System.Exception x)
			{
				//UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="jlca1043"'
				terminate("Error sending error. " + x.Message);
			}
		}