%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% comm.pl         6/25/2002 
%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%                      Communication predicates 
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

:- multifile exec/2. 


:- ['./comm/ixwrap']. 
:- ['./comm/newutil']. 
:- ['./comm/address.db']. 

% read current server address: e.g. server('summer.aiai.ed.ac.uk',1106). 

:- ['./comm/server.addr'].  


t1 :-
   exec(_, post_issue(header, issue_occ(test, body))). 

t2 :-
   exec(_, retrieve_issue(Issue)), 
   write('---- test: receive issue --------'), nl, 
   write(Issue),nl,nl. 
   

%%%%%%%%%%%% post issue %%%%%%%%%%
% 
%  write_to_chars(issue_occ(X, Y), String), 
%  name(Atom, String), 

%% work: below is only for testing purpose !! 

exec(_, post_issue(X, Y)) :- 

     writeln('--- calling exe/2 post_issue'),
     writeln(X/Y), !. 


%% real predicate 

exec(_, post_issue(Header, Issue_occ)) :- 

     writeln('--- calling exe/2 post_issue'),
     writeln(Issue_occ), 
     
     %% copy server address from comm sub-dir to current dir. 
     server(Machine, Port),  
     told, 
     tell('server.addr'),
     portray_clause(server(Machine, Port)),
     told,   

     Message = issue_packet(Header, Issue_occ), 

     ix_port(edinburgh, Port), 
     ix_send(9876, Port, Message). 


%%%%%%%%%%%% receive issue %%%%%%%%%%
/**********

exec(X, retrieve_issue(Y)). 

**********/ 

%% below is only for testing purpose !!!

exec(_, retrieve_issue(_)) :- 
    
  writeln('--- calling exe/2 retrieve_issue'),

  IssueOcc = 
  issue_occ(e1, 
            solve_technical_constraint,
            _Status, 
            _Priority, 
            _Begin_time/_End_time, 
            (john/_Requester_type, _Provider/_Provider_type), 

             solution(john, 

                    [total_cost(M), 
                     less_than(M, 1000)
                    ], 

                    _Old, 

                    [ static_constraint(
                      [ instance_of(processor_x, processor),    
                        instance_of(disk_controller_x, disk_controller),
                        instance_of(io_x, io) ], 

                      [ allocate(processor_x,  Slot1),
                        allocate(disk_controller_x,  Slot2),
                        allocate(io_x,  Slot3), 
                        instance_of(Slot1, slot),
                        instance_of(Slot2, slot),
                        instance_of(Slot3, slot), 
                        total_cost(M) ]
                      )
                    ], 

                    _Difference )
          ), 


  writeln(IssueOcc ), 
  assert(dyn(IssueOcc )), !. 


%% real predicate below 

exec(_, retrieve_issue(Content )) :- 
    
    writeln('--- calling exe/2 retrieve_issue'),
    writeln(Content), 

    wfe_read_message(Sender, Message), 
    write('>>>>> exec: receive sender and msg <<<<<'), nl, 
    write(Sender),nl,
    write(Message),
    write('>>>>> end: receive sender and msg  <<<<<'), nl, 

    Message = issue_packet(_header, Content), 

    assert(dyn(Content )). 


%%%%%%%%%%%%%%%%%%%%%% end %%%%%%%%%%%%%%%%%%%%%%

