author | 'Gr?gory Tr?lat <gregory.trelat@lolitech.fr>' |
Fri, 10 Jul 2009 11:06:30 +0200 | |
changeset 358 | 855b6fd21416 |
parent 357 | 19db1076e93c |
child 361 | 331d698e1118 |
child 374 | 8787fa8c6792 |
permissions | -rwxr-xr-x |
203 | 1 |
#!/usr/bin/env python |
2 |
# -*- coding: utf-8 -*- |
|
3 |
||
4 |
#This file is part of Beremiz, a Integrated Development Environment for |
|
5 |
#programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
|
6 |
# |
|
7 |
#Copyright (C) 2007: Edouard TISSERANT and Laurent BESSARD |
|
8 |
# |
|
9 |
#See COPYING file for copyrights details. |
|
10 |
# |
|
11 |
#This library is free software; you can redistribute it and/or |
|
12 |
#modify it under the terms of the GNU General Public |
|
13 |
#License as published by the Free Software Foundation; either |
|
14 |
#version 2.1 of the License, or (at your option) any later version. |
|
15 |
# |
|
16 |
#This library is distributed in the hope that it will be useful, |
|
17 |
#but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
18 |
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
19 |
#General Public License for more details. |
|
20 |
# |
|
21 |
#You should have received a copy of the GNU General Public |
|
22 |
#License along with this library; if not, write to the Free Software |
|
23 |
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
24 |
||
25 |
import wx |
|
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
26 |
from Zeroconf import * |
203 | 27 |
import socket |
28 |
import wx.lib.mixins.listctrl as listmix |
|
29 |
||
30 |
class TestListCtrl(wx.ListCtrl, listmix.ListCtrlAutoWidthMixin): |
|
31 |
def __init__(self, parent, ID, pos=wx.DefaultPosition, |
|
32 |
size=wx.DefaultSize, style=0): |
|
33 |
wx.ListCtrl.__init__(self, parent, ID, pos, size, style) |
|
34 |
listmix.ListCtrlAutoWidthMixin.__init__(self) |
|
35 |
||
36 |
class DiscoveryDialog(wx.Dialog, listmix.ColumnSorterMixin): |
|
37 |
def __init__(self, parent, id=-1, title='Service Discovery'): |
|
38 |
self.my_result=None |
|
39 |
wx.Dialog.__init__(self, parent, id, title, size=(600,600), style=wx.DEFAULT_DIALOG_STYLE) |
|
40 |
||
221 | 41 |
sizer = wx.FlexGridSizer(2, 1, 2, 2) # rows, cols, vgap, hgap |
42 |
sizer.AddGrowableRow(0) |
|
43 |
sizer.AddGrowableCol(0) |
|
44 |
||
203 | 45 |
self.list = TestListCtrl(self, -1, |
221 | 46 |
#pos=(50,20), |
47 |
#size=(500,300), |
|
203 | 48 |
style=wx.LC_REPORT |
49 |
| wx.LC_EDIT_LABELS |
|
50 |
| wx.LC_SORT_ASCENDING |
|
51 |
) |
|
221 | 52 |
sizer.Add(self.list, 1, wx.EXPAND) |
53 |
||
54 |
btsizer = wx.FlexGridSizer(1, 6, 2, 2) # rows, cols, vgap, hgap |
|
55 |
||
56 |
sizer.Add(btsizer, 1, wx.EXPAND) |
|
57 |
||
203 | 58 |
self.PopulateList() |
59 |
||
60 |
self.Bind(wx.EVT_LIST_ITEM_SELECTED, self.OnItemSelected, self.list) |
|
61 |
self.Bind(wx.EVT_LIST_ITEM_ACTIVATED, self.OnItemActivated, self.list) |
|
62 |
||
221 | 63 |
local_id = wx.NewId() |
64 |
b = wx.Button(self, local_id, "Refresh") |
|
65 |
self.Bind(wx.EVT_BUTTON, self.OnRefreshButton, b) |
|
66 |
btsizer.Add(b) |
|
203 | 67 |
|
221 | 68 |
btsizer.AddSpacer(0) |
69 |
btsizer.AddGrowableCol(1) |
|
203 | 70 |
|
221 | 71 |
local_id = wx.NewId() |
72 |
b = wx.Button(self, local_id, "Local") |
|
73 |
self.Bind(wx.EVT_BUTTON, self.ChooseLocalID, b) |
|
74 |
btsizer.Add(b) |
|
75 |
||
76 |
btsizer.AddSpacer(0) |
|
77 |
btsizer.AddGrowableCol(3) |
|
78 |
||
79 |
b = wx.Button(self, wx.ID_CANCEL, "Cancel") |
|
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
80 |
self.Bind(wx.EVT_BUTTON, self.OnCancel, b) |
221 | 81 |
btsizer.Add(b) |
82 |
||
83 |
b = wx.Button(self, wx.ID_OK, "OK") |
|
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
84 |
self.Bind(wx.EVT_BUTTON, self.OnOk, b) |
221 | 85 |
b.SetDefault() |
86 |
btsizer.Add(b) |
|
87 |
||
88 |
self.SetSizer(sizer) |
|
203 | 89 |
|
90 |
listmix.ColumnSorterMixin.__init__(self, 4) |
|
221 | 91 |
|
92 |
#type = "_http._tcp.local." |
|
262 | 93 |
self.browser = None |
221 | 94 |
self.zConfInstance = Zeroconf() |
95 |
self.RefreshList() |
|
96 |
||
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
97 |
self.Bind(wx.EVT_CLOSE, self.OnClose) |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
98 |
|
221 | 99 |
def RefreshList(self): |
100 |
type = "_PYRO._tcp.local." |
|
262 | 101 |
self.browser = ServiceBrowser(self.zConfInstance, type, self) |
221 | 102 |
|
103 |
def OnRefreshButton(self, event): |
|
104 |
self.list.DeleteAllItems() |
|
262 | 105 |
self.browser.cancel() |
221 | 106 |
self.RefreshList() |
107 |
||
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
108 |
def OnClose(self, event): |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
109 |
self.zConfInstance.close() |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
110 |
event.Skip() |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
111 |
|
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
112 |
def OnCancel(self, event): |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
113 |
self.zConfInstance.close() |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
114 |
event.Skip() |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
115 |
|
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
116 |
def OnOk(self, event): |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
117 |
self.zConfInstance.close() |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
118 |
event.Skip() |
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
119 |
|
203 | 120 |
# Used by the ColumnSorterMixin, see wx/lib/mixins/listctrl.py |
121 |
def GetListCtrl(self): |
|
122 |
return self.list |
|
123 |
||
124 |
def PopulateList(self): |
|
221 | 125 |
self.list.InsertColumn(0, 'NAME') |
203 | 126 |
self.list.InsertColumn(1, 'TYPE') |
127 |
self.list.InsertColumn(2, 'IP') |
|
128 |
self.list.InsertColumn(3, 'PORT') |
|
129 |
self.list.SetColumnWidth(0, 150) |
|
130 |
self.list.SetColumnWidth(1, 150) |
|
131 |
self.list.SetColumnWidth(2, 150) |
|
132 |
self.list.SetColumnWidth(3, 150) |
|
133 |
||
134 |
def getColumnText(self, index, col): |
|
135 |
item = self.list.GetItem(index, col) |
|
136 |
return item.GetText() |
|
137 |
||
138 |
def OnItemSelected(self, event): |
|
139 |
self.currentItem = event.m_itemIndex |
|
221 | 140 |
self.setresult() |
203 | 141 |
event.Skip() |
142 |
||
143 |
def OnItemActivated(self, event): |
|
144 |
self.currentItem = event.m_itemIndex |
|
221 | 145 |
self.setresult() |
146 |
self.Close() |
|
203 | 147 |
event.Skip() |
148 |
||
221 | 149 |
def setresult(self): |
203 | 150 |
connect_type = self.getColumnText(self.currentItem, 1) |
151 |
connect_address = self.getColumnText(self.currentItem, 2) |
|
152 |
connect_port = self.getColumnText(self.currentItem, 3) |
|
153 |
||
154 |
uri = self.CreateURI(connect_type, connect_address, connect_port) |
|
155 |
self.my_result=uri |
|
156 |
||
157 |
def GetResult(self): |
|
158 |
return self.my_result |
|
159 |
||
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
160 |
def removeService(self, zeroconf, type, name): |
221 | 161 |
pass |
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
162 |
|
262 | 163 |
def addService(self, zeroconf, type, name): |
357
19db1076e93c
close ZeroConf object when exiting the Service Discovery window.
btaylor@grond.willowglen.ab.ca
parents:
277
diff
changeset
|
164 |
info = self.zConfInstance.getServiceInfo(type, name) |
203 | 165 |
typename = type.split(".")[0][1:] |
166 |
num_items = self.list.GetItemCount() |
|
167 |
self.list.InsertStringItem(num_items, name.split(".")[0]) |
|
168 |
self.list.SetStringItem(num_items, 1, "%s"%typename) |
|
169 |
self.list.SetStringItem(num_items, 2, "%s"%str(socket.inet_ntoa(info.getAddress()))) |
|
170 |
self.list.SetStringItem(num_items, 3, "%s"%info.getPort()) |
|
171 |
||
172 |
def CreateURI(self, connect_type, connect_address, connect_port): |
|
173 |
uri = "%s://%s:%s"%(connect_type, connect_address, connect_port) |
|
174 |
return uri |
|
175 |
||
221 | 176 |
def ChooseLocalID(self, event): |
277
73890551f021
Local PYRO connection is designated by LOCAL://, not PYRO://localhost:3000 anymore
etisserant
parents:
262
diff
changeset
|
177 |
self.my_result = "LOCAL://" |
203 | 178 |
self.Close() |