File: BooShellModel.boo

package info (click to toggle)
monodevelop-boo 2.4-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 528 kB
  • ctags: 57
  • sloc: makefile: 246; xml: 193; sh: 160
file content (164 lines) | stat: -rw-r--r-- 3,673 bytes parent folder | download
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
152
153
154
155
156
157
158
159
160
161
162
163
164
#region license
// Copyright (c) 2005, Peter Johanson (latexer@gentoo.org)
// All rights reserved.
//
// BooBinding is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
// 
// BooBinding is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with BooBinding; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#endregion

namespace BooBinding.Gui

import System
import System.Collections
import System.IO
import System.Threading

import BooBinding.Properties
import BooBinding.BooShell

class BooShellModel(IShellModel):
	private _props = BooShellProperties()

	private _commandQueue = Queue()
	private _outputQueue = Queue()

	private _outputHandler as callable
	
	private _thread as System.Threading.Thread

	private _booShell as BooShell

	MimeType as string:
		get:
			return "text/x-boo"

	LanguageName as string:
		get:
			return "Boo"

	MimeTypeExtension as string:
		get:
			return "boo"
	
	Properties as ShellProperties:
		get:
			return _props
	
	References as IList:
		get:	
			refs as IList
			try:
				refs = _booShell.References
			except:
				pass
			return refs
	
	def constructor():
		getRemoteShellObject()
		try:
			_booShell.Run ()
		except:
			pass

	private def getRemoteShellObject ():
		path as string  = Path.Combine (Path.GetDirectoryName (typeof(BooShellModel).Assembly.Location), "BooShell.dll")
		_booShell = MonoDevelop.Core.Runtime.ProcessService.CreateExternalProcessObject (path, "BooBinding.BooShell.BooShell", false)
		if _booShell is null:
			raise Exception ("Unable to instantiate remote BooShell object")
	
	def Reset () as bool:
		try:
			_booShell.Reset()
		except:
			return false
		return true
	
	def LoadAssembly (assemblyPath as string) as bool:
		try:
			_booShell.LoadAssembly (assemblyPath)
		except:
			return false
		return true
	
	def GetOutput () as (string):
		ret as (string)
		lock _outputQueue:
			if _outputQueue.Count > 0:
				ret = array (string, _outputQueue.Count)
				_outputQueue.CopyTo (ret, 0)
				_outputQueue.Clear ()

		return ret

		
	def QueueInput (line as string):
		try:
			Monitor.Enter (_commandQueue)
			_commandQueue.Enqueue (line)
			Monitor.Pulse (_commandQueue)
		ensure:
			Monitor.Exit (_commandQueue)

	def ThreadRun ():
		while true:
			com as string
			try:
				Monitor.Enter (_commandQueue)
				if _commandQueue.Count == 0:
					Monitor.Wait (_commandQueue)

				com = _commandQueue.Dequeue ()


				if com is not null:
					try:
						_booShell.QueueInput (com)
						lines = _booShell.GetOutput ()
					except:
						pass
					if lines is not null:
						EnqueueOutput (lines)
					com = null
					lock _outputQueue:
						if _outputHandler is not null:
							_outputHandler ()

			except:
				return
			ensure:
				Monitor.Exit (_commandQueue)

	
	def Run ():
		_thread = System.Threading.Thread (ThreadRun)
		_thread.Start ()
	
	def RegisterOutputHandler (handler as callable):
		_outputHandler = handler
	
	def EnqueueOutput (lines as (string)):
		lock _outputQueue:
			for line in lines:
				_outputQueue.Enqueue (line)
	
	def Dispose ():
		_thread.Abort ()
		try:
			_booShell.Dispose ()
		except:
			pass
		
	def print (obj):
		lock _outputQueue:
			_outputQueue.Enqueue (obj)