connection.py 4.05 KB
Newer Older
Bruno Laurencich's avatar
Bruno Laurencich committed
1 2 3 4 5 6
import bpy
import threading

if "mdns" in locals():
	import imp
	imp.reload(mdns)
7
	imp.reload(u)
Bruno Laurencich's avatar
Bruno Laurencich committed
8 9
else:
	from . import mdns
10
	from . import utils as u
Bruno Laurencich's avatar
Bruno Laurencich committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33




# =====================================================
# =         OPERATOR: GET THE NOTOCHORD IP            =
# =====================================================

class Chord_Get_Ip(bpy.types.Operator):
	"""Tries to get the ip of the notochord"""
	bl_idname = "chordata.get_ip"
	bl_label = "Chordata: get notochord ip"
	bl_options = {'REGISTER'}

	# -----------  PROPS  -----------
	counter = 0

	# -----------  MODAL  -----------

	def modal(self, context, event):
		if event.type in {'RIGHTMOUSE', 'ESC'}:
			self.report({"WARNING"}, "Connection canceled")
			self.cancel(context)
34 35 36 37
			with self.query_lock:
				if not self.found_ip:
					self.chord_global.state = u.possible_states()[0][0]

Bruno Laurencich's avatar
Bruno Laurencich committed
38 39 40 41 42
			return {'CANCELLED'}

		if event.type == 'TIMER':
			context.area.tag_redraw()

Bruno Laurencich's avatar
Bruno Laurencich committed
43
			if self.query_thread.is_alive():		
Bruno Laurencich's avatar
Bruno Laurencich committed
44
				self.chord_global.state = \
45
					u.possible_states()[self.counter%2+3][0]
Bruno Laurencich's avatar
Bruno Laurencich committed
46 47 48
			else:
				with self.query_lock:
					if self.found_ip:
49
						self.chord_global.state = u.possible_states()[1][0]
Bruno Laurencich's avatar
Bruno Laurencich committed
50
						self.report({"INFO"}, "IP found :" + self.found_ip)
Bruno Laurencich's avatar
Bruno Laurencich committed
51
						self.chord_global.notochord_ip = self.found_ip
Bruno Laurencich's avatar
Bruno Laurencich committed
52 53

					else:	
54
						self.chord_global.state = u.possible_states()[0][0]
Bruno Laurencich's avatar
Bruno Laurencich committed
55
						self.report({"WARNING"}, "IP NOT found")
Bruno Laurencich's avatar
Bruno Laurencich committed
56
					
Bruno Laurencich's avatar
Bruno Laurencich committed
57 58 59 60 61 62 63 64 65 66 67

				self.cancel(context)
				return {'FINISHED'}

			self.counter += 1
		
		return {'PASS_THROUGH'}

	# -----------  DO QUERY (thread handler)  -----------

	def do_query(self):
Bruno Laurencich's avatar
Bruno Laurencich committed
68 69 70 71 72 73 74 75 76 77
		self.report({"DEBUG"}, "Query Thread running.")
		ip = mdns.get_ip(self.chord_global.notochord_hostname+".local")
		max_attempts = self.chord_global.max_ip_discover_attempts
		if max_attempts == 0: max_attempts = 100 
		n = 1
		while not ip and n < max_attempts:
			ip = mdns.get_ip(self.chord_global.notochord_hostname+".local")
			if ip: break

			mdns.set_timeout(mdns.timeout * 2)
Bruno Laurencich's avatar
Bruno Laurencich committed
78 79 80 81 82
			
			with self.query_lock:
				self.report({"INFO"},\
					"ip not found, retrying in %d secs" %mdns.timeout)

Bruno Laurencich's avatar
Bruno Laurencich committed
83
			n += 1
Bruno Laurencich's avatar
Bruno Laurencich committed
84 85 86 87

		with self.query_lock:
			self.found_ip = ip

Bruno Laurencich's avatar
Bruno Laurencich committed
88
		self.report({"DEBUG"}, "Query Thread terminating..")
Bruno Laurencich's avatar
Bruno Laurencich committed
89 90
		return

91 92 93 94 95 96 97 98 99 100 101 102 103
	# -----------  INVOKE  -----------
		
	def find_armature(self, context):
		if not context.scene.chordata.target:
			context.scene.chordata.target = u.get_chordata_armature()

		if not context.scene.chordata.target:
			self.report({"ERROR"},"No Chordata Armature in the scene")
			return False

		return True


Bruno Laurencich's avatar
Bruno Laurencich committed
104 105 106
	# -----------  EXECUTE  -----------
		
	def execute(self, context):
107
		self.chord_global = context.user_preferences.addons[__package__].preferences
108 109 110 111
		
		if not self.find_armature(context):
			return {"CANCELLED"}

112
		self.chord_global.state = u.possible_states()[0][0]
Bruno Laurencich's avatar
Bruno Laurencich committed
113
		self.chord_global.notochord_ip = ""
Bruno Laurencich's avatar
Bruno Laurencich committed
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
		self.report({"INFO"},"ip query ongoing..")
		
		wm = context.window_manager
		self._timer = wm.event_timer_add( 0.8 , context.window)
		wm.modal_handler_add(self)

		self.query_lock = threading.Lock()
		self.found_ip = False
		self.query_thread = threading.Thread(target=self.do_query, 
			name="mdns_query")
		self.query_thread.start()

		return {'RUNNING_MODAL'}

	# -----------  CANCEL  -----------

	def cancel(self, context):
		wm = context.window_manager
		wm.event_timer_remove(self._timer)

		print("waiting thread..")
		self.query_thread.join()
		print("thread joined..")

# ======  End of OPERATOR: GET THE NOTOCHORD IP   =======
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

# ==============================================
# =           OPERATOR: GET LOCAL IP           =
# ==============================================
class Chord_Local_Ip(bpy.types.Operator):
	bl_idname = "chordata.get_local_ip"
	bl_label = "Chordata: get local ip"
	bl_options = {'REGISTER'}

	@classmethod
	def poll(cls, context):
		chord_global = context.user_preferences.addons[__package__].preferences
		return chord_global.trans_method is not "Multicast"

	def execute(self, context):
		chord_global = context.user_preferences.addons[__package__].preferences
		chord_global.local_ip_addr = u.get_local_ip()
		return {"FINISHED"}
		


# ======  End of OPERATOR: GET LOCAL IP  =======