This commit is contained in:
persan 2020-03-28 20:34:07 +01:00
parent 9223e0c322
commit a2437a7079
9 changed files with 93 additions and 87 deletions

View File

@ -31,11 +31,11 @@ with ZMQ.Sockets;
with ZMQ.Contexts;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Text_IO.Unbounded_IO; use Ada.Text_IO.Unbounded_IO;
procedure ZMQ.Examples.HWServer is
Context : ZMQ.Contexts.Context;
Socket : ZMQ.Sockets.Socket;
inbuffer : Ada.Strings.Unbounded.Unbounded_String;
Inbuffer : Ada.Strings.Unbounded.Unbounded_String;
begin
-- Prepare our context and socket
Socket.Initialize (Context, ZMQ.Sockets.REP);
@ -43,8 +43,8 @@ begin
loop
-- Wait for next request from client
inbuffer := Socket.Recv;
Put_Line ("Received request:" & To_String (inbuffer));
Inbuffer := Socket.Recv;
Put_Line ("Received request:" & Inbuffer);
-- Do some 'work'
delay 1.0;

View File

@ -2,30 +2,22 @@
package body ZMQ.Examples.JSON_Data is
-- =========================================================================
-- Coordinate
-- =========================================================================
Coordinate_X_Name : constant String := "X";
Coordinate_Y_Name : constant String := "Y";
Coordinate_Z_Name : constant String := "Z";
------------
-- Create --
------------
function Create (Val : Coordinate) return JSON_Value is
begin
return Ret : constant JSON_Value := Create_Object do
Ret.Set_Field ("X", Create (Val.X));
Ret.Set_Field ("Y", Create (Val.Y));
Ret.Set_Field ("Z", Create (Val.Z));
end return;
end Create;
------------
-- Create --
------------
function Create (Val : Data_Type) return JSON_Value is
begin
return Ret : constant JSON_Value := Create_Object do
Ret.Set_Field ("Sensor_Name", Create (Val.Sensor_Name));
Ret.Set_Field ("OK", Create (Val.OK));
Ret.Set_Field ("Location", Create (Val.Location));
Ret.Set_Field ("Orientation", Create (Val.Orientation));
Ret.Set_Field (Coordinate_X_Name, Create (Val.X));
Ret.Set_Field (Coordinate_Y_Name, Create (Val.Y));
Ret.Set_Field (Coordinate_Z_Name, Create (Val.Z));
end return;
end Create;
@ -42,6 +34,47 @@ package body ZMQ.Examples.JSON_Data is
Val.Set_Field (Field_Name, Create (Field));
end Set_Field;
-------------------
-- Cb_Coordinate --
-------------------
procedure Cb_Coordinate
(User_Object : in out Coordinate;
Name : UTF8_String;
Value : JSON_Value)
is
begin
if Name = Coordinate_X_Name then
User_Object.X := Value.Get;
elsif Name = Coordinate_Y_Name then
User_Object.Y := Value.Get;
elsif Name = Coordinate_Z_Name then
User_Object.Z := Value.Get;
end if;
end Cb_Coordinate;
-- =========================================================================
-- Data_Type
-- =========================================================================
Data_Type_Sensor_Name_Name : constant String := "Sensor_Name";
Data_Type_OK_Name : constant String := "OK";
Data_Type_Location_Name : constant String := "Location";
Data_Type_Orientation_Name : constant String := "Orientation";
------------
-- Create --
------------
function Create (Val : Data_Type) return JSON_Value is
begin
return Ret : constant JSON_Value := Create_Object do
Ret.Set_Field (Data_Type_Sensor_Name_Name, Create (Val.Sensor_Name));
Ret.Set_Field (Data_Type_OK_Name, Create (Val.OK));
Ret.Set_Field (Data_Type_Location_Name, Create (Val.Location));
Ret.Set_Field (Data_Type_Orientation_Name, Create (Val.Orientation));
end return;
end Create;
---------------
-- Set_Field --
---------------
@ -55,32 +88,6 @@ package body ZMQ.Examples.JSON_Data is
Val.Set_Field (Field_Name, Create (Field));
end Set_Field;
-------------------
-- Cb_Long_Float --
-------------------
-------------------
-- Cb_Coordinate --
-------------------
procedure Cb_Coordinate
(User_Object : in out Coordinate;
Name : UTF8_String;
Value : JSON_Value)
is
begin
if Name = "X" then
User_Object.X := Value.Get;
elsif Name = "Y" then
User_Object.Y := Value.Get;
elsif Name = "Z" then
User_Object.Z := Value.Get;
end if;
end Cb_Coordinate;
------------------
-- Cb_Data_Type --
------------------
@ -91,13 +98,13 @@ package body ZMQ.Examples.JSON_Data is
Value : JSON_Value)
is
begin
if Name = "Sensor_Name" then
if Name = Data_Type_Sensor_Name_Name then
User_Object.Sensor_Name := Value.Get;
elsif Name = "OK" then
elsif Name = Data_Type_OK_Name then
User_Object.OK := Value.Get;
elsif Name = "Location" then
elsif Name = Data_Type_Location_Name then
Read (Value, Cb_Coordinate'Access, User_Object.Location);
elsif Name = "Orientation" then
elsif Name = Data_Type_Orientation_Name then
Read (Value, Cb_Coordinate'Access, User_Object.Orientation);
end if;
end Cb_Data_Type;

View File

@ -42,9 +42,7 @@ package ZMQ.Examples.JSON_Data is
procedure Read (Src : JSON_Value; Into : in out Data_Type);
procedure Read is new
Gen_Map_JSON_Object (Data_Type);
procedure Read is new
Gen_Map_JSON_Object (Coordinate);
procedure Read is new Gen_Map_JSON_Object (Data_Type);
procedure Read is new Gen_Map_JSON_Object (Coordinate);
end ZMQ.Examples.JSON_Data;

View File

@ -26,25 +26,25 @@ with ZMQ.Sockets;
with ZMQ.Contexts;
with Ada.Text_IO;
with GNAT.Sockets;
procedure ZMQ.examples.prompt is
procedure ZMQ.Examples.Prompt is
ctx : aliased Contexts.Context;
s : Sockets.Socket;
Ctx : aliased Contexts.Context;
S : Sockets.Socket;
begin
ctx.Set_number_of_IO_threads (1);
s.Initialize (ctx, Sockets.PUB);
s.Connect ("tcp://localhost:5555");
Ctx.Set_Number_Of_IO_Threads (1);
S.Initialize (Ctx, Sockets.PUB);
S.Connect ("tcp://localhost:5555");
Read_Loop : loop
Ada.Text_IO.Put (">");
declare
textbuf : constant String := Ada.Text_IO.Get_Line;
Textbuf : constant String := Ada.Text_IO.Get_Line;
begin
exit Read_Loop when textbuf'Length = 0;
s.Send ("hej" & ASCII.NUL & GNAT.Sockets.Host_Name & ":" & textbuf);
exit Read_Loop when Textbuf'Length = 0;
S.Send ("Hello " & ASCII.NUL & GNAT.Sockets.Host_Name & ":" & Textbuf);
delay 0.02;
end;
end loop Read_Loop;
s.Send (END_MESSAGE);
end ZMQ.Examples.prompt;
S.Send (END_MESSAGE);
end ZMQ.Examples.Prompt;

View File

@ -46,7 +46,7 @@ begin
declare
Query : ZMQ.Messages.Message;
begin
Query.Initialize(0);
Query.Initialize (0);
-- Receive a message, blocks until one is available
S.Recv (Query);
-- Process the query

View File

@ -31,6 +31,7 @@
with Interfaces.C;
with Interfaces.C.Strings;
with ZMQ.Low_Level;
with GNAT.IO;
package body ZMQ is
@ -41,8 +42,10 @@ package body ZMQ is
function Error_Message (No : Integer) return String is
S : constant String := No'Img;
use all type Interfaces.C.Strings.chars_ptr;
begin
return "[" & S (S'First + 1 .. S'Last) & "] ";
return "[" & S (S'First + 1 .. S'Last) & "] " &
Value (ZMQ.Low_Level.zmq_strerror (int (No)));
end Error_Message;
function Library_Version return Version_Type is
@ -72,8 +75,9 @@ package body ZMQ is
Lib_Version : constant Version_Type := Library_Version;
begin
if Binding_Version.Major /= Lib_Version.Major then
GNAT.IO.Put_Line ("Warning libzmq: " & Image (Lib_Version) &
" found, binding version is: " & Image (Binding_Version));
GNAT.IO.Put_Line
("Warning libzmq: " & Image (Lib_Version) &
" found, binding version is: " & Image (Binding_Version));
end if;
end Validate_Library_Version;

View File

@ -1,12 +1,13 @@
with GNAT.Strings;
with Ada.Text_IO;
procedure ZMQ.Examples.JSON_Data.Test is
use GNAT.Strings;
V : Data_Type;
Src : JSON_Value;
Tgt : JSON_Value;
V1 : Data_Type;
S : access String;
S : GNAT.Strings.String_Access;
begin
V := (Sensor_Name => To_Unbounded_String ("bannme"),
OK => True,
@ -14,10 +15,10 @@ begin
Orientation => (1.1, 2.2, 3.3));
Src := Create (V);
S := new String'(Src.Write);
S := new String'(Src.Write);
Tgt := Read (S.all, "");
Read (Tgt, V1);
V1.OK := False;
Ada.Text_IO.Put_Line (Create (V1).Write);
Ada.Text_IO.Put_Line (Create (V1).Write (Compact => False));
Free (S);
end ZMQ.Examples.JSON_Data.Test;

13
zmq.gpr
View File

@ -31,7 +31,7 @@
------------------------------------------------------------------------------
with "libzmq.gpr";
project ZMQ is
For Languages use ("Ada", "Makefile", "Python");
for Languages use ("Ada", "Makefile", "Python");
type ZMQ_Kind_Type is ("static", "relocatable");
ZMQ_Kind : ZMQ_Kind_Type := external ("LIBRARY_TYPE", "static");
@ -82,15 +82,14 @@ project ZMQ is
end Check;
package Ide is
for gnatlist use project'project_dir & "proj.gnatls";
for Vcs_Kind use "git";
end Ide;
package naming is
for Spec_suffix ("Makefile") use ".mk";
package Naming is
for Spec_Suffix ("Makefile") use ".mk";
for Specification_Exceptions ("Makefile") use ("Makefile");
for Spec_suffix ("sed") use ".sed";
for Body_suffix ("Python") use ".py";
for Spec_Suffix ("sed") use ".sed";
for Body_Suffix ("Python") use ".py";
for Implementation_Exceptions ("Python") use ("configure");
end naming;
end Naming;
end ZMQ;

View File

@ -9,14 +9,11 @@ def do_load():
if exists(path):
with open(path) as inf:
buffer = inf.read()
print buffer
GPS.parse_xml(buffer)
print "OK"
try:
if GPS.zmq_config_is_loaded:
print "OK"
pass
except:
GPS.zmq_config_is_loaded = True
print "\nLOAD\n"
do_load()