e9a5e7984836b765ab4143d370f2841513743943
[sdk] / samples / net / eCom / mainPanel.ec
1 import "ecere"
2 import "connection"
3
4 define ECOMMUNICATOR_PORT = 3113;
5
6 class eComApp;
7
8 define app = (eComApp) __thisModule;
9
10 class ConnectionSocket;
11 class Connection;
12 class eComService;
13
14 class MainPanel : Window
15 {
16    eComService service { mainPanel = this };
17    OldList connections;
18
19    background = activeBorder, tabCycle = true;
20    borderStyle = sizable, hasClose = true, hasMinimize = true, text = app.appName, size = Size { 500, 320 }, minClientSize = Size { 320, 200 };
21
22    Button connect
23    {
24       this, isDefault = true, text = "Connect", position = Point { 220, 30 }, size = Size { 80, 0 }, hotKey = altC;
25
26       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
27       {
28          ConnectionSocket { mainPanel = this }.Connect(address.contents, service.port);
29          return true;
30       }
31    };
32    Button host
33    {
34       this, text = "Host", position = Point { 220, 60 }, size = Size { 80, 0 }, hotKey = altH;
35
36       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
37       {
38          if(service.Start())
39          {
40             log.End();
41             log.Printf("\nHosting on port %d", service.port);
42             host.disabled = true;
43             stopHosting.disabled = false;
44          }
45          return true;
46       }
47    };
48    Button localHost
49    {
50       this, text = "Local Host", position = Point { 320, 30 }, size = Size { 80, 0 }, hotKey = altL;
51
52       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
53       {
54          char hostName[256], address[64];
55          GetHostName(hostName, sizeof(hostName));
56          GetAddressFromName(hostName, address);
57          log.End();
58          log.Printf("\nLocal Host: %s => %s", hostName, address);
59          return true;
60       }
61    };
62    Button stopHosting
63    {
64        this, text = "Stop", position = Point { 320, 60 }, size = Size { 80, 0 }, hotKey = altS;
65
66        bool NotifyClicked(Button button, int x, int y, Modifiers mods)
67        {
68          service.Stop();
69          stopHosting.disabled = true;
70          host.disabled = false;
71          return true;
72        }
73    };
74
75    EditBox log
76    {
77       this, readOnly = true, multiLine = true, autoEmpty = true, hasVertScroll = true, hasHorzScroll = true, inactive = true,
78       text = "Communication Log",
79       anchor = Anchor { left = 10, top = 100, right = 10, bottom = 30 },
80       contents = app.appName
81    };
82    Label logLabel
83    {
84       this, position = Point { 10, 80 }, labeledWindow = log
85    };
86    EditBox nameBox
87    {
88       this, text = "Name", position = Point { 50, 55 }, size = Size { 160,20 };
89
90       void NotifyUpdate(EditBox editBox)
91       {
92          char * string = nameBox.contents;
93          OldLink link;
94          for(link = connections.first; link; link = link.next)
95             ((Connection)link.data).SendName(string);
96       }
97    };
98    Label nameLabel
99    {
100       this, position = Point { 10, 55 }, labeledWindow = nameBox
101    };
102
103    Button clear
104    {
105       this, text = "Clear", anchor = Anchor { left = 10, bottom = 5 }, size = Size { 80, 0 }, hotKey = altR;
106
107       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
108       {
109          log.Clear();
110          return true;
111       }
112    };
113    Button exit
114    {
115       this, text = "Exit", size = Size { 80, 0 }, anchor = Anchor { right = 10, bottom = 5 }, hotKey = altX;
116
117       bool NotifyClicked(Button button, int x, int y, Modifiers mods)
118       {
119          Destroy(0);
120          return true;
121       }
122    };
123
124    EditBox address
125    {
126       this, contents = "localhost", hasHorzScroll = true, text = "Internet Address", position = Point { 10, 30 }, size = Size { 200,20 }, hotKey = altA;
127
128       void NotifyUpdate(EditBox editBox)
129       {
130          connect.disabled = address.contents[0] ? false : true;
131       }
132    };
133    Label addressLabel
134    {
135       this, position = Point { 10, 10 }, labeledWindow = address
136    };
137
138    void ConnectionDestroyed(Connection connection)
139    {
140       log.End();
141       log.Printf("\n%s disconnected", connection.text);
142       connections.Delete(connections.FindLink(connection));
143    }
144
145    void OnConnect(Connection connection, char * inetAddress, char * address)
146    {
147       log.End();
148       log.Printf("\nConnected to %s (%s)", inetAddress, address);
149       connection.SendName(nameBox.contents);
150       connections.Add(OldLink{data = connection});
151    }
152 }
153
154 MainPanel mainPanel {};
155
156 class eComApp : GuiApplication
157 {
158    appName = "ECERE Communicator (Build 0002.5)";
159 }
160
161 class eComService : Service
162 {
163    MainPanel mainPanel;
164
165    port = ECOMMUNICATOR_PORT;
166
167    void OnAccept()
168    {
169       char address[256] = "";
170       Connection connection;
171
172       ConnectionSocket socket { this, mainPanel = mainPanel };
173
174       GetNameFromAddress(socket.inetAddress, address);
175       mainPanel.log.End();
176       mainPanel.log.Printf("\nConnection accepted from %s (%s)", socket.inetAddress, address);
177
178       socket.connection = connection = Connection { text = "Incoming Connection...", socket = socket, master = mainPanel };
179       mainPanel.connections.Add(OldLink{data = connection});
180
181       connection.Create();
182       connection.SendName(mainPanel.nameBox.contents);
183    }
184
185    property MainPanel mainPanel { set { mainPanel = value; } }
186 }