Skip to content

Commit

Permalink
Added corba implementation to modeq. Accessed with +d=interactiveCorba.
Browse files Browse the repository at this point in the history
git-svn-id: https://openmodelica.org/svn/OpenModelica/trunk@1061 f25d12d1-65f4-0310-ae8a-bbce733d8d8e
  • Loading branch information
Peter Aronsson committed Jan 7, 2004
1 parent d220b8b commit 6a61bc5
Show file tree
Hide file tree
Showing 8 changed files with 277 additions and 7 deletions.
8 changes: 6 additions & 2 deletions modeq/Makefile.in
Expand Up @@ -20,11 +20,15 @@ RML = @rmlc_bin@ -Wc,-O3
RMLINC = -I$(RMLHOME)/include/plain
MYRML = ./myrmlc

LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr $(LIBSOCKET)

LDFLAGS = -L$(RMLHOME)/lib/plain $(ANTLR_LIBP) -lrml -lm -lantlr $(LIBSOCKET) \
-lssl -lmico -lcrypto -ldl -lm -lpthread

PROG = modeq
AST = absyn_builder/absyn_builder.a
RTOBJ = runtime/systemimpl.o ../c_runtime/libc_runtime.a runtime/rtopts.o runtime/socketimpl.o runtime/printimpl.o runtime/cacheimpl.o runtime/ptolemyio.o
RTOBJ = runtime/systemimpl.o ../c_runtime/libc_runtime.a runtime/rtopts.o runtime/socketimpl.o \
runtime/printimpl.o runtime/cacheimpl.o runtime/ptolemyio.o runtime/corbaimpl.o \
runtime/modeq_communication_impl.o runtime/modeq_communication.o

SRCRML= absyn.rml \
algorithm.rml \
Expand Down
11 changes: 11 additions & 0 deletions modeq/debug.rml
Expand Up @@ -38,6 +38,7 @@ module Debug:
relation fprintl: (string, string list) => ()
relation fprint_list: (string, 'a list, 'a => (), string) => ()
relation fcall: (string, 'a => (), 'a) => ()
relation bcall: (bool, 'a => (), 'a) => ()
relation notfcall: (string, 'a => (), 'a) => ()
end

Expand Down Expand Up @@ -137,6 +138,16 @@ relation fcall: (string, 'a => (), 'a) => () =

end

relation bcall: (bool, 'a => (), 'a) => () =

rule func(str)
------------
bcall (true, func, str)

axiom bcall (false,_,_)

end

(* Call the given function (2nd arg) if the flag given in 1st arg is NOT set *)

relation notfcall: (string, 'a => (), 'a) => () =
Expand Down
37 changes: 35 additions & 2 deletions modeq/main.rml
Expand Up @@ -47,6 +47,7 @@ with "debug.rml"
with "codegen.rml"
with "socket.rml"
with "print.rml"
with "corba.rml"

(** relation: server_loop
**
Expand Down Expand Up @@ -251,6 +252,33 @@ relation interactivemode: string list => () =
interactivemode _
end

relation interactivemode_corba: string list => () =

rule Corba.initialize &
server_loop_corba (Interactive.SYMBOLTABLE(Absyn.PROGRAM([],Absyn.TOP),[],[],[],[])) => _
-------------------
interactivemode_corba _
end

(** relation: server_loop_corba
**
** This relation is the main loop of the server for a CORBA impl.
**)
relation server_loop_corba: (Interactive.InteractiveSymbolTable) => Interactive.InteractiveSymbolTable =

rule Corba.wait_for_command() => str &
handle_command (str,isymb) => (true,replystr,newsymb) &
Corba.sendreply(replystr) &
server_loop_corba (newsymb) => ressymb
-----------------------------
server_loop_corba (isymb) => ressymb

rule Print.print_buf "Exiting\n" &
Corba.sendreply("quit requested, shutting server down\n") &
Corba.close
---------------------
server_loop_corba (isymb) => isymb
end

(** relation: main
**
Expand All @@ -261,8 +289,13 @@ end
relation main : string list => () =

rule RTOpts.args args => args' &
Debug.fcall ("interactive", interactivemode, args' ) &
Debug.notfcall ("interactive", translate_file, args')
RTOpts.debug_flag("interactive") => ismode &
RTOpts.debug_flag("interactiveCorba") => icmode &
bool_or(ismode,icmode) => imode &
bool_not(imode) => imode' &
Debug.bcall (ismode, interactivemode, args' ) &
Debug.bcall (icmode, interactivemode_corba, args') &
Debug.bcall (imode', translate_file, args')
--------------------
main args

Expand Down
7 changes: 4 additions & 3 deletions modeq/runtime/Makefile.in
Expand Up @@ -14,12 +14,13 @@ CC = gcc
CFLAGS += -I$(RMLINCLUDE) -I../../c_runtime
CXXFLAGS = $(CFLAGS)
SRC = rtopts.c socketimpl.c printimpl.c systemimpl.c
CPPSRC = cacheimpl.cpp ptolemyio.cpp
OBJ = $(SRC:.c=.o) $(CPPSRC:.cpp=.o)
CPPSRC = cacheimpl.cpp ptolemyio.cpp modeq_communication_impl.cpp \
modeq_communication.cc corbaimpl.cpp
OBJ = $(SRC:.c=.o) $(CPPSRC:.cpp=.o) $(CPPSRC:.cc=.o)

all: $(OBJ)

clean:
$(RM) $(OBJ)
$(RM) -rf *.o


133 changes: 133 additions & 0 deletions modeq/runtime/corbaimpl.cpp
@@ -0,0 +1,133 @@
#include "modeq_communication.h"
#include "modeq_communication_impl.h"



extern "C" {
#include "rml.h"
#include "../values.h"
#include <stdio.h>
#include "../absyn_builder/yacclib.h"
#include <pthread.h>
}

#include <cstdlib>
#include <iostream>
#include <fstream>

using namespace std;

pthread_mutex_t lock;

// Condition variable for keeping modeq waiting for client requests
pthread_cond_t modeq_waitformsg;
pthread_mutex_t modeq_waitlock;
bool modeq_waiting=false;

// Condition variable for keeping corba waiting for returnvalue from modeq
pthread_cond_t corba_waitformsg;
pthread_mutex_t corba_waitlock;
bool corba_waiting=false;

char * modeq_message;


CORBA::ORB_var orb;
CORBA::BOA_var boa;

ModeqCommunication_impl * server;

extern "C" {
void writeServerId(CORBA::ORB_var orb);
void* runOrb(void*arg);

void Corba_5finit(void)
{

}

RML_BEGIN_LABEL(Corba__initialize)
{
char *dummyArgv="modeq";
int zero=0;
pthread_cond_init(&modeq_waitformsg,NULL);
pthread_cond_init(&corba_waitformsg,NULL);
pthread_mutex_init(&corba_waitlock,NULL);
pthread_mutex_init(&modeq_waitlock,NULL);

orb = CORBA::ORB_init(zero, 0,"mico-local-orb");
boa = orb->BOA_init(zero,0,"mico-local-boa");

server = new ModeqCommunication_impl();

writeServerId(orb);
boa->impl_is_ready(CORBA::ImplementationDef::_nil());

// Start thread that listens on incomming messages.
pthread_t orb_thr_id;
if( pthread_create(&orb_thr_id,NULL,&runOrb,NULL)) {
cerr << "Error creating thread for corba communication." << endl;
RML_TAILCALLK(rmlFC);
}

std::cout << "Created server." << std::endl;
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

void* runOrb(void* arg)
{
orb->run();
CORBA::release(server);
return NULL;
}

void writeServerId(CORBA::ORB_var orb)
{
ofstream tmpFile ("/tmp/openmodelica.objid"); // Should be different on Windows
CORBA::String_var ref = orb->object_to_string(server);
tmpFile << ref << endl;
tmpFile.close();
}

RML_BEGIN_LABEL(Corba__wait_5ffor_5fcommand)
{
pthread_mutex_lock(&modeq_waitlock);
while (!modeq_waiting) {
pthread_cond_wait(&modeq_waitformsg,&modeq_waitlock);
}
modeq_waiting = false;
pthread_mutex_unlock(&modeq_waitlock);

rmlA0=mk_scon(modeq_message);
pthread_mutex_lock(&lock); // Lock so no other tread can talk to modeq.
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(Corba__sendreply)
{
char *msg=RML_STRINGDATA(rmlA0);

// Signal to Corba that it can return, taking the value in message
pthread_mutex_lock(&corba_waitlock);
corba_waiting=true;
modeq_message =CORBA::string_dup(msg);

pthread_cond_signal(&corba_waitformsg);
pthread_mutex_unlock(&corba_waitlock);

pthread_mutex_unlock(&lock); // Unlock, so other threads can ask modeq stuff.
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL

RML_BEGIN_LABEL(Corba__close)
{
boa->deactivate_impl(CORBA::ImplementationDef::_nil());
orb->shutdown(TRUE);
pthread_yield(); // Allowing other thread to shutdown.
RML_TAILCALLK(rmlSC);
}
RML_END_LABEL
}
6 changes: 6 additions & 0 deletions modeq/runtime/modeq_communication.idl
@@ -0,0 +1,6 @@
// As simple as can be modeq communication, sending and recieving of strings.

interface ModeqCommunication {
string sendExpression( in string expr);
string sendClass( in string model);
};
69 changes: 69 additions & 0 deletions modeq/runtime/modeq_communication_impl.cpp
@@ -0,0 +1,69 @@
#include "modeq_communication_impl.h"

extern "C" {
#include "rml.h"
#include <pthread.h>
}

extern pthread_cond_t modeq_waitformsg;
extern pthread_mutex_t modeq_waitlock;

extern bool modeq_waiting;

extern pthread_cond_t corba_waitformsg;
extern pthread_mutex_t corba_waitlock;

extern bool corba_waiting;

extern char * modeq_message;
using namespace std;

//This is the implementation of the modeq communication using mico (CORBA)

ModeqCommunication_impl::ModeqCommunication_impl()
{
}

char* ModeqCommunication_impl::sendExpression( const char* expr )
{
char* result;
// Signal to modeq that message has arrived.
pthread_mutex_lock(&modeq_waitlock);
modeq_waiting=true;
modeq_message = (char*)expr;
pthread_cond_signal(&modeq_waitformsg);
pthread_mutex_unlock(&modeq_waitlock);

// Wait for modeq to process message
pthread_mutex_lock(&corba_waitlock);
while (!corba_waiting) {
pthread_cond_wait(&corba_waitformsg,&corba_waitlock);
}
corba_waiting = false;
pthread_mutex_unlock(&corba_waitlock);

return modeq_message; // Has already been string_dup (prepared for CORBA)
}

char* ModeqCommunication_impl::sendClass( const char* expr )
{
// Signal to modeq that message has arrived.
pthread_mutex_lock(&modeq_waitlock);
modeq_waiting=true;
modeq_message = (char*)expr;
pthread_cond_signal(&modeq_waitformsg);
pthread_mutex_unlock(&modeq_waitlock);

// Wait for modeq to process message
pthread_mutex_lock(&corba_waitlock);

while (!modeq_waiting) {
pthread_cond_wait(&corba_waitformsg,&corba_waitlock);
}
corba_waiting = false;
pthread_mutex_unlock(&corba_waitlock);

return modeq_message; // Has already been string_dup (prepared for CORBA)

}

13 changes: 13 additions & 0 deletions modeq/runtime/modeq_communication_impl.h
@@ -0,0 +1,13 @@
#ifndef _MODEQ_COMMUNICATION_IMPL_H
#define _MODEQ_COMMUNICATION_IMPL_H
#include "modeq_communication.h"

class ModeqCommunication_impl : virtual public ModeqCommunication_skel{
public:
ModeqCommunication_impl();
char* sendExpression( const char* expr );
char* sendClass( const char* expr );
};


#endif

0 comments on commit 6a61bc5

Please sign in to comment.